8ec93342cd0dbb872d9e3b262df0da3b3de216e3
[cacao.git] / src / native / vm / gnuclasspath / sun_reflect_ConstantPool.cpp
1 /* src/native/vm/gnuclasspath/sun_reflect_ConstantPool.cpp
2
3    Copyright (C) 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
27    XXX: The Methods in this file are very redundant to thouse in
28         src/native/vm/sun/jvm.c Unless someone has a good idea how to cover
29         such redundancy I leave it how it is.
30
31   The ConstantPool class represents an interface to the constant pool of a
32   class and is used by the annotations parser (sun.reflect.annotation.
33   AnnotationParser) to get the values of the constants refered by the
34   annotations.
35
36 *******************************************************************************/
37
38 #include "config.h"
39
40 #include <assert.h>
41 #include <stdint.h>
42
43 #include "mm/memory.h"
44
45 #include "native/jni.h"
46 #include "native/llni.h"
47 #include "native/native.h"
48
49 // FIXME
50 //#include "native/include/sun_reflect_ConstantPool.h"
51
52 #include "native/vm/reflection.hpp"
53
54 #include "toolbox/logging.h"
55
56 #include "vm/vm.hpp"
57 #include "vm/exceptions.hpp"
58 #include "vm/resolve.h"
59 #include "vm/string.hpp"
60
61 #include "vmcore/class.h"
62 #include "vmcore/javaobjects.hpp"
63 #include "vmcore/utf8.h"
64
65
66 extern "C" {
67
68 /*
69  * Class:     sun/reflect/ConstantPool
70  * Method:    getSize0
71  * Signature: (Ljava/lang/Object;)I
72  */
73 JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, jobject _this, jobject jcpool)
74 {
75         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
76         return cls->cpcount;
77 }
78
79
80 /*
81  * Class:     sun/reflect/ConstantPool
82  * Method:    getClassAt0
83  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
84  */
85 JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
86 {
87         constant_classref *ref;
88         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
89
90         ref = (constant_classref*)class_getconstant(
91                 cls, index, CONSTANT_Class);
92
93         if (ref == NULL) {
94                 exceptions_throw_illegalargumentexception();
95                 return NULL;
96         }
97
98         return (jclass) LLNI_classinfo_wrap(resolve_classref_eager(ref));
99 }
100
101
102 /*
103  * Class:     sun/reflect/ConstantPool
104  * Method:    getClassAtIfLoaded0
105  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
106  */
107 JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
108 {
109         constant_classref *ref;
110         classinfo *c = NULL;
111         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
112
113         ref = (constant_classref*)class_getconstant(
114                 cls, index, CONSTANT_Class);
115
116         if (ref == NULL) {
117                 exceptions_throw_illegalargumentexception();
118                 return NULL;
119         }
120         
121         if (!resolve_classref(NULL,ref,resolveLazy,true,true,&c)) {
122                 return NULL;
123         }
124
125         if (c == NULL || !(c->state & CLASS_LOADED)) {
126                 return NULL;
127         }
128         
129         return (jclass) LLNI_classinfo_wrap(c);
130 }
131
132
133 /*
134  * Class:     sun/reflect/ConstantPool
135  * Method:    getMethodAt0
136  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
137  */
138 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
139 {
140         constant_FMIref *ref;
141         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
142         
143         ref = (constant_FMIref*)class_getconstant(
144                 cls, index, CONSTANT_Methodref);
145         
146         if (ref == NULL) {
147                 exceptions_throw_illegalargumentexception();
148                 return NULL;
149         }
150
151         /* XXX: is that right? or do I have to use resolve_method_*? */
152         return (jobject) java_lang_reflect_Method::create(ref->p.method);
153 }
154
155
156 /*
157  * Class:     sun/reflect/ConstantPool
158  * Method:    getMethodAtIfLoaded0
159  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
160  */
161 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
162 {
163         constant_FMIref *ref;
164         classinfo *c = NULL;
165         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
166
167         ref = (constant_FMIref*)class_getconstant(
168                 cls, index, CONSTANT_Methodref);
169
170         if (ref == NULL) {
171                 exceptions_throw_illegalargumentexception();
172                 return NULL;
173         }
174
175         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
176                 return NULL;
177         }
178
179         if (c == NULL || !(c->state & CLASS_LOADED)) {
180                 return NULL;
181         }
182
183         return (jobject) java_lang_reflect_Method::create(ref->p.method);
184 }
185
186
187 /*
188  * Class:     sun/reflect/ConstantPool
189  * Method:    getFieldAt0
190  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
191  */
192 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
193 {
194         constant_FMIref *ref;
195         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
196
197         ref = (constant_FMIref*)class_getconstant(
198                 cls, index, CONSTANT_Fieldref);
199
200         if (ref == NULL) {
201                 exceptions_throw_illegalargumentexception();
202                 return NULL;
203         }
204
205         return (jobject) java_lang_reflect_Field::create(ref->p.field);
206 }
207
208
209 /*
210  * Class:     sun/reflect/ConstantPool
211  * Method:    getFieldAtIfLoaded0
212  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
213  */
214 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
215 {
216         constant_FMIref *ref;
217         classinfo *c;
218         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
219
220         ref = (constant_FMIref*)class_getconstant(
221                 cls, index, CONSTANT_Fieldref);
222
223         if (ref == NULL) {
224                 exceptions_throw_illegalargumentexception();
225                 return NULL;
226         }
227
228         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
229                 return NULL;
230         }
231
232         if (c == NULL || !(c->state & CLASS_LOADED)) {
233                 return NULL;
234         }
235
236         return (jobject) java_lang_reflect_Field::create(ref->p.field);
237 }
238
239
240 /*
241  * Class:     sun/reflect/ConstantPool
242  * Method:    getMemberRefInfoAt0
243  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
244  */
245 JNIEXPORT jobjectArray JNICALL Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
246 {
247         log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(env=%p, jcpool=%p, index=%d): IMPLEMENT ME!", env, jcpool, index);
248         return NULL;
249 }
250
251
252 /*
253  * Class:     sun/reflect/ConstantPool
254  * Method:    getIntAt0
255  * Signature: (Ljava/lang/Object;I)I
256  */
257 JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
258 {
259         constant_integer *ref;
260         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
261
262         ref = (constant_integer*)class_getconstant(
263                 cls, index, CONSTANT_Integer);
264
265         if (ref == NULL) {
266                 exceptions_throw_illegalargumentexception();
267                 return 0;
268         }
269
270         return ref->value;
271 }
272
273
274 /*
275  * Class:     sun/reflect/ConstantPool
276  * Method:    getLongAt0
277  * Signature: (Ljava/lang/Object;I)J
278  */
279 JNIEXPORT jlong JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
280 {
281         constant_long *ref;
282         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
283
284         ref = (constant_long*)class_getconstant(
285                 cls, index, CONSTANT_Long);
286
287         if (ref == NULL) {
288                 exceptions_throw_illegalargumentexception();
289                 return 0;
290         }
291
292         return ref->value;
293 }
294
295
296 /*
297  * Class:     sun/reflect/ConstantPool
298  * Method:    getFloatAt0
299  * Signature: (Ljava/lang/Object;I)F
300  */
301 JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
302 {
303         constant_float *ref;
304         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
305
306         ref = (constant_float*)class_getconstant(
307                 cls, index, CONSTANT_Float);
308
309         if (ref == NULL) {
310                 exceptions_throw_illegalargumentexception();
311                 return 0;
312         }
313
314         return ref->value;
315 }
316
317
318 /*
319  * Class:     sun/reflect/ConstantPool
320  * Method:    getDoubleAt0
321  * Signature: (Ljava/lang/Object;I)D
322  */
323 JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
324 {
325         constant_double *ref;
326         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
327
328         ref = (constant_double*)class_getconstant(
329                 cls, index, CONSTANT_Double);
330
331         if (ref == NULL) {
332                 exceptions_throw_illegalargumentexception();
333                 return 0;
334         }
335
336         return ref->value;
337 }
338
339
340 /*
341  * Class:     sun/reflect/ConstantPool
342  * Method:    getStringAt0
343  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
344  */
345 JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
346 {
347         utf *ref;
348         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
349         
350         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
351
352         if (ref == NULL) {
353                 exceptions_throw_illegalargumentexception();
354                 return NULL;
355         }
356
357         /* XXX: I hope literalstring_new is the right Function. */
358         return (jstring) literalstring_new(ref);
359 }
360
361
362 /*
363  * Class:     sun/reflect/ConstantPool
364  * Method:    getUTF8At0
365  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
366  */
367 JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
368 {
369         utf *ref;
370         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
371
372         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
373
374         if (ref == NULL) {
375                 exceptions_throw_illegalargumentexception();
376                 return NULL;
377         }
378
379         /* XXX: I hope literalstring_new is the right Function. */
380         return (jstring) literalstring_new(ref);
381 }
382
383 } // extern "C"
384
385
386 /* native methods implemented by this file ************************************/
387
388 static JNINativeMethod methods[] = {
389         { (char*) "getSize0",             (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
390         { (char*) "getClassAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
391         { (char*) "getClassAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
392         { (char*) "getMethodAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
393         { (char*) "getMethodAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
394         { (char*) "getFieldAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
395         { (char*) "getFieldAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
396         { (char*) "getMemberRefInfoAt0",  (char*) "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
397         { (char*) "getIntAt0",            (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
398         { (char*) "getLongAt0",           (char*) "(Ljava/lang/Object;I)J",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
399         { (char*) "getFloatAt0",          (char*) "(Ljava/lang/Object;I)F",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
400         { (char*) "getDoubleAt0",         (char*) "(Ljava/lang/Object;I)D",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
401         { (char*) "getStringAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
402         { (char*) "getUTF8At0",           (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
403 };
404
405
406 /* _Jv_sun_reflect_ConstantPool_init ******************************************
407
408    Register native functions.
409
410 *******************************************************************************/
411
412 // FIXME
413 extern "C" {
414 void _Jv_sun_reflect_ConstantPool_init(void)
415 {
416         native_method_register(utf_new_char("sun/reflect/ConstantPool"), methods, NATIVE_METHODS_COUNT);
417 }
418 }
419
420
421 /*
422  * These are local overrides for various environment variables in Emacs.
423  * Please do not remove this and leave it at the end of the file, where
424  * Emacs will automagically detect them.
425  * ---------------------------------------------------------------------
426  * Local variables:
427  * mode: c++
428  * indent-tabs-mode: t
429  * c-basic-offset: 4
430  * tab-width: 4
431  * End:
432  * vim:noexpandtab:sw=4:ts=4:
433  */