* Removed all Id tags.
[cacao.git] / src / native / vm / reflect.c
index 60f91bed5bb900bd037dac586f125fe2a4596282..72b268ab6f590094a162bd38cbaaf8890fb251ff 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: reflect.c 8262 2007-08-06 12:44:01Z panzi $
-
 */
 
 
@@ -42,6 +40,7 @@
 #endif
 
 #include "native/jni.h"
+#include "native/llni.h"
 #include "native/native.h"
 
 /* keep this order of the native includes */
@@ -59,7 +58,6 @@
 #include "native/include/java_lang_reflect_Field.h"
 #include "native/include/java_lang_reflect_Method.h"
 
-#include "native/vm/java_lang_String.h"
 #include "native/vm/reflect.h"
 
 #include "vm/builtin.h"
 java_lang_reflect_Constructor *reflect_constructor_new(methodinfo *m)
 {
        classinfo                     *c;
-       java_objectheader             *o;
+       java_handle_t                 *o;
        java_lang_reflect_Constructor *rc;
        int32_t                        slot;
-       java_bytearray                *annotations          = NULL;
-       java_bytearray                *parameterAnnotations = NULL;
-       annotation_bytearray_t        *ba                   = NULL;
-
-#if defined(ENABLE_ANNOTATIONS)
-       /* get annotations */
-       ba = method_get_annotations(m);
-
-       if (ba != NULL) {
-               annotations = builtin_newarray_byte(ba->size);
-
-               if (annotations == NULL)
-                       return NULL;
-               
-               MCOPY(annotations->data, ba->data, uint8_t, ba->size);
-       }
-       
-       /* get parameter annotations */
-       ba = method_get_parameterannotations(m);
-
-       if (ba != NULL) {
-               parameterAnnotations = builtin_newarray_byte(ba->size);
-
-               if (parameterAnnotations == NULL)
-                       return NULL;
-               
-               MCOPY(parameterAnnotations->data, ba->data, uint8_t, ba->size);
-       }
-#endif
 
        /* get declaring class */
 
-       c = (classinfo *) m->class;
+       c = m->class;
 
        /* allocate a new object */
 
@@ -133,24 +102,21 @@ java_lang_reflect_Constructor *reflect_constructor_new(methodinfo *m)
 
 #if defined(WITH_CLASSPATH_GNU)
 
-       rc->clazz                = (java_lang_Class *) c;
-       rc->slot                 = slot;
-
-       /* TODO: add these private fields to java.lang.reflect.Constructor
-       rc->annotations          = annotations;
-       rc->parameterAnnotations = parameterAnnotations;
-       */
+       LLNI_field_set_cls(rc, clazz               , c);
+       LLNI_field_set_val(rc, slot                , slot);
+       LLNI_field_set_ref(rc, annotations         , method_get_annotations(m));
+       LLNI_field_set_ref(rc, parameterAnnotations, method_get_parameterannotations(m));
 
 #elif defined(WITH_CLASSPATH_SUN)
 
-       rc->clazz                = (java_lang_Class *) c;
-       rc->parameterTypes       = method_get_parametertypearray(m);
-       rc->exceptionTypes       = method_get_exceptionarray(m);
-       rc->modifiers            = m->flags & ACC_CLASS_REFLECT_MASK;
-       rc->slot                 = slot;
-       rc->signature            = m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL;
-       rc->annotations          = annotations;
-       rc->parameterAnnotations = parameterAnnotations;
+       LLNI_field_set_cls(rc, clazz               , c);
+       LLNI_field_set_ref(rc, parameterTypes      , method_get_parametertypearray(m));
+       LLNI_field_set_ref(rc, exceptionTypes      , method_get_exceptionarray(m));
+       LLNI_field_set_val(rc, modifiers           , m->flags & ACC_CLASS_REFLECT_MASK);
+       LLNI_field_set_val(rc, slot                , slot);
+       LLNI_field_set_ref(rc, signature           , m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
+       LLNI_field_set_ref(rc, annotations         , method_get_annotations(m));
+       LLNI_field_set_ref(rc, parameterAnnotations, method_get_parameterannotations(m));
 
 #else
 # error unknown classpath configuration
@@ -170,29 +136,13 @@ java_lang_reflect_Constructor *reflect_constructor_new(methodinfo *m)
 java_lang_reflect_Field *reflect_field_new(fieldinfo *f)
 {
        classinfo               *c;
-       java_objectheader       *o;
+       java_handle_t           *o;
        java_lang_reflect_Field *rf;
        int32_t                  slot;
-       java_bytearray          *annotations = NULL;
-       annotation_bytearray_t  *ba          = NULL;
-
-#if defined(ENABLE_ANNOTATIONS)
-       /* get annotations */
-       ba = field_get_annotations(f);
-
-       if (ba != NULL) {
-               annotations = builtin_newarray_byte(ba->size);
-
-               if (annotations == NULL)
-                       return NULL;
-               
-               MCOPY(annotations->data, ba->data, uint8_t, ba->size);
-       }
-#endif
 
        /* get declaring class */
 
-       c = (classinfo *) f->class;
+       c = f->class;
 
        /* allocate a new object */
 
@@ -211,28 +161,28 @@ java_lang_reflect_Field *reflect_field_new(fieldinfo *f)
 
 #if defined(WITH_CLASSPATH_GNU)
 
-       rf->clazz = (java_lang_Class *) c;
+       LLNI_field_set_cls(rf, clazz         , c);
 
        /* The name needs to be interned */
        /* XXX implement me better! */
 
-       rf->name           = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(f->name));
-       rf->slot           = slot;
-       rf->annotations    = annotations;
+       LLNI_field_set_ref(rf, name          , javastring_intern((java_lang_String *) javastring_new(f->name)));
+       LLNI_field_set_val(rf, slot          , slot);
+       LLNI_field_set_ref(rf, annotations   , field_get_annotations(f));
 
 #elif defined(WITH_CLASSPATH_SUN)
 
-       rf->clazz          = (java_lang_Class *) c;
+       LLNI_field_set_cls(rf, clazz         , c);
 
        /* The name needs to be interned */
        /* XXX implement me better! */
 
-       rf->name           = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(f->name));
-       rf->type           = (java_lang_Class *) field_get_type(f);
-       rf->modifiers      = f->flags;
-       rf->slot           = slot;
-       rf->signature      = f->signature ? (java_lang_String *) javastring_new(f->signature) : NULL;
-       rf->annotations    = annotations;
+       LLNI_field_set_ref(rf, name          , javastring_intern((java_lang_String *) javastring_new(f->name)));
+       LLNI_field_set_cls(rf, type          , (java_lang_Class *) field_get_type(f));
+       LLNI_field_set_val(rf, modifiers     , f->flags);
+       LLNI_field_set_val(rf, slot          , slot);
+       LLNI_field_set_ref(rf, signature     , f->signature ? (java_lang_String *) javastring_new(f->signature) : NULL);
+       LLNI_field_set_ref(rf, annotations   , field_get_annotations(f));
 
 #else
 # error unknown classpath configuration
@@ -252,55 +202,13 @@ java_lang_reflect_Field *reflect_field_new(fieldinfo *f)
 java_lang_reflect_Method *reflect_method_new(methodinfo *m)
 {
        classinfo                *c;
-       java_objectheader        *o;
+       java_handle_t            *o;
        java_lang_reflect_Method *rm;
        int32_t                   slot;
-       java_bytearray           *annotations          = NULL;
-       java_bytearray           *parameterAnnotations = NULL;
-       java_bytearray           *annotationDefault    = NULL;
-       annotation_bytearray_t   *ba                   = NULL;
-
-#if defined(ENABLE_ANNOTATIONS)
-       /* get annotations */
-       ba = method_get_annotations(m);
-
-       if (ba != NULL) {
-               annotations = builtin_newarray_byte(ba->size);
-
-               if (annotations == NULL)
-                       return NULL;
-               
-               MCOPY(annotations->data, ba->data, uint8_t, ba->size);
-       }
-       
-       /* get parameter annotations */
-       ba = method_get_parameterannotations(m);
-
-       if (ba != NULL) {
-               parameterAnnotations = builtin_newarray_byte(ba->size);
-
-               if (parameterAnnotations == NULL)
-                       return NULL;
-               
-               MCOPY(parameterAnnotations->data, ba->data, uint8_t, ba->size);
-       }
-
-       /* get annotation default value */
-       ba = method_get_annotationdefault(m);
-
-       if (ba != NULL) {
-               annotationDefault = builtin_newarray_byte(ba->size);
-
-               if (annotationDefault == NULL)
-                       return NULL;
-               
-               MCOPY(annotationDefault->data, ba->data, uint8_t, ba->size);
-       }
-#endif
 
        /* get declaring class */
 
-       c = (classinfo *) m->class;
+       c = m->class;
 
        /* allocate a new object */
 
@@ -319,34 +227,34 @@ java_lang_reflect_Method *reflect_method_new(methodinfo *m)
 
 #if defined(WITH_CLASSPATH_GNU)
 
-       rm->clazz                = (java_lang_Class *) m->class;
+       LLNI_field_set_cls(rm, clazz               , m->class);
 
        /* The name needs to be interned */
        /* XXX implement me better! */
 
-       rm->name                 = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(m->name));
-       rm->slot                 = slot;
-       rm->annotations          = annotations;
-       rm->parameterAnnotations = parameterAnnotations;
-       rm->annotationDefault    = annotationDefault;
+       LLNI_field_set_ref(rm, name                , javastring_intern((java_lang_String *) javastring_new(m->name)));
+       LLNI_field_set_val(rm, slot                , slot);
+       LLNI_field_set_ref(rm, annotations         , method_get_annotations(m));
+       LLNI_field_set_ref(rm, parameterAnnotations, method_get_parameterannotations(m));
+       LLNI_field_set_ref(rm, annotationDefault   , method_get_annotationdefault(m));
 
 #elif defined(WITH_CLASSPATH_SUN)
 
-       rm->clazz                = (java_lang_Class *) m->class;
+       LLNI_field_set_cls(rm, clazz               , m->class);
 
        /* The name needs to be interned */
        /* XXX implement me better! */
 
-       rm->name                 = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(m->name));
-       rm->parameterTypes       = method_get_parametertypearray(m);
-       rm->returnType           = (java_lang_Class *) method_returntype_get(m);
-       rm->exceptionTypes       = method_get_exceptionarray(m);
-       rm->modifiers            = m->flags & ACC_CLASS_REFLECT_MASK;
-       rm->slot                 = slot;
-       rm->signature            = m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL;
-       rm->annotations          = annotations;
-       rm->parameterAnnotations = parameterAnnotations;
-       rm->annotationDefault    = annotationDefault;
+       LLNI_field_set_ref(rm, name                , javastring_intern((java_lang_String *) javastring_new(m->name)));
+       LLNI_field_set_ref(rm, parameterTypes      , method_get_parametertypearray(m));
+       LLNI_field_set_cls(rm, returnType          , (java_lang_Class *) method_returntype_get(m));
+       LLNI_field_set_ref(rm, exceptionTypes      , method_get_exceptionarray(m));
+       LLNI_field_set_val(rm, modifiers           , m->flags & ACC_CLASS_REFLECT_MASK);
+       LLNI_field_set_val(rm, slot                , slot);
+       LLNI_field_set_ref(rm, signature           , m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
+       LLNI_field_set_ref(rm, annotations         , method_get_annotations(m));
+       LLNI_field_set_ref(rm, parameterAnnotations, method_get_parameterannotations(m));
+       LLNI_field_set_ref(rm, annotationDefault   , method_get_annotationdefault(m));
 
 #else
 # error unknown classpath configuration
@@ -359,67 +267,61 @@ java_lang_reflect_Method *reflect_method_new(methodinfo *m)
 #if defined(WITH_CLASSPATH_GNU) && defined(ENABLE_ANNOTATIONS)
 /* reflect_get_declaredannotatios *********************************************
 
-   Returnes a java.util.Map<Class, Annotation> of the declared
-   annotations. Only calls the AnnotationParser if the declared
-   annotations are not yet parsed.
+   Returns a java.util.Map<Class<? extends Annotation>, Annotation>
+   of the declared annotations.
 
 *******************************************************************************/
 
 struct java_util_Map* reflect_get_declaredannotatios(
-       struct java_util_Map **declaredAnnotations,
-       java_bytearray        *annotations,
-       java_lang_Class       *declaringClass,
-       classinfo             *referer)
+       java_handle_bytearray_t *annotations,
+       java_lang_Class         *declaringClass,
+       classinfo               *referer)
 {
        static methodinfo        *m_parseAnnotations   = NULL;
        utf                      *utf_parseAnnotations = NULL;
        utf                      *utf_desc             = NULL;
        sun_reflect_ConstantPool *constantPool         = NULL;
+       java_lang_Object         *constantPoolOop      = (java_lang_Object*)declaringClass;
 
-       if (*declaredAnnotations == NULL) {
-               constantPool = 
-                       (sun_reflect_ConstantPool*)native_new_and_init(
-                               class_sun_reflect_ConstantPool);
+       constantPool = 
+               (sun_reflect_ConstantPool*)native_new_and_init(
+                       class_sun_reflect_ConstantPool);
+               
+       if(constantPool == NULL) {
+               /* out of memory */
+               return NULL;
+       }
+               
+       LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
                
-               if(constantPool == NULL) {
+       /* only resolve the method the first time */
+       if (m_parseAnnotations == NULL) {
+               utf_parseAnnotations = utf_new_char("parseAnnotations");
+               utf_desc = utf_new_char(
+                       "([BLsun/reflect/ConstantPool;Ljava/lang/Class;)"
+                       "Ljava/util/Map;");
+
+               if (utf_parseAnnotations == NULL || utf_desc == NULL) {
                        /* out of memory */
                        return NULL;
                }
                
-               constantPool->constantPoolOop = (java_lang_Object*)declaringClass;
-               
-               /* only resolve the method the first time */
-               if (m_parseAnnotations == NULL) {
-                       utf_parseAnnotations = utf_new_char("parseAnnotations");
-                       utf_desc = utf_new_char(
-                               "([BLsun/reflect/ConstantPool;Ljava/lang/Class;)"
-                               "Ljava/util/Map;");
-       
-                       if (utf_parseAnnotations == NULL || utf_desc == NULL) {
-                               /* out of memory */
-                               return NULL;
-                       }
-               
-                       m_parseAnnotations = class_resolveclassmethod(
-                               class_sun_reflect_annotation_AnnotationParser,
-                               utf_parseAnnotations,
-                               utf_desc,
-                               referer,
-                               true);
+               m_parseAnnotations = class_resolveclassmethod(
+                       class_sun_reflect_annotation_AnnotationParser,
+                       utf_parseAnnotations,
+                       utf_desc,
+                       referer,
+                       true);
        
-                       if (m_parseAnnotations == NULL) {
-                               /* method not found */
-                               return NULL;
-                       }
+               if (m_parseAnnotations == NULL) {
+                       /* method not found */
+                       return NULL;
                }
-       
-               *declaredAnnotations =
-                       (struct java_util_Map*)vm_call_method(
-                               m_parseAnnotations, NULL, annotations,
-                               constantPool, declaringClass);
        }
        
-       return *declaredAnnotations;
+       return (struct java_util_Map*)vm_call_method(
+                       m_parseAnnotations, NULL, annotations,
+                       constantPool, declaringClass);
 }
 
 
@@ -429,8 +331,8 @@ struct java_util_Map* reflect_get_declaredannotatios(
 
 *******************************************************************************/
 
-java_objectarray* reflect_get_parameterannotations(
-       java_objectheader *parameterAnnotations,
+java_handle_objectarray_t* reflect_get_parameterannotations(
+       java_handle_t     *parameterAnnotations,
        int32_t            slot,
        java_lang_Class   *declaringClass,
        classinfo         *referer)
@@ -451,15 +353,16 @@ java_objectarray* reflect_get_parameterannotations(
         */
        static methodinfo        *m_parseParameterAnnotations   = NULL;
        utf                      *utf_parseParameterAnnotations = NULL;
-       utf                      *utf_desc      = NULL;
-       sun_reflect_ConstantPool *constantPool  = NULL;
-       classinfo                *c             = NULL;
-       methodinfo               *m             = NULL;
-       int32_t                   numParameters = -1;
+       utf                      *utf_desc        = NULL;
+       sun_reflect_ConstantPool *constantPool    = NULL;
+       java_lang_Object         *constantPoolOop = (java_lang_Object*)declaringClass;
+       classinfo                *c               = NULL;
+       methodinfo               *m               = NULL;
+       int32_t                   numParameters   = -1;
 
        /* get parameter count */
 
-       c = (classinfo *)declaringClass;
+       c = LLNI_classinfo_unwrap(declaringClass);
        m = &(c->methods[slot]);
 
        numParameters = method_get_parametercount(m);
@@ -480,7 +383,7 @@ java_objectarray* reflect_get_parameterannotations(
                return NULL;
        }
 
-       constantPool->constantPoolOop = (java_lang_Object*)declaringClass;
+       LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
 
        /* only resolve the method the first time */
        if (m_parseParameterAnnotations == NULL) {
@@ -510,7 +413,7 @@ java_objectarray* reflect_get_parameterannotations(
                }
        }
 
-       return (java_objectarray*)vm_call_method(
+       return (java_handle_objectarray_t*)vm_call_method(
                m_parseParameterAnnotations, NULL, parameterAnnotations,
                constantPool, declaringClass, numParameters);
 }