3882c4f29a2160f464c4e4ce5bafa3a3b992f20c
[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 8321 2007-08-16 11:37:25Z 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 (java_lang_Class *) 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, struct java_lang_reflect_Method* this)
292 {
293         java_handle_t        *o                   = (java_handle_t*)this;
294         struct java_util_Map *declaredAnnotations = NULL;
295         java_bytearray       *annotations         = NULL;
296         java_lang_Class      *declaringClass      = NULL;
297
298         if (this == NULL) {
299                 exceptions_throw_nullpointerexception();
300                 return NULL;
301         }
302
303         LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
304
305         if (declaredAnnotations == NULL) {
306                 LLNI_field_get_val(this, annotations, annotations);
307                 LLNI_field_get_ref(this, clazz, declaringClass);
308
309                 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, o->vftbl->class);
310
311                 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
312         }
313
314         return declaredAnnotations;
315 }
316
317
318 /*
319  * Class:     java/lang/reflect/Method
320  * Method:    getParameterAnnotations
321  * Signature: ()[[Ljava/lang/annotation/Annotation;
322  */
323 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterAnnotations(JNIEnv *env, struct java_lang_reflect_Method* this)
324 {
325         java_handle_t   *o                    = (java_handle_t*)this;
326         java_bytearray  *parameterAnnotations = NULL;
327         int32_t          slot                 = -1;
328         java_lang_Class *declaringClass       = NULL;
329
330         if (this == NULL) {
331                 exceptions_throw_nullpointerexception();
332                 return NULL;
333         }
334
335         LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
336         LLNI_field_get_val(this, slot, slot);
337         LLNI_field_get_ref(this, clazz, declaringClass);
338
339         return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, o->vftbl->class);
340 }
341 #endif
342
343
344 /*
345  * These are local overrides for various environment variables in Emacs.
346  * Please do not remove this and leave it at the end of the file, where
347  * Emacs will automagically detect them.
348  * ---------------------------------------------------------------------
349  * Local variables:
350  * mode: c
351  * indent-tabs-mode: t
352  * c-basic-offset: 4
353  * tab-width: 4
354  * End:
355  */