1 /* src/native/vm/Field.c - java/lang/reflect/Field
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Roman Obermaiser
29 Changes: Joseph Wenninger
32 $Id: Field.c 2967 2005-07-10 15:17:06Z twisti $
39 #include "native/jni.h"
40 #include "native/native.h"
41 #include "native/include/java_lang_Object.h"
42 #include "native/include/java_lang_Class.h"
43 #include "native/include/java_lang_reflect_Field.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/global.h"
47 #include "vm/initialize.h"
48 #include "vm/loader.h"
49 #include "vm/stringlocal.h"
50 #include "vm/tables.h"
51 #include "vm/resolve.h"
52 #include "vm/jit/stacktrace.h"
56 /* XXX FIXE SET NATIVES */
58 #if (defined(__ALPHA__) || defined(__I386__))
59 /*this = java_lang_reflect_Field, fi=fieldinfo, c=declaredClass (classinfo)*/
60 #define CHECKFIELDACCESS(this,fi,c,doret) \
61 /*log_text("Checking access rights");*/ \
62 if (!(getField(this,jboolean,getFieldID_critical(env,this->header.vftbl->class,"flag","Z")))) { \
64 struct methodinfo *callingMethod; \
66 if ((fi->flags & ACC_PUBLIC)==0) { \
67 callingMethod=cacao_callingMethod(); \
69 if ((fi->flags & ACC_PRIVATE)!=0) { \
70 if (c!=callingMethod->class) { \
74 if ((fi->flags & ACC_PROTECTED)!=0) { \
75 if (!builtin_isanysubclass(callingMethod->class, c)) { \
79 /* default visibility*/ \
80 if (c->packagename!=callingMethod->class->packagename) { \
88 *exceptionptr = new_exception(string_java_lang_IllegalAccessException); \
93 #define CHECKFIELDACCESS(this,fi,c,doret) \
94 (c) = (c) /* prevent compiler warning */
104 * Class: java/lang/reflect/Field
106 * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
108 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Field_get(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
110 jfieldID target_fid; /* the JNI-fieldid of the wrapping object */
111 jfieldID fid; /* the JNI-fieldid of the field containing the value */
112 jobject o; /* the object for wrapping the primitive type */
113 classinfo *c = (classinfo *) this->declaringClass;
116 /* get the fieldid of the field represented by this Field-object */
117 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
118 /*fid = class_findfield_approx((classinfo *) this->declaringClass,javastring_toutf(this->name, false));*/
119 st = (fid->flags & ACC_STATIC); /* true if field is static */
122 CHECKFIELDACCESS(this,fid,c,return 0);
124 /* The fieldid is used to retrieve the value, for primitive types a new
125 object for wrapping the primitive type is created. */
127 switch ((((classinfo *) this->declaringClass)->fields[this->slot]).descriptor->text[0]) {
129 /* create wrapping class */
130 c = class_java_lang_Integer;
132 /* get fieldid to store the value */
133 target_fid = (*env)->GetFieldID(env, c, "value", "I");
139 SetIntField(env,o,target_fid, (*env)->GetStaticIntField(env, c, fid));
142 SetIntField(env,o,target_fid, (*env)->GetIntField(env,(jobject) obj, fid));
144 /* return the wrapped object */
145 return (java_lang_Object *) o;
148 c = class_java_lang_Long;
150 target_fid = (*env)->GetFieldID(env, c, "value", "J");
155 SetLongField(env,o,target_fid, (*env)->GetStaticLongField(env, c, fid));
157 SetLongField(env,o,target_fid, (*env)->GetLongField(env,(jobject) obj, fid));
159 return (java_lang_Object *) o;
162 c = class_java_lang_Float;
164 target_fid = (*env)->GetFieldID(env, c, "value", "F");
169 SetFloatField(env,o,target_fid, (*env)->GetStaticFloatField(env, c, fid));
171 SetFloatField(env,o,target_fid, (*env)->GetFloatField(env, (jobject) obj, fid));
173 return (java_lang_Object *) o;
176 c = class_java_lang_Double;
178 target_fid = (*env)->GetFieldID(env, c, "value", "D");
183 SetDoubleField(env,o,target_fid, (*env)->GetStaticDoubleField(env, c, fid));
185 SetDoubleField(env,o,target_fid, (*env)->GetDoubleField(env, (jobject) obj, fid));
187 return (java_lang_Object *) o;
190 c = class_java_lang_Byte;
192 target_fid = (*env)->GetFieldID(env, c, "value", "B");
197 SetByteField(env,o,target_fid, (*env)->GetStaticByteField(env, c, fid));
199 SetByteField(env,o,target_fid, (*env)->GetByteField(env, (jobject) obj, fid));
201 return (java_lang_Object *) o;
204 c = class_java_lang_Character;
206 target_fid = (*env)->GetFieldID(env, c, "value", "C");
211 SetCharField(env,o,target_fid, (*env)->GetStaticCharField(env, c, fid));
213 SetCharField(env,o,target_fid, (*env)->GetCharField(env, (jobject) obj, fid));
215 return (java_lang_Object *) o;
218 c = class_java_lang_Short;
220 target_fid = (*env)->GetFieldID(env, c, "value", "S");
225 SetShortField(env,o,target_fid, (*env)->GetStaticShortField(env, c, fid));
227 SetShortField(env,o,target_fid, (*env)->GetShortField(env, (jobject) obj, fid));
229 return (java_lang_Object *) o;
232 c = class_java_lang_Boolean;
234 target_fid = (*env)->GetFieldID(env, c, "value", "Z");
239 SetBooleanField(env,o,target_fid, (*env)->GetStaticBooleanField(env, c, fid));
241 SetBooleanField(env,o,target_fid, (*env)->GetBooleanField(env, (jobject) obj, fid));
243 return (java_lang_Object *) o;
249 return (java_lang_Object*) (*env)->GetStaticObjectField(env, c, fid);
252 return (java_lang_Object*) (*env)->GetObjectField(env, (jobject) obj, fid);
255 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
262 * Class: java/lang/reflect/Field
264 * Signature: (Ljava/lang/Object;)Z
266 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getBoolean(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
270 classinfo *c = (classinfo *) this->declaringClass;
273 /* check if the specified slot could be a valid field of this class*/
274 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
275 "declaring class: fieldscount mismatch");
277 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
278 CHECKFIELDACCESS(this,fid,c,return 0);
280 /* check if the field really has the same name and check if the type descriptor is not empty*/
281 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
282 "declaring class: field name mismatch");
283 if (fid->descriptor->blength<1) {
284 log_text("Type-Descriptor is empty");
288 /* check if obj would be needed (not static field), but is 0)*/
289 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
290 *exceptionptr = new_exception(string_java_lang_NullPointerException);
294 /*if (!(fid->flags & ACC_PUBLIC)) {
295 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
300 if (fid->flags & ACC_STATIC) {
301 /* initialize class if needed*/
302 initialize_class((classinfo *) this->declaringClass);
303 if (*exceptionptr) return 0;
305 utf_ptr = fid->descriptor->text;
307 case 'Z': return fid->value.i;
309 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
313 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
314 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
317 utf_ptr = fid->descriptor->text;
319 case 'Z':return getField(obj,jboolean,fid);
321 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
329 * Class: java/lang/reflect/Field
331 * Signature: (Ljava/lang/Object;)B
333 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getByte(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
337 classinfo *c = (classinfo *) this->declaringClass;
340 /* check if the specified slot could be a valid field of this class*/
341 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
342 "declaring class: fieldscount mismatch");
344 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
346 CHECKFIELDACCESS(this,fid,c,return 0);
348 /* check if the field really has the same name and check if the type descriptor is not empty*/
349 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
350 "declaring class: field name mismatch");
351 if (fid->descriptor->blength<1) {
352 log_text("Type-Descriptor is empty");
356 /* check if obj would be needed (not static field), but is 0)*/
357 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
358 *exceptionptr = new_exception(string_java_lang_NullPointerException);
362 /* if (!(fid->flags & ACC_PUBLIC)) {
363 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
368 if (fid->flags & ACC_STATIC) {
369 /* initialize class if needed*/
370 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
371 initialize_class((classinfo *) this->declaringClass);
372 if (*exceptionptr) return 0;
374 utf_ptr = fid->descriptor->text;
376 case 'B': return fid->value.i;
378 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
382 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
383 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
386 utf_ptr = fid->descriptor->text;
388 case 'B':return getField(obj,jbyte,fid);
390 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
398 * Class: java/lang/reflect/Field
400 * Signature: (Ljava/lang/Object;)C
402 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getChar(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
406 classinfo *c = (classinfo *) this->declaringClass;
409 /* check if the specified slot could be a valid field of this class*/
410 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
411 "declaring class: fieldscount mismatch");
413 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
414 CHECKFIELDACCESS(this,fid,c,return 0);
417 /* check if the field really has the same name and check if the type descriptor is not empty*/
418 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
419 "declaring class: field name mismatch");
420 if (fid->descriptor->blength<1) {
421 log_text("Type-Descriptor is empty");
425 /* check if obj would be needed (not static field), but is 0)*/
426 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
427 *exceptionptr = new_exception(string_java_lang_NullPointerException);
431 /*if (!(fid->flags & ACC_PUBLIC)) {
432 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
437 if (fid->flags & ACC_STATIC) {
438 /* initialize class if needed*/
439 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
440 initialize_class((classinfo *) this->declaringClass);
441 if (*exceptionptr) return 0;
443 utf_ptr = fid->descriptor->text;
445 case 'C': return fid->value.i;
447 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
451 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
452 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
455 utf_ptr = fid->descriptor->text;
457 case 'C':return getField(obj,jchar,fid);
459 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
467 * Class: java/lang/reflect/Field
469 * Signature: (Ljava/lang/Object;)D
471 JNIEXPORT double JNICALL Java_java_lang_reflect_Field_getDouble(JNIEnv *env , java_lang_reflect_Field *this, java_lang_Object *obj)
475 classinfo *c = (classinfo *) this->declaringClass;
478 /* check if the specified slot could be a valid field of this class*/
479 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
480 "declaring class: fieldscount mismatch");
482 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
483 CHECKFIELDACCESS(this,fid,c,return 0);
486 /* check if the field really has the same name and check if the type descriptor is not empty*/
487 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
488 "declaring class: field name mismatch");
489 if (fid->descriptor->blength<1) {
490 log_text("Type-Descriptor is empty");
494 /* check if obj would be needed (not static field), but is 0)*/
495 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
496 *exceptionptr = new_exception(string_java_lang_NullPointerException);
500 /*if (!(fid->flags & ACC_PUBLIC)) {
501 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
506 if (fid->flags & ACC_STATIC) {
507 /* initialize class if needed*/
508 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
509 initialize_class((classinfo *) this->declaringClass);
510 if (*exceptionptr) return 0;
512 utf_ptr = fid->descriptor->text;
517 case 'I': return fid->value.i;
518 case 'F': return fid->value.f;
519 case 'D': return fid->value.d;
521 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
525 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
526 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
529 utf_ptr = fid->descriptor->text;
531 case 'B':return getField(obj,jbyte,fid);
532 case 'S':return getField(obj,jshort,fid);
533 case 'C':return getField(obj,jchar,fid);
534 case 'I':return getField(obj,jint,fid);
535 case 'D':return getField(obj,jdouble,fid);
537 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
545 * Class: java/lang/reflect/Field
547 * Signature: (Ljava/lang/Object;)F
549 JNIEXPORT float JNICALL Java_java_lang_reflect_Field_getFloat(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
553 classinfo *c = (classinfo *) this->declaringClass;
556 /* check if the specified slot could be a valid field of this class*/
557 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
558 "declaring class: fieldscount mismatch");
560 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
561 CHECKFIELDACCESS(this,fid,c,return 0);
564 /* check if the field really has the same name and check if the type descriptor is not empty*/
565 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
566 "declaring class: field name mismatch");
567 if (fid->descriptor->blength<1) {
568 log_text("Type-Descriptor is empty");
572 /* check if obj would be needed (not static field), but is 0)*/
573 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
574 *exceptionptr = new_exception(string_java_lang_NullPointerException);
578 /*if (!(fid->flags & ACC_PUBLIC)) {
579 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
584 if (fid->flags & ACC_STATIC) {
585 /* initialize class if needed*/
586 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
587 initialize_class((classinfo *) this->declaringClass);
588 if (*exceptionptr) return 0;
590 utf_ptr = fid->descriptor->text;
595 case 'I': return fid->value.i;
596 case 'F': return fid->value.f;
598 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
602 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
603 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
606 utf_ptr = fid->descriptor->text;
608 case 'B':return getField(obj,jbyte,fid);
609 case 'S':return getField(obj,jshort,fid);
610 case 'C':return getField(obj,jchar,fid);
611 case 'I':return getField(obj,jint,fid);
612 case 'F':return getField(obj,jfloat,fid);
614 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
622 * Class: java/lang/reflect/Field
624 * Signature: (Ljava/lang/Object;)I
626 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getInt(JNIEnv *env , java_lang_reflect_Field *this, java_lang_Object *obj)
630 classinfo *c = (classinfo *) this->declaringClass;
633 /* check if the specified slot could be a valid field of this class*/
634 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
635 "declaring class: fieldscount mismatch");
637 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
638 CHECKFIELDACCESS(this,fid,c,return 0);
640 /* check if the field really has the same name and check if the type descriptor is not empty*/
641 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
642 "declaring class: field name mismatch");
643 if (fid->descriptor->blength<1) {
644 log_text("Type-Descriptor is empty");
648 /* check if obj would be needed (not static field), but is 0)*/
649 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
650 *exceptionptr = new_exception(string_java_lang_NullPointerException);
654 /*if (!(fid->flags & ACC_PUBLIC)) {
655 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
660 if (fid->flags & ACC_STATIC) {
661 /* initialize class if needed*/
662 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
663 initialize_class((classinfo *) this->declaringClass);
664 if (*exceptionptr) return 0;
666 utf_ptr = fid->descriptor->text;
671 case 'I': return fid->value.i;
673 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
677 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
678 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
681 utf_ptr = fid->descriptor->text;
683 case 'B':return getField(obj,jbyte,fid);
684 case 'S':return getField(obj,jshort,fid);
685 case 'C':return getField(obj,jchar,fid);
686 case 'I':return getField(obj,jint,fid);
688 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
696 * Class: java/lang/reflect/Field
698 * Signature: (Ljava/lang/Object;)J
700 JNIEXPORT s8 JNICALL Java_java_lang_reflect_Field_getLong(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
704 classinfo *c = (classinfo *) this->declaringClass;
707 /* check if the specified slot could be a valid field of this class*/
708 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
709 "declaring class: fieldscount mismatch");
711 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
712 CHECKFIELDACCESS(this,fid,c,return 0);
715 /* check if the field really has the same name and check if the type descriptor is not empty*/
716 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
717 "declaring class: field name mismatch");
718 if (fid->descriptor->blength<1) {
719 log_text("Type-Descriptor is empty");
723 /* check if obj would be needed (not static field), but is 0)*/
724 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
725 *exceptionptr = new_exception(string_java_lang_NullPointerException);
729 /*if (!(fid->flags & ACC_PUBLIC)) {
730 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
735 if (fid->flags & ACC_STATIC) {
736 /* initialize class if needed*/
737 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
738 initialize_class((classinfo *) this->declaringClass);
739 if (*exceptionptr) return 0;
741 utf_ptr = fid->descriptor->text;
746 case 'I': return fid->value.i;
747 case 'J': return fid->value.l;
749 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
753 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
754 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
757 utf_ptr = fid->descriptor->text;
759 case 'B':return getField(obj,jbyte,fid);
760 case 'S':return getField(obj,jshort,fid);
761 case 'C':return getField(obj,jchar,fid);
762 case 'I':return getField(obj,jint,fid);
763 case 'J':return getField(obj,jlong,fid);
765 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
773 * Class: java/lang/reflect/Field
775 * Signature: (Ljava/lang/Object;)S
777 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getShort(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj)
781 classinfo *c = (classinfo *) this->declaringClass;
784 /* check if the specified slot could be a valid field of this class*/
785 if (this->slot>((classinfo*)this->declaringClass)->fieldscount) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
786 "declaring class: fieldscount mismatch");
788 fid=&((classinfo*)this->declaringClass)->fields[this->slot]; /*get field*/
789 CHECKFIELDACCESS(this,fid,c,return 0);
792 /* check if the field really has the same name and check if the type descriptor is not empty*/
793 if (fid->name!=javastring_toutf(this->name,false)) throw_cacao_exception_exit(string_java_lang_IncompatibleClassChangeError,
794 "declaring class: field name mismatch");
795 if (fid->descriptor->blength<1) {
796 log_text("Type-Descriptor is empty");
800 /* check if obj would be needed (not static field), but is 0)*/
801 if ((!(fid->flags & ACC_STATIC)) && (obj==0)) {
802 *exceptionptr = new_exception(string_java_lang_NullPointerException);
806 /*if (!(fid->flags & ACC_PUBLIC)) {
807 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
812 if (fid->flags & ACC_STATIC) {
813 /* initialize class if needed*/
814 fprintf(stderr, "calling initialize_class %s\n",((classinfo*) this->declaringClass)->name->text);
815 initialize_class((classinfo *) this->declaringClass);
816 if (*exceptionptr) return 0;
818 utf_ptr = fid->descriptor->text;
821 case 'S': return fid->value.i;
823 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
827 if (!builtin_instanceof((java_objectheader*)obj,(classinfo*)this->declaringClass)) {
828 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
831 utf_ptr = fid->descriptor->text;
833 case 'B':return getField(obj,jbyte,fid);
834 case 'S':return getField(obj,jshort,fid);
836 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
844 * Class: java/lang/reflect/Field
846 * Signature: (Ljava/lang/Object;Ljava/lang/Object;)V
848 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, java_lang_Object *val)
850 jfieldID source_fid; /* the field containing the value to be written */
851 jfieldID fid; /* the field to be written */
855 c = (classinfo *) this->declaringClass;
857 fid = class_findfield_approx(c, javastring_toutf(this->name, false));
859 st = (fid->flags & ACC_STATIC); /* true if the field is static */
861 CHECKFIELDACCESS(this,fid,c,return);
863 if (val && (st || obj)) {
865 c = val->header.vftbl->class;
867 /* The fieldid is used to set the new value, for primitive types the value
868 has to be retrieved from the wrapping object */
869 switch ((((classinfo *) this->declaringClass)->fields[this->slot]).descriptor->text[0]) {
871 /* illegal argument specified */
872 if (c != class_java_lang_Integer)
874 /* determine the field to read the value */
875 source_fid = (*env)->GetFieldID(env, c, "value", "I");
879 /* set the new value */
882 (*env)->SetStaticIntField(env, c, fid, GetIntField(env, (jobject) val, source_fid));
885 (*env)->SetIntField(env, (jobject) obj, fid, GetIntField(env, (jobject) val, source_fid));
890 if (c != class_java_lang_Long)
892 source_fid = (*env)->GetFieldID(env, c, "value", "J");
897 (*env)->SetStaticLongField(env, c, fid, GetLongField(env, (jobject) val, source_fid));
899 (*env)->SetLongField(env, (jobject) obj, fid, GetLongField(env, (jobject) val, source_fid));
904 if (c != class_java_lang_Float)
906 source_fid = (*env)->GetFieldID(env, c, "value", "F");
911 (*env)->SetStaticFloatField(env, c, fid, GetFloatField(env, (jobject) val, source_fid));
913 (*env)->SetFloatField(env, (jobject) obj, fid, GetFloatField(env, (jobject) val, source_fid));
918 if (c != class_java_lang_Double)
920 source_fid = (*env)->GetFieldID(env, c, "value", "D");
921 if (!source_fid) break;
924 (*env)->SetStaticDoubleField(env, c, fid, GetDoubleField(env,(jobject) val,source_fid));
926 (*env)->SetDoubleField(env, (jobject) obj, fid, GetDoubleField(env,(jobject) val,source_fid));
931 if (c != class_java_lang_Byte)
933 source_fid = (*env)->GetFieldID(env, c, "value", "B");
938 (*env)->SetStaticByteField(env, c, fid, GetByteField(env, (jobject) val, source_fid));
940 (*env)->SetByteField(env, (jobject) obj, fid, GetByteField(env, (jobject) val, source_fid));
945 if (c != class_java_lang_Character)
947 source_fid = (*env)->GetFieldID(env, c, "value", "C");
952 (*env)->SetStaticCharField(env, c, fid, GetCharField(env, (jobject) val, source_fid));
954 (*env)->SetCharField(env, (jobject) obj, fid, GetCharField(env, (jobject) val, source_fid));
959 if (c != class_java_lang_Short)
961 source_fid = (*env)->GetFieldID(env, c, "value", "S");
966 (*env)->SetStaticShortField(env, c, fid, GetShortField(env, (jobject) val, source_fid));
968 (*env)->SetShortField(env, (jobject) obj, fid, GetShortField(env, (jobject) val, source_fid));
973 if (c != class_java_lang_Boolean)
975 source_fid = (*env)->GetFieldID(env, c, "value", "Z");
980 (*env)->SetStaticBooleanField(env, c, fid, GetBooleanField(env, (jobject) val, source_fid));
982 (*env)->SetBooleanField(env, (jobject) obj, fid, GetBooleanField(env, (jobject) val, source_fid));
989 (*env)->SetStaticObjectField(env, c, fid, (jobject) val);
991 (*env)->SetObjectField(env, (jobject) obj, fid, (jobject) val);
997 /* raise exception */
998 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1003 * Class: java/lang/reflect/Field
1004 * Method: setBoolean
1005 * Signature: (Ljava/lang/Object;Z)V
1007 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setBoolean(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, s4 val)
1011 if (this->declaringClass && obj) {
1012 fid = class_findfield_approx((classinfo *) this->declaringClass,
1013 javastring_toutf(this->name, false));
1016 (*env)->SetBooleanField(env, (jobject) obj, fid, val);
1021 /* raise exception */
1022 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1027 * Class: java/lang/reflect/Field
1029 * Signature: (Ljava/lang/Object;B)V
1031 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setByte(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, s4 val)
1035 if (this->declaringClass && obj) {
1036 fid = class_findfield_approx((classinfo *) this->declaringClass,
1037 javastring_toutf(this->name, false));
1040 (*env)->SetByteField(env, (jobject) obj, fid, val);
1045 /* raise exception */
1046 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1051 * Class: java/lang/reflect/Field
1053 * Signature: (Ljava/lang/Object;C)V
1055 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setChar(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, s4 val)
1059 if (this->declaringClass && obj) {
1060 fid = class_findfield_approx((classinfo *) this->declaringClass,
1061 javastring_toutf(this->name, false));
1064 (*env)->SetCharField(env, (jobject) obj, fid, val);
1069 /* raise exception */
1070 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1075 * Class: java/lang/reflect/Field
1077 * Signature: (Ljava/lang/Object;D)V
1079 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setDouble(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, double val)
1083 if (this->declaringClass && obj) {
1084 fid = class_findfield_approx((classinfo *) this->declaringClass,
1085 javastring_toutf(this->name, false));
1088 (*env)->SetDoubleField(env, (jobject) obj, fid, val);
1093 /* raise exception */
1094 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1099 * Class: java/lang/reflect/Field
1101 * Signature: (Ljava/lang/Object;F)V
1103 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setFloat(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, float val)
1107 if (this->declaringClass && obj) {
1108 fid = class_findfield_approx((classinfo *) this->declaringClass,
1109 javastring_toutf(this->name, false));
1112 (*env)->SetFloatField(env, (jobject) obj, fid, val);
1117 /* raise exception */
1118 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1123 * Class: java/lang/reflect/Field
1125 * Signature: (Ljava/lang/Object;I)V
1127 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setInt(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, s4 val)
1131 if (this->declaringClass && obj) {
1132 fid = class_findfield_approx((classinfo *) this->declaringClass,
1133 javastring_toutf(this->name, false));
1136 (*env)->SetIntField(env, (jobject) obj, fid, val);
1141 /* raise exception */
1142 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1147 * Class: java/lang/reflect/Field
1149 * Signature: (Ljava/lang/Object;J)V
1151 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setLong(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, s8 val)
1155 if (this->declaringClass && obj) {
1156 fid = class_findfield_approx((classinfo *) this->declaringClass,
1157 javastring_toutf(this->name, false));
1160 (*env)->SetLongField(env, (jobject) obj, fid, val);
1165 /* raise exception */
1166 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1171 * Class: java/lang/reflect/Field
1173 * Signature: (Ljava/lang/Object;S)V
1175 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setShort(JNIEnv *env, java_lang_reflect_Field *this, java_lang_Object *obj, s4 val)
1179 if (this->declaringClass && obj) {
1180 fid = class_findfield_approx((classinfo *) this->declaringClass,
1181 javastring_toutf(this->name, false));
1184 (*env)->SetShortField(env, (jobject) obj, fid, val);
1189 /* raise exception */
1190 *exceptionptr = new_exception(string_java_lang_IllegalArgumentException);
1195 * Class: java_lang_reflect_Field
1197 * Signature: ()Ljava/lang/Class;
1199 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Field_getType(JNIEnv *env, java_lang_reflect_Field *this)
1201 typedesc *desc = (((classinfo *) this->declaringClass)->fields[this->slot]).parseddesc;
1202 java_lang_Class *ret;
1206 if (!resolve_class_from_typedesc(desc,true,false,(classinfo **)&ret))
1207 return NULL; /* exception */
1209 use_class_as_object((classinfo*)ret);
1215 * Class: java_lang_reflect_Field
1216 * Method: getModifiers
1219 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getModifiers(JNIEnv *env, java_lang_reflect_Field *this)
1221 return (((classinfo *) this->declaringClass)->fields[this->slot]).flags;
1226 * These are local overrides for various environment variables in Emacs.
1227 * Please do not remove this and leave it at the end of the file, where
1228 * Emacs will automagically detect them.
1229 * ---------------------------------------------------------------------
1232 * indent-tabs-mode: t