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