* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.cpp
[cacao.git] / src / native / vm / gnuclasspath / java_lang_reflect_VMConstructor.cpp
1 /* src/native/vm/gnuclasspath/java_lang_reflect_VMConstructor.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 <assert.h>
29 #include <stdlib.h>
30
31 #include "native/jni.h"
32 #include "native/llni.h"
33 #include "native/native.h"
34
35 #include "native/include/java_lang_Class.h"
36 #include "native/include/java_lang_Object.h"
37 #include "native/include/java_lang_String.h"
38
39 #if defined(ENABLE_ANNOTATIONS)
40 # include "native/include/java_util_Map.h"
41 # include "native/include/sun_reflect_ConstantPool.h"
42 #endif
43
44 #include "native/include/java_lang_reflect_Constructor.h"
45 // FIXME
46 extern "C" {
47 #include "native/include/java_lang_reflect_VMConstructor.h"
48 }
49
50 #include "native/vm/reflect.h"
51
52 #include "vm/string.hpp"
53
54 #include "vmcore/utf8.h"
55
56
57 /* native methods implemented by this file ************************************/
58
59 static JNINativeMethod methods[] = {
60         { (char*) "getModifiersInternal",    (char*) "()I",                                     (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_getModifiersInternal    },
61         { (char*) "getParameterTypes",       (char*) "()[Ljava/lang/Class;",                    (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_getParameterTypes       },
62         { (char*) "getExceptionTypes",       (char*) "()[Ljava/lang/Class;",                    (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_getExceptionTypes       },
63         { (char*) "construct",               (char*) "([Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_construct               },
64         { (char*) "getSignature",            (char*) "()Ljava/lang/String;",                    (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_getSignature            },
65 #if defined(ENABLE_ANNOTATIONS)
66         { (char*) "declaredAnnotations",     (char*) "()Ljava/util/Map;",                       (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_declaredAnnotations     },
67         { (char*) "getParameterAnnotations", (char*) "()[[Ljava/lang/annotation/Annotation;",   (void*) (uintptr_t) &Java_java_lang_reflect_VMConstructor_getParameterAnnotations },
68 #endif
69 };
70
71
72 /* _Jv_java_lang_reflect_VMConstructor_init ************************************
73
74    Register native functions.
75
76 *******************************************************************************/
77
78 extern "C" {
79 void _Jv_java_lang_reflect_VMConstructor_init(void)
80 {
81         utf *u;
82
83         u = utf_new_char("java/lang/reflect/VMConstructor");
84
85         native_method_register(u, methods, NATIVE_METHODS_COUNT);
86 }
87 }
88
89
90 // Native functions are exported as C functions.
91 extern "C" {
92
93 /*
94  * Class:     java/lang/reflect/VMConstructor
95  * Method:    getModifiersInternal
96  * Signature: ()I
97  */
98 JNIEXPORT int32_t JNICALL Java_java_lang_reflect_VMConstructor_getModifiersInternal(JNIEnv *env, java_lang_reflect_VMConstructor *_this)
99 {
100         classinfo  *c;
101         methodinfo *m;
102         int32_t     slot;
103
104         LLNI_field_get_cls(_this, clazz, c);
105         LLNI_field_get_val(_this, slot,  slot);
106
107         m = &(c->methods[slot]);
108
109         return m->flags;
110 }
111
112
113 /*
114  * Class:     java/lang/reflect/VMConstructor
115  * Method:    getParameterTypes
116  * Signature: ()[Ljava/lang/Class;
117  */
118 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMConstructor_getParameterTypes(JNIEnv *env, java_lang_reflect_VMConstructor *_this)
119 {
120         classinfo  *c;
121         methodinfo *m;
122         int32_t     slot;
123
124         LLNI_field_get_cls(_this, clazz, c);
125         LLNI_field_get_val(_this, slot,  slot);
126
127         m = &(c->methods[slot]);
128
129         return method_get_parametertypearray(m);
130 }
131
132
133 /*
134  * Class:     java/lang/reflect/VMConstructor
135  * Method:    getExceptionTypes
136  * Signature: ()[Ljava/lang/Class;
137  */
138 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMConstructor_getExceptionTypes(JNIEnv *env, java_lang_reflect_VMConstructor *_this)
139 {
140         classinfo  *c;
141         methodinfo *m;
142         int32_t     slot;
143
144         LLNI_field_get_cls(_this, clazz, c);
145         LLNI_field_get_val(_this, slot,  slot);
146
147         m = &(c->methods[slot]);
148
149         return method_get_exceptionarray(m);
150 }
151
152
153 /*
154  * Class:     java/lang/reflect/VMConstructor
155  * Method:    construct
156  * Signature: ([Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
157  */
158 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_VMConstructor_construct(JNIEnv *env, java_lang_reflect_VMConstructor *_this, java_handle_objectarray_t *args)
159 {
160         classinfo                     *c;
161         int32_t                        slot;
162         java_lang_reflect_Constructor *rc;
163         int32_t                        override;
164         methodinfo                    *m;
165         java_handle_t                 *o;
166
167         LLNI_field_get_cls(_this, clazz, c);
168         LLNI_field_get_val(_this, slot,  slot);
169
170         LLNI_field_get_ref(_this, cons,  rc);
171         LLNI_field_get_val(rc,   flag,  override);
172
173         m = &(c->methods[slot]);
174
175         o = reflect_constructor_newinstance(m, args, override);
176
177         return (java_lang_Object *) o;
178 }
179
180
181 /*
182  * Class:     java/lang/reflect/VMConstructor
183  * Method:    getSignature
184  * Signature: ()Ljava/lang/String;
185  */
186 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_VMConstructor_getSignature(JNIEnv *env, java_lang_reflect_VMConstructor *_this)
187 {
188         classinfo     *c;
189         methodinfo    *m;
190         java_handle_t *o;
191         int32_t        slot;
192
193         LLNI_field_get_cls(_this, clazz, c);
194         LLNI_field_get_val(_this, slot,  slot);
195
196         m = &(c->methods[slot]);
197
198         if (m->signature == NULL)
199                 return NULL;
200
201         o = javastring_new(m->signature);
202
203         /* In error case o is NULL. */
204
205         return (java_lang_String *) o;
206 }
207
208
209 #if defined(ENABLE_ANNOTATIONS)
210 /*
211  * Class:     java/lang/reflect/VMConstructor
212  * Method:    declaredAnnotations
213  * Signature: ()Ljava/util/Map;
214  *
215  * Parses the annotations (if they aren't parsed yet) and stores them into
216  * the declaredAnnotations map and return this map.
217  */
218 JNIEXPORT struct java_util_Map* JNICALL Java_java_lang_reflect_VMConstructor_declaredAnnotations(JNIEnv *env, java_lang_reflect_VMConstructor *_this)
219 {
220         java_util_Map           *declaredAnnotations = NULL; /* parsed annotations                                */
221         java_handle_bytearray_t *annotations         = NULL; /* unparsed annotations                              */
222         java_lang_Class         *declaringClass      = NULL; /* the constant pool of this class is used           */
223         classinfo               *referer             = NULL; /* class, which calles the annotation parser         */
224                                                              /* (for the parameter 'referer' of vm_call_method()) */
225
226         LLNI_field_get_ref(_this, declaredAnnotations, declaredAnnotations);
227
228         /* are the annotations parsed yet? */
229         if (declaredAnnotations == NULL) {
230                 LLNI_field_get_ref(_this, annotations, annotations);
231                 LLNI_field_get_ref(_this, clazz, declaringClass);
232                 LLNI_class_get(_this, referer);
233
234                 declaredAnnotations = reflect_get_declaredannotations(annotations, (classinfo*) declaringClass, referer);
235
236                 LLNI_field_set_ref(_this, declaredAnnotations, declaredAnnotations);
237         }
238
239         return declaredAnnotations;
240 }
241
242
243 /*
244  * Class:     java/lang/reflect/VMConstructor
245  * Method:    getParameterAnnotations
246  * Signature: ()[[Ljava/lang/annotation/Annotation;
247  *
248  * Parses the parameter annotations and returns them in an 2 dimensional array.
249  */
250 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_reflect_VMConstructor_getParameterAnnotations(JNIEnv *env, java_lang_reflect_VMConstructor *_this)
251 {
252         java_handle_bytearray_t *parameterAnnotations = NULL; /* unparsed parameter annotations                    */
253         int32_t                  slot                 = -1;   /* slot of the method                                */
254         classinfo               *c;
255         methodinfo              *m;
256         classinfo               *referer              = NULL; /* class, which calles the annotation parser         */
257                                                               /* (for the parameter 'referer' of vm_call_method()) */
258
259         LLNI_field_get_ref(_this, parameterAnnotations, parameterAnnotations);
260         LLNI_field_get_val(_this, slot, slot);
261         LLNI_field_get_cls(_this, clazz, c);
262         m = &(c->methods[slot]);
263
264         LLNI_class_get(_this, referer);
265
266         return reflect_get_parameterannotations((java_handle_t*)parameterAnnotations, m, referer);
267 }
268 #endif
269
270 } // extern "C"
271
272
273 /*
274  * These are local overrides for various environment variables in Emacs.
275  * Please do not remove this and leave it at the end of the file, where
276  * Emacs will automagically detect them.
277  * ---------------------------------------------------------------------
278  * Local variables:
279  * mode: c++
280  * indent-tabs-mode: t
281  * c-basic-offset: 4
282  * tab-width: 4
283  * End:
284  */