1 /* src/native/vm/gnu/java_lang_reflect_Method.c
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #if defined(ENABLE_ANNOTATIONS)
38 #include "native/jni.h"
39 #include "native/llni.h"
40 #include "native/native.h"
42 #include "native/include/java_lang_Object.h"
43 #include "native/include/java_lang_Class.h"
44 #include "native/include/java_lang_String.h"
46 #if defined(ENABLE_ANNOTATIONS)
47 #include "native/include/java_util_Map.h"
48 #include "native/include/sun_reflect_ConstantPool.h"
49 #include "native/vm/reflect.h"
52 #include "native/include/java_lang_reflect_Method.h"
54 #include "native/vm/java_lang_reflect_Method.h"
56 #include "vm/access.h"
57 #include "vm/global.h"
58 #include "vm/builtin.h"
59 #include "vm/exceptions.h"
60 #include "vm/initialize.h"
61 #include "vm/resolve.h"
62 #include "vm/stringlocal.h"
64 #include "vmcore/method.h"
67 /* native methods implemented by this file ************************************/
69 static JNINativeMethod methods[] = {
70 { "getModifiersInternal", "()I", (void *) (ptrint) &Java_java_lang_reflect_Method_getModifiersInternal },
71 { "getReturnType", "()Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_reflect_Method_getReturnType },
72 { "getParameterTypes", "()[Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterTypes },
73 { "getExceptionTypes", "()[Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_reflect_Method_getExceptionTypes },
74 { "invokeNative", "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;", (void *) (ptrint) &Java_java_lang_reflect_Method_invokeNative },
75 { "getSignature", "()Ljava/lang/String;", (void *) (ptrint) &Java_java_lang_reflect_Method_getSignature },
76 #if defined(ENABLE_ANNOTATIONS)
77 { "getDefaultValue", "()Ljava/lang/Object;", (void *) (ptrint) &Java_java_lang_reflect_Method_getDefaultValue },
78 { "declaredAnnotations", "()Ljava/util/Map;", (void *) (ptrint) &Java_java_lang_reflect_Method_declaredAnnotations },
79 { "getParameterAnnotations", "()[[Ljava/lang/annotation/Annotation;", (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterAnnotations },
84 /* _Jv_java_lang_reflect_Method_init *******************************************
86 Register native functions.
88 *******************************************************************************/
90 void _Jv_java_lang_reflect_Method_init(void)
94 u = utf_new_char("java/lang/reflect/Method");
96 native_method_register(u, methods, NATIVE_METHODS_COUNT);
101 * Class: java/lang/reflect/Method
102 * Method: getModifiersInternal
105 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiersInternal(JNIEnv *env, java_lang_reflect_Method *this)
111 LLNI_field_get_cls(this, clazz, c);
112 LLNI_field_get_val(this, slot , slot);
113 m = &(c->methods[slot]);
120 * Class: java/lang/reflect/Method
121 * Method: getReturnType
122 * Signature: ()Ljava/lang/Class;
124 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(JNIEnv *env, java_lang_reflect_Method *this)
131 LLNI_field_get_cls(this, clazz, c);
132 LLNI_field_get_val(this, slot , slot);
133 m = &(c->methods[slot]);
135 result = method_returntype_get(m);
137 return LLNI_classinfo_wrap(result);
142 * Class: java/lang/reflect/Method
143 * Method: getParameterTypes
144 * Signature: ()[Ljava/lang/Class;
146 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
152 LLNI_field_get_cls(this, clazz, c);
153 LLNI_field_get_val(this, slot , slot);
154 m = &(c->methods[slot]);
156 return method_get_parametertypearray(m);
161 * Class: java/lang/reflect/Method
162 * Method: getExceptionTypes
163 * Signature: ()[Ljava/lang/Class;
165 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
171 LLNI_field_get_cls(this, clazz, c);
172 LLNI_field_get_val(this, slot , slot);
173 m = &(c->methods[slot]);
175 return method_get_exceptionarray(m);
180 * Class: java/lang/reflect/Method
181 * Method: invokeNative
182 * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
184 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)
186 /* just to be sure */
188 assert(LLNI_field_direct(this, clazz) == LLNI_DIRECT(clazz));
189 assert(LLNI_field_direct(this, slot) == slot);
191 return _Jv_java_lang_reflect_Method_invoke(this, o, args);
196 * Class: java/lang/reflect/Method
197 * Method: getSignature
198 * Signature: ()Ljava/lang/String;
200 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Method_getSignature(JNIEnv *env, java_lang_reflect_Method* this)
207 LLNI_field_get_cls(this, clazz, c);
208 LLNI_field_get_val(this, slot , slot);
209 m = &(c->methods[slot]);
211 if (m->signature == NULL)
214 o = javastring_new(m->signature);
216 /* in error case o is NULL */
218 return (java_lang_String *) o;
221 #if defined(ENABLE_ANNOTATIONS)
223 * Class: java/lang/reflect/Method
224 * Method: getDefaultValue
225 * Signature: ()Ljava/lang/Object;
227 * Parses the annotation default value and returnes it (boxed, if it's a primitive).
229 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_Method_getDefaultValue(JNIEnv *env, struct java_lang_reflect_Method* this)
231 java_handle_bytearray_t *annotationDefault = NULL; /* unparsed annotation default value */
232 static methodinfo *m_parseAnnotationDefault = NULL; /* parser method (will be chached, therefore static) */
233 utf *utf_parseAnnotationDefault = NULL; /* parser method name */
234 utf *utf_desc = NULL; /* parser method descriptor (signature) */
235 sun_reflect_ConstantPool *constantPool = NULL; /* constant pool object to use */
236 java_lang_Class *constantPoolOop = NULL; /* methods declaring class */
237 classinfo *referer = NULL; /* class, which calles the annotation parser */
238 /* (for the parameter 'referer' of vm_call_method()) */
241 exceptions_throw_nullpointerexception();
246 (sun_reflect_ConstantPool*)native_new_and_init(
247 class_sun_reflect_ConstantPool);
249 if (constantPool == NULL) {
254 LLNI_field_get_ref(this, clazz, constantPoolOop);
255 LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
257 /* only resolve the parser method the first time */
258 if (m_parseAnnotationDefault == NULL) {
259 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
260 utf_desc = utf_new_char(
261 "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
262 "Ljava/lang/Object;");
264 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
269 LLNI_class_get(this, referer);
271 m_parseAnnotationDefault = class_resolveclassmethod(
272 class_sun_reflect_annotation_AnnotationParser,
273 utf_parseAnnotationDefault,
278 if (m_parseAnnotationDefault == NULL) {
279 /* method not found */
284 LLNI_field_get_ref(this, annotationDefault, annotationDefault);
286 return (java_lang_Object*)vm_call_method(
287 m_parseAnnotationDefault, NULL,
288 this, annotationDefault, constantPool);
293 * Class: java/lang/reflect/Method
294 * Method: declaredAnnotations
295 * Signature: ()Ljava/util/Map;
297 * Parses the annotations (if they aren't parsed yet) and stores them into
298 * the declaredAnnotations map and return this map.
300 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Method_declaredAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
302 java_util_Map *declaredAnnotations = NULL; /* parsed annotations */
303 java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
304 java_lang_Class *declaringClass = NULL; /* the constant pool of this class is used */
305 classinfo *referer = NULL; /* class, which calles the annotation parser */
306 /* (for the parameter 'referer' of vm_call_method()) */
308 LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
310 /* are the annotations parsed yet? */
311 if (declaredAnnotations == NULL) {
312 LLNI_field_get_ref(this, annotations, annotations);
313 LLNI_field_get_ref(this, clazz, declaringClass);
314 LLNI_class_get(this, referer);
316 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
318 LLNI_field_set_ref(this, declaredAnnotations, (java_handle_t*) declaredAnnotations);
321 return declaredAnnotations;
326 * Class: java/lang/reflect/Method
327 * Method: getParameterAnnotations
328 * Signature: ()[[Ljava/lang/annotation/Annotation;
330 * Parses the parameter annotations and returns them in an 2 dimensional array.
332 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
334 java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations */
335 int32_t slot = -1; /* slot of the method */
336 java_lang_Class *declaringClass = NULL; /* the constant pool of this class is used */
337 classinfo *referer = NULL; /* class, which calles the annotation parser */
338 /* (for the parameter 'referer' of vm_call_method()) */
340 LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
341 LLNI_field_get_val(this, slot, slot);
342 LLNI_field_get_ref(this, clazz, declaringClass);
343 LLNI_class_get(this, referer);
345 return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, referer);
351 * These are local overrides for various environment variables in Emacs.
352 * Please do not remove this and leave it at the end of the file, where
353 * Emacs will automagically detect them.
354 * ---------------------------------------------------------------------
357 * indent-tabs-mode: t