* Removed all Id tags.
[cacao.git] / src / native / vm / gnu / java_lang_reflect_Method.c
index dde38297ff1b5b77a37875a33967cc769f3fb7d4..150e46201c91d15310c4d2b8fb6b1467a3098491 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: java_lang_reflect_Method.c 7328 2007-02-11 21:22:07Z twisti $
-
 */
 
 
 
 #include <assert.h>
 
+#if defined(ENABLE_ANNOTATIONS)
+#include "vm/vm.h"
+#endif
+
 #include "vm/types.h"
 
 #include "native/jni.h"
+#include "native/llni.h"
 #include "native/native.h"
+
 #include "native/include/java_lang_Object.h"
 #include "native/include/java_lang_Class.h"
 #include "native/include/java_lang_String.h"
+
+#if defined(ENABLE_ANNOTATIONS)
+#include "native/include/sun_reflect_ConstantPool.h"
+#include "native/vm/reflect.h"
+#endif
+
 #include "native/include/java_lang_reflect_Method.h"
 
+#include "native/vm/java_lang_reflect_Method.h"
+
 #include "vm/access.h"
 #include "vm/global.h"
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/initialize.h"
+#include "vm/resolve.h"
 #include "vm/stringlocal.h"
 
+#include "vmcore/method.h"
+
+
+/* native methods implemented by this file ************************************/
+
+static JNINativeMethod methods[] = {
+       { "getModifiersInternal",    "()I",                                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getModifiersInternal    },
+       { "getReturnType",           "()Ljava/lang/Class;",                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getReturnType           },
+       { "getParameterTypes",       "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterTypes       },
+       { "getExceptionTypes",       "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getExceptionTypes       },
+       { "invokeNative",            "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;", (void *) (ptrint) &Java_java_lang_reflect_Method_invokeNative            },
+       { "getSignature",            "()Ljava/lang/String;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getSignature            },
+#if defined(ENABLE_ANNOTATIONS)
+       { "getDefaultValue",         "()Ljava/lang/Object;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getDefaultValue         },
+       { "declaredAnnotations",     "()Ljava/util/Map;",                                                           (void *) (ptrint) &Java_java_lang_reflect_Method_declaredAnnotations     },
+       { "getParameterAnnotations", "()[[Ljava/lang/annotation/Annotation;",                                       (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterAnnotations },
+#endif
+};
+
+
+/* _Jv_java_lang_reflect_Method_init *******************************************
+
+   Register native functions.
+
+*******************************************************************************/
+
+void _Jv_java_lang_reflect_Method_init(void)
+{
+       utf *u;
+
+       u = utf_new_char("java/lang/reflect/Method");
+
+       native_method_register(u, methods, NATIVE_METHODS_COUNT);
+}
+
 
 /*
  * Class:     java/lang/reflect/Method
@@ -57,9 +105,11 @@ JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiersInternal(JNIEnv *
 {
        classinfo  *c;
        methodinfo *m;
+       int32_t     slot;
 
-       c = (classinfo *) this->declaringClass;
-       m = &(c->methods[this->slot]);
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       m = &(c->methods[slot]);
 
        return m->flags;
 }
@@ -74,17 +124,16 @@ JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(J
 {
        classinfo  *c;
        methodinfo *m;
-       typedesc   *td;
-
-       c = (classinfo *) this->declaringClass;
-       m = &(c->methods[this->slot]);
+       classinfo  *result;
+       int32_t     slot;
 
-       td = &(m->parseddesc->returntype);
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       m = &(c->methods[slot]);
 
-       if (!resolve_class_from_typedesc(td, true, false, &c))
-               return NULL;
+       result = method_returntype_get(m);
 
-       return (java_lang_Class *) c;
+       return LLNI_classinfo_wrap(result);
 }
 
 
@@ -93,15 +142,17 @@ JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(J
  * Method:    getParameterTypes
  * Signature: ()[Ljava/lang/Class;
  */
-JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
+JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
 {
        classinfo  *c;
        methodinfo *m;
+       int32_t     slot;
 
-       c = (classinfo *) this->declaringClass;
-       m = &(c->methods[this->slot]);
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       m = &(c->methods[slot]);
 
-       return native_get_parametertypes(m);
+       return method_get_parametertypearray(m);
 }
 
 
@@ -110,15 +161,17 @@ JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getParameterTy
  * Method:    getExceptionTypes
  * Signature: ()[Ljava/lang/Class;
  */
-JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
+JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
 {
        classinfo  *c;
        methodinfo *m;
+       int32_t     slot;
 
-       c = (classinfo *) this->declaringClass;
-       m = &(c->methods[this->slot]);
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
+       m = &(c->methods[slot]);
 
-       return native_get_exceptiontypes(m);
+       return method_get_exceptionarray(m);
 }
 
 
@@ -127,57 +180,159 @@ JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getExceptionTy
  * Method:    invokeNative
  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
  */
-JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Method_invokeNative(JNIEnv *env, java_lang_reflect_Method *this, java_lang_Object *o, java_objectarray *args, java_lang_Class *declaringClass, s4 slot)
+JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Method_invokeNative(JNIEnv *env, java_lang_reflect_Method *this, java_lang_Object *o, java_handle_objectarray_t *args, java_lang_Class *clazz, s4 slot)
+{
+       /* just to be sure */
+
+       assert(LLNI_field_direct(this, clazz) == clazz);
+       assert(LLNI_field_direct(this, slot)  == slot);
+
+       return _Jv_java_lang_reflect_Method_invoke(this, o, args);
+}
+
+
+/*
+ * Class:     java/lang/reflect/Method
+ * Method:    getSignature
+ * Signature: ()Ljava/lang/String;
+ */
+JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Method_getSignature(JNIEnv *env, java_lang_reflect_Method* this)
 {
-       classinfo        *c;
-       methodinfo       *m;
+       classinfo     *c;
+       methodinfo    *m;
+       java_handle_t *o;
+       int32_t        slot;
 
-       c = (classinfo *) declaringClass;
+       LLNI_field_get_cls(this, clazz, c);
+       LLNI_field_get_val(this, slot , slot);
        m = &(c->methods[slot]);
 
-       /* check method access */
-       /* check if we should bypass security checks (AccessibleObject) */
+       if (m->signature == NULL)
+               return NULL;
 
-       if (this->flag == false) {
-               if (!access_check_caller(c, m->flags, 1))
-                       return NULL;
+       o = javastring_new(m->signature);
+
+       /* in error case o is NULL */
+
+       return (java_lang_String *) o;
+}
+
+#if defined(ENABLE_ANNOTATIONS)
+/*
+ * Class:     java/lang/reflect/Method
+ * Method:    getDefaultValue
+ * Signature: ()Ljava/lang/Object;
+ */
+JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_Method_getDefaultValue(JNIEnv *env, struct java_lang_reflect_Method* this)
+{
+       static methodinfo        *m_parseAnnotationDefault   = NULL;
+       utf                      *utf_parseAnnotationDefault = NULL;
+       utf                      *utf_desc        = NULL;
+       sun_reflect_ConstantPool *constantPool    = NULL;
+       java_handle_t            *o               = (java_handle_t*)this;
+       java_lang_Class          *constantPoolOop = NULL;
+       classinfo                *referer         = NULL;
+
+       if (this == NULL) {
+               exceptions_throw_nullpointerexception();
+               return NULL;
        }
 
-       /* check if method class is initialized */
+       constantPool = 
+               (sun_reflect_ConstantPool*)native_new_and_init(
+                       class_sun_reflect_ConstantPool);
+       
+       if(constantPool == NULL) {
+               /* out of memory */
+               return NULL;
+       }
+
+       LLNI_field_get_ref(this, clazz, constantPoolOop);
+       LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
+
+       /* only resolve the method the first time */
+       if (m_parseAnnotationDefault == NULL) {
+               utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
+               utf_desc = utf_new_char(
+                       "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
+                       "Ljava/lang/Object;");
 
-       if (!(c->state & CLASS_INITIALIZED))
-               if (!initialize_class(c))
+               if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
+                       /* out of memory */
                        return NULL;
+               }
 
-       /* call the Java method via a helper function */
+               LLNI_class_get(this, referer);
 
-       return (java_lang_Object *) _Jv_jni_invokeNative(m, (jobject) o, args);
+               m_parseAnnotationDefault = class_resolveclassmethod(
+                       class_sun_reflect_annotation_AnnotationParser,
+                       utf_parseAnnotationDefault,
+                       utf_desc,
+                       referer,
+                       true);
+
+               if (m_parseAnnotationDefault == NULL) {
+                       /* method not found */
+                       return NULL;
+               }
+       }
+
+       return (java_lang_Object*)vm_call_method(
+               m_parseAnnotationDefault, NULL,
+               this, this->annotationDefault, constantPool);
 }
 
 
 /*
  * Class:     java/lang/reflect/Method
- * Method:    getSignature
- * Signature: ()Ljava/lang/String;
+ * Method:    declaredAnnotations
+ * Signature: ()Ljava/util/Map;
  */
-JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Method_getSignature(JNIEnv *env, java_lang_reflect_Method* this)
+JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Method_declaredAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
 {
-       classinfo        *c;
-       methodinfo       *m;
-       java_lang_String *s;
+       java_handle_t           *o                   = (java_handle_t*)this;
+       struct java_util_Map    *declaredAnnotations = NULL;
+       java_handle_bytearray_t *annotations         = NULL;
+       java_lang_Class         *declaringClass      = NULL;
+       classinfo               *referer             = NULL;
 
-       c = (classinfo *) this->declaringClass;
-       m = &(c->methods[this->slot]);
+       LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
 
-       if (m->signature == NULL)
-               return NULL;
+       if (declaredAnnotations == NULL) {
+               LLNI_field_get_val(this, annotations, annotations);
+               LLNI_field_get_ref(this, clazz, declaringClass);
+               LLNI_class_get(this, referer);
 
-       s = javastring_new(m->signature);
+               declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
+
+               LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
+       }
+
+       return declaredAnnotations;
+}
 
-       /* in error case, s == NULL */
 
-       return s;
+/*
+ * Class:     java/lang/reflect/Method
+ * Method:    getParameterAnnotations
+ * Signature: ()[[Ljava/lang/annotation/Annotation;
+ */
+JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
+{
+       java_handle_t           *o                    = (java_handle_t*)this;
+       java_handle_bytearray_t *parameterAnnotations = NULL;
+       int32_t                  slot                 = -1;
+       java_lang_Class         *declaringClass       = NULL;
+       classinfo               *referer              = NULL;
+
+       LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
+       LLNI_field_get_val(this, slot, slot);
+       LLNI_field_get_ref(this, clazz, declaringClass);
+       LLNI_class_get(this, referer);
+
+       return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, referer);
 }
+#endif
 
 
 /*