ad48b2fda428abbc60a71ba05524588844245219
[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/class.h"
57 #include "vm/exceptions.hpp"
58 #include "vm/javaobjects.hpp"
59 #include "vm/resolve.h"
60 #include "vm/string.hpp"
61 #include "vm/utf8.h"
62 #include "vm/vm.hpp"
63
64
65 // Native functions are exported as C functions.
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         java_lang_reflect_Method jlrm(ref->p.method);
153
154         return (jobject) jlrm.get_handle();
155 }
156
157
158 /*
159  * Class:     sun/reflect/ConstantPool
160  * Method:    getMethodAtIfLoaded0
161  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
162  */
163 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
164 {
165         constant_FMIref *ref;
166         classinfo *c = NULL;
167         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
168
169         ref = (constant_FMIref*)class_getconstant(
170                 cls, index, CONSTANT_Methodref);
171
172         if (ref == NULL) {
173                 exceptions_throw_illegalargumentexception();
174                 return NULL;
175         }
176
177         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
178                 return NULL;
179         }
180
181         if (c == NULL || !(c->state & CLASS_LOADED)) {
182                 return NULL;
183         }
184
185         java_lang_reflect_Method jlrm(ref->p.method);
186
187         return (jobject) jlrm.get_handle();
188 }
189
190
191 /*
192  * Class:     sun/reflect/ConstantPool
193  * Method:    getFieldAt0
194  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
195  */
196 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
197 {
198         constant_FMIref *ref;
199         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
200
201         ref = (constant_FMIref*) class_getconstant(cls, index, CONSTANT_Fieldref);
202
203         if (ref == NULL) {
204                 exceptions_throw_illegalargumentexception();
205                 return NULL;
206         }
207
208         // Create a new java.lang.reflect.Field Java object.
209         java_lang_reflect_Field jlrf(ref->p.field);
210
211         return (jobject) jlrf.get_handle();
212 }
213
214
215 /*
216  * Class:     sun/reflect/ConstantPool
217  * Method:    getFieldAtIfLoaded0
218  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
219  */
220 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
221 {
222         constant_FMIref *ref;
223         classinfo *c;
224         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
225
226         ref = (constant_FMIref*) class_getconstant(cls, index, CONSTANT_Fieldref);
227
228         if (ref == NULL) {
229                 exceptions_throw_illegalargumentexception();
230                 return NULL;
231         }
232
233         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
234                 return NULL;
235         }
236
237         if (c == NULL || !(c->state & CLASS_LOADED)) {
238                 return NULL;
239         }
240
241         // Create a new java.lang.reflect.Field Java object.
242         java_lang_reflect_Field jlrf(ref->p.field);
243
244         return (jobject) jlrf.get_handle();
245 }
246
247
248 /*
249  * Class:     sun/reflect/ConstantPool
250  * Method:    getMemberRefInfoAt0
251  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
252  */
253 JNIEXPORT jobjectArray JNICALL Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
254 {
255         log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(env=%p, jcpool=%p, index=%d): IMPLEMENT ME!", env, jcpool, index);
256         return NULL;
257 }
258
259
260 /*
261  * Class:     sun/reflect/ConstantPool
262  * Method:    getIntAt0
263  * Signature: (Ljava/lang/Object;I)I
264  */
265 JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
266 {
267         constant_integer *ref;
268         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
269
270         ref = (constant_integer*) class_getconstant(cls, index, CONSTANT_Integer);
271
272         if (ref == NULL) {
273                 exceptions_throw_illegalargumentexception();
274                 return 0;
275         }
276
277         return ref->value;
278 }
279
280
281 /*
282  * Class:     sun/reflect/ConstantPool
283  * Method:    getLongAt0
284  * Signature: (Ljava/lang/Object;I)J
285  */
286 JNIEXPORT jlong JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
287 {
288         constant_long *ref;
289         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
290
291         ref = (constant_long*)class_getconstant(
292                 cls, index, CONSTANT_Long);
293
294         if (ref == NULL) {
295                 exceptions_throw_illegalargumentexception();
296                 return 0;
297         }
298
299         return ref->value;
300 }
301
302
303 /*
304  * Class:     sun/reflect/ConstantPool
305  * Method:    getFloatAt0
306  * Signature: (Ljava/lang/Object;I)F
307  */
308 JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
309 {
310         constant_float *ref;
311         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
312
313         ref = (constant_float*)class_getconstant(
314                 cls, index, CONSTANT_Float);
315
316         if (ref == NULL) {
317                 exceptions_throw_illegalargumentexception();
318                 return 0;
319         }
320
321         return ref->value;
322 }
323
324
325 /*
326  * Class:     sun/reflect/ConstantPool
327  * Method:    getDoubleAt0
328  * Signature: (Ljava/lang/Object;I)D
329  */
330 JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
331 {
332         constant_double *ref;
333         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
334
335         ref = (constant_double*)class_getconstant(
336                 cls, index, CONSTANT_Double);
337
338         if (ref == NULL) {
339                 exceptions_throw_illegalargumentexception();
340                 return 0;
341         }
342
343         return ref->value;
344 }
345
346
347 /*
348  * Class:     sun/reflect/ConstantPool
349  * Method:    getStringAt0
350  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
351  */
352 JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
353 {
354         utf *ref;
355         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
356         
357         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
358
359         if (ref == NULL) {
360                 exceptions_throw_illegalargumentexception();
361                 return NULL;
362         }
363
364         /* XXX: I hope literalstring_new is the right Function. */
365         return (jstring) literalstring_new(ref);
366 }
367
368
369 /*
370  * Class:     sun/reflect/ConstantPool
371  * Method:    getUTF8At0
372  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
373  */
374 JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
375 {
376         utf *ref;
377         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
378
379         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
380
381         if (ref == NULL) {
382                 exceptions_throw_illegalargumentexception();
383                 return NULL;
384         }
385
386         /* XXX: I hope literalstring_new is the right Function. */
387         return (jstring) literalstring_new(ref);
388 }
389
390 } // extern "C"
391
392
393 /* native methods implemented by this file ************************************/
394
395 static JNINativeMethod methods[] = {
396         { (char*) "getSize0",             (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
397         { (char*) "getClassAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
398         { (char*) "getClassAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
399         { (char*) "getMethodAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
400         { (char*) "getMethodAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
401         { (char*) "getFieldAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
402         { (char*) "getFieldAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
403         { (char*) "getMemberRefInfoAt0",  (char*) "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
404         { (char*) "getIntAt0",            (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
405         { (char*) "getLongAt0",           (char*) "(Ljava/lang/Object;I)J",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
406         { (char*) "getFloatAt0",          (char*) "(Ljava/lang/Object;I)F",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
407         { (char*) "getDoubleAt0",         (char*) "(Ljava/lang/Object;I)D",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
408         { (char*) "getStringAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
409         { (char*) "getUTF8At0",           (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
410 };
411
412
413 /* _Jv_sun_reflect_ConstantPool_init ******************************************
414
415    Register native functions.
416
417 *******************************************************************************/
418
419 // FIXME
420 extern "C" {
421 void _Jv_sun_reflect_ConstantPool_init(void)
422 {
423         native_method_register(utf_new_char("sun/reflect/ConstantPool"), methods, NATIVE_METHODS_COUNT);
424 }
425 }
426
427
428 /*
429  * These are local overrides for various environment variables in Emacs.
430  * Please do not remove this and leave it at the end of the file, where
431  * Emacs will automagically detect them.
432  * ---------------------------------------------------------------------
433  * Local variables:
434  * mode: c++
435  * indent-tabs-mode: t
436  * c-basic-offset: 4
437  * tab-width: 4
438  * End:
439  * vim:noexpandtab:sw=4:ts=4:
440  */