e79dce6957c21af3b95e39d10ca9fda137280088
[cacao.git] / src / native / vm / gnu / java_lang_reflect_Field.c
1 /* src/native/vm/gnu/java_lang_reflect_Field.c
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: java_lang_reflect_Field.c 8393 2007-08-22 01:10:09Z panzi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "native/jni.h"
36 #include "native/llni.h"
37 #include "native/native.h"
38
39 #include "native/include/java_lang_Boolean.h"
40 #include "native/include/java_lang_Byte.h"
41 #include "native/include/java_lang_Character.h"
42 #include "native/include/java_lang_Short.h"
43 #include "native/include/java_lang_Integer.h"
44 #include "native/include/java_lang_Long.h"
45 #include "native/include/java_lang_Float.h"
46 #include "native/include/java_lang_Double.h"
47 #include "native/include/java_lang_Object.h"
48 #include "native/include/java_lang_Class.h"
49 #include "native/include/java_lang_String.h"
50
51 #include "native/include/java_lang_reflect_Field.h"
52
53 #if defined(ENABLE_ANNOTATIONS)
54 #include "native/include/sun_reflect_ConstantPool.h"
55 #include "native/vm/reflect.h"
56 #endif
57
58 #include "vm/access.h"
59 #include "vm/builtin.h"
60 #include "vm/exceptions.h"
61 #include "vm/global.h"
62 #include "vm/initialize.h"
63 #include "vm/primitive.h"
64 #include "vm/resolve.h"
65 #include "vm/stringlocal.h"
66
67 #include "vm/jit/stacktrace.h"
68
69 #include "vmcore/loader.h"
70 #include "vmcore/utf8.h"
71
72
73 /* native methods implemented by this file ************************************/
74
75 static JNINativeMethod methods[] = {
76         { "getModifiersInternal", "()I",                                     (void *) (intptr_t) &Java_java_lang_reflect_Field_getModifiersInternal },
77         { "getType",              "()Ljava/lang/Class;",                     (void *) (intptr_t) &Java_java_lang_reflect_Field_getType              },
78         { "get",                  "(Ljava/lang/Object;)Ljava/lang/Object;",  (void *) (intptr_t) &Java_java_lang_reflect_Field_get                  },
79         { "getBoolean",           "(Ljava/lang/Object;)Z",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getBoolean           },
80         { "getByte",              "(Ljava/lang/Object;)B",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getByte              },
81         { "getChar",              "(Ljava/lang/Object;)C",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getChar              },
82         { "getShort",             "(Ljava/lang/Object;)S",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getShort             },
83         { "getInt",               "(Ljava/lang/Object;)I",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getInt               },
84         { "getLong",              "(Ljava/lang/Object;)J",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getLong              },
85         { "getFloat",             "(Ljava/lang/Object;)F",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getFloat             },
86         { "getDouble",            "(Ljava/lang/Object;)D",                   (void *) (intptr_t) &Java_java_lang_reflect_Field_getDouble            },
87         { "set",                  "(Ljava/lang/Object;Ljava/lang/Object;)V", (void *) (intptr_t) &Java_java_lang_reflect_Field_set                  },
88         { "setBoolean",           "(Ljava/lang/Object;Z)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setBoolean           },
89         { "setByte",              "(Ljava/lang/Object;B)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setByte              },
90         { "setChar",              "(Ljava/lang/Object;C)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setChar              },
91         { "setShort",             "(Ljava/lang/Object;S)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setShort             },
92         { "setInt",               "(Ljava/lang/Object;I)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setInt               },
93         { "setLong",              "(Ljava/lang/Object;J)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setLong              },
94         { "setFloat",             "(Ljava/lang/Object;F)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setFloat             },
95         { "setDouble",            "(Ljava/lang/Object;D)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setDouble            },
96         { "getSignature",         "()Ljava/lang/String;",                    (void *) (intptr_t) &Java_java_lang_reflect_Field_getSignature         },
97 #if defined(ENABLE_ANNOTATIONS)
98         { "declaredAnnotations",  "()Ljava/util/Map;",                       (void *) (intptr_t) &Java_java_lang_reflect_Field_declaredAnnotations  },
99 #endif
100 };
101
102
103 /* _Jv_java_lang_reflect_Field_init ********************************************
104
105    Register native functions.
106
107 *******************************************************************************/
108
109 void _Jv_java_lang_reflect_Field_init(void)
110 {
111         utf *u;
112
113         u = utf_new_char("java/lang/reflect/Field");
114
115         native_method_register(u, methods, NATIVE_METHODS_COUNT);
116 }
117
118
119 /* cacao_get_field_address *****************************************************
120
121    Return the address of a field of an object.
122
123    IN:
124       this.........the field (a java.lang.reflect.Field object)
125           o............the object of which to get the field
126
127    RETURN VALUE:
128       a pointer to the field, or
129           NULL if an exception has been thrown
130
131 *******************************************************************************/
132
133 static void *cacao_get_field_address(java_lang_reflect_Field *this,
134                                                                          java_lang_Object *o)
135 {
136         classinfo *c;
137         fieldinfo *f;
138         int32_t    slot;
139         int32_t    flag;
140
141         LLNI_field_get_cls(this, clazz, c);
142         LLNI_field_get_val(this, slot , slot);
143         f = &c->fields[slot];
144
145         /* check field access */
146         /* check if we should bypass security checks (AccessibleObject) */
147
148         LLNI_field_get_val(this, flag, flag);
149         if (flag == false) {
150                 /* this function is always called like this:
151
152                            java.lang.reflect.Field.xxx (Native Method)
153                    [0] <caller>
154                 */
155                 if (!access_check_field(f, 0))
156                         return NULL;
157         }
158
159         /* get the address of the field */
160
161         if (f->flags & ACC_STATIC) {
162                 /* initialize class if required */
163
164                 if (!(c->state & CLASS_INITIALIZED))
165                         if (!initialize_class(c))
166                                 return NULL;
167
168                 /* return value pointer */
169
170                 return f->value;
171
172         } else {
173                 /* obj is required for not-static fields */
174
175                 if (o == NULL) {
176                         exceptions_throw_nullpointerexception();
177                         return NULL;
178                 }
179         
180                 if (builtin_instanceof((java_handle_t *) o, c))
181                         return (void *) (((intptr_t) o) + f->offset);
182         }
183
184         /* exception path */
185
186         exceptions_throw_illegalargumentexception();
187
188         return NULL;
189 }
190
191
192 /*
193  * Class:     java/lang/reflect/Field
194  * Method:    getModifiersInternal
195  * Signature: ()I
196  */
197 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getModifiersInternal(JNIEnv *env, java_lang_reflect_Field *this)
198 {
199         classinfo *c;
200         fieldinfo *f;
201         int32_t    slot;
202
203         LLNI_field_get_cls(this, clazz, c);
204         LLNI_field_get_val(this, slot , slot);
205         f = &(c->fields[slot]);
206
207         return f->flags;
208 }
209
210
211 /*
212  * Class:     java/lang/reflect/Field
213  * Method:    getType
214  * Signature: ()Ljava/lang/Class;
215  */
216 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Field_getType(JNIEnv *env, java_lang_reflect_Field *this)
217 {
218         classinfo *c;
219         typedesc  *desc;
220         classinfo *ret;
221         int32_t    slot;
222
223         LLNI_field_get_cls(this, clazz, c);
224         LLNI_field_get_val(this, slot , slot);
225         desc = c->fields[slot].parseddesc;
226
227         if (desc == NULL)
228                 return NULL;
229
230         if (!resolve_class_from_typedesc(desc, true, false, &ret))
231                 return NULL;
232         
233         return LLNI_classinfo_wrap(ret);
234 }
235
236
237 /*
238  * Class:     java/lang/reflect/Field
239  * Method:    get
240  * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
241  */
242 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Field_get(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *object)
243 {
244         classinfo *c;
245         fieldinfo *f;
246         void      *addr;
247         int32_t    slot;
248         imm_union  value;
249         java_handle_t *o;
250
251         LLNI_field_get_cls(this, clazz, c);
252         LLNI_field_get_val(this, slot , slot);
253         f = &c->fields[slot];
254
255         /* get address of the source field value */
256
257         if ((addr = cacao_get_field_address(this, object)) == NULL)
258                 return NULL;
259
260         switch (f->parseddesc->decltype) {
261         case PRIMITIVETYPE_BOOLEAN:
262         case PRIMITIVETYPE_BYTE:
263         case PRIMITIVETYPE_CHAR:
264         case PRIMITIVETYPE_SHORT:
265         case PRIMITIVETYPE_INT:
266                 value.i = *((int32_t *) addr);
267                 break;
268
269         case PRIMITIVETYPE_LONG:
270                 value.l = *((int64_t *) addr);
271                 break;
272
273         case PRIMITIVETYPE_FLOAT:
274                 value.f = *((float *) addr);
275                 break;
276
277         case PRIMITIVETYPE_DOUBLE:
278                 value.d = *((double *) addr);
279                 break;
280
281         case TYPE_ADR:
282 #warning this whole thing needs to be inside a critical section!
283                 return (java_lang_Object *) *((java_handle_t **) addr);
284         }
285
286         /* Now box the primitive types. */
287
288         o = primitive_box(f->parseddesc->decltype, value);
289
290         return (java_lang_Object *) o;
291 }
292
293
294 /*
295  * Class:     java/lang/reflect/Field
296  * Method:    getBoolean
297  * Signature: (Ljava/lang/Object;)Z
298  */
299 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getBoolean(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o)
300 {
301         classinfo *c;
302         fieldinfo *f;
303         void      *addr;
304         int32_t    slot;
305
306         /* get the class and the field */
307
308         LLNI_field_get_cls(this, clazz, c);
309         LLNI_field_get_val(this, slot , slot);
310         f = &c->fields[slot];
311
312         /* get the address of the field with an internal helper */
313
314         if ((addr = cacao_get_field_address(this, o)) == NULL)
315                 return 0;
316
317         /* check the field type and return the value */
318
319         switch (f->parseddesc->decltype) {
320         case PRIMITIVETYPE_BOOLEAN:
321                 return (int32_t) *((int32_t *) addr);
322         default:
323                 exceptions_throw_illegalargumentexception();
324                 return 0;
325         }
326 }
327
328
329 /*
330  * Class:     java/lang/reflect/Field
331  * Method:    getByte
332  * Signature: (Ljava/lang/Object;)B
333  */
334 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getByte(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o)
335 {
336         classinfo *c;
337         fieldinfo *f;
338         void      *addr;
339         int32_t    slot;
340
341         /* get the class and the field */
342
343         LLNI_field_get_cls(this, clazz, c);
344         LLNI_field_get_val(this, slot , slot);
345         f = &c->fields[slot];
346
347         /* get the address of the field with an internal helper */
348
349         if ((addr = cacao_get_field_address(this, o)) == NULL)
350                 return 0;
351
352         /* check the field type and return the value */
353
354         switch (f->parseddesc->decltype) {
355         case PRIMITIVETYPE_BYTE:
356                 return (int32_t) *((int32_t *) addr);
357         default:
358                 exceptions_throw_illegalargumentexception();
359                 return 0;
360         }
361 }
362
363
364 /*
365  * Class:     java/lang/reflect/Field
366  * Method:    getChar
367  * Signature: (Ljava/lang/Object;)C
368  */
369 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getChar(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o)
370 {
371         classinfo *c;
372         fieldinfo *f;
373         void      *addr;
374         int32_t    slot;
375
376         /* get the class and the field */
377
378         LLNI_field_get_cls(this, clazz, c);
379         LLNI_field_get_val(this, slot , slot);
380         f = &c->fields[slot];
381
382         /* get the address of the field with an internal helper */
383
384         if ((addr = cacao_get_field_address(this, o)) == NULL)
385                 return 0;
386
387         /* check the field type and return the value */
388
389         switch (f->parseddesc->decltype) {
390         case PRIMITIVETYPE_CHAR:
391                 return (int32_t) *((int32_t *) addr);
392         default:
393                 exceptions_throw_illegalargumentexception();
394                 return 0;
395         }
396 }
397
398
399 /*
400  * Class:     java/lang/reflect/Field
401  * Method:    getShort
402  * Signature: (Ljava/lang/Object;)S
403  */
404 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getShort(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o)
405 {
406         classinfo *c;
407         fieldinfo *f;
408         void      *addr;
409         int32_t    slot;
410
411         /* get the class and the field */
412
413         LLNI_field_get_cls(this, clazz, c);
414         LLNI_field_get_val(this, slot , slot);
415         f = &c->fields[slot];
416
417         /* get the address of the field with an internal helper */
418
419         if ((addr = cacao_get_field_address(this, o)) == NULL)
420                 return 0;
421
422         /* check the field type and return the value */
423
424         switch (f->parseddesc->decltype) {
425         case PRIMITIVETYPE_BYTE:
426         case PRIMITIVETYPE_SHORT:
427                 return (int32_t) *((int32_t *) addr);
428         default:
429                 exceptions_throw_illegalargumentexception();
430                 return 0;
431         }
432 }
433
434
435 /*
436  * Class:     java/lang/reflect/Field
437  * Method:    getInt
438  * Signature: (Ljava/lang/Object;)I
439  */
440 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getInt(JNIEnv *env , java_lang_reflect_Field *this, java_lang_Object *o)
441 {
442         classinfo *c;
443         fieldinfo *f;
444         void      *addr;
445         int32_t    slot;
446
447         /* get the class and the field */
448
449         LLNI_field_get_cls(this, clazz, c);
450         LLNI_field_get_val(this, slot , slot);
451         f = &c->fields[slot];
452
453         /* get the address of the field with an internal helper */
454
455         if ((addr = cacao_get_field_address(this, o)) == NULL)
456                 return 0;
457
458         /* check the field type and return the value */
459
460         switch (f->parseddesc->decltype) {
461         case PRIMITIVETYPE_BYTE:
462         case PRIMITIVETYPE_CHAR:
463         case PRIMITIVETYPE_SHORT:
464         case PRIMITIVETYPE_INT:
465                 return (int32_t) *((int32_t *) addr);
466         default:
467                 exceptions_throw_illegalargumentexception();
468                 return 0;
469         }
470 }
471
472
473 /*
474  * Class:     java/lang/reflect/Field
475  * Method:    getLong
476  * Signature: (Ljava/lang/Object;)J
477  */
478 JNIEXPORT int64_t JNICALL Java_java_lang_reflect_Field_getLong(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o)
479 {
480         classinfo *c;
481         fieldinfo *f;
482         void      *addr;
483         int32_t    slot;
484
485         /* get the class and the field */
486
487         LLNI_field_get_cls(this, clazz, c);
488         LLNI_field_get_val(this, slot , slot);
489         f = &c->fields[slot];
490
491         /* get the address of the field with an internal helper */
492
493         if ((addr = cacao_get_field_address(this, o)) == NULL)
494                 return 0;
495
496         /* check the field type and return the value */
497
498         switch (f->parseddesc->decltype) {
499         case PRIMITIVETYPE_BYTE:
500         case PRIMITIVETYPE_CHAR:
501         case PRIMITIVETYPE_SHORT:
502         case PRIMITIVETYPE_INT:
503                 return (int64_t) *((int32_t *) addr);
504         case PRIMITIVETYPE_LONG:
505                 return (int64_t) *((int64_t *) addr);
506         default:
507                 exceptions_throw_illegalargumentexception();
508                 return 0;
509         }
510 }
511
512
513 /*
514  * Class:     java/lang/reflect/Field
515  * Method:    getFloat
516  * Signature: (Ljava/lang/Object;)F
517  */
518 JNIEXPORT float JNICALL Java_java_lang_reflect_Field_getFloat(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o)
519 {
520         classinfo *c;
521         fieldinfo *f;
522         void      *addr;
523         int32_t    slot;
524
525         /* get the class and the field */
526
527         LLNI_field_get_cls(this, clazz, c);
528         LLNI_field_get_val(this, slot , slot);
529         f = &c->fields[slot];
530
531         /* get the address of the field with an internal helper */
532
533         if ((addr = cacao_get_field_address(this, o)) == NULL)
534                 return 0;
535
536         /* check the field type and return the value */
537
538         switch (f->parseddesc->decltype) {
539         case PRIMITIVETYPE_BYTE:
540         case PRIMITIVETYPE_CHAR:
541         case PRIMITIVETYPE_SHORT:
542         case PRIMITIVETYPE_INT:
543                 return (float) *((int32_t *) addr);
544         case PRIMITIVETYPE_LONG:
545                 return (float) *((int64_t *) addr);
546         case PRIMITIVETYPE_FLOAT:
547                 return (float) *((float *) addr);
548         default:
549                 exceptions_throw_illegalargumentexception();
550                 return 0;
551         }
552 }
553
554
555 /*
556  * Class:     java/lang/reflect/Field
557  * Method:    getDouble
558  * Signature: (Ljava/lang/Object;)D
559  */
560 JNIEXPORT double JNICALL Java_java_lang_reflect_Field_getDouble(JNIEnv *env , java_lang_reflect_Field *this, java_lang_Object *o)
561 {
562         classinfo *c;
563         fieldinfo *f;
564         void      *addr;
565         int32_t    slot;
566
567         /* get the class and the field */
568
569         LLNI_field_get_cls(this, clazz, c);
570         LLNI_field_get_val(this, slot , slot);
571         f = &c->fields[slot];
572
573         /* get the address of the field with an internal helper */
574
575         if ((addr = cacao_get_field_address(this, o)) == NULL)
576                 return 0;
577
578         /* check the field type and return the value */
579
580         switch (f->parseddesc->decltype) {
581         case PRIMITIVETYPE_BYTE:
582         case PRIMITIVETYPE_CHAR:
583         case PRIMITIVETYPE_SHORT:
584         case PRIMITIVETYPE_INT:
585                 return (double) *((int32_t *) addr);
586         case PRIMITIVETYPE_LONG:
587                 return (double) *((int64_t *) addr);
588         case PRIMITIVETYPE_FLOAT:
589                 return (double) *((float *) addr);
590         case PRIMITIVETYPE_DOUBLE:
591                 return (double) *((double *) addr);
592         default:
593                 exceptions_throw_illegalargumentexception();
594                 return 0;
595         }
596 }
597
598
599 /*
600  * Class:     java/lang/reflect/Field
601  * Method:    set
602  * Signature: (Ljava/lang/Object;Ljava/lang/Object;)V
603  */
604 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, java_lang_Object *value)
605 {
606         classinfo *sc;
607         classinfo *dc;
608         fieldinfo *sf;
609         fieldinfo *df;
610         void      *faddr;
611         int32_t    slot;
612
613         /* get the class and the field */
614
615         LLNI_field_get_cls(this, clazz, dc);
616         LLNI_field_get_val(this, slot , slot);
617         df = &dc->fields[slot];
618
619         /* get the address of the destination field */
620
621         if ((faddr = cacao_get_field_address(this, o)) == NULL)
622                 return;
623
624         /* get the source classinfo from the object */
625
626         if (value == NULL)
627                 sc = NULL;
628         else
629                 LLNI_class_get(value, sc);
630
631         /* The fieldid is used to set the new value, for primitive
632            types the value has to be retrieved from the wrapping
633            object */
634
635         switch (df->parseddesc->decltype) {
636         case PRIMITIVETYPE_BOOLEAN: {
637                 int32_t val;
638
639                 /* determine the field to read the value */
640
641                 if ((sc == NULL) || !(sf = class_findfield(sc, utf_value, utf_Z)))
642                         break;
643
644                 switch (sf->parseddesc->decltype) {
645                 case PRIMITIVETYPE_BOOLEAN:
646                         LLNI_field_get_val((java_lang_Boolean *) value, value, val);
647                         break;
648                 default:
649                         exceptions_throw_illegalargumentexception();
650                         return;
651                 }
652
653                 *((int32_t *) faddr) = val;
654                 return;
655         }
656
657         case PRIMITIVETYPE_BYTE: {
658                 int32_t val;
659
660                 if ((sc == NULL) || !(sf = class_findfield(sc, utf_value, utf_B)))
661                         break;
662
663                 switch (sf->parseddesc->decltype) {
664                 case PRIMITIVETYPE_BYTE:
665                         LLNI_field_get_val((java_lang_Byte *) value, value, val);
666                         break;
667                 default:        
668                         exceptions_throw_illegalargumentexception();
669                         return;
670                 }
671
672                 *((int32_t *) faddr) = val;
673                 return;
674         }
675
676         case PRIMITIVETYPE_CHAR: {
677                 int32_t val;
678
679                 if ((sc == NULL) || !(sf = class_findfield(sc, utf_value, utf_C)))
680                         break;
681                                    
682                 switch (sf->parseddesc->decltype) {
683                 case PRIMITIVETYPE_CHAR:
684                         LLNI_field_get_val((java_lang_Character *) value, value, val);
685                         break;
686                 default:
687                         exceptions_throw_illegalargumentexception();
688                         return;
689                 }
690
691                 *((int32_t *) faddr) = val;
692                 return;
693         }
694
695         case PRIMITIVETYPE_SHORT: {
696                 int32_t val;
697
698                 /* get field only by name, it can be one of B, S */
699
700                 if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf_value)))
701                         break;
702                                    
703                 switch (sf->parseddesc->decltype) {
704                 case PRIMITIVETYPE_BYTE:
705                         LLNI_field_get_val((java_lang_Byte *) value, value, val);
706                         break;
707                 case PRIMITIVETYPE_SHORT:
708                         LLNI_field_get_val((java_lang_Short *) value, value, val);
709                         break;
710                 default:
711                         exceptions_throw_illegalargumentexception();
712                         return;
713                 }
714
715                 *((int32_t *) faddr) = val;
716                 return;
717         }
718
719         case PRIMITIVETYPE_INT: {
720                 int32_t val;
721
722                 /* get field only by name, it can be one of B, S, C, I */
723
724                 if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf_value)))
725                         break;
726
727                 switch (sf->parseddesc->decltype) {
728                 case PRIMITIVETYPE_BYTE:
729                         LLNI_field_get_val((java_lang_Byte *) value, value, val);
730                         break;
731                 case PRIMITIVETYPE_CHAR:
732                         LLNI_field_get_val((java_lang_Character *) value, value, val);
733                         break;
734                 case PRIMITIVETYPE_SHORT:
735                         LLNI_field_get_val((java_lang_Short *) value, value, val);
736                         break;
737                 case PRIMITIVETYPE_INT:
738                         LLNI_field_get_val((java_lang_Integer *) value, value, val);
739                         break;
740                 default:
741                         exceptions_throw_illegalargumentexception();
742                         return;
743                 }
744
745                 *((int32_t *) faddr) = val;
746                 return;
747         }
748
749         case PRIMITIVETYPE_LONG: {
750                 int64_t val;
751
752                 /* get field only by name, it can be one of B, S, C, I, J */
753
754                 if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf_value)))
755                         break;
756
757                 switch (sf->parseddesc->decltype) {
758                 case PRIMITIVETYPE_BYTE:
759                         LLNI_field_get_val((java_lang_Byte *) value, value, val);
760                         break;
761                 case PRIMITIVETYPE_CHAR:
762                         LLNI_field_get_val((java_lang_Character *) value, value, val);
763                         break;
764                 case PRIMITIVETYPE_SHORT:
765                         LLNI_field_get_val((java_lang_Short *) value, value, val);
766                         break;
767                 case PRIMITIVETYPE_INT:
768                         LLNI_field_get_val((java_lang_Integer *) value, value, val);
769                         break;
770                 case PRIMITIVETYPE_LONG:
771                         LLNI_field_get_val((java_lang_Long *) value, value, val);
772                         break;
773                 default:
774                         exceptions_throw_illegalargumentexception();
775                         return;
776                 }
777
778                 *((int64_t *) faddr) = val;
779                 return;
780         }
781
782         case PRIMITIVETYPE_FLOAT: {
783                 float val;
784
785                 /* get field only by name, it can be one of B, S, C, I, J, F */
786
787                 if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf_value)))
788                         break;
789
790                 switch (sf->parseddesc->decltype) {
791                 case PRIMITIVETYPE_BYTE:
792                         LLNI_field_get_val((java_lang_Byte *) value, value, val);
793                         break;
794                 case PRIMITIVETYPE_CHAR:
795                         LLNI_field_get_val((java_lang_Character *) value, value, val);
796                         break;
797                 case PRIMITIVETYPE_SHORT:
798                         LLNI_field_get_val((java_lang_Short *) value, value, val);
799                         break;
800                 case PRIMITIVETYPE_INT:
801                         LLNI_field_get_val((java_lang_Integer *) value, value, val);
802                         break;
803                 case PRIMITIVETYPE_LONG:
804                         LLNI_field_get_val((java_lang_Long *) value, value, val);
805                         break;
806                 case PRIMITIVETYPE_FLOAT:
807                         LLNI_field_get_val((java_lang_Float *) value, value, val);
808                         break;
809                 default:
810                         exceptions_throw_illegalargumentexception();
811                         return;
812                 }
813
814                 *((float *) faddr) = val;
815                 return;
816         }
817
818         case PRIMITIVETYPE_DOUBLE: {
819                 double val;
820
821                 /* get field only by name, it can be one of B, S, C, I, J, F, D */
822
823                 if ((sc == NULL) || !(sf = class_findfield_by_name(sc, utf_value)))
824                         break;
825
826                 switch (sf->parseddesc->decltype) {
827                 case PRIMITIVETYPE_BYTE:
828                         LLNI_field_get_val((java_lang_Byte *) value, value, val);
829                         break;
830                 case PRIMITIVETYPE_CHAR:
831                         LLNI_field_get_val((java_lang_Character *) value, value, val);
832                         break;
833                 case PRIMITIVETYPE_SHORT:
834                         LLNI_field_get_val((java_lang_Short *) value, value, val);
835                         break;
836                 case PRIMITIVETYPE_INT:
837                         LLNI_field_get_val((java_lang_Integer *) value, value, val);
838                         break;
839                 case PRIMITIVETYPE_LONG:
840                         LLNI_field_get_val((java_lang_Long *) value, value, val);
841                         break;
842                 case PRIMITIVETYPE_FLOAT:
843                         LLNI_field_get_val((java_lang_Float *) value, value, val);
844                         break;
845                 case PRIMITIVETYPE_DOUBLE:
846                         LLNI_field_get_val((java_lang_Double *) value, value, val);
847                         break;
848                 default:
849                         exceptions_throw_illegalargumentexception();
850                         return;
851                 }
852
853                 *((double *) faddr) = val;
854                 return;
855         }
856
857         case TYPE_ADR:
858                 /* check if value is an instance of the destination class */
859
860                 /* XXX TODO */
861                 /*                      if (!builtin_instanceof((java_handle_t *) value, df->class)) */
862                 /*                              break; */
863
864                 *((java_lang_Object **) faddr) = value;
865                 return;
866         }
867
868         /* raise exception */
869
870         exceptions_throw_illegalargumentexception();
871 }
872
873
874 /*
875  * Class:     java/lang/reflect/Field
876  * Method:    setBoolean
877  * Signature: (Ljava/lang/Object;Z)V
878  */
879 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setBoolean(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, int32_t value)
880 {
881         classinfo *c;
882         fieldinfo *f;
883         void      *addr;
884         int32_t    slot;
885
886         /* get the class and the field */
887
888         LLNI_field_get_cls(this, clazz, c);
889         LLNI_field_get_val(this, slot , slot);
890         f = &c->fields[slot];
891
892         /* get the address of the field with an internal helper */
893
894         if ((addr = cacao_get_field_address(this, o)) == NULL)
895                 return;
896
897         /* check the field type and set the value */
898
899         switch (f->parseddesc->decltype) {
900         case PRIMITIVETYPE_BOOLEAN:
901                 *((int32_t *) addr) = value;
902                 break;
903         default:
904                 exceptions_throw_illegalargumentexception();
905         }
906
907         return;
908 }
909
910
911 /*
912  * Class:     java/lang/reflect/Field
913  * Method:    setByte
914  * Signature: (Ljava/lang/Object;B)V
915  */
916 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setByte(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, int32_t value)
917 {
918         classinfo *c;
919         fieldinfo *f;
920         void      *addr;
921         int32_t    slot;
922
923         /* get the class and the field */
924
925         LLNI_field_get_cls(this, clazz, c);
926         LLNI_field_get_val(this, slot , slot);
927         f = &c->fields[slot];
928
929         /* get the address of the field with an internal helper */
930
931         if ((addr = cacao_get_field_address(this, o)) == NULL)
932                 return;
933
934         /* check the field type and set the value */
935
936         switch (f->parseddesc->decltype) {
937         case PRIMITIVETYPE_BYTE:
938         case PRIMITIVETYPE_SHORT:
939         case PRIMITIVETYPE_INT:
940                 *((int32_t *) addr) = value;
941                 break;
942         case PRIMITIVETYPE_LONG:
943                 *((int64_t *) addr) = value;
944                 break;
945         case PRIMITIVETYPE_FLOAT:
946                 *((float *) addr) = value;
947                 break;
948         case PRIMITIVETYPE_DOUBLE:
949                 *((double *) addr) = value;
950                 break;
951         default:
952                 exceptions_throw_illegalargumentexception();
953         }
954
955         return;
956 }
957
958
959 /*
960  * Class:     java/lang/reflect/Field
961  * Method:    setChar
962  * Signature: (Ljava/lang/Object;C)V
963  */
964 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setChar(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, int32_t value)
965 {
966         classinfo *c;
967         fieldinfo *f;
968         void      *addr;
969         int32_t    slot;
970
971         /* get the class and the field */
972
973         LLNI_field_get_cls(this, clazz, c);
974         LLNI_field_get_val(this, slot , slot);
975         f = &c->fields[slot];
976
977         /* get the address of the field with an internal helper */
978
979         if ((addr = cacao_get_field_address(this, o)) == NULL)
980                 return;
981
982         /* check the field type and set the value */
983
984         switch (f->parseddesc->decltype) {
985         case PRIMITIVETYPE_CHAR:
986         case PRIMITIVETYPE_INT:
987                 *((int32_t *) addr) = value;
988                 break;
989         case PRIMITIVETYPE_LONG:
990                 *((int64_t *) addr) = value;
991                 break;
992         case PRIMITIVETYPE_FLOAT:
993                 *((float *) addr) = value;
994                 break;
995         case PRIMITIVETYPE_DOUBLE:
996                 *((double *) addr) = value;
997                 break;
998         default:
999                 exceptions_throw_illegalargumentexception();
1000         }
1001
1002         return;
1003 }
1004
1005
1006 /*
1007  * Class:     java/lang/reflect/Field
1008  * Method:    setShort
1009  * Signature: (Ljava/lang/Object;S)V
1010  */
1011 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setShort(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, int32_t value)
1012 {
1013         classinfo *c;
1014         fieldinfo *f;
1015         void      *addr;
1016         int32_t    slot;
1017
1018         /* get the class and the field */
1019
1020         LLNI_field_get_cls(this, clazz, c);
1021         LLNI_field_get_val(this, slot , slot);
1022         f = &c->fields[slot];
1023
1024         /* get the address of the field with an internal helper */
1025
1026         if ((addr = cacao_get_field_address(this, o)) == NULL)
1027                 return;
1028
1029         /* check the field type and set the value */
1030
1031         switch (f->parseddesc->decltype) {
1032         case PRIMITIVETYPE_SHORT:
1033         case PRIMITIVETYPE_INT:
1034                 *((int32_t *) addr) = value;
1035                 break;
1036         case PRIMITIVETYPE_LONG:
1037                 *((int64_t *) addr) = value;
1038                 break;
1039         case PRIMITIVETYPE_FLOAT:
1040                 *((float *) addr) = value;
1041                 break;
1042         case PRIMITIVETYPE_DOUBLE:
1043                 *((double *) addr) = value;
1044                 break;
1045         default:
1046                 exceptions_throw_illegalargumentexception();
1047         }
1048
1049         return;
1050 }
1051
1052
1053 /*
1054  * Class:     java/lang/reflect/Field
1055  * Method:    setInt
1056  * Signature: (Ljava/lang/Object;I)V
1057  */
1058 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setInt(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, int32_t value)
1059 {
1060         classinfo *c;
1061         fieldinfo *f;
1062         void      *addr;
1063         int32_t    slot;
1064
1065         /* get the class and the field */
1066
1067         LLNI_field_get_cls(this, clazz, c);
1068         LLNI_field_get_val(this, slot , slot);
1069         f = &c->fields[slot];
1070
1071         /* get the address of the field with an internal helper */
1072
1073         if ((addr = cacao_get_field_address(this, o)) == NULL)
1074                 return;
1075
1076         /* check the field type and set the value */
1077
1078         switch (f->parseddesc->decltype) {
1079         case PRIMITIVETYPE_INT:
1080                 *((int32_t *) addr) = value;
1081                 break;
1082         case PRIMITIVETYPE_LONG:
1083                 *((int64_t *) addr) = value;
1084                 break;
1085         case PRIMITIVETYPE_FLOAT:
1086                 *((float *) addr) = value;
1087                 break;
1088         case PRIMITIVETYPE_DOUBLE:
1089                 *((double *) addr) = value;
1090                 break;
1091         default:
1092                 exceptions_throw_illegalargumentexception();
1093         }
1094
1095         return;
1096 }
1097
1098
1099 /*
1100  * Class:     java/lang/reflect/Field
1101  * Method:    setLong
1102  * Signature: (Ljava/lang/Object;J)V
1103  */
1104 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setLong(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, int64_t value)
1105 {
1106         classinfo *c;
1107         fieldinfo *f;
1108         void      *addr;
1109         int32_t    slot;
1110
1111         /* get the class and the field */
1112
1113         LLNI_field_get_cls(this, clazz, c);
1114         LLNI_field_get_val(this, slot , slot);
1115         f = &c->fields[slot];
1116
1117         /* get the address of the field with an internal helper */
1118
1119         if ((addr = cacao_get_field_address(this, o)) == NULL)
1120                 return;
1121
1122         /* check the field type and set the value */
1123
1124         switch (f->parseddesc->decltype) {
1125         case PRIMITIVETYPE_LONG:
1126                 *((int64_t *) addr) = value;
1127                 break;
1128         case PRIMITIVETYPE_FLOAT:
1129                 *((float *) addr) = value;
1130                 break;
1131         case PRIMITIVETYPE_DOUBLE:
1132                 *((double *) addr) = value;
1133                 break;
1134         default:
1135                 exceptions_throw_illegalargumentexception();
1136         }
1137
1138         return;
1139 }
1140
1141
1142 /*
1143  * Class:     java/lang/reflect/Field
1144  * Method:    setFloat
1145  * Signature: (Ljava/lang/Object;F)V
1146  */
1147 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setFloat(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, float value)
1148 {
1149         classinfo *c;
1150         fieldinfo *f;
1151         void      *addr;
1152         int32_t    slot;
1153
1154         /* get the class and the field */
1155
1156         LLNI_field_get_cls(this, clazz, c);
1157         LLNI_field_get_val(this, slot , slot);
1158         f = &c->fields[slot];
1159
1160         /* get the address of the field with an internal helper */
1161
1162         if ((addr = cacao_get_field_address(this, o)) == NULL)
1163                 return;
1164
1165         /* check the field type and set the value */
1166
1167         switch (f->parseddesc->decltype) {
1168         case PRIMITIVETYPE_FLOAT:
1169                 *((float *) addr) = value;
1170                 break;
1171         case PRIMITIVETYPE_DOUBLE:
1172                 *((double *) addr) = value;
1173                 break;
1174         default:
1175                 exceptions_throw_illegalargumentexception();
1176         }
1177
1178         return;
1179 }
1180
1181
1182 /*
1183  * Class:     java/lang/reflect/Field
1184  * Method:    setDouble
1185  * Signature: (Ljava/lang/Object;D)V
1186  */
1187 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setDouble(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *o, double value)
1188 {
1189         classinfo *c;
1190         fieldinfo *f;
1191         void      *addr;
1192         int32_t    slot;
1193
1194         /* get the class and the field */
1195
1196         LLNI_field_get_cls(this, clazz, c);
1197         LLNI_field_get_val(this, slot , slot);
1198         f = &c->fields[slot];
1199
1200         /* get the address of the field with an internal helper */
1201
1202         if ((addr = cacao_get_field_address(this, o)) == NULL)
1203                 return;
1204
1205         /* check the field type and set the value */
1206
1207         switch (f->parseddesc->decltype) {
1208         case PRIMITIVETYPE_DOUBLE:
1209                 *((double *) addr) = value;
1210                 break;
1211         default:
1212                 exceptions_throw_illegalargumentexception();
1213         }
1214
1215         return;
1216 }
1217
1218
1219 /*
1220  * Class:     java/lang/reflect/Field
1221  * Method:    getSignature
1222  * Signature: ()Ljava/lang/String;
1223  */
1224 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Field_getSignature(JNIEnv *env, java_lang_reflect_Field* this)
1225 {
1226         classinfo     *c;
1227         fieldinfo     *f;
1228         java_handle_t *o;
1229         int32_t        slot;
1230
1231         /* get the class and the field */
1232
1233         LLNI_field_get_cls(this, clazz, c);
1234         LLNI_field_get_val(this, slot , slot);
1235         f = &c->fields[slot];
1236
1237         if (f->signature == NULL)
1238                 return NULL;
1239
1240         o = javastring_new(f->signature);
1241
1242         /* in error case o is NULL */
1243
1244         return (java_lang_String *) o;
1245 }
1246
1247
1248 #if defined(ENABLE_ANNOTATIONS)
1249 /*
1250  * Class:     java/lang/reflect/Field
1251  * Method:    declaredAnnotations
1252  * Signature: ()Ljava/util/Map;
1253  */
1254 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Field_declaredAnnotations(JNIEnv *env, java_lang_reflect_Field *this)
1255 {
1256         java_handle_t           *o                   = (java_handle_t*)this;
1257         struct java_util_Map    *declaredAnnotations = NULL;
1258         java_handle_bytearray_t *annotations         = NULL;
1259         java_lang_Class         *declaringClass      = NULL;
1260         classinfo               *referer             = NULL;
1261
1262         LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
1263
1264         if (declaredAnnotations == NULL) {
1265                 LLNI_field_get_val(this, annotations, annotations);
1266                 LLNI_field_get_ref(this, clazz, declaringClass);
1267                 LLNI_class_get(this, referer);
1268
1269                 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
1270
1271                 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
1272         }
1273
1274         return declaredAnnotations;
1275 }
1276 #endif
1277
1278
1279 /*
1280  * These are local overrides for various environment variables in Emacs.
1281  * Please do not remove this and leave it at the end of the file, where
1282  * Emacs will automagically detect them.
1283  * ---------------------------------------------------------------------
1284  * Local variables:
1285  * mode: c
1286  * indent-tabs-mode: t
1287  * c-basic-offset: 4
1288  * tab-width: 4
1289  * End:
1290  */