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.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 { (char*) "getModifiersInternal", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getModifiersInternal },
71 { (char*) "getReturnType", (char*) "()Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getReturnType },
72 { (char*) "getParameterTypes", (char*) "()[Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterTypes },
73 { (char*) "getExceptionTypes", (char*) "()[Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getExceptionTypes },
74 { (char*) "invoke", (char*) "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_invoke },
75 { (char*) "getSignature", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getSignature },
76 #if defined(ENABLE_ANNOTATIONS)
77 { (char*) "getDefaultValue", (char*) "()Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getDefaultValue },
78 { (char*) "declaredAnnotations", (char*) "()Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_declaredAnnotations },
79 { (char*) "getParameterAnnotations", (char*) "()[[Ljava/lang/annotation/Annotation;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterAnnotations },
84 /* _Jv_java_lang_reflect_VMMethod_init *****************************************
86 Register native functions.
88 *******************************************************************************/
92 void _Jv_java_lang_reflect_VMMethod_init(void)
96 u = utf_new_char("java/lang/reflect/VMMethod");
98 native_method_register(u, methods, NATIVE_METHODS_COUNT);
103 // Native functions are exported as C functions.
107 * Class: java/lang/reflect/VMMethod
108 * Method: getModifiersInternal
111 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_VMMethod_getModifiersInternal(JNIEnv *env, java_lang_reflect_VMMethod *_this)
117 LLNI_field_get_cls(_this, clazz, c);
118 LLNI_field_get_val(_this, slot , slot);
119 m = &(c->methods[slot]);
126 * Class: java/lang/reflect/VMMethod
127 * Method: getReturnType
128 * Signature: ()Ljava/lang/Class;
130 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_VMMethod_getReturnType(JNIEnv *env, java_lang_reflect_VMMethod *_this)
137 LLNI_field_get_cls(_this, clazz, c);
138 LLNI_field_get_val(_this, slot , slot);
139 m = &(c->methods[slot]);
141 result = method_returntype_get(m);
143 return LLNI_classinfo_wrap(result);
148 * Class: java/lang/reflect/VMMethod
149 * Method: getParameterTypes
150 * Signature: ()[Ljava/lang/Class;
152 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterTypes(JNIEnv *env, java_lang_reflect_VMMethod *_this)
158 LLNI_field_get_cls(_this, clazz, c);
159 LLNI_field_get_val(_this, slot , slot);
160 m = &(c->methods[slot]);
162 return method_get_parametertypearray(m);
167 * Class: java/lang/reflect/VMMethod
168 * Method: getExceptionTypes
169 * Signature: ()[Ljava/lang/Class;
171 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getExceptionTypes(JNIEnv *env, java_lang_reflect_VMMethod *_this)
177 LLNI_field_get_cls(_this, clazz, c);
178 LLNI_field_get_val(_this, slot , slot);
179 m = &(c->methods[slot]);
181 return method_get_exceptionarray(m);
186 * Class: java/lang/reflect/VMMethod
188 * Signature: (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
190 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)
194 java_lang_reflect_Method *rm;
199 LLNI_field_get_cls(_this, clazz, c);
200 LLNI_field_get_val(_this, slot, slot);
202 LLNI_field_get_ref(_this, m, rm);
203 LLNI_field_get_val(rm, flag, override);
205 m = &(c->methods[slot]);
207 ro = reflect_method_invoke(m, (java_handle_t *) o, args, override);
209 return (java_lang_Object *) ro;
214 * Class: java/lang/reflect/VMMethod
215 * Method: getSignature
216 * Signature: ()Ljava/lang/String;
218 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_VMMethod_getSignature(JNIEnv *env, java_lang_reflect_VMMethod* _this)
225 LLNI_field_get_cls(_this, clazz, c);
226 LLNI_field_get_val(_this, slot , slot);
227 m = &(c->methods[slot]);
229 if (m->signature == NULL)
232 o = javastring_new(m->signature);
234 /* in error case o is NULL */
236 return (java_lang_String *) o;
239 #if defined(ENABLE_ANNOTATIONS)
241 * Class: java/lang/reflect/VMMethod
242 * Method: getDefaultValue
243 * Signature: ()Ljava/lang/Object;
245 * Parses the annotation default value and returnes it (boxed, if it's a primitive).
247 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_VMMethod_getDefaultValue(JNIEnv *env, struct java_lang_reflect_VMMethod* _this)
249 java_handle_bytearray_t *annotationDefault = NULL; /* unparsed annotation default value */
250 static methodinfo *m_parseAnnotationDefault = NULL; /* parser method (will be chached, therefore static) */
251 utf *utf_parseAnnotationDefault = NULL; /* parser method name */
252 utf *utf_desc = NULL; /* parser method descriptor (signature) */
253 sun_reflect_ConstantPool *constantPool = NULL; /* constant pool object to use */
254 java_lang_Class *constantPoolOop = NULL; /* methods declaring class */
255 classinfo *referer = NULL; /* class, which calles the annotation parser */
256 /* (for the parameter 'referer' of vm_call_method()) */
257 java_lang_reflect_Method* rm;
261 exceptions_throw_nullpointerexception();
266 (sun_reflect_ConstantPool*)native_new_and_init(
267 class_sun_reflect_ConstantPool);
269 if (constantPool == NULL) {
274 LLNI_field_get_ref(_this, clazz, constantPoolOop);
275 LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
277 /* only resolve the parser method the first time */
278 if (m_parseAnnotationDefault == NULL) {
279 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
280 utf_desc = utf_new_char(
281 "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
282 "Ljava/lang/Object;");
284 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
289 LLNI_class_get(_this, referer);
291 m_parseAnnotationDefault = class_resolveclassmethod(
292 class_sun_reflect_annotation_AnnotationParser,
293 utf_parseAnnotationDefault,
298 if (m_parseAnnotationDefault == NULL) {
299 /* method not found */
304 LLNI_field_get_ref(_this, m, rm);
305 LLNI_field_get_ref(_this, annotationDefault, annotationDefault);
307 h = vm_call_method(m_parseAnnotationDefault, NULL, rm, annotationDefault, constantPool);
309 return (java_lang_Object*) h;
314 * Class: java/lang/reflect/VMMethod
315 * Method: declaredAnnotations
316 * Signature: ()Ljava/util/Map;
318 * Parses the annotations (if they aren't parsed yet) and stores them into
319 * the declaredAnnotations map and return this map.
321 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_VMMethod_declaredAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *_this)
323 java_util_Map *declaredAnnotations = NULL; /* parsed annotations */
324 java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
325 java_lang_Class *declaringClass = NULL; /* the constant pool of _this class is used */
326 classinfo *referer = NULL; /* class, which calles the annotation parser */
327 /* (for the parameter 'referer' of vm_call_method()) */
329 LLNI_field_get_ref(_this, declaredAnnotations, declaredAnnotations);
331 /* are the annotations parsed yet? */
332 if (declaredAnnotations == NULL) {
333 LLNI_field_get_ref(_this, annotations, annotations);
334 LLNI_field_get_ref(_this, clazz, declaringClass);
335 LLNI_class_get(_this, referer);
337 declaredAnnotations = reflect_get_declaredannotations(annotations, (classinfo*) declaringClass, referer);
339 LLNI_field_set_ref(_this, declaredAnnotations, declaredAnnotations);
342 return declaredAnnotations;
347 * Class: java/lang/reflect/VMMethod
348 * Method: getParameterAnnotations
349 * Signature: ()[[Ljava/lang/annotation/Annotation;
351 * Parses the parameter annotations and returns them in an 2 dimensional array.
353 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *_this)
355 java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations */
356 int32_t slot = -1; /* slot of the method */
359 classinfo *referer = NULL; /* class, which calles the annotation parser */
360 /* (for the parameter 'referer' of vm_call_method()) */
362 LLNI_field_get_ref(_this, parameterAnnotations, parameterAnnotations);
363 LLNI_field_get_val(_this, slot, slot);
364 LLNI_field_get_cls(_this, clazz, c);
365 m = &(c->methods[slot]);
367 LLNI_class_get(_this, referer);
369 return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, m, referer);
377 * These are local overrides for various environment variables in Emacs.
378 * Please do not remove this and leave it at the end of the file, where
379 * Emacs will automagically detect them.
380 * ---------------------------------------------------------------------
383 * indent-tabs-mode: t