* src/mm/memory.cpp,
[cacao.git] / src / native / vm / gnuclasspath / sun_reflect_ConstantPool.cpp
index 27fde6cbdb9189d23365a18805e5a04dd93f3887..b811d6068dca24d65060697de33a1f9a57f3d283 100644 (file)
 #include <assert.h>
 #include <stdint.h>
 
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
-#include "native/jni.h"
+#include "native/jni.hpp"
 #include "native/llni.h"
-#include "native/native.h"
-
-#include "native/include/java_lang_Object.h"
-#include "native/include/java_lang_Class.h"
+#include "native/native.hpp"
 
 // FIXME
-extern "C" {
-#include "native/include/sun_reflect_ConstantPool.h"
-}
-
-#include "native/vm/reflect.h"
-
-#include "toolbox/logging.h"
-
-#include "vm/vm.h"
-#include "vm/exceptions.h"
-#include "vm/resolve.h"
-#include "vm/stringlocal.h"
-
-#include "vmcore/class.h"
-#include "vmcore/utf8.h"
-
-
-/* native methods implemented by this file ************************************/
-
-static JNINativeMethod methods[] = {
-       { (char*) "getSize0",             (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
-       { (char*) "getClassAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
-       { (char*) "getClassAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
-       { (char*) "getMethodAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
-       { (char*) "getMethodAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
-       { (char*) "getFieldAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
-       { (char*) "getFieldAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
-       { (char*) "getMemberRefInfoAt0",  (char*) "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
-       { (char*) "getIntAt0",            (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
-       { (char*) "getLongAt0",           (char*) "(Ljava/lang/Object;I)J",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
-       { (char*) "getFloatAt0",          (char*) "(Ljava/lang/Object;I)F",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
-       { (char*) "getDoubleAt0",         (char*) "(Ljava/lang/Object;I)D",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
-       { (char*) "getStringAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
-       { (char*) "getUTF8At0",           (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
-};
+//#include "native/include/sun_reflect_ConstantPool.h"
 
+#include "native/vm/reflection.hpp"
 
-/* _Jv_sun_reflect_ConstantPool_init ******************************************
+#include "toolbox/logging.hpp"
 
-   Register native functions.
-
-*******************************************************************************/
-
-// FIXME
-extern "C" {
-void _Jv_sun_reflect_ConstantPool_init(void)
-{
-       native_method_register(utf_new_char("sun/reflect/ConstantPool"), methods, NATIVE_METHODS_COUNT);
-}
-}
+#include "vm/class.hpp"
+#include "vm/exceptions.hpp"
+#include "vm/javaobjects.hpp"
+#include "vm/resolve.hpp"
+#include "vm/string.hpp"
+#include "vm/utf8.h"
+#include "vm/vm.hpp"
 
 
+// Native functions are exported as C functions.
 extern "C" {
 
 /*
@@ -109,7 +70,7 @@ extern "C" {
  * Method:    getSize0
  * Signature: (Ljava/lang/Object;)I
  */
-JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool)
+JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, jobject _this, jobject jcpool)
 {
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
        return cls->cpcount;
@@ -121,7 +82,7 @@ JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, st
  * Method:    getClassAt0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
  */
-JNIEXPORT struct java_lang_Class* JNICALL Java_sun_reflect_ConstantPool_getClassAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_classref *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -134,7 +95,7 @@ JNIEXPORT struct java_lang_Class* JNICALL Java_sun_reflect_ConstantPool_getClass
                return NULL;
        }
 
-       return LLNI_classinfo_wrap(resolve_classref_eager(ref));
+       return (jclass) LLNI_classinfo_wrap(resolve_classref_eager(ref));
 }
 
 
@@ -143,7 +104,7 @@ JNIEXPORT struct java_lang_Class* JNICALL Java_sun_reflect_ConstantPool_getClass
  * Method:    getClassAtIfLoaded0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
  */
-JNIEXPORT struct java_lang_Class* JNICALL Java_sun_reflect_ConstantPool_getClassAtIfLoaded0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_classref *ref;
        classinfo *c = NULL;
@@ -165,7 +126,7 @@ JNIEXPORT struct java_lang_Class* JNICALL Java_sun_reflect_ConstantPool_getClass
                return NULL;
        }
        
-       return LLNI_classinfo_wrap(c);
+       return (jclass) LLNI_classinfo_wrap(c);
 }
 
 
@@ -174,7 +135,7 @@ JNIEXPORT struct java_lang_Class* JNICALL Java_sun_reflect_ConstantPool_getClass
  * Method:    getMethodAt0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
  */
-JNIEXPORT struct java_lang_reflect_Member* JNICALL Java_sun_reflect_ConstantPool_getMethodAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_FMIref *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -188,7 +149,9 @@ JNIEXPORT struct java_lang_reflect_Member* JNICALL Java_sun_reflect_ConstantPool
        }
 
        /* XXX: is that right? or do I have to use resolve_method_*? */
-       return (struct java_lang_reflect_Member*) reflect_method_new(ref->p.method);
+       java_lang_reflect_Method jlrm(ref->p.method);
+
+       return (jobject) jlrm.get_handle();
 }
 
 
@@ -197,7 +160,7 @@ JNIEXPORT struct java_lang_reflect_Member* JNICALL Java_sun_reflect_ConstantPool
  * Method:    getMethodAtIfLoaded0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
  */
-JNIEXPORT struct java_lang_reflect_Member* JNICALL Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_FMIref *ref;
        classinfo *c = NULL;
@@ -219,7 +182,9 @@ JNIEXPORT struct java_lang_reflect_Member* JNICALL Java_sun_reflect_ConstantPool
                return NULL;
        }
 
-       return (struct java_lang_reflect_Member*) reflect_method_new(ref->p.method);
+       java_lang_reflect_Method jlrm(ref->p.method);
+
+       return (jobject) jlrm.get_handle();
 }
 
 
@@ -228,20 +193,22 @@ JNIEXPORT struct java_lang_reflect_Member* JNICALL Java_sun_reflect_ConstantPool
  * Method:    getFieldAt0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
  */
-JNIEXPORT struct java_lang_reflect_Field* JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_FMIref *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
 
-       ref = (constant_FMIref*)class_getconstant(
-               cls, index, CONSTANT_Fieldref);
+       ref = (constant_FMIref*) class_getconstant(cls, index, CONSTANT_Fieldref);
 
        if (ref == NULL) {
                exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
-       return (struct java_lang_reflect_Field*) reflect_field_new(ref->p.field);
+       // Create a new java.lang.reflect.Field Java object.
+       java_lang_reflect_Field jlrf(ref->p.field);
+
+       return (jobject) jlrf.get_handle();
 }
 
 
@@ -250,14 +217,13 @@ JNIEXPORT struct java_lang_reflect_Field* JNICALL Java_sun_reflect_ConstantPool_
  * Method:    getFieldAtIfLoaded0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
  */
-JNIEXPORT struct java_lang_reflect_Field* JNICALL Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_FMIref *ref;
        classinfo *c;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
 
-       ref = (constant_FMIref*)class_getconstant(
-               cls, index, CONSTANT_Fieldref);
+       ref = (constant_FMIref*) class_getconstant(cls, index, CONSTANT_Fieldref);
 
        if (ref == NULL) {
                exceptions_throw_illegalargumentexception();
@@ -272,7 +238,10 @@ JNIEXPORT struct java_lang_reflect_Field* JNICALL Java_sun_reflect_ConstantPool_
                return NULL;
        }
 
-       return (struct java_lang_reflect_Field*) reflect_field_new(ref->p.field);
+       // Create a new java.lang.reflect.Field Java object.
+       java_lang_reflect_Field jlrf(ref->p.field);
+
+       return (jobject) jlrf.get_handle();
 }
 
 
@@ -281,7 +250,7 @@ JNIEXPORT struct java_lang_reflect_Field* JNICALL Java_sun_reflect_ConstantPool_
  * Method:    getMemberRefInfoAt0
  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
  */
-JNIEXPORT java_handle_objectarray_t* JNICALL Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jobjectArray JNICALL Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(env=%p, jcpool=%p, index=%d): IMPLEMENT ME!", env, jcpool, index);
        return NULL;
@@ -293,13 +262,12 @@ JNIEXPORT java_handle_objectarray_t* JNICALL Java_sun_reflect_ConstantPool_getMe
  * Method:    getIntAt0
  * Signature: (Ljava/lang/Object;I)I
  */
-JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_integer *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
 
-       ref = (constant_integer*)class_getconstant(
-               cls, index, CONSTANT_Integer);
+       ref = (constant_integer*) class_getconstant(cls, index, CONSTANT_Integer);
 
        if (ref == NULL) {
                exceptions_throw_illegalargumentexception();
@@ -315,7 +283,7 @@ JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, s
  * Method:    getLongAt0
  * Signature: (Ljava/lang/Object;I)J
  */
-JNIEXPORT int64_t JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jlong JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_long *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -337,7 +305,7 @@ JNIEXPORT int64_t JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env,
  * Method:    getFloatAt0
  * Signature: (Ljava/lang/Object;I)F
  */
-JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_float *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -359,7 +327,7 @@ JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, s
  * Method:    getDoubleAt0
  * Signature: (Ljava/lang/Object;I)D
  */
-JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        constant_double *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -381,7 +349,7 @@ JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env,
  * Method:    getStringAt0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
  */
-JNIEXPORT struct java_lang_String* JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        utf *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -394,7 +362,7 @@ JNIEXPORT struct java_lang_String* JNICALL Java_sun_reflect_ConstantPool_getStri
        }
 
        /* XXX: I hope literalstring_new is the right Function. */
-       return (java_lang_String*)literalstring_new(ref);
+       return (jstring) literalstring_new(ref);
 }
 
 
@@ -403,7 +371,7 @@ JNIEXPORT struct java_lang_String* JNICALL Java_sun_reflect_ConstantPool_getStri
  * Method:    getUTF8At0
  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
  */
-JNIEXPORT struct java_lang_String* JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, struct sun_reflect_ConstantPool* _this, struct java_lang_Object* jcpool, int32_t index)
+JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
 {
        utf *ref;
        classinfo *cls = LLNI_classinfo_unwrap(jcpool);
@@ -416,19 +384,54 @@ JNIEXPORT struct java_lang_String* JNICALL Java_sun_reflect_ConstantPool_getUTF8
        }
 
        /* XXX: I hope literalstring_new is the right Function. */
-       return (java_lang_String*)literalstring_new(ref);
+       return (jstring) literalstring_new(ref);
 }
 
 } // extern "C"
 
 
+/* native methods implemented by this file ************************************/
+
+static JNINativeMethod methods[] = {
+       { (char*) "getSize0",             (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
+       { (char*) "getClassAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
+       { (char*) "getClassAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
+       { (char*) "getMethodAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
+       { (char*) "getMethodAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
+       { (char*) "getFieldAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
+       { (char*) "getFieldAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
+       { (char*) "getMemberRefInfoAt0",  (char*) "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
+       { (char*) "getIntAt0",            (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
+       { (char*) "getLongAt0",           (char*) "(Ljava/lang/Object;I)J",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
+       { (char*) "getFloatAt0",          (char*) "(Ljava/lang/Object;I)F",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
+       { (char*) "getDoubleAt0",         (char*) "(Ljava/lang/Object;I)D",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
+       { (char*) "getStringAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
+       { (char*) "getUTF8At0",           (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
+};
+
+
+/* _Jv_sun_reflect_ConstantPool_init ******************************************
+
+   Register native functions.
+
+*******************************************************************************/
+
+void _Jv_sun_reflect_ConstantPool_init(void)
+{
+       utf* u = utf_new_char("sun/reflect/ConstantPool");
+
+       NativeMethods& nm = VM::get_current()->get_nativemethods();
+       nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
+}
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where
  * Emacs will automagically detect them.
  * ---------------------------------------------------------------------
  * Local variables:
- * mode: c
+ * mode: c++
  * indent-tabs-mode: t
  * c-basic-offset: 4
  * tab-width: 4