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