bb60dcfd685ad59428c3ba0bee59ba8ea41c5813
[cacao.git] / src / native / vm / gnu / sun_reflect_ConstantPool.c
1 /* src/native/vm/gnu/sun_reflect_ConstantPool.c
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, M. S. Panzenböck Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: sun_reflect_ConstantPool.c 8393 2007-08-22 01:10:09Z panzi $
26
27 */
28
29 /*******************************************************************************
30
31    XXX: The Methods in this file are very redundant to thouse in
32         src/native/vm/sun/jvm.c Unless someone has a good idea how to cover
33         such redundancy I leave it how it is.
34
35 *******************************************************************************/
36
37 #include "config.h"
38
39 #include <assert.h>
40 #include <stdint.h>
41
42 #include "mm/memory.h"
43
44 #include "native/jni.h"
45 #include "native/llni.h"
46 #include "native/native.h"
47
48 #include "native/include/java_lang_Object.h"
49 #include "native/include/java_lang_Class.h"
50 #include "native/include/sun_reflect_ConstantPool.h"
51
52 #include "native/vm/reflect.h"
53
54 #include "toolbox/logging.h"
55
56 #include "vm/vm.h"
57 #include "vm/resolve.h"
58 #include "vm/stringlocal.h"
59
60 #include "vmcore/class.h"
61 #include "vmcore/utf8.h"
62
63
64 /* native methods implemented by this file ************************************/
65
66 static JNINativeMethod methods[] = {
67         { "getSize0",             "(Ljava/lang/Object;I)I",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
68         { "getClassAt0",          "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
69         { "getClassAtIfLoaded0",  "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
70         { "getMethodAt0",         "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
71         { "getMethodAtIfLoaded0", "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
72         { "getFieldAt0",          "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
73         { "getFieldAtIfLoaded0",  "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
74         { "getMemberRefInfoAt0",  "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
75         { "getIntAt0",            "(Ljava/lang/Object;I)I",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
76         { "getLongAt0",           "(Ljava/lang/Object;I)J",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
77         { "getFloatAt0",          "(Ljava/lang/Object;I)F",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
78         { "getDoubleAt0",         "(Ljava/lang/Object;I)D",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
79         { "getStringAt0",         "(Ljava/lang/Object;I)Ljava/lang/String;",         (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
80         { "getUTF8At0",           "(Ljava/lang/Object;I)Ljava/lang/String;",         (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
81 };
82
83
84 /* _Jv_sun_reflect_ConstantPool_init ********************************************
85
86    Register native functions.
87
88 *******************************************************************************/
89
90 void _Jv_sun_reflect_ConstantPool_init(void)
91 {
92         native_method_register(utf_sun_reflect_ConstantPool, methods, NATIVE_METHODS_COUNT);
93 }
94
95 /*
96  * Class:     sun/reflect/ConstantPool
97  * Method:    getSize0
98  * Signature: (Ljava/lang/Object;)I
99  */
100 JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool)
101 {
102         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
103         return cls->cpcount;
104 }
105
106
107 /*
108  * Class:     sun/reflect/ConstantPool
109  * Method:    getClassAt0
110  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
111  */
112 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)
113 {
114         constant_classref *ref;
115         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
116
117         ref = (constant_classref*)class_getconstant(
118                 cls, index, CONSTANT_Class);
119
120         if (ref == NULL) {
121                 return NULL;
122         }
123
124         return LLNI_classinfo_wrap(resolve_classref_eager(ref));
125 }
126
127
128 /*
129  * Class:     sun/reflect/ConstantPool
130  * Method:    getClassAtIfLoaded0
131  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
132  */
133 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)
134 {
135         constant_classref *ref;
136         classinfo *c = NULL;
137         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
138
139         ref = (constant_classref*)class_getconstant(
140                 cls, index, CONSTANT_Class);
141
142         if (ref == NULL) {
143                 return NULL;
144         }
145         
146         if (!resolve_classref(NULL,ref,resolveLazy,true,true,&c)) {
147                 return NULL;
148         }
149
150         if (c == NULL || !(c->state & CLASS_LOADED)) {
151                 return NULL;
152         }
153         
154         return LLNI_classinfo_wrap(c);
155 }
156
157
158 /*
159  * Class:     sun/reflect/ConstantPool
160  * Method:    getMethodAt0
161  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
162  */
163 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)
164 {
165         constant_FMIref *ref;
166         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
167         
168         ref = (constant_FMIref*)class_getconstant(
169                 cls, index, CONSTANT_Methodref);
170         
171         if (ref == NULL) {
172                 return NULL;
173         }
174
175         /* XXX: is that right? or do I have to use resolve_method_*? */
176         return (jobject)reflect_method_new(ref->p.method);
177 }
178
179
180 /*
181  * Class:     sun/reflect/ConstantPool
182  * Method:    getMethodAtIfLoaded0
183  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
184  */
185 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)
186 {
187         constant_FMIref *ref;
188         classinfo *c = NULL;
189         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
190
191         ref = (constant_FMIref*)class_getconstant(
192                 cls, index, CONSTANT_Methodref);
193
194         if (ref == NULL) {
195                 return NULL;
196         }
197
198         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
199                 return NULL;
200         }
201
202         if (c == NULL || !(c->state & CLASS_LOADED)) {
203                 return NULL;
204         }
205
206         return (jobject)reflect_method_new(ref->p.method);
207 }
208
209
210 /*
211  * Class:     sun/reflect/ConstantPool
212  * Method:    getFieldAt0
213  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
214  */
215 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)
216 {
217         constant_FMIref *ref;
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                 return NULL;
225         }
226
227         return (jobject)reflect_field_new(ref->p.field);
228 }
229
230
231 /*
232  * Class:     sun/reflect/ConstantPool
233  * Method:    getFieldAtIfLoaded0
234  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
235  */
236 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)
237 {
238         constant_FMIref *ref;
239         classinfo *c;
240         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
241
242         ref = (constant_FMIref*)class_getconstant(
243                 cls, index, CONSTANT_Fieldref);
244
245         if (ref == NULL) {
246                 return NULL;
247         }
248
249         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
250                 return NULL;
251         }
252
253         if (c == NULL || !(c->state & CLASS_LOADED)) {
254                 return NULL;
255         }
256
257         return (jobject)reflect_field_new(ref->p.field);
258 }
259
260
261 /*
262  * Class:     sun/reflect/ConstantPool
263  * Method:    getMemberRefInfoAt0
264  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
265  */
266 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)
267 {
268         log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(env=%p, jcpool=%p, index=%d): IMPLEMENT ME!", env, jcpool, index);
269         return NULL;
270 }
271
272
273 /*
274  * Class:     sun/reflect/ConstantPool
275  * Method:    getIntAt0
276  * Signature: (Ljava/lang/Object;I)I
277  */
278 JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
279 {
280         constant_integer *ref;
281         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
282
283         ref = (constant_integer*)class_getconstant(
284                 cls, index, CONSTANT_Integer);
285
286         if (ref == NULL) {
287                 return 0;
288         }
289
290         return ref->value;
291 }
292
293
294 /*
295  * Class:     sun/reflect/ConstantPool
296  * Method:    getLongAt0
297  * Signature: (Ljava/lang/Object;I)J
298  */
299 JNIEXPORT int64_t JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
300 {
301         constant_long *ref;
302         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
303
304         ref = (constant_long*)class_getconstant(
305                 cls, index, CONSTANT_Long);
306
307         if (ref == NULL) {
308                 return 0;
309         }
310
311         return ref->value;
312 }
313
314
315 /*
316  * Class:     sun/reflect/ConstantPool
317  * Method:    getFloatAt0
318  * Signature: (Ljava/lang/Object;I)F
319  */
320 JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
321 {
322         constant_float *ref;
323         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
324
325         ref = (constant_float*)class_getconstant(
326                 cls, index, CONSTANT_Float);
327
328         if (ref == NULL) {
329                 return 0;
330         }
331
332         return ref->value;
333 }
334
335
336 /*
337  * Class:     sun/reflect/ConstantPool
338  * Method:    getDoubleAt0
339  * Signature: (Ljava/lang/Object;I)D
340  */
341 JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
342 {
343         constant_double *ref;
344         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
345
346         ref = (constant_double*)class_getconstant(
347                 cls, index, CONSTANT_Double);
348
349         if (ref == NULL) {
350                 return 0;
351         }
352
353         return ref->value;
354 }
355
356
357 /*
358  * Class:     sun/reflect/ConstantPool
359  * Method:    getStringAt0
360  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
361  */
362 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)
363 {
364         utf *ref;
365         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
366         
367         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
368
369         if (ref == NULL) {
370                 return NULL;
371         }
372
373         /* XXX: I hope literalstring_new is the right Function. */
374         return (java_lang_String*)literalstring_new(ref);
375 }
376
377
378 /*
379  * Class:     sun/reflect/ConstantPool
380  * Method:    getUTF8At0
381  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
382  */
383 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)
384 {
385         utf *ref;
386         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
387
388         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
389
390         if (ref == NULL) {
391                 return NULL;
392         }
393
394         /* XXX: I hope literalstring_new is the right Function. */
395         return (java_lang_String*)literalstring_new(ref);
396 }
397
398
399 /*
400  * These are local overrides for various environment variables in Emacs.
401  * Please do not remove this and leave it at the end of the file, where
402  * Emacs will automagically detect them.
403  * ---------------------------------------------------------------------
404  * Local variables:
405  * mode: c
406  * indent-tabs-mode: t
407  * c-basic-offset: 4
408  * tab-width: 4
409  * End:
410  * vim:noexpandtab:sw=4:ts=4:
411  */