Line data Source code
1 : /* src/native/vm/gnuclasspath/java_lang_reflect_VMField.cpp
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2, or (at
11 : your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #include "config.h"
27 :
28 : #include <assert.h>
29 : #include <stdint.h>
30 :
31 : #include "native/jni.hpp"
32 : #include "native/llni.hpp"
33 : #include "native/native.hpp"
34 :
35 : #if defined(ENABLE_JNI_HEADERS)
36 : # include "native/vm/include/java_lang_reflect_VMField.h"
37 : #endif
38 :
39 : #if defined(ENABLE_ANNOTATIONS)
40 : // REMOVEME
41 : # include "native/vm/reflection.hpp"
42 : #endif
43 :
44 : #include "vm/access.hpp"
45 : #include "vm/descriptor.hpp"
46 : #include "vm/exceptions.hpp"
47 : #include "vm/global.hpp"
48 : #include "vm/initialize.hpp"
49 : #include "vm/javaobjects.hpp"
50 : #include "vm/loader.hpp"
51 : #include "vm/primitive.hpp"
52 : #include "vm/resolve.hpp"
53 : #include "vm/string.hpp"
54 : #include "vm/utf8.hpp"
55 : #include "vm/vm.hpp"
56 :
57 : #include "vm/jit/builtin.hpp"
58 :
59 : /* _field_access_check *********************************************************
60 :
61 : Checks if the field can be accessed.
62 :
63 : RETURN VALUE:
64 : true......field can be accessed, or
65 : false.....otherwise (maybe an Exception was thrown).
66 :
67 : *******************************************************************************/
68 :
69 156 : static bool _field_access_check(const java_lang_reflect_VMField& rvmf, fieldinfo *f, java_handle_t *o)
70 : {
71 : // Check if we should bypass security checks (AccessibleObject).
72 :
73 156 : java_lang_reflect_Field rf(rvmf.get_f());
74 156 : int32_t override = rf.get_flag();
75 :
76 156 : if (override == false) {
77 : /* This function is always called like this:
78 : [0] java.lang.reflect.VMField.xxx (Native Method)
79 : [1] java.lang.reflect.Field.xxx
80 : [2] <caller>
81 : */
82 :
83 1 : if (!access_check_field(f, 2))
84 0 : return false;
85 : }
86 :
87 : /* some general checks */
88 :
89 156 : if (f->flags & ACC_STATIC) {
90 : /* initialize class if required */
91 :
92 156 : if (!(f->clazz->state & CLASS_INITIALIZED))
93 26 : if (!initialize_class(f->clazz))
94 0 : return false;
95 :
96 : /* everything is ok */
97 :
98 156 : return true;
99 : }
100 : else {
101 : /* obj is required for not-static fields */
102 :
103 0 : if (o == NULL) {
104 0 : exceptions_throw_nullpointerexception();
105 0 : return false;
106 : }
107 :
108 0 : if (builtin_instanceof(o, f->clazz))
109 0 : return true;
110 : }
111 :
112 : /* exception path */
113 :
114 0 : exceptions_throw_illegalargumentexception();
115 0 : return false;
116 : }
117 :
118 :
119 : /* _field_get_type *************************************************************
120 :
121 : Returns the content of the given field.
122 :
123 : *******************************************************************************/
124 :
125 : #define _FIELD_GET_TYPE(name, type, uniontype) \
126 : static inline type _field_get_##name(fieldinfo *f, java_handle_t* h) \
127 : { \
128 : type ret; \
129 : if (f->flags & ACC_STATIC) { \
130 : ret = f->value->uniontype; \
131 : } else { \
132 : LLNI_CRITICAL_START; \
133 : ret = *(type *) (((intptr_t) LLNI_DIRECT(h)) + f->offset); \
134 : LLNI_CRITICAL_END; \
135 : } \
136 : return ret; \
137 : }
138 :
139 133 : static inline java_handle_t *_field_get_handle(fieldinfo *f, java_handle_t* h)
140 : {
141 : java_object_t* result;
142 : java_handle_t* hresult;
143 :
144 : LLNI_CRITICAL_START;
145 :
146 133 : if (f->flags & ACC_STATIC) {
147 133 : result = (java_object_t*) f->value->a;
148 : } else {
149 0 : result = *(java_object_t**) (((intptr_t) LLNI_DIRECT(h)) + f->offset);
150 : }
151 :
152 133 : hresult = LLNI_WRAP(result);
153 :
154 : LLNI_CRITICAL_END;
155 :
156 133 : return hresult;
157 : }
158 :
159 1 : _FIELD_GET_TYPE(int, int32_t, i)
160 0 : _FIELD_GET_TYPE(long, int64_t, l)
161 0 : _FIELD_GET_TYPE(float, float, f)
162 0 : _FIELD_GET_TYPE(double, double, d)
163 :
164 :
165 : /* _field_set_type *************************************************************
166 :
167 : Sets the content of the given field to the given value.
168 :
169 : *******************************************************************************/
170 :
171 : #define _FIELD_SET_TYPE(name, type, uniontype) \
172 : static inline void _field_set_##name(fieldinfo* f, java_handle_t* h, type value) \
173 : { \
174 : if (f->flags & ACC_STATIC) { \
175 : f->value->uniontype = value; \
176 : } else { \
177 : LLNI_CRITICAL_START; \
178 : *(type *) (((intptr_t) LLNI_DIRECT(h)) + f->offset) = value; \
179 : LLNI_CRITICAL_END; \
180 : } \
181 : }
182 :
183 22 : static inline void _field_set_handle(fieldinfo* f, java_handle_t* h, java_handle_t* hvalue)
184 : {
185 : LLNI_CRITICAL_START;
186 :
187 22 : if (f->flags & ACC_STATIC) {
188 22 : f->value->a = LLNI_DIRECT(hvalue);
189 : } else {
190 0 : *(java_object_t**) (((intptr_t) LLNI_DIRECT(h)) + f->offset) = LLNI_DIRECT(hvalue);
191 : }
192 :
193 : LLNI_CRITICAL_END;
194 22 : }
195 :
196 0 : _FIELD_SET_TYPE(int, int32_t, i)
197 0 : _FIELD_SET_TYPE(long, int64_t, l)
198 0 : _FIELD_SET_TYPE(float, float, f)
199 0 : _FIELD_SET_TYPE(double, double, d)
200 :
201 :
202 : // Native functions are exported as C functions.
203 : extern "C" {
204 :
205 : /*
206 : * Class: java/lang/reflect/VMField
207 : * Method: getModifiersInternal
208 : * Signature: ()I
209 : */
210 133 : JNIEXPORT jint JNICALL Java_java_lang_reflect_VMField_getModifiersInternal(JNIEnv *env, jobject _this)
211 : {
212 133 : java_lang_reflect_VMField rvmf(_this);
213 133 : fieldinfo* f = rvmf.get_field();
214 133 : return f->flags;
215 : }
216 :
217 :
218 : /*
219 : * Class: java/lang/reflect/VMField
220 : * Method: getType
221 : * Signature: ()Ljava/lang/Class;
222 : */
223 0 : JNIEXPORT jclass JNICALL Java_java_lang_reflect_VMField_getType(JNIEnv *env, jobject _this)
224 : {
225 0 : java_lang_reflect_VMField rvmf(_this);
226 0 : fieldinfo* f = rvmf.get_field();
227 : classinfo *ret;
228 :
229 0 : typedesc* desc = f->parseddesc;
230 :
231 0 : if (desc == NULL)
232 0 : return NULL;
233 :
234 0 : if (!resolve_class_from_typedesc(desc, true, false, &ret))
235 0 : return NULL;
236 :
237 0 : return (jclass) LLNI_classinfo_wrap(ret);
238 : }
239 :
240 :
241 : /*
242 : * Class: java/lang/reflect/VMField
243 : * Method: get
244 : * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
245 : */
246 133 : JNIEXPORT jobject JNICALL Java_java_lang_reflect_VMField_get(JNIEnv *env, jobject _this, jobject o)
247 : {
248 133 : java_lang_reflect_VMField rvmf(_this);
249 133 : fieldinfo* f = rvmf.get_field();
250 :
251 : // Check if the field can be accessed.
252 133 : if (!_field_access_check(rvmf, f, o))
253 0 : return NULL;
254 :
255 : imm_union value;
256 :
257 133 : switch (f->parseddesc->primitivetype) {
258 : case PRIMITIVETYPE_BOOLEAN:
259 : case PRIMITIVETYPE_BYTE:
260 : case PRIMITIVETYPE_CHAR:
261 : case PRIMITIVETYPE_SHORT:
262 : case PRIMITIVETYPE_INT:
263 0 : value.i = _field_get_int(f, o);
264 0 : break;
265 :
266 : case PRIMITIVETYPE_LONG:
267 0 : value.l = _field_get_long(f, o);
268 0 : break;
269 :
270 : case PRIMITIVETYPE_FLOAT:
271 0 : value.f = _field_get_float(f, o);
272 0 : break;
273 :
274 : case PRIMITIVETYPE_DOUBLE:
275 0 : value.d = _field_get_double(f, o);
276 0 : break;
277 :
278 : case TYPE_ADR:
279 133 : return (jobject) _field_get_handle(f, o);
280 :
281 : default:
282 0 : assert(false);
283 : break;
284 : }
285 :
286 : // Now box the primitive types.
287 0 : java_handle_t* object = Primitive::box(f->parseddesc->primitivetype, value);
288 :
289 0 : return object;
290 : }
291 :
292 :
293 : /*
294 : * Class: java/lang/reflect/VMField
295 : * Method: getBoolean
296 : * Signature: (Ljava/lang/Object;)Z
297 : */
298 0 : JNIEXPORT jboolean JNICALL Java_java_lang_reflect_VMField_getBoolean(JNIEnv *env, jobject _this, jobject o)
299 : {
300 0 : java_lang_reflect_VMField rvmf(_this);
301 0 : fieldinfo* f = rvmf.get_field();
302 :
303 : // Check if the field can be accessed.
304 0 : if (!_field_access_check(rvmf, f, o))
305 0 : return 0;
306 :
307 : // Check the field type and return the value.
308 0 : switch (f->parseddesc->primitivetype) {
309 : case PRIMITIVETYPE_BOOLEAN:
310 0 : return (jint) _field_get_int(f, o);
311 : default:
312 0 : exceptions_throw_illegalargumentexception();
313 0 : return 0;
314 0 : }
315 : }
316 :
317 :
318 : /*
319 : * Class: java/lang/reflect/VMField
320 : * Method: getByte
321 : * Signature: (Ljava/lang/Object;)B
322 : */
323 0 : JNIEXPORT jbyte JNICALL Java_java_lang_reflect_VMField_getByte(JNIEnv *env, jobject _this, jobject o)
324 : {
325 0 : java_lang_reflect_VMField rvmf(_this);
326 0 : fieldinfo* f = rvmf.get_field();
327 :
328 : // Check if the field can be accessed.
329 0 : if (!_field_access_check(rvmf, f, o))
330 0 : return 0;
331 :
332 : // Check the field type and return the value.
333 0 : switch (f->parseddesc->primitivetype) {
334 : case PRIMITIVETYPE_BYTE:
335 0 : return (jint) _field_get_int(f, o);
336 : default:
337 0 : exceptions_throw_illegalargumentexception();
338 0 : return 0;
339 0 : }
340 : }
341 :
342 :
343 : /*
344 : * Class: java/lang/reflect/VMField
345 : * Method: getChar
346 : * Signature: (Ljava/lang/Object;)C
347 : */
348 0 : JNIEXPORT jchar JNICALL Java_java_lang_reflect_VMField_getChar(JNIEnv *env, jobject _this, jobject o)
349 : {
350 0 : java_lang_reflect_VMField rvmf(_this);
351 0 : fieldinfo* f = rvmf.get_field();
352 :
353 : // Check if the field can be accessed.
354 0 : if (!_field_access_check(rvmf, f, o))
355 0 : return 0;
356 :
357 : // Check the field type and return the value.
358 0 : switch (f->parseddesc->primitivetype) {
359 : case PRIMITIVETYPE_CHAR:
360 0 : return (jint) _field_get_int(f, o);
361 : default:
362 0 : exceptions_throw_illegalargumentexception();
363 0 : return 0;
364 0 : }
365 : }
366 :
367 :
368 : /*
369 : * Class: java/lang/reflect/VMField
370 : * Method: getShort
371 : * Signature: (Ljava/lang/Object;)S
372 : */
373 0 : JNIEXPORT jshort JNICALL Java_java_lang_reflect_VMField_getShort(JNIEnv *env, jobject _this, jobject o)
374 : {
375 0 : java_lang_reflect_VMField rvmf(_this);
376 0 : fieldinfo* f = rvmf.get_field();
377 :
378 : // Check if the field can be accessed.
379 0 : if (!_field_access_check(rvmf, f, o))
380 0 : return 0;
381 :
382 : // Check the field type and return the value.
383 0 : switch (f->parseddesc->primitivetype) {
384 : case PRIMITIVETYPE_BYTE:
385 : case PRIMITIVETYPE_SHORT:
386 0 : return (jint) _field_get_int(f, o);
387 : default:
388 0 : exceptions_throw_illegalargumentexception();
389 0 : return 0;
390 0 : }
391 : }
392 :
393 :
394 : /*
395 : * Class: java/lang/reflect/VMField
396 : * Method: getInt
397 : * Signature: (Ljava/lang/Object;)I
398 : */
399 1 : JNIEXPORT jint JNICALL Java_java_lang_reflect_VMField_getInt(JNIEnv *env , jobject _this, jobject o)
400 : {
401 1 : java_lang_reflect_VMField rvmf(_this);
402 1 : fieldinfo* f = rvmf.get_field();
403 :
404 : // Check if the field can be accessed.
405 1 : if (!_field_access_check(rvmf, f, o))
406 0 : return 0;
407 :
408 : // Check the field type and return the value.
409 1 : switch (f->parseddesc->primitivetype) {
410 : case PRIMITIVETYPE_BYTE:
411 : case PRIMITIVETYPE_CHAR:
412 : case PRIMITIVETYPE_SHORT:
413 : case PRIMITIVETYPE_INT:
414 1 : return (jint) _field_get_int(f, o);
415 : default:
416 0 : exceptions_throw_illegalargumentexception();
417 0 : return 0;
418 0 : }
419 : }
420 :
421 :
422 : /*
423 : * Class: java/lang/reflect/VMField
424 : * Method: getLong
425 : * Signature: (Ljava/lang/Object;)J
426 : */
427 0 : JNIEXPORT jlong JNICALL Java_java_lang_reflect_VMField_getLong(JNIEnv *env, jobject _this, jobject o)
428 : {
429 0 : java_lang_reflect_VMField rvmf(_this);
430 0 : fieldinfo* f = rvmf.get_field();
431 :
432 : // Check if the field can be accessed.
433 0 : if (!_field_access_check(rvmf, f, o))
434 0 : return 0;
435 :
436 : // Check the field type and return the value.
437 0 : switch (f->parseddesc->primitivetype) {
438 : case PRIMITIVETYPE_BYTE:
439 : case PRIMITIVETYPE_CHAR:
440 : case PRIMITIVETYPE_SHORT:
441 : case PRIMITIVETYPE_INT:
442 0 : return (jlong) _field_get_int(f, o);
443 : case PRIMITIVETYPE_LONG:
444 0 : return (jlong) _field_get_long(f, o);
445 : default:
446 0 : exceptions_throw_illegalargumentexception();
447 0 : return 0;
448 0 : }
449 : }
450 :
451 :
452 : /*
453 : * Class: java/lang/reflect/VMField
454 : * Method: getFloat
455 : * Signature: (Ljava/lang/Object;)F
456 : */
457 0 : JNIEXPORT jfloat JNICALL Java_java_lang_reflect_VMField_getFloat(JNIEnv *env, jobject _this, jobject o)
458 : {
459 0 : java_lang_reflect_VMField rvmf(_this);
460 0 : fieldinfo* f = rvmf.get_field();
461 :
462 : // Check if the field can be accessed.
463 0 : if (!_field_access_check(rvmf, f, o))
464 0 : return 0;
465 :
466 : // Check the field type and return the value.
467 0 : switch (f->parseddesc->primitivetype) {
468 : case PRIMITIVETYPE_BYTE:
469 : case PRIMITIVETYPE_CHAR:
470 : case PRIMITIVETYPE_SHORT:
471 : case PRIMITIVETYPE_INT:
472 0 : return (jfloat) _field_get_int(f, o);
473 : case PRIMITIVETYPE_LONG:
474 0 : return (jfloat) _field_get_long(f, o);
475 : case PRIMITIVETYPE_FLOAT:
476 0 : return (jfloat) _field_get_float(f, o);
477 : default:
478 0 : exceptions_throw_illegalargumentexception();
479 0 : return 0;
480 0 : }
481 : }
482 :
483 :
484 : /*
485 : * Class: java/lang/reflect/VMField
486 : * Method: getDouble
487 : * Signature: (Ljava/lang/Object;)D
488 : */
489 0 : JNIEXPORT jdouble JNICALL Java_java_lang_reflect_VMField_getDouble(JNIEnv *env , jobject _this, jobject o)
490 : {
491 0 : java_lang_reflect_VMField rvmf(_this);
492 0 : fieldinfo* f = rvmf.get_field();
493 :
494 : // Check if the field can be accessed.
495 0 : if (!_field_access_check(rvmf, f, o))
496 0 : return 0;
497 :
498 : // Check the field type and return the value.
499 0 : switch (f->parseddesc->primitivetype) {
500 : case PRIMITIVETYPE_BYTE:
501 : case PRIMITIVETYPE_CHAR:
502 : case PRIMITIVETYPE_SHORT:
503 : case PRIMITIVETYPE_INT:
504 0 : return (jdouble) _field_get_int(f, o);
505 : case PRIMITIVETYPE_LONG:
506 0 : return (jdouble) _field_get_long(f, o);
507 : case PRIMITIVETYPE_FLOAT:
508 0 : return (jdouble) _field_get_float(f, o);
509 : case PRIMITIVETYPE_DOUBLE:
510 0 : return (jdouble) _field_get_double(f, o);
511 : default:
512 0 : exceptions_throw_illegalargumentexception();
513 0 : return 0;
514 0 : }
515 : }
516 :
517 :
518 : /*
519 : * Class: java/lang/reflect/VMField
520 : * Method: set
521 : * Signature: (Ljava/lang/Object;Ljava/lang/Object;)V
522 : */
523 22 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_set(JNIEnv *env, jobject _this, jobject o, jobject value)
524 : {
525 22 : java_lang_reflect_VMField rvmf(_this);
526 22 : fieldinfo* df = rvmf.get_field();
527 :
528 : classinfo *sc;
529 : fieldinfo *sf;
530 :
531 : // Check if the field can be accessed.
532 22 : if (!_field_access_check(rvmf, df, o))
533 : return;
534 :
535 : // Get the source classinfo from the object.
536 22 : if (value == NULL)
537 0 : sc = NULL;
538 : else
539 22 : LLNI_class_get(value, sc);
540 :
541 : /* The fieldid is used to set the new value, for primitive
542 : types the value has to be retrieved from the wrapping
543 : object */
544 :
545 22 : switch (df->parseddesc->primitivetype) {
546 : case PRIMITIVETYPE_BOOLEAN: {
547 : int32_t val;
548 :
549 : /* determine the field to read the value */
550 :
551 0 : if ((sc == NULL) || !(sf = class_findfield(sc, utf8::value, utf8::Z)))
552 0 : break;
553 :
554 0 : switch (sf->parseddesc->primitivetype) {
555 : case PRIMITIVETYPE_BOOLEAN:
556 0 : val = java_lang_Boolean(value).get_value();
557 : break;
558 : default:
559 0 : exceptions_throw_illegalargumentexception();
560 : return;
561 : }
562 :
563 0 : _field_set_int(df, o, val);
564 : return;
565 : }
566 :
567 : case PRIMITIVETYPE_BYTE: {
568 : int32_t val;
569 :
570 0 : if ((sc == NULL) || !(sf = class_findfield(sc, utf8::value, utf8::B)))
571 0 : break;
572 :
573 0 : switch (sf->parseddesc->primitivetype) {
574 : case PRIMITIVETYPE_BYTE:
575 0 : val = java_lang_Byte(value).get_value();
576 : break;
577 : default:
578 0 : exceptions_throw_illegalargumentexception();
579 : return;
580 : }
581 :
582 0 : _field_set_int(df, o, val);
583 : return;
584 : }
585 :
586 : case PRIMITIVETYPE_CHAR: {
587 : int32_t val;
588 :
589 0 : if ((sc == NULL) || !(sf = class_findfield(sc, utf8::value, utf8::C)))
590 0 : break;
591 :
592 0 : switch (sf->parseddesc->primitivetype) {
593 : case PRIMITIVETYPE_CHAR:
594 0 : val = java_lang_Character(value).get_value();
595 : break;
596 : default:
597 0 : exceptions_throw_illegalargumentexception();
598 : return;
599 : }
600 :
601 0 : _field_set_int(df, o, val);
602 : return;
603 : }
604 :
605 : case PRIMITIVETYPE_SHORT: {
606 : int32_t val;
607 :
608 : /* get field only by name, it can be one of B, S */
609 :
610 0 : if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf8::value)))
611 0 : break;
612 :
613 0 : switch (sf->parseddesc->primitivetype) {
614 : case PRIMITIVETYPE_BYTE:
615 0 : val = java_lang_Byte(value).get_value();
616 0 : break;
617 : case PRIMITIVETYPE_SHORT:
618 0 : val = java_lang_Short(value).get_value();
619 0 : break;
620 : default:
621 0 : exceptions_throw_illegalargumentexception();
622 : return;
623 : }
624 :
625 0 : _field_set_int(df, o, val);
626 : return;
627 : }
628 :
629 : case PRIMITIVETYPE_INT: {
630 : int32_t val;
631 :
632 : /* get field only by name, it can be one of B, S, C, I */
633 :
634 0 : if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf8::value)))
635 0 : break;
636 :
637 0 : switch (sf->parseddesc->primitivetype) {
638 : case PRIMITIVETYPE_BYTE:
639 0 : val = java_lang_Byte(value).get_value();
640 0 : break;
641 : case PRIMITIVETYPE_CHAR:
642 0 : val = java_lang_Character(value).get_value();
643 0 : break;
644 : case PRIMITIVETYPE_SHORT:
645 0 : val = java_lang_Short(value).get_value();
646 0 : break;
647 : case PRIMITIVETYPE_INT:
648 0 : val = java_lang_Integer(value).get_value();
649 0 : break;
650 : default:
651 0 : exceptions_throw_illegalargumentexception();
652 : return;
653 : }
654 :
655 0 : _field_set_int(df, o, val);
656 : return;
657 : }
658 :
659 : case PRIMITIVETYPE_LONG: {
660 : int64_t val;
661 :
662 : /* get field only by name, it can be one of B, S, C, I, J */
663 :
664 0 : if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf8::value)))
665 0 : break;
666 :
667 0 : switch (sf->parseddesc->primitivetype) {
668 : case PRIMITIVETYPE_BYTE:
669 0 : val = java_lang_Byte(value).get_value();
670 0 : break;
671 : case PRIMITIVETYPE_CHAR:
672 0 : val = java_lang_Character(value).get_value();
673 0 : break;
674 : case PRIMITIVETYPE_SHORT:
675 0 : val = java_lang_Short(value).get_value();
676 0 : break;
677 : case PRIMITIVETYPE_INT:
678 0 : val = java_lang_Integer(value).get_value();
679 0 : break;
680 : case PRIMITIVETYPE_LONG:
681 0 : val = java_lang_Long(value).get_value();
682 0 : break;
683 : default:
684 0 : exceptions_throw_illegalargumentexception();
685 : return;
686 : }
687 :
688 0 : _field_set_long(df, o, val);
689 : return;
690 : }
691 :
692 : case PRIMITIVETYPE_FLOAT: {
693 : float val;
694 :
695 : /* get field only by name, it can be one of B, S, C, I, J, F */
696 :
697 0 : if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf8::value)))
698 0 : break;
699 :
700 0 : switch (sf->parseddesc->primitivetype) {
701 : case PRIMITIVETYPE_BYTE:
702 0 : val = java_lang_Byte(value).get_value();
703 0 : break;
704 : case PRIMITIVETYPE_CHAR:
705 0 : val = java_lang_Character(value).get_value();
706 0 : break;
707 : case PRIMITIVETYPE_SHORT:
708 0 : val = java_lang_Short(value).get_value();
709 0 : break;
710 : case PRIMITIVETYPE_INT:
711 0 : val = java_lang_Integer(value).get_value();
712 0 : break;
713 : case PRIMITIVETYPE_LONG:
714 0 : val = java_lang_Long(value).get_value();
715 0 : break;
716 : case PRIMITIVETYPE_FLOAT:
717 0 : val = java_lang_Float(value).get_value();
718 0 : break;
719 : default:
720 0 : exceptions_throw_illegalargumentexception();
721 : return;
722 : }
723 :
724 0 : _field_set_float(df, o, val);
725 : return;
726 : }
727 :
728 : case PRIMITIVETYPE_DOUBLE: {
729 : double val;
730 :
731 : /* get field only by name, it can be one of B, S, C, I, J, F, D */
732 :
733 0 : if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf8::value)))
734 0 : break;
735 :
736 0 : switch (sf->parseddesc->primitivetype) {
737 : case PRIMITIVETYPE_BYTE:
738 0 : val = java_lang_Byte(value).get_value();
739 0 : break;
740 : case PRIMITIVETYPE_CHAR:
741 0 : val = java_lang_Character(value).get_value();
742 0 : break;
743 : case PRIMITIVETYPE_SHORT:
744 0 : val = java_lang_Short(value).get_value();
745 0 : break;
746 : case PRIMITIVETYPE_INT:
747 0 : val = java_lang_Integer(value).get_value();
748 0 : break;
749 : case PRIMITIVETYPE_LONG:
750 0 : val = java_lang_Long(value).get_value();
751 0 : break;
752 : case PRIMITIVETYPE_FLOAT:
753 0 : val = java_lang_Float(value).get_value();
754 0 : break;
755 : case PRIMITIVETYPE_DOUBLE:
756 0 : val = java_lang_Double(value).get_value();
757 0 : break;
758 : default:
759 0 : exceptions_throw_illegalargumentexception();
760 : return;
761 : }
762 :
763 0 : _field_set_double(df, o, val);
764 : return;
765 : }
766 :
767 : case TYPE_ADR:
768 : /* check if value is an instance of the destination class */
769 :
770 : /* XXX TODO */
771 : /* if (!builtin_instanceof((java_handle_t *) value, df->class)) */
772 : /* break; */
773 :
774 22 : _field_set_handle(df, o, value);
775 : return;
776 :
777 : default:
778 0 : assert(false);
779 : break;
780 : }
781 :
782 : /* raise exception */
783 :
784 0 : exceptions_throw_illegalargumentexception();
785 : }
786 :
787 :
788 : /*
789 : * Class: java/lang/reflect/VMField
790 : * Method: setBoolean
791 : * Signature: (Ljava/lang/Object;Z)V
792 : */
793 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setBoolean(JNIEnv *env, jobject _this, jobject o, jboolean value)
794 : {
795 0 : java_lang_reflect_VMField rvmf(_this);
796 0 : fieldinfo* f = rvmf.get_field();
797 :
798 : // Check if the field can be accessed.
799 0 : if (!_field_access_check(rvmf, f, o))
800 : return;
801 :
802 : // Check the field type and set the value.
803 0 : switch (f->parseddesc->primitivetype) {
804 : case PRIMITIVETYPE_BOOLEAN:
805 0 : _field_set_int(f, o, value);
806 0 : break;
807 : default:
808 0 : exceptions_throw_illegalargumentexception();
809 0 : }
810 : }
811 :
812 :
813 : /*
814 : * Class: java/lang/reflect/VMField
815 : * Method: setByte
816 : * Signature: (Ljava/lang/Object;B)V
817 : */
818 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setByte(JNIEnv *env, jobject _this, jobject o, jbyte value)
819 : {
820 0 : java_lang_reflect_VMField rvmf(_this);
821 0 : fieldinfo* f = rvmf.get_field();
822 :
823 : // Check if the field can be accessed.
824 0 : if (!_field_access_check(rvmf, f, o))
825 : return;
826 :
827 : // Check the field type and set the value.
828 0 : switch (f->parseddesc->primitivetype) {
829 : case PRIMITIVETYPE_BYTE:
830 : case PRIMITIVETYPE_SHORT:
831 : case PRIMITIVETYPE_INT:
832 0 : _field_set_int(f, o, value);
833 0 : break;
834 : case PRIMITIVETYPE_LONG:
835 0 : _field_set_long(f, o, value);
836 0 : break;
837 : case PRIMITIVETYPE_FLOAT:
838 0 : _field_set_float(f, o, value);
839 0 : break;
840 : case PRIMITIVETYPE_DOUBLE:
841 0 : _field_set_double(f, o, value);
842 0 : break;
843 : default:
844 0 : exceptions_throw_illegalargumentexception();
845 0 : }
846 : }
847 :
848 :
849 : /*
850 : * Class: java/lang/reflect/VMField
851 : * Method: setChar
852 : * Signature: (Ljava/lang/Object;C)V
853 : */
854 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setChar(JNIEnv *env, jobject _this, jobject o, jchar value)
855 : {
856 0 : java_lang_reflect_VMField rvmf(_this);
857 0 : fieldinfo* f = rvmf.get_field();
858 :
859 : // Check if the field can be accessed.
860 0 : if (!_field_access_check(rvmf, f, o))
861 : return;
862 :
863 : // Check the field type and set the value.
864 0 : switch (f->parseddesc->primitivetype) {
865 : case PRIMITIVETYPE_CHAR:
866 : case PRIMITIVETYPE_INT:
867 0 : _field_set_int(f, o, value);
868 0 : break;
869 : case PRIMITIVETYPE_LONG:
870 0 : _field_set_long(f, o, value);
871 0 : break;
872 : case PRIMITIVETYPE_FLOAT:
873 0 : _field_set_float(f, o, value);
874 0 : break;
875 : case PRIMITIVETYPE_DOUBLE:
876 0 : _field_set_double(f, o, value);
877 0 : break;
878 : default:
879 0 : exceptions_throw_illegalargumentexception();
880 0 : }
881 : }
882 :
883 :
884 : /*
885 : * Class: java/lang/reflect/VMField
886 : * Method: setShort
887 : * Signature: (Ljava/lang/Object;S)V
888 : */
889 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setShort(JNIEnv *env, jobject _this, jobject o, jshort value)
890 : {
891 0 : java_lang_reflect_VMField rvmf(_this);
892 0 : fieldinfo* f = rvmf.get_field();
893 :
894 : // Check if the field can be accessed.
895 0 : if (!_field_access_check(rvmf, f, o))
896 : return;
897 :
898 : // Check the field type and set the value.
899 0 : switch (f->parseddesc->primitivetype) {
900 : case PRIMITIVETYPE_SHORT:
901 : case PRIMITIVETYPE_INT:
902 0 : _field_set_int(f, o, value);
903 0 : break;
904 : case PRIMITIVETYPE_LONG:
905 0 : _field_set_long(f, o, value);
906 0 : break;
907 : case PRIMITIVETYPE_FLOAT:
908 0 : _field_set_float(f, o, value);
909 0 : break;
910 : case PRIMITIVETYPE_DOUBLE:
911 0 : _field_set_double(f, o, value);
912 0 : break;
913 : default:
914 0 : exceptions_throw_illegalargumentexception();
915 0 : }
916 : }
917 :
918 :
919 : /*
920 : * Class: java/lang/reflect/VMField
921 : * Method: setInt
922 : * Signature: (Ljava/lang/Object;I)V
923 : */
924 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setInt(JNIEnv *env, jobject _this, jobject o, jint value)
925 : {
926 0 : java_lang_reflect_VMField rvmf(_this);
927 0 : fieldinfo* f = rvmf.get_field();
928 :
929 : // Check if the field can be accessed.
930 0 : if (!_field_access_check(rvmf, f, o))
931 : return;
932 :
933 : // Check the field type and set the value.
934 0 : switch (f->parseddesc->primitivetype) {
935 : case PRIMITIVETYPE_INT:
936 0 : _field_set_int(f, o, value);
937 0 : break;
938 : case PRIMITIVETYPE_LONG:
939 0 : _field_set_long(f, o, value);
940 0 : break;
941 : case PRIMITIVETYPE_FLOAT:
942 0 : _field_set_float(f, o, value);
943 0 : break;
944 : case PRIMITIVETYPE_DOUBLE:
945 0 : _field_set_double(f, o, value);
946 0 : break;
947 : default:
948 0 : exceptions_throw_illegalargumentexception();
949 0 : }
950 : }
951 :
952 :
953 : /*
954 : * Class: java/lang/reflect/VMField
955 : * Method: setLong
956 : * Signature: (Ljava/lang/Object;J)V
957 : */
958 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setLong(JNIEnv *env, jobject _this, jobject o, jlong value)
959 : {
960 0 : java_lang_reflect_VMField rvmf(_this);
961 0 : fieldinfo* f = rvmf.get_field();
962 :
963 : // Check if the field can be accessed.
964 0 : if (!_field_access_check(rvmf, f, o))
965 : return;
966 :
967 : // Check the field type and set the value.
968 0 : switch (f->parseddesc->primitivetype) {
969 : case PRIMITIVETYPE_LONG:
970 0 : _field_set_long(f, o, value);
971 0 : break;
972 : case PRIMITIVETYPE_FLOAT:
973 0 : _field_set_float(f, o, value);
974 0 : break;
975 : case PRIMITIVETYPE_DOUBLE:
976 0 : _field_set_double(f, o, value);
977 0 : break;
978 : default:
979 0 : exceptions_throw_illegalargumentexception();
980 0 : }
981 : }
982 :
983 :
984 : /*
985 : * Class: java/lang/reflect/VMField
986 : * Method: setFloat
987 : * Signature: (Ljava/lang/Object;F)V
988 : */
989 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setFloat(JNIEnv *env, jobject _this, jobject o, jfloat value)
990 : {
991 0 : java_lang_reflect_VMField rvmf(_this);
992 0 : fieldinfo* f = rvmf.get_field();
993 :
994 : // Check if the field can be accessed.
995 0 : if (!_field_access_check(rvmf, f, o))
996 : return;
997 :
998 : // Check the field type and set the value.
999 0 : switch (f->parseddesc->primitivetype) {
1000 : case PRIMITIVETYPE_FLOAT:
1001 0 : _field_set_float(f, o, value);
1002 0 : break;
1003 : case PRIMITIVETYPE_DOUBLE:
1004 0 : _field_set_double(f, o, value);
1005 0 : break;
1006 : default:
1007 0 : exceptions_throw_illegalargumentexception();
1008 0 : }
1009 : }
1010 :
1011 :
1012 : /*
1013 : * Class: java/lang/reflect/VMField
1014 : * Method: setDouble
1015 : * Signature: (Ljava/lang/Object;D)V
1016 : */
1017 0 : JNIEXPORT void JNICALL Java_java_lang_reflect_VMField_setDouble(JNIEnv *env, jobject _this, jobject o, jdouble value)
1018 : {
1019 0 : java_lang_reflect_VMField rvmf(_this);
1020 0 : fieldinfo* f = rvmf.get_field();
1021 :
1022 : // Check if the field can be accessed.
1023 0 : if (!_field_access_check(rvmf, f, o))
1024 : return;
1025 :
1026 : // Check the field type and set the value.
1027 0 : switch (f->parseddesc->primitivetype) {
1028 : case PRIMITIVETYPE_DOUBLE:
1029 0 : _field_set_double(f, o, value);
1030 0 : break;
1031 : default:
1032 0 : exceptions_throw_illegalargumentexception();
1033 0 : }
1034 : }
1035 :
1036 :
1037 : /*
1038 : * Class: java/lang/reflect/VMField
1039 : * Method: getSignature
1040 : * Signature: ()Ljava/lang/String;
1041 : */
1042 0 : JNIEXPORT jstring JNICALL Java_java_lang_reflect_VMField_getSignature(JNIEnv *env, jobject _this)
1043 : {
1044 0 : java_lang_reflect_VMField rvmf(_this);
1045 0 : fieldinfo* f = rvmf.get_field();
1046 :
1047 0 : if (f->signature == NULL)
1048 0 : return NULL;
1049 :
1050 0 : java_handle_t* o = JavaString::from_utf8(f->signature);
1051 :
1052 : /* in error case o is NULL */
1053 :
1054 0 : return o;
1055 : }
1056 :
1057 :
1058 : #if defined(ENABLE_ANNOTATIONS)
1059 : /*
1060 : * Class: java/lang/reflect/VMField
1061 : * Method: declaredAnnotations
1062 : * Signature: ()Ljava/util/Map;
1063 : */
1064 41 : JNIEXPORT jobject JNICALL Java_java_lang_reflect_VMField_declaredAnnotations(JNIEnv *env, jobject _this)
1065 : {
1066 41 : java_lang_reflect_VMField rvmf(_this);
1067 :
1068 41 : java_handle_t* declaredAnnotations = rvmf.get_declaredAnnotations();
1069 :
1070 : // Are the annotations parsed yet?
1071 41 : if (declaredAnnotations == NULL) {
1072 31 : java_handle_bytearray_t* annotations = rvmf.get_annotations();
1073 31 : classinfo* declaringClass = rvmf.get_clazz();
1074 31 : classinfo* referer = rvmf.get_Class();
1075 :
1076 31 : declaredAnnotations = Reflection::get_declaredannotations(annotations, declaringClass, referer);
1077 :
1078 31 : rvmf.set_declaredAnnotations(declaredAnnotations);
1079 : }
1080 :
1081 41 : return (jobject) declaredAnnotations;
1082 : }
1083 : #endif
1084 :
1085 : } // extern "C"
1086 :
1087 :
1088 : /* native methods implemented by this file ************************************/
1089 :
1090 : static const JNINativeMethod methods[] = {
1091 : { (char*) "getModifiersInternal", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getModifiersInternal },
1092 : { (char*) "getType", (char*) "()Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getType },
1093 : { (char*) "get", (char*) "(Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_get },
1094 : { (char*) "getBoolean", (char*) "(Ljava/lang/Object;)Z", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getBoolean },
1095 : { (char*) "getByte", (char*) "(Ljava/lang/Object;)B", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getByte },
1096 : { (char*) "getChar", (char*) "(Ljava/lang/Object;)C", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getChar },
1097 : { (char*) "getShort", (char*) "(Ljava/lang/Object;)S", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getShort },
1098 : { (char*) "getInt", (char*) "(Ljava/lang/Object;)I", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getInt },
1099 : { (char*) "getLong", (char*) "(Ljava/lang/Object;)J", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getLong },
1100 : { (char*) "getFloat", (char*) "(Ljava/lang/Object;)F", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getFloat },
1101 : { (char*) "getDouble", (char*) "(Ljava/lang/Object;)D", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getDouble },
1102 : { (char*) "set", (char*) "(Ljava/lang/Object;Ljava/lang/Object;)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_set },
1103 : { (char*) "setBoolean", (char*) "(Ljava/lang/Object;Z)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setBoolean },
1104 : { (char*) "setByte", (char*) "(Ljava/lang/Object;B)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setByte },
1105 : { (char*) "setChar", (char*) "(Ljava/lang/Object;C)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setChar },
1106 : { (char*) "setShort", (char*) "(Ljava/lang/Object;S)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setShort },
1107 : { (char*) "setInt", (char*) "(Ljava/lang/Object;I)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setInt },
1108 : { (char*) "setLong", (char*) "(Ljava/lang/Object;J)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setLong },
1109 : { (char*) "setFloat", (char*) "(Ljava/lang/Object;F)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setFloat },
1110 : { (char*) "setDouble", (char*) "(Ljava/lang/Object;D)V", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_setDouble },
1111 : { (char*) "getSignature", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_getSignature },
1112 : #if defined(ENABLE_ANNOTATIONS)
1113 : { (char*) "declaredAnnotations", (char*) "()Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_reflect_VMField_declaredAnnotations },
1114 : #endif
1115 : };
1116 :
1117 :
1118 : /* _Jv_java_lang_reflect_VMField_init ******************************************
1119 :
1120 : Register native functions.
1121 :
1122 : *******************************************************************************/
1123 :
1124 163 : void _Jv_java_lang_reflect_VMField_init(void)
1125 : {
1126 163 : Utf8String u = Utf8String::from_utf8("java/lang/reflect/VMField");
1127 :
1128 163 : NativeMethods& nm = VM::get_current()->get_nativemethods();
1129 163 : nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
1130 163 : }
1131 :
1132 :
1133 : /*
1134 : * These are local overrides for various environment variables in Emacs.
1135 : * Please do not remove this and leave it at the end of the file, where
1136 : * Emacs will automagically detect them.
1137 : * ---------------------------------------------------------------------
1138 : * Local variables:
1139 : * mode: c++
1140 : * indent-tabs-mode: t
1141 : * c-basic-offset: 4
1142 : * tab-width: 4
1143 : * End:
1144 : * vim:noexpandtab:sw=4:ts=4:
1145 : */
|