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