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