fc916c985a0e5c902591a08e3bae463a987872bc
[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 8393 2007-08-22 01:10:09Z panzi $
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         classinfo                *referer         = NULL;
237
238         if (this == NULL) {
239                 exceptions_throw_nullpointerexception();
240                 return NULL;
241         }
242
243         constantPool = 
244                 (sun_reflect_ConstantPool*)native_new_and_init(
245                         class_sun_reflect_ConstantPool);
246         
247         if(constantPool == NULL) {
248                 /* out of memory */
249                 return NULL;
250         }
251
252         LLNI_field_get_ref(this, clazz, constantPoolOop);
253         LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
254
255         /* only resolve the method the first time */
256         if (m_parseAnnotationDefault == NULL) {
257                 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
258                 utf_desc = utf_new_char(
259                         "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
260                         "Ljava/lang/Object;");
261
262                 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
263                         /* out of memory */
264                         return NULL;
265                 }
266
267                 LLNI_class_get(this, referer);
268
269                 m_parseAnnotationDefault = class_resolveclassmethod(
270                         class_sun_reflect_annotation_AnnotationParser,
271                         utf_parseAnnotationDefault,
272                         utf_desc,
273                         referer,
274                         true);
275
276                 if (m_parseAnnotationDefault == NULL) {
277                         /* method not found */
278                         return NULL;
279                 }
280         }
281
282         return (java_lang_Object*)vm_call_method(
283                 m_parseAnnotationDefault, NULL,
284                 this, this->annotationDefault, constantPool);
285 }
286
287
288 /*
289  * Class:     java/lang/reflect/Method
290  * Method:    declaredAnnotations
291  * Signature: ()Ljava/util/Map;
292  */
293 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_Method_declaredAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
294 {
295         java_handle_t           *o                   = (java_handle_t*)this;
296         struct java_util_Map    *declaredAnnotations = NULL;
297         java_handle_bytearray_t *annotations         = NULL;
298         java_lang_Class         *declaringClass      = NULL;
299         classinfo               *referer             = NULL;
300
301         LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
302
303         if (declaredAnnotations == NULL) {
304                 LLNI_field_get_val(this, annotations, annotations);
305                 LLNI_field_get_ref(this, clazz, declaringClass);
306                 LLNI_class_get(this, referer);
307
308                 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
309
310                 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
311         }
312
313         return declaredAnnotations;
314 }
315
316
317 /*
318  * Class:     java/lang/reflect/Method
319  * Method:    getParameterAnnotations
320  * Signature: ()[[Ljava/lang/annotation/Annotation;
321  */
322 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_Method_getParameterAnnotations(JNIEnv *env, java_lang_reflect_Method *this)
323 {
324         java_handle_t           *o                    = (java_handle_t*)this;
325         java_handle_bytearray_t *parameterAnnotations = NULL;
326         int32_t                  slot                 = -1;
327         java_lang_Class         *declaringClass       = NULL;
328         classinfo               *referer              = NULL;
329
330         LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
331         LLNI_field_get_val(this, slot, slot);
332         LLNI_field_get_ref(this, clazz, declaringClass);
333         LLNI_class_get(this, referer);
334
335         return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, referer);
336 }
337 #endif
338
339
340 /*
341  * These are local overrides for various environment variables in Emacs.
342  * Please do not remove this and leave it at the end of the file, where
343  * Emacs will automagically detect them.
344  * ---------------------------------------------------------------------
345  * Local variables:
346  * mode: c
347  * indent-tabs-mode: t
348  * c-basic-offset: 4
349  * tab-width: 4
350  * End:
351  */