1 /* src/native/vm/gnuclasspath/java_lang_reflect_VMMethod.cpp
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 #if defined(ENABLE_ANNOTATIONS)
34 #include "native/jni.h"
35 #include "native/llni.h"
36 #include "native/native.h"
38 #include "native/include/java_lang_Object.h"
39 #include "native/include/java_lang_Class.h"
40 #include "native/include/java_lang_String.h"
42 #if defined(ENABLE_ANNOTATIONS)
43 # include "native/include/java_util_Map.h"
44 # include "native/include/sun_reflect_ConstantPool.h"
47 #include "native/include/java_lang_reflect_Method.h"
51 #include "native/include/java_lang_reflect_VMMethod.h"
54 #include "native/vm/reflect.h"
56 #include "vm/access.h"
57 #include "vm/global.h"
58 #include "vm/builtin.h"
59 #include "vm/exceptions.hpp"
60 #include "vm/initialize.h"
61 #include "vm/resolve.h"
62 #include "vm/string.hpp"
64 #include "vmcore/globals.hpp"
65 #include "vmcore/method.h"
68 /* native methods implemented by this file ************************************/
70 static JNINativeMethod methods[] = {
71 { (char*) "getModifiersInternal", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getModifiersInternal },
72 { (char*) "getReturnType", (char*) "()Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getReturnType },
73 { (char*) "getParameterTypes", (char*) "()[Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterTypes },
74 { (char*) "getExceptionTypes", (char*) "()[Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getExceptionTypes },
75 { (char*) "invoke", (char*) "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_invoke },
76 { (char*) "getSignature", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getSignature },
77 #if defined(ENABLE_ANNOTATIONS)
78 { (char*) "getDefaultValue", (char*) "()Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getDefaultValue },
79 { (char*) "declaredAnnotations", (char*) "()Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_declaredAnnotations },
80 { (char*) "getParameterAnnotations", (char*) "()[[Ljava/lang/annotation/Annotation;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterAnnotations },
85 /* _Jv_java_lang_reflect_VMMethod_init *****************************************
87 Register native functions.
89 *******************************************************************************/
93 void _Jv_java_lang_reflect_VMMethod_init(void)
97 u = utf_new_char("java/lang/reflect/VMMethod");
99 native_method_register(u, methods, NATIVE_METHODS_COUNT);
104 // Native functions are exported as C functions.
108 * Class: java/lang/reflect/VMMethod
109 * Method: getModifiersInternal
112 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_VMMethod_getModifiersInternal(JNIEnv *env, java_lang_reflect_VMMethod *_this)
118 LLNI_field_get_cls(_this, clazz, c);
119 LLNI_field_get_val(_this, slot , slot);
120 m = &(c->methods[slot]);
127 * Class: java/lang/reflect/VMMethod
128 * Method: getReturnType
129 * Signature: ()Ljava/lang/Class;
131 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_VMMethod_getReturnType(JNIEnv *env, java_lang_reflect_VMMethod *_this)
138 LLNI_field_get_cls(_this, clazz, c);
139 LLNI_field_get_val(_this, slot , slot);
140 m = &(c->methods[slot]);
142 result = method_returntype_get(m);
144 return LLNI_classinfo_wrap(result);
149 * Class: java/lang/reflect/VMMethod
150 * Method: getParameterTypes
151 * Signature: ()[Ljava/lang/Class;
153 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterTypes(JNIEnv *env, java_lang_reflect_VMMethod *_this)
159 LLNI_field_get_cls(_this, clazz, c);
160 LLNI_field_get_val(_this, slot , slot);
161 m = &(c->methods[slot]);
163 return method_get_parametertypearray(m);
168 * Class: java/lang/reflect/VMMethod
169 * Method: getExceptionTypes
170 * Signature: ()[Ljava/lang/Class;
172 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getExceptionTypes(JNIEnv *env, java_lang_reflect_VMMethod *_this)
178 LLNI_field_get_cls(_this, clazz, c);
179 LLNI_field_get_val(_this, slot , slot);
180 m = &(c->methods[slot]);
182 return method_get_exceptionarray(m);
187 * Class: java/lang/reflect/VMMethod
189 * Signature: (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
191 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_VMMethod_invoke(JNIEnv *env, java_lang_reflect_VMMethod *_this, java_lang_Object *o, java_handle_objectarray_t *args)
195 java_lang_reflect_Method *rm;
200 LLNI_field_get_cls(_this, clazz, c);
201 LLNI_field_get_val(_this, slot, slot);
203 LLNI_field_get_ref(_this, m, rm);
204 LLNI_field_get_val(rm, flag, override);
206 m = &(c->methods[slot]);
208 ro = reflect_method_invoke(m, (java_handle_t *) o, args, override);
210 return (java_lang_Object *) ro;
215 * Class: java/lang/reflect/VMMethod
216 * Method: getSignature
217 * Signature: ()Ljava/lang/String;
219 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_VMMethod_getSignature(JNIEnv *env, java_lang_reflect_VMMethod* _this)
226 LLNI_field_get_cls(_this, clazz, c);
227 LLNI_field_get_val(_this, slot , slot);
228 m = &(c->methods[slot]);
230 if (m->signature == NULL)
233 o = javastring_new(m->signature);
235 /* in error case o is NULL */
237 return (java_lang_String *) o;
240 #if defined(ENABLE_ANNOTATIONS)
242 * Class: java/lang/reflect/VMMethod
243 * Method: getDefaultValue
244 * Signature: ()Ljava/lang/Object;
246 * Parses the annotation default value and returnes it (boxed, if it's a primitive).
248 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_VMMethod_getDefaultValue(JNIEnv *env, struct java_lang_reflect_VMMethod* _this)
250 java_handle_bytearray_t *annotationDefault = NULL; /* unparsed annotation default value */
251 static methodinfo *m_parseAnnotationDefault = NULL; /* parser method (will be chached, therefore static) */
252 utf *utf_parseAnnotationDefault = NULL; /* parser method name */
253 utf *utf_desc = NULL; /* parser method descriptor (signature) */
254 sun_reflect_ConstantPool *constantPool = NULL; /* constant pool object to use */
255 java_lang_Class *constantPoolOop = NULL; /* methods declaring class */
256 classinfo *referer = NULL; /* class, which calles the annotation parser */
257 /* (for the parameter 'referer' of vm_call_method()) */
258 java_lang_reflect_Method* rm;
262 exceptions_throw_nullpointerexception();
267 (sun_reflect_ConstantPool*)native_new_and_init(
268 class_sun_reflect_ConstantPool);
270 if (constantPool == NULL) {
275 LLNI_field_get_ref(_this, clazz, constantPoolOop);
276 LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
278 /* only resolve the parser method the first time */
279 if (m_parseAnnotationDefault == NULL) {
280 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
281 utf_desc = utf_new_char(
282 "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
283 "Ljava/lang/Object;");
285 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
290 LLNI_class_get(_this, referer);
292 m_parseAnnotationDefault = class_resolveclassmethod(
293 class_sun_reflect_annotation_AnnotationParser,
294 utf_parseAnnotationDefault,
299 if (m_parseAnnotationDefault == NULL) {
300 /* method not found */
305 LLNI_field_get_ref(_this, m, rm);
306 LLNI_field_get_ref(_this, annotationDefault, annotationDefault);
308 h = vm_call_method(m_parseAnnotationDefault, NULL, rm, annotationDefault, constantPool);
310 return (java_lang_Object*) h;
315 * Class: java/lang/reflect/VMMethod
316 * Method: declaredAnnotations
317 * Signature: ()Ljava/util/Map;
319 * Parses the annotations (if they aren't parsed yet) and stores them into
320 * the declaredAnnotations map and return this map.
322 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_VMMethod_declaredAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *_this)
324 java_util_Map *declaredAnnotations = NULL; /* parsed annotations */
325 java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
326 java_lang_Class *declaringClass = NULL; /* the constant pool of _this class is used */
327 classinfo *referer = NULL; /* class, which calles the annotation parser */
328 /* (for the parameter 'referer' of vm_call_method()) */
330 LLNI_field_get_ref(_this, declaredAnnotations, declaredAnnotations);
332 /* are the annotations parsed yet? */
333 if (declaredAnnotations == NULL) {
334 LLNI_field_get_ref(_this, annotations, annotations);
335 LLNI_field_get_ref(_this, clazz, declaringClass);
336 LLNI_class_get(_this, referer);
338 declaredAnnotations = reflect_get_declaredannotations(annotations, (classinfo*) declaringClass, referer);
340 LLNI_field_set_ref(_this, declaredAnnotations, declaredAnnotations);
343 return declaredAnnotations;
348 * Class: java/lang/reflect/VMMethod
349 * Method: getParameterAnnotations
350 * Signature: ()[[Ljava/lang/annotation/Annotation;
352 * Parses the parameter annotations and returns them in an 2 dimensional array.
354 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *_this)
356 java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations */
357 int32_t slot = -1; /* slot of the method */
360 classinfo *referer = NULL; /* class, which calles the annotation parser */
361 /* (for the parameter 'referer' of vm_call_method()) */
363 LLNI_field_get_ref(_this, parameterAnnotations, parameterAnnotations);
364 LLNI_field_get_val(_this, slot, slot);
365 LLNI_field_get_cls(_this, clazz, c);
366 m = &(c->methods[slot]);
368 LLNI_class_get(_this, referer);
370 return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, m, referer);
378 * These are local overrides for various environment variables in Emacs.
379 * Please do not remove this and leave it at the end of the file, where
380 * Emacs will automagically detect them.
381 * ---------------------------------------------------------------------
384 * indent-tabs-mode: t