* src/vmcore/linker.c (build_display): Removed superfluous recursion; return
[cacao.git] / src / native / vm / gnuclasspath / java_lang_reflect_VMMethod.c
1 /* src/native/vm/gnu/java_lang_reflect_VMMethod.c
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #if defined(ENABLE_ANNOTATIONS)
31 #include "vm/vm.h"
32 #endif
33
34 #include "native/jni.h"
35 #include "native/llni.h"
36 #include "native/native.h"
37
38 #include "native/include/java_lang_Object.h"
39 #include "native/include/java_lang_Class.h"
40 #include "native/include/java_lang_String.h"
41
42 #if defined(ENABLE_ANNOTATIONS)
43 # include "native/include/java_util_Map.h"
44 # include "native/include/sun_reflect_ConstantPool.h"
45 #endif
46
47 #include "native/include/java_lang_reflect_Method.h"
48 #include "native/include/java_lang_reflect_VMMethod.h"
49
50 #include "native/vm/reflect.h"
51
52 #include "vm/access.h"
53 #include "vm/global.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/initialize.h"
57 #include "vm/resolve.h"
58 #include "vm/stringlocal.h"
59
60 #include "vmcore/method.h"
61
62
63 /* native methods implemented by this file ************************************/
64
65 static JNINativeMethod methods[] = {
66         { "getModifiersInternal",    "()I",                                                       (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getModifiersInternal    },
67         { "getReturnType",           "()Ljava/lang/Class;",                                       (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getReturnType           },
68         { "getParameterTypes",       "()[Ljava/lang/Class;",                                      (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterTypes       },
69         { "getExceptionTypes",       "()[Ljava/lang/Class;",                                      (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getExceptionTypes       },
70         { "invoke",                  "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_invoke                  },
71         { "getSignature",            "()Ljava/lang/String;",                                      (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getSignature            },
72 #if defined(ENABLE_ANNOTATIONS)
73         { "getDefaultValue",         "()Ljava/lang/Object;",                                      (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getDefaultValue         },
74         { "declaredAnnotations",     "()Ljava/util/Map;",                                         (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_declaredAnnotations     },
75         { "getParameterAnnotations", "()[[Ljava/lang/annotation/Annotation;",                     (void *) (uintptr_t) &Java_java_lang_reflect_VMMethod_getParameterAnnotations },
76 #endif
77 };
78
79
80 /* _Jv_java_lang_reflect_VMMethod_init *******************************************
81
82    Register native functions.
83
84 *******************************************************************************/
85
86 void _Jv_java_lang_reflect_VMMethod_init(void)
87 {
88         utf *u;
89
90         u = utf_new_char("java/lang/reflect/VMMethod");
91
92         native_method_register(u, methods, NATIVE_METHODS_COUNT);
93 }
94
95
96 /*
97  * Class:     java/lang/reflect/VMMethod
98  * Method:    getModifiersInternal
99  * Signature: ()I
100  */
101 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_VMMethod_getModifiersInternal(JNIEnv *env, java_lang_reflect_VMMethod *this)
102 {
103         classinfo  *c;
104         methodinfo *m;
105         int32_t     slot;
106
107         LLNI_field_get_cls(this, clazz, c);
108         LLNI_field_get_val(this, slot , slot);
109         m = &(c->methods[slot]);
110
111         return m->flags;
112 }
113
114
115 /*
116  * Class:     java/lang/reflect/VMMethod
117  * Method:    getReturnType
118  * Signature: ()Ljava/lang/Class;
119  */
120 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_VMMethod_getReturnType(JNIEnv *env, java_lang_reflect_VMMethod *this)
121 {
122         classinfo  *c;
123         methodinfo *m;
124         classinfo  *result;
125         int32_t     slot;
126
127         LLNI_field_get_cls(this, clazz, c);
128         LLNI_field_get_val(this, slot , slot);
129         m = &(c->methods[slot]);
130
131         result = method_returntype_get(m);
132
133         return LLNI_classinfo_wrap(result);
134 }
135
136
137 /*
138  * Class:     java/lang/reflect/VMMethod
139  * Method:    getParameterTypes
140  * Signature: ()[Ljava/lang/Class;
141  */
142 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterTypes(JNIEnv *env, java_lang_reflect_VMMethod *this)
143 {
144         classinfo  *c;
145         methodinfo *m;
146         int32_t     slot;
147
148         LLNI_field_get_cls(this, clazz, c);
149         LLNI_field_get_val(this, slot , slot);
150         m = &(c->methods[slot]);
151
152         return method_get_parametertypearray(m);
153 }
154
155
156 /*
157  * Class:     java/lang/reflect/VMMethod
158  * Method:    getExceptionTypes
159  * Signature: ()[Ljava/lang/Class;
160  */
161 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getExceptionTypes(JNIEnv *env, java_lang_reflect_VMMethod *this)
162 {
163         classinfo  *c;
164         methodinfo *m;
165         int32_t     slot;
166
167         LLNI_field_get_cls(this, clazz, c);
168         LLNI_field_get_val(this, slot , slot);
169         m = &(c->methods[slot]);
170
171         return method_get_exceptionarray(m);
172 }
173
174
175 /*
176  * Class:     java/lang/reflect/VMMethod
177  * Method:    invoke
178  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
179  */
180 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_VMMethod_invoke(JNIEnv *env, java_lang_reflect_VMMethod *this, java_lang_Object *o, java_handle_objectarray_t *args)
181 {
182         classinfo                *c;
183         int32_t                   slot;
184         java_lang_reflect_Method *rm;
185         int32_t                   override;
186         methodinfo               *m;
187         java_handle_t            *ro;
188
189         LLNI_field_get_cls(this, clazz, c);
190         LLNI_field_get_val(this, slot,  slot);
191
192         LLNI_field_get_ref(this, m,     rm);
193         LLNI_field_get_val(rm,   flag,  override);
194
195         m = &(c->methods[slot]);
196
197         ro = reflect_method_invoke(m, (java_handle_t *) o, args, override);
198
199         return (java_lang_Object *) ro;
200 }
201
202
203 /*
204  * Class:     java/lang/reflect/VMMethod
205  * Method:    getSignature
206  * Signature: ()Ljava/lang/String;
207  */
208 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_VMMethod_getSignature(JNIEnv *env, java_lang_reflect_VMMethod* this)
209 {
210         classinfo     *c;
211         methodinfo    *m;
212         java_handle_t *o;
213         int32_t        slot;
214
215         LLNI_field_get_cls(this, clazz, c);
216         LLNI_field_get_val(this, slot , slot);
217         m = &(c->methods[slot]);
218
219         if (m->signature == NULL)
220                 return NULL;
221
222         o = javastring_new(m->signature);
223
224         /* in error case o is NULL */
225
226         return (java_lang_String *) o;
227 }
228
229 #if defined(ENABLE_ANNOTATIONS)
230 /*
231  * Class:     java/lang/reflect/VMMethod
232  * Method:    getDefaultValue
233  * Signature: ()Ljava/lang/Object;
234  *
235  * Parses the annotation default value and returnes it (boxed, if it's a primitive).
236  */
237 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_VMMethod_getDefaultValue(JNIEnv *env, struct java_lang_reflect_VMMethod* this)
238 {
239         java_handle_bytearray_t  *annotationDefault          = NULL; /* unparsed annotation default value                 */
240         static methodinfo        *m_parseAnnotationDefault   = NULL; /* parser method (will be chached, therefore static) */
241         utf                      *utf_parseAnnotationDefault = NULL; /* parser method name                                */
242         utf                      *utf_desc        = NULL;            /* parser method descriptor (signature)              */
243         sun_reflect_ConstantPool *constantPool    = NULL;            /* constant pool object to use                       */
244         java_lang_Class          *constantPoolOop = NULL;            /* methods declaring class                           */
245         classinfo                *referer         = NULL;            /* class, which calles the annotation parser         */
246                                                                      /* (for the parameter 'referer' of vm_call_method()) */
247         java_lang_reflect_Method* rm;
248         java_handle_t*            h;
249
250         if (this == NULL) {
251                 exceptions_throw_nullpointerexception();
252                 return NULL;
253         }
254
255         constantPool = 
256                 (sun_reflect_ConstantPool*)native_new_and_init(
257                         class_sun_reflect_ConstantPool);
258         
259         if (constantPool == NULL) {
260                 /* out of memory */
261                 return NULL;
262         }
263
264         LLNI_field_get_ref(this, clazz, constantPoolOop);
265         LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
266
267         /* only resolve the parser method the first time */
268         if (m_parseAnnotationDefault == NULL) {
269                 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
270                 utf_desc = utf_new_char(
271                         "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
272                         "Ljava/lang/Object;");
273
274                 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
275                         /* out of memory */
276                         return NULL;
277                 }
278
279                 LLNI_class_get(this, referer);
280
281                 m_parseAnnotationDefault = class_resolveclassmethod(
282                         class_sun_reflect_annotation_AnnotationParser,
283                         utf_parseAnnotationDefault,
284                         utf_desc,
285                         referer,
286                         true);
287
288                 if (m_parseAnnotationDefault == NULL) {
289                         /* method not found */
290                         return NULL;
291                 }
292         }
293
294         LLNI_field_get_ref(this, m,                 rm);
295         LLNI_field_get_ref(this, annotationDefault, annotationDefault);
296
297         h = vm_call_method(m_parseAnnotationDefault, NULL, rm, annotationDefault, constantPool);
298
299         return (java_lang_Object*) h;
300 }
301
302
303 /*
304  * Class:     java/lang/reflect/VMMethod
305  * Method:    declaredAnnotations
306  * Signature: ()Ljava/util/Map;
307  *
308  * Parses the annotations (if they aren't parsed yet) and stores them into
309  * the declaredAnnotations map and return this map.
310  */
311 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_VMMethod_declaredAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *this)
312 {
313         java_util_Map           *declaredAnnotations = NULL; /* parsed annotations                                */
314         java_handle_bytearray_t *annotations         = NULL; /* unparsed annotations                              */
315         java_lang_Class         *declaringClass      = NULL; /* the constant pool of this class is used           */
316         classinfo               *referer             = NULL; /* class, which calles the annotation parser         */
317                                                              /* (for the parameter 'referer' of vm_call_method()) */
318
319         LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
320
321         /* are the annotations parsed yet? */
322         if (declaredAnnotations == NULL) {
323                 LLNI_field_get_ref(this, annotations, annotations);
324                 LLNI_field_get_ref(this, clazz, declaringClass);
325                 LLNI_class_get(this, referer);
326
327                 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
328
329                 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
330         }
331
332         return declaredAnnotations;
333 }
334
335
336 /*
337  * Class:     java/lang/reflect/VMMethod
338  * Method:    getParameterAnnotations
339  * Signature: ()[[Ljava/lang/annotation/Annotation;
340  *
341  * Parses the parameter annotations and returns them in an 2 dimensional array.
342  */
343 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *this)
344 {
345         java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations                    */
346         int32_t                  slot                 = -1;   /* slot of the method                                */
347         java_lang_Class         *declaringClass       = NULL; /* the constant pool of this class is used           */
348         classinfo               *referer              = NULL; /* class, which calles the annotation parser         */
349                                                               /* (for the parameter 'referer' of vm_call_method()) */
350
351         LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
352         LLNI_field_get_val(this, slot, slot);
353         LLNI_field_get_ref(this, clazz, declaringClass);
354         LLNI_class_get(this, referer);
355
356         return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, referer);
357 }
358 #endif
359
360
361 /*
362  * These are local overrides for various environment variables in Emacs.
363  * Please do not remove this and leave it at the end of the file, where
364  * Emacs will automagically detect them.
365  * ---------------------------------------------------------------------
366  * Local variables:
367  * mode: c
368  * indent-tabs-mode: t
369  * c-basic-offset: 4
370  * tab-width: 4
371  * End:
372  */