* src/vmcore/annotation.c: Fixed comment.
[cacao.git] / src / vmcore / annotation.c
index 1807653fa8ada71b2d8865a73b51631318f87782..ad5993cc50b8e3f5ef4bf3efe2e63d025cd78100 100644 (file)
 
 */
 
 
 */
 
+
 #include "config.h"
 
 #include <assert.h>
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 
 #include "native/llni.h"
 
 
 #include "native/llni.h"
 
@@ -41,6 +43,7 @@
 
 #include "vmcore/annotation.h"
 #include "vmcore/class.h"
 
 #include "vmcore/annotation.h"
 #include "vmcore/class.h"
+#include "vmcore/loader.h"
 #include "vmcore/suck.h"
 
 #if !defined(ENABLE_ANNOTATIONS)
 #include "vmcore/suck.h"
 
 #if !defined(ENABLE_ANNOTATIONS)
 
    Resize an array of bytearrays.
 
 
    Resize an array of bytearrays.
 
+   IN:
+       bytearrays.....array of bytearrays
+       size...........new size of the refered array
+   
+   RETURN VALUE:
+       The new array if a resize was neccessarry, the old if the given size
+       equals the current size or NULL if an error occured.
+
 *******************************************************************************/
 
 *******************************************************************************/
 
-static bool annotation_bytearrays_resize(java_handle_objectarray_t **bas,
-       uint32_t size)
+static java_handle_objectarray_t *annotation_bytearrays_resize(
+       java_handle_objectarray_t *bytearrays, uint32_t size)
 {
 {
-       java_handle_objectarray_t *newbas = NULL;
-       java_handle_t             *o;
-       uint32_t i;
-       uint32_t minsize;
-       uint32_t oldsize;
-       
-       assert(bas != NULL);
-       
-       /* if the size already fits do nothing */
-       if (*bas != NULL) {
-               oldsize = array_length_get((java_handle_t*)*bas);
-
+       java_handle_objectarray_t *newbas = NULL; /* new array     */
+       uint32_t minsize = 0;      /* count of object refs to copy */
+       uint32_t oldsize = 0;      /* size of old array            */
+
+       if (bytearrays != NULL) {
+               oldsize = array_length_get((java_handle_t*)bytearrays);
+               
+               /* if the size already fits do nothing */
                if (size == oldsize) {
                if (size == oldsize) {
-                       return true;
+                       return bytearrays;
                }
        }
                }
        }
-
+       
        newbas = builtin_anewarray(size,
                primitive_arrayclass_get_by_type(PRIMITIVETYPE_BYTE));
        
        newbas = builtin_anewarray(size,
                primitive_arrayclass_get_by_type(PRIMITIVETYPE_BYTE));
        
-       if (newbas == NULL) {
-               /* out of memory */
-               return false;
-       }
-       
        /* is there a old byte array array? */
        /* is there a old byte array array? */
-       if (*bas != NULL) {
+       if (newbas != NULL && bytearrays != NULL) {
                minsize = size < oldsize ? size : oldsize;
 
                minsize = size < oldsize ? size : oldsize;
 
-               MCOPY(LLNI_array_data(newbas), LLNI_array_data(*bas), java_object_t*, minsize);
+               LLNI_CRITICAL_START;
+               MCOPY(
+                       LLNI_array_data(newbas), LLNI_array_data(bytearrays),
+                       java_object_t*, minsize);
+               LLNI_CRITICAL_END;
        }
 
        }
 
-       *bas = newbas;
-
-       return true;
+       return newbas;
 }
 
 
 }
 
 
@@ -99,44 +103,60 @@ static bool annotation_bytearrays_resize(java_handle_objectarray_t **bas,
 
    Insert a bytearray into an array of bytearrays.
 
 
    Insert a bytearray into an array of bytearrays.
 
+   IN:
+       bytearrays........array of bytearrays where 'bytearray' has to be
+                         inserted at position 'index'.
+       index.............position where 'ba' has to be inserted into
+                         'bytearrays'.
+       bytearray.........byte array which has to be inserted into
+                         'bytearrays'.
+
+   RETURN VALUE:
+       The new array if a resize was neccessarry, the old if the given size
+       equals the current size or NULL if an error occured.
+
 *******************************************************************************/
 
 *******************************************************************************/
 
-static bool annotation_bytearrays_insert(java_handle_objectarray_t **bas,
-       uint32_t index, java_handle_bytearray_t *ba)
+static java_handle_t *annotation_bytearrays_insert(
+       java_handle_t *bytearrays, uint32_t index,
+       java_handle_bytearray_t *bytearray)
 {
 {
-       uint32_t size = 0;
-
-       assert(bas != NULL);
+       java_handle_objectarray_t *bas; /* bytearrays                */
+       uint32_t size = 0;              /* current size of the array */
 
        /* do nothing if NULL is inserted but no array exists */
 
        /* do nothing if NULL is inserted but no array exists */
-       if (ba == NULL && *bas == NULL) {
-               return true;
+       if (bytearray == NULL && bytearrays == NULL) {
+               return NULL;
        }
 
        /* get lengths if array exists */
        }
 
        /* get lengths if array exists */
-       if (*bas != NULL) {
-               size = array_length_get((java_handle_t*)*bas);
+       if (bytearrays != NULL) {
+               size = array_length_get(bytearrays);
        }
 
        }
 
-       if (ba == NULL) {
+       bas = (java_handle_objectarray_t*)bytearrays;
+
+       if (bytearray == NULL) {
                /* insert NULL only if array is big enough */
                if (size > index) {
                /* insert NULL only if array is big enough */
                if (size > index) {
-                       array_objectarray_element_set(*bas, index, NULL);
+                       array_objectarray_element_set(bas, index, NULL);
                }
        }
        else {
                /* resize array if it's not enough for inserted value */
                if (size <= index) {
                }
        }
        else {
                /* resize array if it's not enough for inserted value */
                if (size <= index) {
-                       if (!annotation_bytearrays_resize(bas, index + 1)) {
+                       bas = annotation_bytearrays_resize(bas, index + 1);
+
+                       if (bas == NULL) {
                                /* out of memory */
                                /* out of memory */
-                               return false;
+                               return NULL;
                        }
                }
 
                        }
                }
 
-               array_objectarray_element_set(*bas, index, (java_handle_t*)ba);
+               array_objectarray_element_set(bas, index, (java_handle_t*)bytearray);
        }
        
        }
        
-       return true;
+       return (java_handle_t*)bas;
 }
 
 
 }
 
 
@@ -168,8 +188,8 @@ static bool annotation_bytearrays_insert(java_handle_objectarray_t **bas,
 static bool annotation_load_attribute_body(classbuffer *cb,
        java_handle_bytearray_t **attribute, const char *errormsg_prefix)
 {
 static bool annotation_load_attribute_body(classbuffer *cb,
        java_handle_bytearray_t **attribute, const char *errormsg_prefix)
 {
-       uint32_t size = 0;
-       java_handle_bytearray_t *ba = NULL;
+       uint32_t                 size = 0;    /* size of the attribute     */
+       java_handle_bytearray_t *ba   = NULL; /* the raw attributes' bytes */
 
        assert(cb != NULL);
        assert(attribute != NULL);
 
        assert(cb != NULL);
        assert(attribute != NULL);
@@ -198,8 +218,12 @@ static bool annotation_load_attribute_body(classbuffer *cb,
                }
 
                /* load data */
                }
 
                /* load data */
+               LLNI_CRITICAL_START;
+
                suck_nbytes((uint8_t*)LLNI_array_data(ba), cb, size);
 
                suck_nbytes((uint8_t*)LLNI_array_data(ba), cb, size);
 
+               LLNI_CRITICAL_END;
+
                /* return data */
                *attribute = ba;
        }
                /* return data */
                *attribute = ba;
        }
@@ -232,14 +256,18 @@ static bool annotation_load_attribute_body(classbuffer *cb,
 bool annotation_load_method_attribute_annotationdefault(
                classbuffer *cb, methodinfo *m)
 {
 bool annotation_load_method_attribute_annotationdefault(
                classbuffer *cb, methodinfo *m)
 {
-       int slot = 0;
-       java_handle_bytearray_t    *annotationdefault  = NULL;
-       java_handle_objectarray_t **annotationdefaults = NULL;
+       int                      slot               = 0;
+                                /* the slot of the method                        */
+       java_handle_bytearray_t *annotationdefault  = NULL;
+                                /* unparsed annotation defalut value             */
+       java_handle_t           *annotationdefaults = NULL;
+                                /* array of unparsed annotation default values   */
 
        assert(cb != NULL);
        assert(m != NULL);
 
 
        assert(cb != NULL);
        assert(m != NULL);
 
-       annotationdefaults = &(m->class->method_annotationdefaults);
+       LLNI_classinfo_field_get(
+               m->class, method_annotationdefaults, annotationdefaults);
 
        if (!annotation_load_attribute_body(
                        cb, &annotationdefault,
 
        if (!annotation_load_attribute_body(
                        cb, &annotationdefault,
@@ -249,11 +277,15 @@ bool annotation_load_method_attribute_annotationdefault(
 
        if (annotationdefault != NULL) {
                slot = m - m->class->methods;
 
        if (annotationdefault != NULL) {
                slot = m - m->class->methods;
+               annotationdefaults = annotation_bytearrays_insert(
+                               annotationdefaults, slot, annotationdefault);
 
 
-               if (!annotation_bytearrays_insert(
-                               annotationdefaults, slot, annotationdefault)) {
+               if (annotationdefaults == NULL) {
                        return false;
                }
                        return false;
                }
+
+               LLNI_classinfo_field_set(
+                       m->class, method_annotationdefaults, annotationdefaults);
        }
 
        return true;
        }
 
        return true;
@@ -288,14 +320,18 @@ bool annotation_load_method_attribute_annotationdefault(
 bool annotation_load_method_attribute_runtimevisibleparameterannotations(
                classbuffer *cb, methodinfo *m)
 {
 bool annotation_load_method_attribute_runtimevisibleparameterannotations(
                classbuffer *cb, methodinfo *m)
 {
-       int slot = 0;
-       java_handle_bytearray_t    *annotations          = NULL;
-       java_handle_objectarray_t **parameterannotations = NULL;
+       int                      slot                 = 0;
+                                /* the slot of the method                  */
+       java_handle_bytearray_t *annotations          = NULL;
+                                /* unparsed parameter annotations          */
+       java_handle_t           *parameterannotations = NULL;
+                                /* array of unparsed parameter annotations */
 
        assert(cb != NULL);
        assert(m != NULL);
 
 
        assert(cb != NULL);
        assert(m != NULL);
 
-       parameterannotations = &(m->class->method_parameterannotations);
+       LLNI_classinfo_field_get(
+               m->class, method_parameterannotations, parameterannotations);
 
        if (!annotation_load_attribute_body(
                        cb, &annotations,
 
        if (!annotation_load_attribute_body(
                        cb, &annotations,
@@ -305,11 +341,15 @@ bool annotation_load_method_attribute_runtimevisibleparameterannotations(
 
        if (annotations != NULL) {
                slot = m - m->class->methods;
 
        if (annotations != NULL) {
                slot = m - m->class->methods;
+               parameterannotations = annotation_bytearrays_insert(
+                               parameterannotations, slot, annotations);
 
 
-               if (!annotation_bytearrays_insert(
-                               parameterannotations, slot, annotations)) {
+               if (parameterannotations == NULL) {
                        return false;
                }
                        return false;
                }
+
+               LLNI_classinfo_field_set(
+                       m->class, method_parameterannotations, parameterannotations);
        }
 
        return true;
        }
 
        return true;
@@ -332,7 +372,7 @@ bool annotation_load_method_attribute_runtimevisibleparameterannotations(
 
    Hotspot loads them into the same bytearray as the runtime visible parameter
    annotations (after the runtime visible parameter annotations). But in J2SE
 
    Hotspot loads them into the same bytearray as the runtime visible parameter
    annotations (after the runtime visible parameter annotations). But in J2SE
-   the bytearray will only be parsed as if ther is only one annotation
+   the bytearray will only be parsed as if there is only one annotation
    structure in it, so the runtime invisible parameter annotatios will be
    ignored.
 
    structure in it, so the runtime invisible parameter annotatios will be
    ignored.
 
@@ -370,14 +410,29 @@ bool annotation_load_method_attribute_runtimeinvisibleparameterannotations(
    
    Load runtime visible annotations of a class.
    
    
    Load runtime visible annotations of a class.
    
+   IN:
+       cb........the classbuffer from which the attribute has to be loaded.
+
+   RETURN VALUE:
+       true if all went good. false otherwhise.
+
 *******************************************************************************/
 
 bool annotation_load_class_attribute_runtimevisibleannotations(
        classbuffer *cb)
 {
 *******************************************************************************/
 
 bool annotation_load_class_attribute_runtimevisibleannotations(
        classbuffer *cb)
 {
-       return annotation_load_attribute_body(
-               cb, &(cb->class->annotations),
-               "invalid runtime visible annotations class attribute");
+       java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
+       
+       if (!annotation_load_attribute_body(
+                       cb, &annotations,
+                       "invalid runtime visible annotations class attribute")) {
+               return false;
+       }
+
+       LLNI_classinfo_field_set(
+               cb->class, annotations, (java_handle_t*)annotations);
+
+       return true;
 }
 
 
 }
 
 
@@ -385,6 +440,12 @@ bool annotation_load_class_attribute_runtimevisibleannotations(
    
    Load runtime invisible annotations of a class (just skip them).
    
    
    Load runtime invisible annotations of a class (just skip them).
    
+   IN:
+       cb........the classbuffer from which the attribute has to be loaded.
+
+   RETURN VALUE:
+       true if all went good. false otherwhise.
+
 *******************************************************************************/
 
 bool annotation_load_class_attribute_runtimeinvisibleannotations(
 *******************************************************************************/
 
 bool annotation_load_class_attribute_runtimeinvisibleannotations(
@@ -397,20 +458,32 @@ bool annotation_load_class_attribute_runtimeinvisibleannotations(
 /* annotation_load_method_attribute_runtimevisibleannotations *****************
    
    Load runtime visible annotations of a method.
 /* annotation_load_method_attribute_runtimevisibleannotations *****************
    
    Load runtime visible annotations of a method.
-   
+  
+   IN:
+       cb........the classbuffer from which the attribute has to be loaded.
+       m.........the method of which the runtime visible annotations have
+                 to be loaded.
+
+   RETURN VALUE:
+       true if all went good. false otherwhise.
+
 *******************************************************************************/
 
 bool annotation_load_method_attribute_runtimevisibleannotations(
        classbuffer *cb, methodinfo *m)
 {
 *******************************************************************************/
 
 bool annotation_load_method_attribute_runtimevisibleannotations(
        classbuffer *cb, methodinfo *m)
 {
-       int slot = 0;
-       java_handle_bytearray_t    *annotations        = NULL;
-       java_handle_objectarray_t **method_annotations = NULL;
+       int                      slot               = 0;
+                                /* slot of the method */
+       java_handle_bytearray_t *annotations        = NULL;
+                                /* unparsed annotations */
+       java_handle_t           *method_annotations = NULL;
+                                /* array of unparsed method annotations */
 
        assert(cb != NULL);
        assert(m != NULL);
 
 
        assert(cb != NULL);
        assert(m != NULL);
 
-       method_annotations = &(m->class->method_annotations);
+       LLNI_classinfo_field_get(
+               m->class, method_annotations, method_annotations);
 
        if (!annotation_load_attribute_body(
                        cb, &annotations,
 
        if (!annotation_load_attribute_body(
                        cb, &annotations,
@@ -420,11 +493,15 @@ bool annotation_load_method_attribute_runtimevisibleannotations(
 
        if (annotations != NULL) {
                slot = m - m->class->methods;
 
        if (annotations != NULL) {
                slot = m - m->class->methods;
+               method_annotations = annotation_bytearrays_insert(
+                               method_annotations, slot, annotations);
 
 
-               if (!annotation_bytearrays_insert(
-                               method_annotations, slot, annotations)) {
+               if (method_annotations == NULL) {
                        return false;
                }
                        return false;
                }
+               
+               LLNI_classinfo_field_set(
+                       m->class, method_annotations, method_annotations);
        }
 
        return true;
        }
 
        return true;
@@ -435,6 +512,14 @@ bool annotation_load_method_attribute_runtimevisibleannotations(
    
    Load runtime invisible annotations of a method (just skip them).
    
    
    Load runtime invisible annotations of a method (just skip them).
    
+   IN:
+       cb........the classbuffer from which the attribute has to be loaded.
+       m.........the method of which the runtime invisible annotations have
+                 to be loaded.
+
+   RETURN VALUE:
+       true if all went good. false otherwhise.
+
 *******************************************************************************/
 
 bool annotation_load_method_attribute_runtimeinvisibleannotations(
 *******************************************************************************/
 
 bool annotation_load_method_attribute_runtimeinvisibleannotations(
@@ -448,19 +533,31 @@ bool annotation_load_method_attribute_runtimeinvisibleannotations(
    
    Load runtime visible annotations of a field.
    
    
    Load runtime visible annotations of a field.
    
+   IN:
+       cb........the classbuffer from which the attribute has to be loaded.
+       f.........the field of which the runtime visible annotations have
+                 to be loaded.
+
+   RETURN VALUE:
+       true if all went good. false otherwhise.
+
 *******************************************************************************/
 
 bool annotation_load_field_attribute_runtimevisibleannotations(
        classbuffer *cb, fieldinfo *f)
 {
 *******************************************************************************/
 
 bool annotation_load_field_attribute_runtimevisibleannotations(
        classbuffer *cb, fieldinfo *f)
 {
-       int slot = 0;
-       java_handle_bytearray_t    *annotations       = NULL;
-       java_handle_objectarray_t **field_annotations = NULL;
+       int                      slot              = 0;
+                                /* slot of the field                   */
+       java_handle_bytearray_t *annotations       = NULL;
+                                /* unparsed annotations                */
+       java_handle_t           *field_annotations = NULL;
+                                /* array of unparsed field annotations */
 
        assert(cb != NULL);
        assert(f != NULL);
 
 
        assert(cb != NULL);
        assert(f != NULL);
 
-       field_annotations = &(f->class->field_annotations);
+       LLNI_classinfo_field_get(
+               f->class, field_annotations, field_annotations);
 
        if (!annotation_load_attribute_body(
                        cb, &annotations,
 
        if (!annotation_load_attribute_body(
                        cb, &annotations,
@@ -470,11 +567,15 @@ bool annotation_load_field_attribute_runtimevisibleannotations(
 
        if (annotations != NULL) {
                slot = f - f->class->fields;
 
        if (annotations != NULL) {
                slot = f - f->class->fields;
+               field_annotations = annotation_bytearrays_insert(
+                               field_annotations, slot, annotations);
 
 
-               if (!annotation_bytearrays_insert(
-                               field_annotations, slot, annotations)) {
+               if (field_annotations == NULL) {
                        return false;
                }
                        return false;
                }
+
+               LLNI_classinfo_field_set(
+                       f->class, field_annotations, field_annotations);
        }
 
        return true;
        }
 
        return true;
@@ -485,6 +586,14 @@ bool annotation_load_field_attribute_runtimevisibleannotations(
    
    Load runtime invisible annotations of a field (just skip them).
    
    
    Load runtime invisible annotations of a field (just skip them).
    
+   IN:
+       cb........the classbuffer from which the attribute has to be loaded.
+       f.........the field of which the runtime invisible annotations have
+                 to be loaded.
+
+   RETURN VALUE:
+       true if all went good. false otherwhise.
+
 *******************************************************************************/
 
 bool annotation_load_field_attribute_runtimeinvisibleannotations(
 *******************************************************************************/
 
 bool annotation_load_field_attribute_runtimeinvisibleannotations(