* src/vm/jit/i386/darwin/md-os.c (md_replace_executionstate_read):
[cacao.git] / src / native / vm / gnu / java_lang_reflect_Field.c
index af9fb08d89c11ac3aa0dd040051b6aa1a81182f8..0dcc8c7e351f883c71b81120512327c19a751965 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: java_lang_reflect_Field.c 8132 2007-06-22 11:15:47Z twisti $
-
 */
 
 
@@ -33,6 +31,7 @@
 #include <stdint.h>
 
 #include "native/jni.h"
+#include "native/llni.h"
 #include "native/native.h"
 
 #include "native/include/java_lang_Boolean.h"
 
 #include "native/include/java_lang_reflect_Field.h"
 
+#if defined(ENABLE_ANNOTATIONS)
+#include "native/include/java_util_Map.h"
+#include "native/include/sun_reflect_ConstantPool.h"
+#include "native/vm/reflect.h"
+#endif
+
 #include "vm/access.h"
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
 #include "vm/initialize.h"
+#include "vm/primitive.h"
 #include "vm/resolve.h"
 #include "vm/stringlocal.h"
 
 #include "vm/jit/stacktrace.h"
 
 #include "vmcore/loader.h"
-#include "vmcore/primitive.h"
 #include "vmcore/utf8.h"
 
 
@@ -88,6 +93,9 @@ static JNINativeMethod methods[] = {
        { "setFloat",             "(Ljava/lang/Object;F)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setFloat             },
        { "setDouble",            "(Ljava/lang/Object;D)V",                  (void *) (intptr_t) &Java_java_lang_reflect_Field_setDouble            },
        { "getSignature",         "()Ljava/lang/String;",                    (void *) (intptr_t) &Java_java_lang_reflect_Field_getSignature         },
+#if defined(ENABLE_ANNOTATIONS)
+       { "declaredAnnotations",  "()Ljava/util/Map;",                       (void *) (intptr_t) &Java_java_lang_reflect_Field_declaredAnnotations  },
+#endif
 };
 
 
@@ -107,74 +115,147 @@ void _Jv_java_lang_reflect_Field_init(void)
 }
 
 
-/* cacao_get_field_address *****************************************************
+/* _field_access_check *********************************************************
 
-   Return the address of a field of an object.
-
-   IN:
-      this.........the field (a java.lang.reflect.Field object)
-         o............the object of which to get the field
+   Checks if the field can be accessed.
 
    RETURN VALUE:
-      a pointer to the field, or
-         NULL if an exception has been thrown
+      true......field can be accessed, or
+      false.....otherwise (maybe an Exception was thrown).
 
 *******************************************************************************/
 
-static void *cacao_get_field_address(java_lang_reflect_Field *this,
-                                                                        java_lang_Object *o)
+static bool _field_access_check(java_lang_reflect_Field *this,
+                                                               fieldinfo *f, classinfo *c, java_handle_t *o)
 {
-       classinfo *c;
-       fieldinfo *f;
-
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       int32_t flag;
 
-       /* check field access */
        /* check if we should bypass security checks (AccessibleObject) */
 
-       if (this->flag == false) {
+       LLNI_field_get_val(this, flag, flag);
+       if (flag == false) {
                /* this function is always called like this:
-
                           java.lang.reflect.Field.xxx (Native Method)
                   [0] <caller>
                */
                if (!access_check_field(f, 0))
-                       return NULL;
+                       return false;
        }
 
-       /* get the address of the field */
+       /* some general checks */
 
        if (f->flags & ACC_STATIC) {
                /* initialize class if required */
 
                if (!(c->state & CLASS_INITIALIZED))
                        if (!initialize_class(c))
-                               return NULL;
+                               return false;
 
-               /* return value address */
+               /* everything is ok */
 
-               return &(f->value);
+               return true;
 
        } else {
                /* obj is required for not-static fields */
 
                if (o == NULL) {
                        exceptions_throw_nullpointerexception();
-                       return NULL;
+                       return false;
                }
        
-               if (builtin_instanceof((java_objectheader *) o, c))
-                       return (void *) ((intptr_t) o + f->offset);
+               if (builtin_instanceof(o, c))
+                       return true;
        }
 
        /* exception path */
 
        exceptions_throw_illegalargumentexception();
+       return false;
+}
 
-       return NULL;
+
+/* _field_get_type *************************************************************
+
+   Returns the content of the given field.
+
+*******************************************************************************/
+
+#define _FIELD_GET_TYPE(name, type, uniontype) \
+static inline type _field_get_##name(fieldinfo *f, java_lang_Object *o) \
+{ \
+       type ret; \
+       if (f->flags & ACC_STATIC) { \
+               ret = f->value->uniontype; \
+       } else { \
+               LLNI_CRITICAL_START; \
+               ret = *(type *) (((intptr_t) LLNI_DIRECT(o)) + f->offset); \
+               LLNI_CRITICAL_END; \
+       } \
+       return ret; \
 }
 
+static inline java_handle_t *_field_get_handle(fieldinfo *f, java_lang_Object *o)
+{
+       java_object_t *obj;
+       java_handle_t *hdl;
+
+       LLNI_CRITICAL_START;
+
+       if (f->flags & ACC_STATIC) {
+               obj = f->value->a;
+       } else {
+               obj = *(java_object_t **) (((intptr_t) LLNI_DIRECT(o)) + f->offset);
+       }
+
+       hdl = LLNI_WRAP(obj);
+
+       LLNI_CRITICAL_END;
+
+       return hdl;
+}
+
+_FIELD_GET_TYPE(int,    int32_t, i)
+_FIELD_GET_TYPE(long,   int64_t, l)
+_FIELD_GET_TYPE(float,  float,   f)
+_FIELD_GET_TYPE(double, double,  d)
+
+
+/* _field_set_type *************************************************************
+
+   Sets the content of the given field to the given value.
+
+*******************************************************************************/
+
+#define _FIELD_SET_TYPE(name, type, uniontype) \
+static inline void _field_set_##name(fieldinfo *f, java_lang_Object *o, type value) \
+{ \
+       if (f->flags & ACC_STATIC) { \
+               f->value->uniontype = value; \
+       } else { \
+               LLNI_CRITICAL_START; \
+               *(type *) (((intptr_t) LLNI_DIRECT(o)) + f->offset) = value; \
+               LLNI_CRITICAL_END; \
+       } \
+}
+
+static inline void _field_set_handle(fieldinfo *f, java_lang_Object *o, java_handle_t *value)
+{
+       LLNI_CRITICAL_START;
+
+       if (f->flags & ACC_STATIC) {
+               f->value->a = LLNI_DIRECT(value);
+       } else {
+               *(java_object_t **) (((intptr_t) LLNI_DIRECT(o)) + f->offset) = LLNI_DIRECT(value);
+       }
+
+       LLNI_CRITICAL_END;
+}
+
+_FIELD_SET_TYPE(int,    int32_t, i)
+_FIELD_SET_TYPE(long,   int64_t, l)
+_FIELD_SET_TYPE(float,  float,   f)
+_FIELD_SET_TYPE(double, double,  d)
+
 
 /*
  * Class:     java/lang/reflect/Field
@@ -185,9 +266,11 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getModifiersInternal(JNIE
 {
        classinfo *c;
        fieldinfo *f;
+       int32_t    slot;
 
-       c = (classinfo *) this->clazz;
-       f = &(c->fields[this->slot]);
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &(c->fields[slot]);
 
        return f->flags;
 }
@@ -203,9 +286,11 @@ JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Field_getType(JNIEnv *
        classinfo *c;
        typedesc  *desc;
        classinfo *ret;
+       int32_t    slot;
 
-       c    = (classinfo *) this->clazz;
-       desc = c->fields[this->slot].parseddesc;
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       desc = c->fields[slot].parseddesc;
 
        if (desc == NULL)
                return NULL;
@@ -213,7 +298,7 @@ JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Field_getType(JNIEnv *
        if (!resolve_class_from_typedesc(desc, true, false, &ret))
                return NULL;
        
-       return (java_lang_Class *) ret;
+       return LLNI_classinfo_wrap(ret);
 }
 
 
@@ -226,122 +311,49 @@ JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Field_get(JNIEnv *env
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
+       imm_union  value;
+       java_handle_t *object;
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get address of the source field value */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return NULL;
 
        switch (f->parseddesc->decltype) {
-       case PRIMITIVETYPE_BOOLEAN: {
-               java_lang_Boolean *bo;
-
-               /* create wrapping class */
-
-               if (!(bo = (java_lang_Boolean *) builtin_new(class_java_lang_Boolean)))
-                       return NULL;
-
-               /* set the object value */
-
-               bo->value = *((int32_t *) addr);
-
-               /* return the wrapped object */
-
-               return (java_lang_Object *) bo;
-       }
-
-       case PRIMITIVETYPE_BYTE: {
-               java_lang_Byte *bo;
-
-               if (!(bo = (java_lang_Byte *) builtin_new(class_java_lang_Byte)))
-                       return NULL;
-
-               bo->value = *((int32_t *) addr);
-
-               return (java_lang_Object *) bo;
-       }
-
-       case PRIMITIVETYPE_CHAR: {
-               java_lang_Character *co;
-
-               if (!(co = (java_lang_Character *) builtin_new(class_java_lang_Character)))
-                       return NULL;
-
-               co->value = *((int32_t *) addr);
-
-               return (java_lang_Object *) co;
-       }
-
-       case PRIMITIVETYPE_SHORT: {
-               java_lang_Short *so;
-
-               if (!(so = (java_lang_Short *) builtin_new(class_java_lang_Short)))
-                       return NULL;
-
-               so->value = (int32_t) *((int32_t *) addr);
-
-               return (java_lang_Object *) so;
-       }
-
-       case PRIMITIVETYPE_INT: {
-               java_lang_Integer *io;
-
-               if (!(io = (java_lang_Integer *) builtin_new(class_java_lang_Integer)))
-                       return NULL;
-
-               io->value = *((int32_t *) addr);
-
-               return (java_lang_Object *) io;
-       }
-
-       case PRIMITIVETYPE_LONG: {
-               java_lang_Long *lo;
-
-               if (!(lo = (java_lang_Long *) builtin_new(class_java_lang_Long)))
-                       return NULL;
-
-               lo->value = *((int64_t *) addr);
-
-               return (java_lang_Object *) lo;
-       }
-
-       case PRIMITIVETYPE_FLOAT: {
-               java_lang_Float *fo;
-
-               if (!(fo = (java_lang_Float *) builtin_new(class_java_lang_Float)))
-                       return NULL;
-
-               fo->value = *((float *) addr);
-
-               return (java_lang_Object *) fo;
-       }
-
-       case PRIMITIVETYPE_DOUBLE: {
-               java_lang_Double *_do;
+       case PRIMITIVETYPE_BOOLEAN:
+       case PRIMITIVETYPE_BYTE:
+       case PRIMITIVETYPE_CHAR:
+       case PRIMITIVETYPE_SHORT:
+       case PRIMITIVETYPE_INT:
+               value.i = _field_get_int(f, o);
+               break;
 
-               if (!(_do = (java_lang_Double *) builtin_new(class_java_lang_Double)))
-                       return NULL;
+       case PRIMITIVETYPE_LONG:
+               value.l = _field_get_long(f, o);
+               break;
 
-               _do->value = *((double *) addr);
+       case PRIMITIVETYPE_FLOAT:
+               value.f = _field_get_float(f, o);
+               break;
 
-               return (java_lang_Object *) _do;
-       }
+       case PRIMITIVETYPE_DOUBLE:
+               value.d = _field_get_double(f, o);
+               break;
 
        case TYPE_ADR:
-               return (java_lang_Object *) *((java_objectheader **) addr);
+               return (java_lang_Object *) _field_get_handle(f, o);
        }
 
-       /* this must not happen */
+       /* Now box the primitive types. */
 
-       assert(0);
+       object = primitive_box(f->parseddesc->decltype, value);
 
-       /* keep compiler happy */
-
-       return NULL;
+       return (java_lang_Object *) object;
 }
 
 
@@ -354,23 +366,24 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getBoolean(JNIEnv *env, j
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_BOOLEAN:
-               return (int32_t) *((int32_t *) addr);
+               return (int32_t) _field_get_int(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -387,23 +400,24 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getByte(JNIEnv *env, java
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_BYTE:
-               return (int32_t) *((int32_t *) addr);
+               return (int32_t) _field_get_int(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -420,23 +434,24 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getChar(JNIEnv *env, java
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_CHAR:
-               return (int32_t) *((int32_t *) addr);
+               return (int32_t) _field_get_int(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -453,16 +468,17 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getShort(JNIEnv *env, jav
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
@@ -470,7 +486,7 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getShort(JNIEnv *env, jav
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_BYTE:
        case PRIMITIVETYPE_SHORT:
-               return (int32_t) *((int32_t *) addr);
+               return (int32_t) _field_get_int(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -487,16 +503,17 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getInt(JNIEnv *env , java
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
@@ -506,7 +523,7 @@ JNIEXPORT int32_t JNICALL Java_java_lang_reflect_Field_getInt(JNIEnv *env , java
        case PRIMITIVETYPE_CHAR:
        case PRIMITIVETYPE_SHORT:
        case PRIMITIVETYPE_INT:
-               return (int32_t) *((int32_t *) addr);
+               return (int32_t) _field_get_int(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -523,16 +540,17 @@ JNIEXPORT int64_t JNICALL Java_java_lang_reflect_Field_getLong(JNIEnv *env, java
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
@@ -542,9 +560,9 @@ JNIEXPORT int64_t JNICALL Java_java_lang_reflect_Field_getLong(JNIEnv *env, java
        case PRIMITIVETYPE_CHAR:
        case PRIMITIVETYPE_SHORT:
        case PRIMITIVETYPE_INT:
-               return (int64_t) *((int32_t *) addr);
+               return (int64_t) _field_get_int(f, o);
        case PRIMITIVETYPE_LONG:
-               return (int64_t) *((int64_t *) addr);
+               return (int64_t) _field_get_long(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -561,16 +579,17 @@ JNIEXPORT float JNICALL Java_java_lang_reflect_Field_getFloat(JNIEnv *env, java_
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
@@ -580,11 +599,11 @@ JNIEXPORT float JNICALL Java_java_lang_reflect_Field_getFloat(JNIEnv *env, java_
        case PRIMITIVETYPE_CHAR:
        case PRIMITIVETYPE_SHORT:
        case PRIMITIVETYPE_INT:
-               return (float) *((int32_t *) addr);
+               return (float) _field_get_int(f, o);
        case PRIMITIVETYPE_LONG:
-               return (float) *((int64_t *) addr);
+               return (float) _field_get_long(f, o);
        case PRIMITIVETYPE_FLOAT:
-               return (float) *((float *) addr);
+               return (float) _field_get_float(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -601,16 +620,17 @@ JNIEXPORT double JNICALL Java_java_lang_reflect_Field_getDouble(JNIEnv *env , ja
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return 0;
 
        /* check the field type and return the value */
@@ -620,13 +640,13 @@ JNIEXPORT double JNICALL Java_java_lang_reflect_Field_getDouble(JNIEnv *env , ja
        case PRIMITIVETYPE_CHAR:
        case PRIMITIVETYPE_SHORT:
        case PRIMITIVETYPE_INT:
-               return (double) *((int32_t *) addr);
+               return (double) _field_get_int(f, o);
        case PRIMITIVETYPE_LONG:
-               return (double) *((int64_t *) addr);
+               return (double) _field_get_long(f, o);
        case PRIMITIVETYPE_FLOAT:
-               return (double) *((float *) addr);
+               return (double) _field_get_float(f, o);
        case PRIMITIVETYPE_DOUBLE:
-               return (double) *((double *) addr);
+               return (double) _field_get_double(f, o);
        default:
                exceptions_throw_illegalargumentexception();
                return 0;
@@ -645,16 +665,17 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
        classinfo *dc;
        fieldinfo *sf;
        fieldinfo *df;
-       void      *faddr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       dc = (classinfo *) this->clazz;
-       df = &dc->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, dc);
+       LLNI_field_get_val(this, slot , slot);
+       df = &dc->fields[slot];
 
-       /* get the address of the destination field */
+       /* check if the field can be accessed */
 
-       if ((faddr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, df, dc, (java_handle_t *) o))
                return;
 
        /* get the source classinfo from the object */
@@ -662,7 +683,7 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
        if (value == NULL)
                sc = NULL;
        else
-               sc = value->header.vftbl->class;
+               LLNI_class_get(value, sc);
 
        /* The fieldid is used to set the new value, for primitive
           types the value has to be retrieved from the wrapping
@@ -679,14 +700,14 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
 
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BOOLEAN:
-                       val = ((java_lang_Boolean *) value)->value;
+                       LLNI_field_get_val((java_lang_Boolean *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((int32_t *) faddr) = val;
+               _field_set_int(df, o, val);
                return;
        }
 
@@ -698,14 +719,14 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
 
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BYTE:
-                       val = ((java_lang_Byte *) value)->value;
+                       LLNI_field_get_val((java_lang_Byte *) value, value, val);
                        break;
                default:        
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((int32_t *) faddr) = val;
+               _field_set_int(df, o, val);
                return;
        }
 
@@ -717,14 +738,14 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
                                   
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_CHAR:
-                       val = ((java_lang_Character *) value)->value;
+                       LLNI_field_get_val((java_lang_Character *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((int32_t *) faddr) = val;
+               _field_set_int(df, o, val);
                return;
        }
 
@@ -738,17 +759,17 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
                                   
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BYTE:
-                       val = ((java_lang_Byte *) value)->value;
+                       LLNI_field_get_val((java_lang_Byte *) value, value, val);
                        break;
                case PRIMITIVETYPE_SHORT:
-                       val = ((java_lang_Short *) value)->value;
+                       LLNI_field_get_val((java_lang_Short *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((int32_t *) faddr) = val;
+               _field_set_int(df, o, val);
                return;
        }
 
@@ -762,23 +783,23 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
 
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BYTE:
-                       val = ((java_lang_Byte *) value)->value;
+                       LLNI_field_get_val((java_lang_Byte *) value, value, val);
                        break;
                case PRIMITIVETYPE_CHAR:
-                       val = ((java_lang_Character *) value)->value;
+                       LLNI_field_get_val((java_lang_Character *) value, value, val);
                        break;
                case PRIMITIVETYPE_SHORT:
-                       val = ((java_lang_Short *) value)->value;
+                       LLNI_field_get_val((java_lang_Short *) value, value, val);
                        break;
                case PRIMITIVETYPE_INT:
-                       val = ((java_lang_Integer *) value)->value;
+                       LLNI_field_get_val((java_lang_Integer *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((int32_t *) faddr) = val;
+               _field_set_int(df, o, val);
                return;
        }
 
@@ -792,26 +813,26 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
 
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BYTE:
-                       val = ((java_lang_Byte *) value)->value;
+                       LLNI_field_get_val((java_lang_Byte *) value, value, val);
                        break;
                case PRIMITIVETYPE_CHAR:
-                       val = ((java_lang_Character *) value)->value;
+                       LLNI_field_get_val((java_lang_Character *) value, value, val);
                        break;
                case PRIMITIVETYPE_SHORT:
-                       val = ((java_lang_Short *) value)->value;
+                       LLNI_field_get_val((java_lang_Short *) value, value, val);
                        break;
                case PRIMITIVETYPE_INT:
-                       val = ((java_lang_Integer *) value)->value;
+                       LLNI_field_get_val((java_lang_Integer *) value, value, val);
                        break;
                case PRIMITIVETYPE_LONG:
-                       val = ((java_lang_Long *) value)->value;
+                       LLNI_field_get_val((java_lang_Long *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((int64_t *) faddr) = val;
+               _field_set_long(df, o, val);
                return;
        }
 
@@ -825,29 +846,29 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
 
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BYTE:
-                       val = ((java_lang_Byte *) value)->value;
+                       LLNI_field_get_val((java_lang_Byte *) value, value, val);
                        break;
                case PRIMITIVETYPE_CHAR:
-                       val = ((java_lang_Character *) value)->value;
+                       LLNI_field_get_val((java_lang_Character *) value, value, val);
                        break;
                case PRIMITIVETYPE_SHORT:
-                       val = ((java_lang_Short *) value)->value;
+                       LLNI_field_get_val((java_lang_Short *) value, value, val);
                        break;
                case PRIMITIVETYPE_INT:
-                       val = ((java_lang_Integer *) value)->value;
+                       LLNI_field_get_val((java_lang_Integer *) value, value, val);
                        break;
                case PRIMITIVETYPE_LONG:
-                       val = ((java_lang_Long *) value)->value;
+                       LLNI_field_get_val((java_lang_Long *) value, value, val);
                        break;
                case PRIMITIVETYPE_FLOAT:
-                       val = ((java_lang_Float *) value)->value;
+                       LLNI_field_get_val((java_lang_Float *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((float *) faddr) = val;
+               _field_set_float(df, o, val);
                return;
        }
 
@@ -861,32 +882,32 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
 
                switch (sf->parseddesc->decltype) {
                case PRIMITIVETYPE_BYTE:
-                       val = ((java_lang_Byte *) value)->value;
+                       LLNI_field_get_val((java_lang_Byte *) value, value, val);
                        break;
                case PRIMITIVETYPE_CHAR:
-                       val = ((java_lang_Character *) value)->value;
+                       LLNI_field_get_val((java_lang_Character *) value, value, val);
                        break;
                case PRIMITIVETYPE_SHORT:
-                       val = ((java_lang_Short *) value)->value;
+                       LLNI_field_get_val((java_lang_Short *) value, value, val);
                        break;
                case PRIMITIVETYPE_INT:
-                       val = ((java_lang_Integer *) value)->value;
+                       LLNI_field_get_val((java_lang_Integer *) value, value, val);
                        break;
                case PRIMITIVETYPE_LONG:
-                       val = ((java_lang_Long *) value)->value;
+                       LLNI_field_get_val((java_lang_Long *) value, value, val);
                        break;
                case PRIMITIVETYPE_FLOAT:
-                       val = ((java_lang_Float *) value)->value;
+                       LLNI_field_get_val((java_lang_Float *) value, value, val);
                        break;
                case PRIMITIVETYPE_DOUBLE:
-                       val = ((java_lang_Double *) value)->value;
+                       LLNI_field_get_val((java_lang_Double *) value, value, val);
                        break;
                default:
                        exceptions_throw_illegalargumentexception();
                        return;
                }
 
-               *((double *) faddr) = val;
+               _field_set_double(df, o, val);
                return;
        }
 
@@ -894,10 +915,10 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set(JNIEnv *env, java_lang_r
                /* check if value is an instance of the destination class */
 
                /* XXX TODO */
-               /*                      if (!builtin_instanceof((java_objectheader *) value, df->class)) */
+               /*                      if (!builtin_instanceof((java_handle_t *) value, df->class)) */
                /*                              break; */
 
-               *((java_lang_Object **) faddr) = value;
+               _field_set_handle(df, o, (java_handle_t *) value);
                return;
        }
 
@@ -916,23 +937,24 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setBoolean(JNIEnv *env, java
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_BOOLEAN:
-               *((int32_t *) addr) = value;
+               _field_set_int(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -951,16 +973,17 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setByte(JNIEnv *env, java_la
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
@@ -969,16 +992,16 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setByte(JNIEnv *env, java_la
        case PRIMITIVETYPE_BYTE:
        case PRIMITIVETYPE_SHORT:
        case PRIMITIVETYPE_INT:
-               *((int32_t *) addr) = value;
+               _field_set_int(f, o, value);
                break;
        case PRIMITIVETYPE_LONG:
-               *((int64_t *) addr) = value;
+               _field_set_long(f, o, value);
                break;
        case PRIMITIVETYPE_FLOAT:
-               *((float *) addr) = value;
+               _field_set_float(f, o, value);
                break;
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -997,16 +1020,17 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setChar(JNIEnv *env, java_la
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
@@ -1014,16 +1038,16 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setChar(JNIEnv *env, java_la
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_CHAR:
        case PRIMITIVETYPE_INT:
-               *((int32_t *) addr) = value;
+               _field_set_int(f, o, value);
                break;
        case PRIMITIVETYPE_LONG:
-               *((int64_t *) addr) = value;
+               _field_set_long(f, o, value);
                break;
        case PRIMITIVETYPE_FLOAT:
-               *((float *) addr) = value;
+               _field_set_float(f, o, value);
                break;
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -1042,16 +1066,17 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setShort(JNIEnv *env, java_l
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
@@ -1059,16 +1084,16 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setShort(JNIEnv *env, java_l
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_SHORT:
        case PRIMITIVETYPE_INT:
-               *((int32_t *) addr) = value;
+               _field_set_int(f, o, value);
                break;
        case PRIMITIVETYPE_LONG:
-               *((int64_t *) addr) = value;
+               _field_set_long(f, o, value);
                break;
        case PRIMITIVETYPE_FLOAT:
-               *((float *) addr) = value;
+               _field_set_float(f, o, value);
                break;
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -1087,32 +1112,33 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setInt(JNIEnv *env, java_lan
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_INT:
-               *((int32_t *) addr) = value;
+               _field_set_int(f, o, value);
                break;
        case PRIMITIVETYPE_LONG:
-               *((int64_t *) addr) = value;
+               _field_set_long(f, o, value);
                break;
        case PRIMITIVETYPE_FLOAT:
-               *((float *) addr) = value;
+               _field_set_float(f, o, value);
                break;
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -1131,29 +1157,30 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setLong(JNIEnv *env, java_la
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_LONG:
-               *((int64_t *) addr) = value;
+               _field_set_long(f, o, value);
                break;
        case PRIMITIVETYPE_FLOAT:
-               *((float *) addr) = value;
+               _field_set_float(f, o, value);
                break;
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -1172,26 +1199,27 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setFloat(JNIEnv *env, java_l
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_FLOAT:
-               *((float *) addr) = value;
+               _field_set_float(f, o, value);
                break;
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -1210,23 +1238,24 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setDouble(JNIEnv *env, java_
 {
        classinfo *c;
        fieldinfo *f;
-       void      *addr;
+       int32_t    slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
-       /* get the address of the field with an internal helper */
+       /* check if the field can be accessed */
 
-       if ((addr = cacao_get_field_address(this, o)) == NULL)
+       if (!_field_access_check(this, f, c, (java_handle_t *) o))
                return;
 
        /* check the field type and set the value */
 
        switch (f->parseddesc->decltype) {
        case PRIMITIVETYPE_DOUBLE:
-               *((double *) addr) = value;
+               _field_set_double(f, o, value);
                break;
        default:
                exceptions_throw_illegalargumentexception();
@@ -1243,14 +1272,16 @@ JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setDouble(JNIEnv *env, java_
  */
 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Field_getSignature(JNIEnv *env, java_lang_reflect_Field* this)
 {
-       classinfo         *c;
-       fieldinfo         *f;
-       java_objectheader *o;
+       classinfo     *c;
+       fieldinfo     *f;
+       java_handle_t *o;
+       int32_t        slot;
 
        /* get the class and the field */
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       f = &c->fields[slot];
 
        if (f->signature == NULL)
                return NULL;
@@ -1263,6 +1294,38 @@ JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Field_getSignature(JN
 }
 
 
+#if defined(ENABLE_ANNOTATIONS)
+/*
+ * Class:     java/lang/reflect/Field
+ * Method:    declaredAnnotations
+ * Signature: ()Ljava/util/Map;
+ */
+JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Field_declaredAnnotations(JNIEnv *env, java_lang_reflect_Field *this)
+{
+       java_util_Map           *declaredAnnotations = NULL; /* parsed annotations                                */
+       java_handle_bytearray_t *annotations         = NULL; /* unparsed annotations                              */
+       java_lang_Class         *declaringClass      = NULL; /* the constant pool of this class is used           */
+       classinfo               *referer             = NULL; /* class, which calles the annotation parser         */
+                                                            /* (for the parameter 'referer' of vm_call_method()) */
+
+       LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
+
+       /* are the annotations parsed yet? */
+       if (declaredAnnotations == NULL) {
+               LLNI_field_get_ref(this, annotations, annotations);
+               LLNI_field_get_ref(this, clazz, declaringClass);
+               LLNI_class_get(this, referer);
+
+               declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
+
+               LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
+       }
+
+       return declaredAnnotations;
+}
+#endif
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where
@@ -1274,4 +1337,5 @@ JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Field_getSignature(JN
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */