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