* src/native/vm/gnu/gnu_classpath_jdwp_VMFrame.c: Use stdint-types.
[cacao.git] / src / native / vm / gnu / 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
248         if (this == NULL) {
249                 exceptions_throw_nullpointerexception();
250                 return NULL;
251         }
252
253         constantPool = 
254                 (sun_reflect_ConstantPool*)native_new_and_init(
255                         class_sun_reflect_ConstantPool);
256         
257         if (constantPool == NULL) {
258                 /* out of memory */
259                 return NULL;
260         }
261
262         LLNI_field_get_ref(this, clazz, constantPoolOop);
263         LLNI_field_set_ref(constantPool, constantPoolOop, (java_lang_Object*)constantPoolOop);
264
265         /* only resolve the parser method the first time */
266         if (m_parseAnnotationDefault == NULL) {
267                 utf_parseAnnotationDefault = utf_new_char("parseAnnotationDefault");
268                 utf_desc = utf_new_char(
269                         "(Ljava/lang/reflect/Method;[BLsun/reflect/ConstantPool;)"
270                         "Ljava/lang/Object;");
271
272                 if (utf_parseAnnotationDefault == NULL || utf_desc == NULL) {
273                         /* out of memory */
274                         return NULL;
275                 }
276
277                 LLNI_class_get(this, referer);
278
279                 m_parseAnnotationDefault = class_resolveclassmethod(
280                         class_sun_reflect_annotation_AnnotationParser,
281                         utf_parseAnnotationDefault,
282                         utf_desc,
283                         referer,
284                         true);
285
286                 if (m_parseAnnotationDefault == NULL) {
287                         /* method not found */
288                         return NULL;
289                 }
290         }
291
292         LLNI_field_get_ref(this, annotationDefault, annotationDefault);
293
294         return (java_lang_Object*)vm_call_method(
295                 m_parseAnnotationDefault, NULL,
296                 this, annotationDefault, constantPool);
297 }
298
299
300 /*
301  * Class:     java/lang/reflect/VMMethod
302  * Method:    declaredAnnotations
303  * Signature: ()Ljava/util/Map;
304  *
305  * Parses the annotations (if they aren't parsed yet) and stores them into
306  * the declaredAnnotations map and return this map.
307  */
308 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_VMMethod_declaredAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *this)
309 {
310         java_util_Map           *declaredAnnotations = NULL; /* parsed annotations                                */
311         java_handle_bytearray_t *annotations         = NULL; /* unparsed annotations                              */
312         java_lang_Class         *declaringClass      = NULL; /* the constant pool of this class is used           */
313         classinfo               *referer             = NULL; /* class, which calles the annotation parser         */
314                                                              /* (for the parameter 'referer' of vm_call_method()) */
315
316         LLNI_field_get_ref(this, declaredAnnotations, declaredAnnotations);
317
318         /* are the annotations parsed yet? */
319         if (declaredAnnotations == NULL) {
320                 LLNI_field_get_ref(this, annotations, annotations);
321                 LLNI_field_get_ref(this, clazz, declaringClass);
322                 LLNI_class_get(this, referer);
323
324                 declaredAnnotations = reflect_get_declaredannotatios(annotations, declaringClass, referer);
325
326                 LLNI_field_set_ref(this, declaredAnnotations, declaredAnnotations);
327         }
328
329         return declaredAnnotations;
330 }
331
332
333 /*
334  * Class:     java/lang/reflect/VMMethod
335  * Method:    getParameterAnnotations
336  * Signature: ()[[Ljava/lang/annotation/Annotation;
337  *
338  * Parses the parameter annotations and returns them in an 2 dimensional array.
339  */
340 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMMethod_getParameterAnnotations(JNIEnv *env, java_lang_reflect_VMMethod *this)
341 {
342         java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations                    */
343         int32_t                  slot                 = -1;   /* slot of the method                                */
344         java_lang_Class         *declaringClass       = NULL; /* the constant pool of this class is used           */
345         classinfo               *referer              = NULL; /* class, which calles the annotation parser         */
346                                                               /* (for the parameter 'referer' of vm_call_method()) */
347
348         LLNI_field_get_ref(this, parameterAnnotations, parameterAnnotations);
349         LLNI_field_get_val(this, slot, slot);
350         LLNI_field_get_ref(this, clazz, declaringClass);
351         LLNI_class_get(this, referer);
352
353         return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, slot, declaringClass, referer);
354 }
355 #endif
356
357
358 /*
359  * These are local overrides for various environment variables in Emacs.
360  * Please do not remove this and leave it at the end of the file, where
361  * Emacs will automagically detect them.
362  * ---------------------------------------------------------------------
363  * Local variables:
364  * mode: c
365  * indent-tabs-mode: t
366  * c-basic-offset: 4
367  * tab-width: 4
368  * End:
369  */