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