* src/vmcore/class.h (classinfo): Removed ENABLE_JAVASE for
[cacao.git] / src / native / vm / gnu / java_lang_reflect_Method.c
1 /* src/native/vm/gnu/java_lang_reflect_Method.c
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: java_lang_reflect_Method.c 8343 2007-08-17 21:39:32Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #if defined(ENABLE_ANNOTATIONS)
35 #include "vm/vm.h"
36 #endif
37
38 #include "vm/types.h"
39
40 #include "native/jni.h"
41 #include "native/llni.h"
42 #include "native/native.h"
43
44 #include "native/include/java_lang_Object.h"
45 #include "native/include/java_lang_Class.h"
46 #include "native/include/java_lang_String.h"
47
48 #if defined(ENABLE_ANNOTATIONS)
49 #include "native/include/sun_reflect_ConstantPool.h"
50 #include "native/vm/reflect.h"
51 #endif
52
53 #include "native/include/java_lang_reflect_Method.h"
54
55 #include "native/vm/java_lang_reflect_Method.h"
56
57 #include "vm/access.h"
58 #include "vm/global.h"
59 #include "vm/builtin.h"
60 #include "vm/exceptions.h"
61 #include "vm/initialize.h"
62 #include "vm/resolve.h"
63 #include "vm/stringlocal.h"
64
65 #include "vmcore/method.h"
66
67
68 /* native methods implemented by this file ************************************/
69
70 static JNINativeMethod methods[] = {
71         { "getModifiersInternal",    "()I",                                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getModifiersInternal    },
72         { "getReturnType",           "()Ljava/lang/Class;",                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getReturnType           },
73         { "getParameterTypes",       "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterTypes       },
74         { "getExceptionTypes",       "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getExceptionTypes       },
75         { "invokeNative",            "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;", (void *) (ptrint) &Java_java_lang_reflect_Method_invokeNative            },
76         { "getSignature",            "()Ljava/lang/String;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getSignature            },
77 #if defined(ENABLE_ANNOTATIONS)
78         { "getDefaultValue",         "()Ljava/lang/Object;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getDefaultValue         },
79         { "declaredAnnotations",     "()Ljava/util/Map;",                                                           (void *) (ptrint) &Java_java_lang_reflect_Method_declaredAnnotations     },
80         { "getParameterAnnotations", "()[[Ljava/lang/annotation/Annotation;",                                       (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterAnnotations },
81 #endif
82 };
83
84
85 /* _Jv_java_lang_reflect_Method_init *******************************************
86
87    Register native functions.
88
89 *******************************************************************************/
90
91 void _Jv_java_lang_reflect_Method_init(void)
92 {
93         utf *u;
94
95         u = utf_new_char("java/lang/reflect/Method");
96
97         native_method_register(u, methods, NATIVE_METHODS_COUNT);
98 }
99
100
101 /*
102  * Class:     java/lang/reflect/Method
103  * Method:    getModifiersInternal
104  * Signature: ()I
105  */
106 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiersInternal(JNIEnv *env, java_lang_reflect_Method *this)
107 {
108         classinfo  *c;
109         methodinfo *m;
110         int32_t     slot;
111
112         LLNI_field_get_cls(this, clazz, c);
113         LLNI_field_get_val(this, slot , slot);
114         m = &(c->methods[slot]);
115
116         return m->flags;
117 }
118
119
120 /*
121  * Class:     java/lang/reflect/Method
122  * Method:    getReturnType
123  * Signature: ()Ljava/lang/Class;
124  */
125 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(JNIEnv *env, java_lang_reflect_Method *this)
126 {
127         classinfo  *c;
128         methodinfo *m;
129         classinfo  *result;
130         int32_t     slot;
131
132         LLNI_field_get_cls(this, clazz, c);
133         LLNI_field_get_val(this, slot , slot);
134         m = &(c->methods[slot]);
135
136         result = method_returntype_get(m);
137
138         return LLNI_classinfo_wrap(result);
139 }
140
141
142 /*
143  * Class:     java/lang/reflect/Method
144  * Method:    getParameterTypes
145  * Signature: ()[Ljava/lang/Class;
146  */
147 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
148 {
149         classinfo  *c;
150         methodinfo *m;
151         int32_t     slot;
152
153         LLNI_field_get_cls(this, clazz, c);
154         LLNI_field_get_val(this, slot , slot);
155         m = &(c->methods[slot]);
156
157         return method_get_parametertypearray(m);
158 }
159
160
161 /*
162  * Class:     java/lang/reflect/Method
163  * Method:    getExceptionTypes
164  * Signature: ()[Ljava/lang/Class;
165  */
166 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
167 {
168         classinfo  *c;
169         methodinfo *m;
170         int32_t     slot;
171
172         LLNI_field_get_cls(this, clazz, c);
173         LLNI_field_get_val(this, slot , slot);
174         m = &(c->methods[slot]);
175
176         return method_get_exceptionarray(m);
177 }
178
179
180 /*
181  * Class:     java/lang/reflect/Method
182  * Method:    invokeNative
183  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
184  */
185 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 {
187         /* just to be sure */
188
189         assert(LLNI_field_direct(this, clazz) == clazz);
190         assert(LLNI_field_direct(this, slot)  == slot);
191
192         return _Jv_java_lang_reflect_Method_invoke(this, o, args);
193 }
194
195
196 /*
197  * Class:     java/lang/reflect/Method
198  * Method:    getSignature
199  * Signature: ()Ljava/lang/String;
200  */
201 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Method_getSignature(JNIEnv *env, java_lang_reflect_Method* this)
202 {
203         classinfo     *c;
204         methodinfo    *m;
205         java_handle_t *o;
206         int32_t        slot;
207
208         LLNI_field_get_cls(this, clazz, c);
209         LLNI_field_get_val(this, slot , slot);
210         m = &(c->methods[slot]);
211
212         if (m->signature == NULL)
213                 return NULL;
214
215         o = javastring_new(m->signature);
216
217         /* in error case o is NULL */
218
219         return (java_lang_String *) o;
220 }
221
222 #if defined(ENABLE_ANNOTATIONS)
223 /*
224  * Class:     java/lang/reflect/Method
225  * Method:    getDefaultValue
226  * Signature: ()Ljava/lang/Object;
227  */
228 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_Method_getDefaultValue(JNIEnv *env, struct java_lang_reflect_Method* this)
229 {
230         static methodinfo        *m_parseAnnotationDefault   = NULL;
231         utf                      *utf_parseAnnotationDefault = NULL;
232         utf                      *utf_desc        = NULL;
233         sun_reflect_ConstantPool *constantPool    = NULL;
234         java_handle_t            *o               = (java_handle_t*)this;
235         java_lang_Class          *constantPoolOop = NULL;
236
237         if (this == NULL) {
238                 exceptions_throw_nullpointerexception();
239                 return NULL;
240         }
241
242         constantPool = 
243                 (sun_reflect_ConstantPool*)native_new_and_init(
244                         class_sun_reflect_ConstantPool);
245         
246         if(constantPool == NULL) {
247                 /* out of memory */
248                 return NULL;
249         }
250
251         LLNI_field_get_ref(this, clazz, constantPoolOop);
252         LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
253
254         /* only resolve the method the first time */
255         if (m_parseAnnotationDefault == NULL) {
256                 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
257                 utf_desc = utf_new_char(
258                         "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
259                         "Ljava/lang/Object;");
260
261                 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
262                         /* out of memory */
263                         return NULL;
264                 }
265
266                 m_parseAnnotationDefault = class_resolveclassmethod(
267                         class_sun_reflect_annotation_AnnotationParser,
268                         utf_parseAnnotationDefault,
269                         utf_desc,
270                         o->vftbl->class,
271                         true);
272
273                 if (m_parseAnnotationDefault == NULL)
274                 {
275                         /* method not found */
276                         return NULL;
277                 }
278         }
279
280         return (java_lang_Object*)vm_call_method(
281                 m_parseAnnotationDefault, NULL,
282                 this, this->annotationDefault, constantPool);
283 }
284
285
286 /*
287  * Class:     java/lang/reflect/Method
288  * Method:    declaredAnnotations
289  * Signature: ()Ljava/util/Map;
290  */
291 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Method_declaredAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
292 {
293         java_handle_t           *o                   = (java_handle_t*)this;
294         struct java_util_Map    *declaredAnnotations = NULL;
295         java_handle_bytearray_t *annotations         = NULL;
296         java_lang_Class         *declaringClass      = NULL;
297
298         LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
299
300         if (declaredAnnotations == NULL) {
301                 LLNI_field_get_val(this, annotations, annotations);
302                 LLNI_field_get_ref(this, clazz, declaringClass);
303
304                 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, o->vftbl->class);
305
306                 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
307         }
308
309         return declaredAnnotations;
310 }
311
312
313 /*
314  * Class:     java/lang/reflect/Method
315  * Method:    getParameterAnnotations
316  * Signature: ()[[Ljava/lang/annotation/Annotation;
317  */
318 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
319 {
320         java_handle_t           *o                    = (java_handle_t*)this;
321         java_handle_bytearray_t *parameterAnnotations = NULL;
322         int32_t                  slot                 = -1;
323         java_lang_Class         *declaringClass       = NULL;
324
325         LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
326         LLNI_field_get_val(this, slot, slot);
327         LLNI_field_get_ref(this, clazz, declaringClass);
328
329         return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, o->vftbl->class);
330 }
331 #endif
332
333
334 /*
335  * These are local overrides for various environment variables in Emacs.
336  * Please do not remove this and leave it at the end of the file, where
337  * Emacs will automagically detect them.
338  * ---------------------------------------------------------------------
339  * Local variables:
340  * mode: c
341  * indent-tabs-mode: t
342  * c-basic-offset: 4
343  * tab-width: 4
344  * End:
345  */