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