* src/vm/jit/i386/darwin/md-os.c (md_replace_executionstate_read):
[cacao.git] / src / native / vm / gnu / java_lang_reflect_Field.c
index 61de92d25b498b2eafbe663bb713c056ea7ece34..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 8262 2007-08-06 12:44:01Z panzi $
-
 */
 
 
@@ -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"
@@ -50,6 +49,7 @@
 #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/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"
 
 
@@ -115,74 +115,147 @@ void _Jv_java_lang_reflect_Field_init(void)
 }
 
 
-/* cacao_get_field_address *****************************************************
-
-   Return the address of a field of an object.
+/* _field_access_check *********************************************************
 
-   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;
+       int32_t flag;
 
-       c = (classinfo *) this->clazz;
-       f = &c->fields[this->slot];
-
-       /* 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;
+}
+
+
+/* _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)
 
-       return NULL;
+
+/* _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
@@ -193,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;
 }
@@ -211,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;
@@ -221,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);
 }
 
 
@@ -234,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 */
-
-       assert(0);
+       /* Now box the primitive types. */
 
-       /* keep compiler happy */
+       object = primitive_box(f->parseddesc->decltype, value);
 
-       return NULL;
+       return (java_lang_Object *) object;
 }
 
 
@@ -362,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;
@@ -395,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;
@@ -428,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;
@@ -461,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 */
@@ -478,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;
@@ -495,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 */
@@ -514,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;
@@ -531,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 */
@@ -550,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;
@@ -569,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 */
@@ -588,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;
@@ -609,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 */
@@ -628,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;
@@ -653,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 */
@@ -670,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
@@ -687,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;
        }
 
@@ -706,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;
        }
 
@@ -725,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;
        }
 
@@ -746,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;
        }
 
@@ -770,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;
        }
 
@@ -800,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;
        }
 
@@ -833,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;
        }
 
@@ -869,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;
        }
 
@@ -902,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;
        }
 
@@ -924,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();
@@ -959,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 */
@@ -977,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();
@@ -1005,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 */
@@ -1022,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();
@@ -1050,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 */
@@ -1067,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();
@@ -1095,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();
@@ -1139,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();
@@ -1180,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();
@@ -1218,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();
@@ -1251,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;
@@ -1277,16 +1300,28 @@ JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Field_getSignature(JN
  * Method:    declaredAnnotations
  * Signature: ()Ljava/util/Map;
  */
-JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Field_declaredAnnotations(JNIEnv *env, struct java_lang_reflect_Field* this)
+JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Field_declaredAnnotations(JNIEnv *env, java_lang_reflect_Field *this)
 {
-       java_objectheader *o = (java_objectheader*)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()) */
 
-       if (this == NULL) {
-               exceptions_throw_nullpointerexception();
-               return NULL;
+       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 reflect_get_declaredannotatios(&(this->declaredAnnotations), this->annotations, this->clazz, o->vftbl->class);
+       return declaredAnnotations;
 }
 #endif
 
@@ -1302,4 +1337,5 @@ JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Field_declaredAnn
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */