* Removed all Id tags.
[cacao.git] / src / vmcore / class.c
index 4dd45889bea03bd073c12588e7f6ff13f501fba4..3b2b0a377de732b4b2825bbc374ad81bef54b511 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: class.c 8309 2007-08-15 16:42:52Z twisti $
-
 */
 
 
 
 #include "mm/memory.h"
 
+#include "native/llni.h"
+
 #include "threads/lock-common.h"
 
 #include "toolbox/logging.h"
 
+#include "vm/array.h"
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
@@ -291,7 +292,7 @@ void class_postset_header_vftbl(void)
 
 *******************************************************************************/
 
-classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data)
+classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd)
 {
        classinfo   *c;
        classinfo   *r;
@@ -357,6 +358,14 @@ classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_
                return NULL;
        }
 
+#if defined(ENABLE_JAVASE)
+# if defined(WITH_CLASSPATH_SUN)
+       /* Store the protection domain. */
+
+       c->protectiondomain = pd;
+# endif
+#endif
+
        /* Store the newly defined class in the class cache. This call
           also checks whether a class of the same name has already been
           defined by the same defining loader, and if so, replaces the
@@ -517,11 +526,16 @@ static bool class_load_attribute_enclosingmethod(classbuffer *cb)
 
 bool class_load_attributes(classbuffer *cb)
 {
-       classinfo *c;
-       u4         i, j;
-       u2         attributes_count;
-       u2         attribute_name_index;
-       utf       *attribute_name;
+       classinfo             *c;
+       uint16_t               attributes_count;
+       uint16_t               attribute_name_index;
+       utf                   *attribute_name;
+       innerclassinfo        *info;
+       classref_or_classinfo  inner;
+       classref_or_classinfo  outer;
+       utf                   *name;
+       uint16_t               flags;
+       int                    i, j;
 
        c = cb->class;
 
@@ -571,19 +585,45 @@ bool class_load_attributes(classbuffer *cb)
                        for (j = 0; j < c->innerclasscount; j++) {
                                /* The innerclass structure contains a class with an encoded
                                   name, its defining scope, its simple name and a bitmask of
-                                  the access flags. If an inner class is not a member, its
-                                  outer_class is NULL, if a class is anonymous, its name is
-                                  NULL. */
+                                  the access flags. */
                                                                
-                               innerclassinfo *info = c->innerclass + j;
-
-                               info->inner_class.ref =
-                                       innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
-                               info->outer_class.ref =
-                                       innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
-                               info->name =
-                                       innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
-                               info->flags = suck_u2(cb);
+                               info = c->innerclass + j;
+
+                               inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
+                               outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
+                               name      = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
+                               flags     = suck_u2(cb);
+
+                               /* If the current inner-class is the currently loaded
+                                  class check for some special flags. */
+
+                               if (inner.ref->name == c->name) {
+                                       /* If an inner-class is not a member, its
+                                          outer-class is NULL. */
+
+                                       if (outer.ref != NULL) {
+                                               c->flags |= ACC_CLASS_MEMBER;
+
+                                               /* A member class doesn't have an
+                                                  EnclosingMethod attribute, so set the
+                                                  enclosing-class to be the same as the
+                                                  declaring-class. */
+
+                                               c->declaringclass = outer;
+                                               c->enclosingclass = outer;
+                                       }
+
+                                       /* If an inner-class is anonymous, its name is
+                                          NULL. */
+
+                                       if (name == NULL)
+                                               c->flags |= ACC_CLASS_ANONYMOUS;
+                               }
+
+                               info->inner_class = inner;
+                               info->outer_class = outer;
+                               info->name        = name;
+                               info->flags       = flags;
                        }
                }
                else if (attribute_name == utf_SourceFile) {
@@ -1499,11 +1539,12 @@ static classinfo *class_resolve_superclass(classinfo *c)
        if (c->super.any == NULL)
                return NULL;
 
-       /* Do we have a super class reference or is it already
-          resolved? */
+       /* Check if the super class is a reference. */
 
        if (IS_CLASSREF(c->super)) {
+               /* XXX I'm very sure this is not correct. */
                super = resolve_classref_or_classinfo_eager(c->super, true);
+/*             super = resolve_classref_or_classinfo_eager(c->super, false); */
 
                if (super == NULL)
                        return NULL;
@@ -1532,7 +1573,13 @@ bool class_issubclass(classinfo *sub, classinfo *super)
                if (sub == super)
                        return true;
 
-               sub = class_resolve_superclass(sub);
+/*             sub = class_resolve_superclass(sub); */
+               if (sub->super.any == NULL)
+                       return false;
+
+               assert(IS_CLASSREF(sub->super) == 0);
+
+               sub = sub->super.cls;
        }
 }
 
@@ -1602,6 +1649,21 @@ bool class_is_primitive(classinfo *c)
 }
 
 
+/* class_is_anonymousclass *****************************************************
+
+   Checks if the given class is an anonymous class.
+
+*******************************************************************************/
+
+bool class_is_anonymousclass(classinfo *c)
+{
+       if (c->flags & ACC_CLASS_ANONYMOUS)
+               return true;
+
+       return false;
+}
+
+
 /* class_is_array **************************************************************
 
    Checks if the given class is an array class.
@@ -1633,6 +1695,36 @@ bool class_is_interface(classinfo *c)
 }
 
 
+/* class_is_localclass *********************************************************
+
+   Checks if the given class is a local class.
+
+*******************************************************************************/
+
+bool class_is_localclass(classinfo *c)
+{
+       if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
+               return true;
+
+       return false;
+}
+
+
+/* class_is_memberclass ********************************************************
+
+   Checks if the given class is a member class.
+
+*******************************************************************************/
+
+bool class_is_memberclass(classinfo *c)
+{
+       if (c->flags & ACC_CLASS_MEMBER)
+               return true;
+
+       return false;
+}
+
+
 /* class_get_superclass ********************************************************
 
    Return the super class of the given class.  If the super-field is a
@@ -1663,20 +1755,53 @@ classinfo *class_get_superclass(classinfo *c)
 }
 
 
+/* class_get_componenttype *****************************************************
+
+   Return the component class of the given class.  If the given class
+   is not an array, return NULL.
+
+*******************************************************************************/
+
+classinfo *class_get_componenttype(classinfo *c)
+{
+       classinfo       *component;
+       arraydescriptor *ad;
+       
+       /* XXX maybe we could find a way to do this without linking. */
+       /* This way should be safe and easy, however.                */
+
+       if (!(c->state & CLASS_LINKED))
+               if (!link_class(c))
+                       return NULL;
+
+       ad = c->vftbl->arraydesc;
+       
+       if (ad == NULL)
+               return NULL;
+       
+       if (ad->arraytype == ARRAYTYPE_OBJECT)
+               component = ad->componentvftbl->class;
+       else
+               component = primitive_class_get_by_type(ad->arraytype);
+               
+       return component;
+}
+
+
 /* class_get_declaredclasses ***************************************************
 
    Return an array of declared classes of the given class.
 
 *******************************************************************************/
 
-java_objectarray *class_get_declaredclasses(classinfo *c, bool publicOnly)
+java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
 {
        classref_or_classinfo  inner;
        classref_or_classinfo  outer;
        utf                   *outername;
        int                    declaredclasscount;  /* number of declared classes */
        int                    pos;                     /* current declared class */
-       java_objectarray      *oa;                   /* array of declared classes */
+       java_handle_objectarray_t *oa;               /* array of declared classes */
        int                    i;
        classinfo             *ic;
 
@@ -1686,9 +1811,15 @@ java_objectarray *class_get_declaredclasses(classinfo *c, bool publicOnly)
                /* Determine number of declared classes. */
 
                for (i = 0; i < c->innerclasscount; i++) {
+                       /* Get outer-class.  If the inner-class is not a member
+                          class, the outer-class is NULL. */
+
                        outer = c->innerclass[i].outer_class;
 
-                       /* Check if outer_class is a classref or a real class and
+                       if (outer.any == NULL)
+                               continue;
+
+                       /* Check if outer-class is a classref or a real class and
                get the class name from the structure. */
 
                        outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
@@ -1712,6 +1843,12 @@ java_objectarray *class_get_declaredclasses(classinfo *c, bool publicOnly)
                inner = c->innerclass[i].inner_class;
                outer = c->innerclass[i].outer_class;
 
+               /* Get outer-class.  If the inner-class is not a member class,
+                  the outer-class is NULL. */
+
+               if (outer.any == NULL)
+                       continue;
+
                /* Check if outer_class is a classref or a real class and get
                   the class name from the structure. */
 
@@ -1731,7 +1868,7 @@ java_objectarray *class_get_declaredclasses(classinfo *c, bool publicOnly)
                                if (!link_class(ic))
                                        return NULL;
 
-                       oa->data[pos++] = (java_object_t *) ic;
+                       LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
                }
        }
 
@@ -1749,52 +1886,71 @@ java_objectarray *class_get_declaredclasses(classinfo *c, bool publicOnly)
 
 classinfo *class_get_declaringclass(classinfo *c)
 {
-       classref_or_classinfo  innercr;
-       utf                   *innername;
-       classref_or_classinfo  outercr;
-       classinfo             *outer;
-       int16_t                i;
+       classref_or_classinfo  cr;
+       classinfo             *dc;
 
-       /* return NULL for arrayclasses and primitive classes */
+       /* Get declaring class. */
 
-       if (class_is_primitive(c) || (c->name->text[0] == '['))
+       cr = c->declaringclass;
+
+       if (cr.any == NULL)
                return NULL;
 
-       /* no innerclasses exist */
+       /* Resolve the class if necessary. */
 
-       if (c->innerclasscount == 0)
-               return NULL;
+       if (IS_CLASSREF(cr)) {
+/*             dc = resolve_classref_eager(cr.ref); */
+               dc = resolve_classref_or_classinfo_eager(cr, true);
 
-       for (i = 0; i < c->innerclasscount; i++) {
-               /* Check if inner_class is a classref or a real class and get
-                  the class name from the structure. */
+               if (dc == NULL)
+                       return NULL;
 
-               innercr = c->innerclass[i].inner_class;
+               /* Store the resolved class in the class structure. */
 
-               innername = IS_CLASSREF(innercr) ?
-                       innercr.ref->name : innercr.cls->name;
+               cr.cls = dc;
+       }
 
-               /* Is the current innerclass this class? */
+       dc = cr.cls;
 
-               if (innername == c->name) {
-                       /* Maybe the outer class is not loaded yet. */
+       return dc;
+}
 
-                       outercr = c->innerclass[i].outer_class;
 
-                       outer = resolve_classref_or_classinfo_eager(outercr, false);
+/* class_get_enclosingclass ****************************************************
 
-                       if (outer == NULL)
-                               return NULL;
+   Return the enclosing class for the given class.
 
-                       if (!(outer->state & CLASS_LINKED))
-                               if (!link_class(outer))
-                                       return NULL;
+*******************************************************************************/
 
-                       return outer;
-               }
+classinfo *class_get_enclosingclass(classinfo *c)
+{
+       classref_or_classinfo  cr;
+       classinfo             *ec;
+
+       /* Get enclosing class. */
+
+       cr = c->enclosingclass;
+
+       if (cr.any == NULL)
+               return NULL;
+
+       /* Resolve the class if necessary. */
+
+       if (IS_CLASSREF(cr)) {
+/*             ec = resolve_classref_eager(cr.ref); */
+               ec = resolve_classref_or_classinfo_eager(cr, true);
+
+               if (ec == NULL)
+                       return NULL;
+
+               /* Store the resolved class in the class structure. */
+
+               cr.cls = ec;
        }
 
-       return NULL;
+       ec = cr.cls;
+
+       return ec;
 }
 
 
@@ -1804,11 +1960,11 @@ classinfo *class_get_declaringclass(classinfo *c)
 
 *******************************************************************************/
 
-java_objectarray *class_get_interfaces(classinfo *c)
+java_handle_objectarray_t *class_get_interfaces(classinfo *c)
 {
-       classinfo        *ic;
-       java_objectarray *oa;
-       u4                i;
+       classinfo                 *ic;
+       java_handle_objectarray_t *oa;
+       u4                         i;
 
        if (!(c->state & CLASS_LINKED))
                if (!link_class(c))
@@ -1822,13 +1978,48 @@ java_objectarray *class_get_interfaces(classinfo *c)
        for (i = 0; i < c->interfacescount; i++) {
                ic = c->interfaces[i].cls;
 
-               oa->data[i] = (java_object_t *) ic;
+               LLNI_array_direct(oa, i) = (java_object_t *) ic;
        }
 
        return oa;
 }
 
 
+/* class_get_annotations *******************************************************
+
+   Return the unparsed declared annotations in an byte array
+   of the given class (or NULL if there aren't any).
+
+*******************************************************************************/
+
+java_handle_bytearray_t *class_get_annotations(classinfo *c)
+{
+#if defined(ENABLE_ANNOTATIONS)
+       java_handle_bytearray_t  *annotations = NULL;
+       uint32_t                  size        = 0;
+       
+       /* Return null for arrays and primitives: */
+       if (class_is_primitive(c) || class_is_array(c)) {
+               return NULL;
+       }
+
+       /* copy the annotations into a java byte array: */
+       if (c->annotations != NULL) {
+               size        = c->annotations->size;
+               annotations = builtin_newarray_byte(size);
+
+               if(annotations != NULL) {
+                       MCOPY(annotations->data, c->annotations->data, uint8_t, size);
+               }
+       }
+
+       return annotations;
+#else
+       return NULL;
+#endif
+}
+
+
 /* class_get_signature *********************************************************
 
    Return the signature of the given class.  For array and primitive