1 /* src/native/vm/gnu/java_lang_reflect_VMMethod.c
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"
48 #include "native/include/java_lang_reflect_VMMethod.h"
50 #include "native/vm/reflect.h"
52 #include "vm/access.h"
53 #include "vm/global.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/initialize.h"
57 #include "vm/resolve.h"
58 #include "vm/stringlocal.h"
60 #include "vmcore/method.h"
63 /* native methods implemented by this file ************************************/
65 static JNINativeMethod methods[] = {
66 { "getModifiersInternal", "()I", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getModifiersInternal },
67 { "getReturnType", "()Ljava/lang/Class;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getReturnType },
68 { "getParameterTypes", "()[Ljava/lang/Class;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterTypes },
69 { "getExceptionTypes", "()[Ljava/lang/Class;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getExceptionTypes },
70 { "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_invoke },
71 { "getSignature", "()Ljava/lang/String;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getSignature },
72 #if defined(ENABLE_ANNOTATIONS)
73 { "getDefaultValue", "()Ljava/lang/Object;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getDefaultValue },
74 { "declaredAnnotations", "()Ljava/util/Map;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_declaredAnnotations },
75 { "getParameterAnnotations", "()[[Ljava/lang/annotation/Annotation;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterAnnotations },
80 /* _Jv_java_lang_reflect_VMMethod_init *******************************************
82 Register native functions.
84 *******************************************************************************/
86 void _Jv_java_lang_reflect_VMMethod_init(void)
90 u = utf_new_char("java/lang/reflect/VMMethod");
92 native_method_register(u, methods, NATIVE_METHODS_COUNT);
97 * Class: java/lang/reflect/VMMethod
98 * Method: getModifiersInternal
101 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_VMMethod_getModifiersInternal(JNIEnv *env, java_lang_reflect_VMMethod *this)
107 LLNI_field_get_cls(this, clazz, c);
108 LLNI_field_get_val(this, slot , slot);
109 m = &(c->methods[slot]);
116 * Class: java/lang/reflect/VMMethod
117 * Method: getReturnType
118 * Signature: ()Ljava/lang/Class;
120 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_VMMethod_getReturnType(JNIEnv *env, java_lang_reflect_VMMethod *this)
127 LLNI_field_get_cls(this, clazz, c);
128 LLNI_field_get_val(this, slot , slot);
129 m = &(c->methods[slot]);
131 result = method_returntype_get(m);
133 return LLNI_classinfo_wrap(result);
138 * Class: java/lang/reflect/VMMethod
139 * Method: getParameterTypes
140 * Signature: ()[Ljava/lang/Class;
142 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterTypes(JNIEnv *env, java_lang_reflect_VMMethod *this)
148 LLNI_field_get_cls(this, clazz, c);
149 LLNI_field_get_val(this, slot , slot);
150 m = &(c->methods[slot]);
152 return method_get_parametertypearray(m);
157 * Class: java/lang/reflect/VMMethod
158 * Method: getExceptionTypes
159 * Signature: ()[Ljava/lang/Class;
161 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getExceptionTypes(JNIEnv *env, java_lang_reflect_VMMethod *this)
167 LLNI_field_get_cls(this, clazz, c);
168 LLNI_field_get_val(this, slot , slot);
169 m = &(c->methods[slot]);
171 return method_get_exceptionarray(m);
176 * Class: java/lang/reflect/VMMethod
178 * Signature: (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
180 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)
184 java_lang_reflect_Method *rm;
189 LLNI_field_get_cls(this, clazz, c);
190 LLNI_field_get_val(this, slot, slot);
192 LLNI_field_get_ref(this, m, rm);
193 LLNI_field_get_val(rm, flag, override);
195 m = &(c->methods[slot]);
197 ro = reflect_method_invoke(m, (java_handle_t *) o, args, override);
199 return (java_lang_Object *) ro;
204 * Class: java/lang/reflect/VMMethod
205 * Method: getSignature
206 * Signature: ()Ljava/lang/String;
208 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_VMMethod_getSignature(JNIEnv *env, java_lang_reflect_VMMethod* this)
215 LLNI_field_get_cls(this, clazz, c);
216 LLNI_field_get_val(this, slot , slot);
217 m = &(c->methods[slot]);
219 if (m->signature == NULL)
222 o = javastring_new(m->signature);
224 /* in error case o is NULL */
226 return (java_lang_String *) o;
229 #if defined(ENABLE_ANNOTATIONS)
231 * Class: java/lang/reflect/VMMethod
232 * Method: getDefaultValue
233 * Signature: ()Ljava/lang/Object;
235 * Parses the annotation default value and returnes it (boxed, if it's a primitive).
237 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_VMMethod_getDefaultValue(JNIEnv *env, struct java_lang_reflect_VMMethod* this)
239 java_handle_bytearray_t *annotationDefault = NULL; /* unparsed annotation default value */
240 static methodinfo *m_parseAnnotationDefault = NULL; /* parser method (will be chached, therefore static) */
241 utf *utf_parseAnnotationDefault = NULL; /* parser method name */
242 utf *utf_desc = NULL; /* parser method descriptor (signature) */
243 sun_reflect_ConstantPool *constantPool = NULL; /* constant pool object to use */
244 java_lang_Class *constantPoolOop = NULL; /* methods declaring class */
245 classinfo *referer = NULL; /* class, which calles the annotation parser */
246 /* (for the parameter 'referer' of vm_call_method()) */
247 java_lang_reflect_Method* rm;
251 exceptions_throw_nullpointerexception();
256 (sun_reflect_ConstantPool*)native_new_and_init(
257 class_sun_reflect_ConstantPool);
259 if (constantPool == NULL) {
264 LLNI_field_get_ref(this, clazz, constantPoolOop);
265 LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
267 /* only resolve the parser method the first time */
268 if (m_parseAnnotationDefault == NULL) {
269 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
270 utf_desc = utf_new_char(
271 "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
272 "Ljava/lang/Object;");
274 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
279 LLNI_class_get(this, referer);
281 m_parseAnnotationDefault = class_resolveclassmethod(
282 class_sun_reflect_annotation_AnnotationParser,
283 utf_parseAnnotationDefault,
288 if (m_parseAnnotationDefault == NULL) {
289 /* method not found */
294 LLNI_field_get_ref(this, m, rm);
295 LLNI_field_get_ref(this, annotationDefault, annotationDefault);
297 h = vm_call_method(m_parseAnnotationDefault, NULL, rm, annotationDefault, constantPool);
299 return (java_lang_Object*) h;
304 * Class: java/lang/reflect/VMMethod
305 * Method: declaredAnnotations
306 * Signature: ()Ljava/util/Map;
308 * Parses the annotations (if they aren't parsed yet) and stores them into
309 * the declaredAnnotations map and return this map.
311 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_VMMethod_declaredAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *this)
313 java_util_Map *declaredAnnotations = NULL; /* parsed annotations */
314 java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
315 java_lang_Class *declaringClass = NULL; /* the constant pool of this class is used */
316 classinfo *referer = NULL; /* class, which calles the annotation parser */
317 /* (for the parameter 'referer' of vm_call_method()) */
319 LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
321 /* are the annotations parsed yet? */
322 if (declaredAnnotations == NULL) {
323 LLNI_field_get_ref(this, annotations, annotations);
324 LLNI_field_get_ref(this, clazz, declaringClass);
325 LLNI_class_get(this, referer);
327 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
329 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
332 return declaredAnnotations;
337 * Class: java/lang/reflect/VMMethod
338 * Method: getParameterAnnotations
339 * Signature: ()[[Ljava/lang/annotation/Annotation;
341 * Parses the parameter annotations and returns them in an 2 dimensional array.
343 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *this)
345 java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations */
346 int32_t slot = -1; /* slot of the method */
347 java_lang_Class *declaringClass = NULL; /* the constant pool of this class is used */
348 classinfo *referer = NULL; /* class, which calles the annotation parser */
349 /* (for the parameter 'referer' of vm_call_method()) */
351 LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
352 LLNI_field_get_val(this, slot, slot);
353 LLNI_field_get_ref(this, clazz, declaringClass);
354 LLNI_class_get(this, referer);
356 return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, referer);
362 * These are local overrides for various environment variables in Emacs.
363 * Please do not remove this and leave it at the end of the file, where
364 * Emacs will automagically detect them.
365 * ---------------------------------------------------------------------
368 * indent-tabs-mode: t