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.hpp"
35 #include "native/llni.h"
36 #include "native/native.hpp"
38 #if defined(ENABLE_JNI_HEADERS)
39 # include "native/vm/include/java_lang_reflect_VMMethod.h"
42 #include "native/vm/reflection.hpp"
44 #include "vm/access.hpp"
45 #include "vm/jit/builtin.hpp"
46 #include "vm/class.hpp"
47 #include "vm/exceptions.hpp"
48 #include "vm/global.h"
49 #include "vm/globals.hpp"
50 #include "vm/initialize.hpp"
51 #include "vm/javaobjects.hpp"
52 #include "vm/method.hpp"
53 #include "vm/resolve.hpp"
54 #include "vm/string.hpp"
57 // Native functions are exported as C functions.
61 * Class: java/lang/reflect/VMMethod
62 * Method: getModifiersInternal
65 JNIEXPORT jint JNICALL Java_java_lang_reflect_VMMethod_getModifiersInternal(JNIEnv *env, jobject _this)
67 java_lang_reflect_VMMethod rvmm(_this);
68 methodinfo* m = rvmm.get_method();
74 * Class: java/lang/reflect/VMMethod
75 * Method: getReturnType
76 * Signature: ()Ljava/lang/Class;
78 JNIEXPORT jclass JNICALL Java_java_lang_reflect_VMMethod_getReturnType(JNIEnv *env, jobject _this)
80 java_lang_reflect_VMMethod rvmm(_this);
81 methodinfo* m = rvmm.get_method();
82 classinfo* c = method_returntype_get(m);
84 return (jclass) LLNI_classinfo_wrap(c);
89 * Class: java/lang/reflect/VMMethod
90 * Method: getParameterTypes
91 * Signature: ()[Ljava/lang/Class;
93 JNIEXPORT jobjectArray JNICALL Java_java_lang_reflect_VMMethod_getParameterTypes(JNIEnv *env, jobject _this)
95 java_lang_reflect_VMMethod rvmm(_this);
96 methodinfo* m = rvmm.get_method();
97 java_handle_objectarray_t* oa = method_get_parametertypearray(m);
98 return (jobjectArray) oa;
103 * Class: java/lang/reflect/VMMethod
104 * Method: getExceptionTypes
105 * Signature: ()[Ljava/lang/Class;
107 JNIEXPORT jobjectArray JNICALL Java_java_lang_reflect_VMMethod_getExceptionTypes(JNIEnv *env, jobject _this)
109 java_lang_reflect_VMMethod rvmm(_this);
110 methodinfo* m = rvmm.get_method();
111 java_handle_objectarray_t* oa = method_get_exceptionarray(m);
112 return (jobjectArray) oa;
117 * Class: java/lang/reflect/VMMethod
119 * Signature: (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
121 JNIEXPORT jobject JNICALL Java_java_lang_reflect_VMMethod_invoke(JNIEnv *env, jobject _this, jobject o, jobjectArray args)
123 java_lang_reflect_VMMethod jlrvmm(_this);
124 java_lang_reflect_Method jlrm(jlrvmm.get_m());
126 java_handle_t* result = jlrm.invoke((java_handle_t*) o, (java_handle_objectarray_t*) args);
128 return (jobject) result;
133 * Class: java/lang/reflect/VMMethod
134 * Method: getSignature
135 * Signature: ()Ljava/lang/String;
137 JNIEXPORT jstring JNICALL Java_java_lang_reflect_VMMethod_getSignature(JNIEnv *env, jobject _this)
139 java_lang_reflect_VMMethod rvmm(_this);
140 methodinfo* m = rvmm.get_method();
142 if (m->signature == NULL)
145 java_handle_t* s = javastring_new(m->signature);
147 /* in error case o is NULL */
153 #if defined(ENABLE_ANNOTATIONS)
155 * Class: java/lang/reflect/VMMethod
156 * Method: getDefaultValue
157 * Signature: ()Ljava/lang/Object;
159 * Parses the annotation default value and returnes it (boxed, if it's a primitive).
161 JNIEXPORT jobject JNICALL Java_java_lang_reflect_VMMethod_getDefaultValue(JNIEnv *env, jobject _this)
163 static methodinfo *m_parseAnnotationDefault = NULL; /* parser method (will be chached, therefore static) */
164 utf *utf_parseAnnotationDefault = NULL; /* parser method name */
165 utf *utf_desc = NULL; /* parser method descriptor (signature) */
168 exceptions_throw_nullpointerexception();
172 // TODO Use a constructor.
173 java_handle_t* h = native_new_and_init(class_sun_reflect_ConstantPool);
178 sun_reflect_ConstantPool cp(h);
180 java_lang_reflect_VMMethod rvmm(_this);
181 classinfo* declaringClass = rvmm.get_clazz();
182 cp.set_constantPoolOop(declaringClass);
184 /* only resolve the parser method the first time */
185 if (m_parseAnnotationDefault == NULL) {
186 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
187 utf_desc = utf_new_char(
188 "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
189 "Ljava/lang/Object;");
191 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
196 classinfo *referer = rvmm.get_Class();
198 m_parseAnnotationDefault = class_resolveclassmethod(
199 class_sun_reflect_annotation_AnnotationParser,
200 utf_parseAnnotationDefault,
205 if (m_parseAnnotationDefault == NULL) {
206 /* method not found */
211 java_lang_reflect_Method rm(rvmm.get_m());
212 java_handle_bytearray_t* annotationDefault = rvmm.get_annotationDefault();
214 java_handle_t* result = vm_call_method(m_parseAnnotationDefault, NULL, rm.get_handle(), annotationDefault, cp.get_handle());
216 return (jobject) result;
221 * Class: java/lang/reflect/VMMethod
222 * Method: declaredAnnotations
223 * Signature: ()Ljava/util/Map;
225 JNIEXPORT jobject JNICALL Java_java_lang_reflect_VMMethod_declaredAnnotations(JNIEnv *env, jobject _this)
227 java_lang_reflect_VMMethod rvmm(_this);
228 java_handle_t* declaredAnnotations = rvmm.get_declaredAnnotations();
230 // Are the annotations parsed yet?
231 if (declaredAnnotations == NULL) {
232 java_handle_bytearray_t* annotations = rvmm.get_annotations();
233 classinfo* declaringClass = rvmm.get_clazz();
234 classinfo* referer = rvmm.get_Class();
236 declaredAnnotations = Reflection::get_declaredannotations(annotations, declaringClass, referer);
238 rvmm.set_declaredAnnotations(declaredAnnotations);
241 return (jobject) declaredAnnotations;
246 * Class: java/lang/reflect/VMMethod
247 * Method: getParameterAnnotations
248 * Signature: ()[[Ljava/lang/annotation/Annotation;
250 JNIEXPORT jobjectArray JNICALL Java_java_lang_reflect_VMMethod_getParameterAnnotations(JNIEnv *env, jobject _this)
252 java_lang_reflect_VMMethod rvmm(_this);
253 java_handle_bytearray_t* parameterAnnotations = rvmm.get_parameterAnnotations();
254 methodinfo* m = rvmm.get_method();
255 classinfo* referer = rvmm.get_Class();
257 java_handle_objectarray_t* oa = Reflection::get_parameterannotations(parameterAnnotations, m, referer);
258 return (jobjectArray) oa;
265 /* native methods implemented by this file ************************************/
267 static JNINativeMethod methods[] = {
268 { (char*) "getModifiersInternal", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getModifiersInternal },
269 { (char*) "getReturnType", (char*) "()Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getReturnType },
270 { (char*) "getParameterTypes", (char*) "()[Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterTypes },
271 { (char*) "getExceptionTypes", (char*) "()[Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getExceptionTypes },
272 { (char*) "invoke", (char*) "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_invoke },
273 { (char*) "getSignature", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getSignature },
274 #if defined(ENABLE_ANNOTATIONS)
275 { (char*) "getDefaultValue", (char*) "()Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getDefaultValue },
276 { (char*) "declaredAnnotations", (char*) "()Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_declaredAnnotations },
277 { (char*) "getParameterAnnotations", (char*) "()[[Ljava/lang/annotation/Annotation;", (void*) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterAnnotations },
282 /* _Jv_java_lang_reflect_VMMethod_init *****************************************
284 Register native functions.
286 *******************************************************************************/
288 void _Jv_java_lang_reflect_VMMethod_init(void)
290 utf* u = utf_new_char("java/lang/reflect/VMMethod");
292 NativeMethods& nm = VM::get_current()->get_nativemethods();
293 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
298 * These are local overrides for various environment variables in Emacs.
299 * Please do not remove this and leave it at the end of the file, where
300 * Emacs will automagically detect them.
301 * ---------------------------------------------------------------------
304 * indent-tabs-mode: t