This commit introduces C++ wrapper classes for Java heap objects.
[cacao.git] / src / vmcore / class.c
index 53aa727027537d0fb28dbeda92e74edfd3b63abd..bdbdb21a75f8ab275cdbcfac09f0ae373e91a763 100644 (file)
@@ -44,7 +44,7 @@
 
 #include "vm/array.h"
 #include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/exceptions.hpp"
 #include "vm/global.h"
 #include "vm/resolve.h"
 
@@ -52,6 +52,8 @@
 
 #include "vmcore/class.h"
 #include "vmcore/classcache.h"
+#include "vmcore/globals.hpp"
+#include "vmcore/javaobjects.hpp"
 #include "vmcore/linker.h"
 #include "vmcore/loader.h"
 #include "vmcore/options.h"
 #include "vmcore/utf8.h"
 
 
-#if defined(ENABLE_JAVASE)
-/* We need to define some reflection functions here since we cannot
-   include native/vm/reflect.h as it includes generated header
-   files. */
-
-java_object_t *reflect_constructor_new(methodinfo *m);
-java_object_t *reflect_field_new(fieldinfo *f);
-java_object_t *reflect_method_new(methodinfo *m);
-#endif
-
-
-/* global variables ***********************************************************/
-
-/* frequently used classes ****************************************************/
-
-/* Important system classes. */
-
-classinfo *class_java_lang_Object;
-classinfo *class_java_lang_Class;
-classinfo *class_java_lang_ClassLoader;
-classinfo *class_java_lang_Cloneable;
-classinfo *class_java_lang_SecurityManager;
-classinfo *class_java_lang_String;
-classinfo *class_java_lang_System;
-classinfo *class_java_lang_Thread;
-classinfo *class_java_lang_ThreadGroup;
-classinfo *class_java_lang_Throwable;
-classinfo *class_java_io_Serializable;
-
-#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-classinfo *class_java_lang_VMSystem;
-classinfo *class_java_lang_VMThread;
-classinfo *class_java_lang_VMThrowable;
-#endif
-
-/* Important system exceptions. */
-
-classinfo *class_java_lang_Exception;
-classinfo *class_java_lang_ClassNotFoundException;
-classinfo *class_java_lang_RuntimeException;
-
-#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
-classinfo *class_sun_reflect_MagicAccessorImpl;
-#endif
-
-#if defined(ENABLE_JAVASE)
-classinfo *class_java_lang_Void;
-#endif
-classinfo *class_java_lang_Boolean;
-classinfo *class_java_lang_Byte;
-classinfo *class_java_lang_Character;
-classinfo *class_java_lang_Short;
-classinfo *class_java_lang_Integer;
-classinfo *class_java_lang_Long;
-classinfo *class_java_lang_Float;
-classinfo *class_java_lang_Double;
-
-/* some classes which may be used more often */
-
-#if defined(ENABLE_JAVASE)
-classinfo *class_java_lang_StackTraceElement;
-classinfo *class_java_lang_reflect_Constructor;
-classinfo *class_java_lang_reflect_Field;
-classinfo *class_java_lang_reflect_Method;
-classinfo *class_java_security_PrivilegedAction;
-classinfo *class_java_util_Vector;
-classinfo *class_java_util_HashMap;
-
-# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-classinfo *class_java_lang_reflect_VMConstructor;
-classinfo *class_java_lang_reflect_VMField;
-classinfo *class_java_lang_reflect_VMMethod;
-# endif
-
-classinfo *arrayclass_java_lang_Object;
-
-# if defined(ENABLE_ANNOTATIONS)
-classinfo *class_sun_reflect_ConstantPool;
-#  if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-classinfo *class_sun_reflect_annotation_AnnotationParser;
-#  endif
-# endif
-#endif
-
-/* pseudo classes for the typechecker */
-
-classinfo *pseudo_class_Arraystub;
-classinfo *pseudo_class_Null;
-classinfo *pseudo_class_New;
-
-
 /* class_set_packagename *******************************************************
 
    Derive the package name from the class name and store it in the
@@ -703,7 +614,7 @@ static void class_freecpool(classinfo *c)
 {
        u4 idx;
        u4 tag;
-       voidptr info;
+       void* info;
        
        if (c->cptags && c->cpinfos) {
                for (idx = 0; idx < c->cpcount; idx++) {
@@ -741,7 +652,7 @@ static void class_freecpool(classinfo *c)
                MFREE(c->cptags, u1, c->cpcount);
 
        if (c->cpinfos)
-               MFREE(c->cpinfos, voidptr, c->cpcount);
+               MFREE(c->cpinfos, void*, c->cpcount);
 }
 
 
@@ -753,7 +664,7 @@ static void class_freecpool(classinfo *c)
 
 *******************************************************************************/
 
-voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
+void* class_getconstant(classinfo *c, u4 pos, u4 ctype)
 {
        /* check index and type of constantpool entry */
        /* (pos == 0 is caught by type comparison) */
@@ -773,7 +684,7 @@ voidptr class_getconstant(classinfo *c, u4 pos, u4 ctype)
        
 *******************************************************************************/
 
-voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
+void* innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
 {
        /* invalid position in constantpool */
 
@@ -1415,41 +1326,18 @@ fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
 
 *******************************************************************************/
  
-fieldinfo *class_findfield_by_name(classinfo *c, utf *name)
-{
-       s4 i;
-
-       /* get field index */
-
-       i = class_findfield_index_by_name(c, name);
-
-       /* field was not found, return */
-
-       if (i == -1)
-               return NULL;
-
-       /* return field address */
-
-       return &(c->fields[i]);
-}
-
-
-s4 class_findfield_index_by_name(classinfo *c, utf *name)
+fieldinfo *class_findfield_by_name(classinfo* c, utf* name)
 {
-       s4 i;
+       for (int32_t i = 0; i < c->fieldscount; i++) {
+               fieldinfo* f = &(c->fields[i]);
 
-       for (i = 0; i < c->fieldscount; i++) {
-               /* compare field names */
-
-               if ((c->fields[i].name == name))
-                       return i;
+               if (f->name == name)
+                       return f;
        }
 
-       /* field was not found, raise exception */      
-
+       // Field not found.
        exceptions_throw_nosuchfielderror(c, name);
-
-       return -1;
+       return NULL;
 }
 
 
@@ -1499,22 +1387,19 @@ static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
        
        Resolves a reference from REFERER to a field with NAME and DESC in class C.
 
-    If the field cannot be resolved the return value is NULL. If EXCEPT is
-    true *exceptionptr is set, too.
+    If the field cannot be resolved, an exception is thrown and the
+    return value is NULL.
 
 *******************************************************************************/
 
-fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc,
-                                                         classinfo *referer, bool throwexception)
+fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer)
 {
        fieldinfo *fi;
 
        fi = class_resolvefield_int(c, name, desc);
 
        if (!fi) {
-               if (throwexception)
-                       exceptions_throw_nosuchfielderror(c, name);
-
+               exceptions_throw_nosuchfielderror(c, name);
                return NULL;
        }
 
@@ -1821,12 +1706,9 @@ java_handle_objectarray_t *class_get_declaredconstructors(classinfo *c, bool pub
 
                if (((m->flags & ACC_PUBLIC) || (publicOnly == 0)) &&
                        (m->name == utf_init)) {
-                       /* Create Constructor object.  This is actualy a
-                          java_lang_reflect_Constructor pointer, but we use a
-                          java_handle_t here, because we don't have the header
-                          available when building vmcore. */
+                       // Create a java.lang.reflect.Constructor object.
 
-                       rc = reflect_constructor_new(m);
+                       rc = java_lang_reflect_Constructor_create(m);
 
                        /* Store object into array. */
 
@@ -1884,12 +1766,9 @@ java_handle_objectarray_t *class_get_declaredfields(classinfo *c, bool publicOnl
                f = &(c->fields[i]);
 
                if ((f->flags & ACC_PUBLIC) || (publicOnly == 0)) {
-                       /* Create Field object.  This is actualy a
-                          java_lang_reflect_Field pointer, but we use a
-                          java_handle_t here, because we don't have the header
-                          available when building vmcore. */
+                       // Create a java.lang.reflect.Field object.
 
-                       h = reflect_field_new(f);
+                       h = java_lang_reflect_Field_create(f);
 
                        /* Store object into array. */
 
@@ -1962,12 +1841,9 @@ java_handle_objectarray_t *class_get_declaredmethods(classinfo *c, bool publicOn
                if (((m->flags & ACC_PUBLIC) || (publicOnly == false)) && 
                        ((m->name != utf_init) && (m->name != utf_clinit)) &&
                        !(m->flags & ACC_MIRANDA)) {
-                       /* Create method object.  This is actualy a
-                          java_lang_reflect_Method pointer, but we use a
-                          java_handle_t here, because we don't have the header
-                          available when building vmcore. */
+                       // Create java.lang.reflect.Method object.
 
-                       h = reflect_method_new(m);
+                       h = java_lang_reflect_Method_create(m);
 
                        /* Store object into array. */
 
@@ -2084,9 +1960,9 @@ java_handle_t* class_get_enclosingconstructor(classinfo *c)
        if (m->name != utf_init)
                return NULL;
 
-       /* Create Constructor object. */
+       // Create a java.lang.reflect.Constructor object.
 
-       rc = reflect_constructor_new(m);
+       rc = java_lang_reflect_Constructor_create(m);
 
        return rc;
 }
@@ -2161,9 +2037,9 @@ java_handle_t* class_get_enclosingmethod(classinfo *c)
        if (m->name == utf_init)
                return NULL;
 
-       /* create java.lang.reflect.Method object */
+       // Create a java.lang.reflect.Method object.
 
-       rm = reflect_method_new(m);
+       rm = java_lang_reflect_Method_create(m);
 
        return rm;
 }
@@ -2450,7 +2326,7 @@ void class_classref_or_classinfo_println(classref_or_classinfo c)
 void class_showconstantpool (classinfo *c) 
 {
        u4 i;
-       voidptr e;
+       void* e;
 
        printf ("---- dump of constant pool ----\n");