Merged revisions 8321-8342 via svnmerge from
[cacao.git] / src / native / vm / gnu / sun_reflect_ConstantPool.c
1 /* src/native/vm/gnu/sun_reflect_ConstantPool.c
2
3    Copyright (C) 1996-2005, 2006, 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 */
26
27 /*******************************************************************************
28
29    XXX: The Methods in this file are very redundant to thouse in
30         src/native/vm/sun/jvm.c Unless someone has a good idea how to cover
31         such redundancy I leave it how it is.
32
33 *******************************************************************************/
34
35 #include "config.h"
36
37 #include <assert.h>
38 #include <stdint.h>
39
40 #include "mm/memory.h"
41
42 #include "native/jni.h"
43 #include "native/native.h"
44 #include "native/include/java_lang_Object.h"
45 #include "native/include/java_lang_Class.h"
46 #include "native/include/sun_reflect_ConstantPool.h"
47 #include "native/vm/reflect.h"
48
49 #include "toolbox/logging.h"
50
51 #include "vm/vm.h"
52 #include "vm/resolve.h"
53 #include "vm/stringlocal.h"
54
55 #include "vmcore/class.h"
56 #include "vmcore/utf8.h"
57
58 /* native methods implemented by this file ************************************/
59
60 static JNINativeMethod methods[] = {
61         { "getSize0",             "(Ljava/lang/Object;I)I",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
62         { "getClassAt0",          "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
63         { "getClassAtIfLoaded0",  "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
64         { "getMethodAt0",         "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
65         { "getMethodAtIfLoaded0", "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
66         { "getFieldAt0",          "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
67         { "getFieldAtIfLoaded0",  "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
68         { "getMemberRefInfoAt0",  "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
69         { "getIntAt0",            "(Ljava/lang/Object;I)I",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
70         { "getLongAt0",           "(Ljava/lang/Object;I)J",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
71         { "getFloatAt0",          "(Ljava/lang/Object;I)F",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
72         { "getDoubleAt0",         "(Ljava/lang/Object;I)D",                          (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
73         { "getStringAt0",         "(Ljava/lang/Object;I)Ljava/lang/String;",         (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
74         { "getUTF8At0",           "(Ljava/lang/Object;I)Ljava/lang/String;",         (void *) (intptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
75 };
76
77
78 /* _Jv_sun_reflect_ConstantPool_init ********************************************
79
80    Register native functions.
81
82 *******************************************************************************/
83
84 void _Jv_sun_reflect_ConstantPool_init(void)
85 {
86         native_method_register(utf_sun_reflect_ConstantPool, methods, NATIVE_METHODS_COUNT);
87 }
88
89 /*
90  * Class:     sun/reflect/ConstantPool
91  * Method:    getSize0
92  * Signature: (Ljava/lang/Object;)I
93  */
94 JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool)
95 {
96         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
97         return cls->cpcount;
98 }
99
100
101 /*
102  * Class:     sun/reflect/ConstantPool
103  * Method:    getClassAt0
104  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
105  */
106 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)
107 {
108         constant_classref *ref;
109         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
110
111         ref = (constant_classref*)class_getconstant(
112                 cls, index, CONSTANT_Class);
113
114         if (ref == NULL) {
115                 return NULL;
116         }
117
118         return LLNI_classinfo_wrap(resolve_classref_eager(ref));
119 }
120
121
122 /*
123  * Class:     sun/reflect/ConstantPool
124  * Method:    getClassAtIfLoaded0
125  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
126  */
127 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)
128 {
129         constant_classref *ref;
130         classinfo *c = NULL;
131         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
132
133         ref = (constant_classref*)class_getconstant(
134                 cls, index, CONSTANT_Class);
135
136         if (ref == NULL) {
137                 return NULL;
138         }
139         
140         if (!resolve_classref(NULL,ref,resolveLazy,true,true,&c)) {
141                 return NULL;
142         }
143
144         if (c == NULL || !(c->state & CLASS_LOADED)) {
145                 return NULL;
146         }
147         
148         return LLNI_classinfo_wrap(c);
149 }
150
151
152 /*
153  * Class:     sun/reflect/ConstantPool
154  * Method:    getMethodAt0
155  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
156  */
157 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)
158 {
159         constant_FMIref *ref;
160         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
161         
162         ref = (constant_FMIref*)class_getconstant(
163                 cls, index, CONSTANT_Methodref);
164         
165         if (ref == NULL) {
166                 return NULL;
167         }
168
169         /* XXX: is that right? or do I have to use resolve_method_*? */
170         return (jobject)reflect_method_new(ref->p.method);
171 }
172
173
174 /*
175  * Class:     sun/reflect/ConstantPool
176  * Method:    getMethodAtIfLoaded0
177  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
178  */
179 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)
180 {
181         constant_FMIref *ref;
182         classinfo *c = NULL;
183         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
184
185         ref = (constant_FMIref*)class_getconstant(
186                 cls, index, CONSTANT_Methodref);
187
188         if (ref == NULL) {
189                 return NULL;
190         }
191
192         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
193                 return NULL;
194         }
195
196         if (c == NULL || !(c->state & CLASS_LOADED)) {
197                 return NULL;
198         }
199
200         return (jobject)reflect_method_new(ref->p.method);
201 }
202
203
204 /*
205  * Class:     sun/reflect/ConstantPool
206  * Method:    getFieldAt0
207  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
208  */
209 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)
210 {
211         constant_FMIref *ref;
212         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
213
214         ref = (constant_FMIref*)class_getconstant(
215                 cls, index, CONSTANT_Fieldref);
216
217         if (ref == NULL) {
218                 return NULL;
219         }
220
221         return (jobject)reflect_field_new(ref->p.field);
222 }
223
224
225 /*
226  * Class:     sun/reflect/ConstantPool
227  * Method:    getFieldAtIfLoaded0
228  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
229  */
230 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)
231 {
232         constant_FMIref *ref;
233         classinfo *c;
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                 return NULL;
241         }
242
243         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
244                 return NULL;
245         }
246
247         if (c == NULL || !(c->state & CLASS_LOADED)) {
248                 return NULL;
249         }
250
251         return (jobject)reflect_field_new(ref->p.field);
252 }
253
254
255 /*
256  * Class:     sun/reflect/ConstantPool
257  * Method:    getMemberRefInfoAt0
258  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
259  */
260 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)
261 {
262         log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0: jcpool=%p, index=%d, IMPLEMENT ME!", jcpool, index);
263         return NULL;
264 }
265
266
267 /*
268  * Class:     sun/reflect/ConstantPool
269  * Method:    getIntAt0
270  * Signature: (Ljava/lang/Object;I)I
271  */
272 JNIEXPORT int32_t JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
273 {
274         constant_integer *ref;
275         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
276
277         ref = (constant_integer*)class_getconstant(
278                 cls, index, CONSTANT_Integer);
279
280         if (ref == NULL) {
281                 return 0;
282         }
283
284         return ref->value;
285 }
286
287
288 /*
289  * Class:     sun/reflect/ConstantPool
290  * Method:    getLongAt0
291  * Signature: (Ljava/lang/Object;I)J
292  */
293 JNIEXPORT int64_t JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
294 {
295         constant_long *ref;
296         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
297
298         ref = (constant_long*)class_getconstant(
299                 cls, index, CONSTANT_Long);
300
301         if (ref == NULL) {
302                 return 0;
303         }
304
305         return ref->value;
306 }
307
308
309 /*
310  * Class:     sun/reflect/ConstantPool
311  * Method:    getFloatAt0
312  * Signature: (Ljava/lang/Object;I)F
313  */
314 JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
315 {
316         constant_float *ref;
317         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
318
319         ref = (constant_float*)class_getconstant(
320                 cls, index, CONSTANT_Float);
321
322         if (ref == NULL) {
323                 return 0;
324         }
325
326         return ref->value;
327 }
328
329
330 /*
331  * Class:     sun/reflect/ConstantPool
332  * Method:    getDoubleAt0
333  * Signature: (Ljava/lang/Object;I)D
334  */
335 JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, struct sun_reflect_ConstantPool* this, struct java_lang_Object* jcpool, int32_t index)
336 {
337         constant_double *ref;
338         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
339
340         ref = (constant_double*)class_getconstant(
341                 cls, index, CONSTANT_Double);
342
343         if (ref == NULL) {
344                 return 0;
345         }
346
347         return ref->value;
348 }
349
350
351 /*
352  * Class:     sun/reflect/ConstantPool
353  * Method:    getStringAt0
354  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
355  */
356 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)
357 {
358         utf *ref;
359         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
360         
361         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
362
363         if (ref == NULL) {
364                 return NULL;
365         }
366
367         /* XXX: I hope literalstring_new is the right Function. */
368         return (java_lang_String*)literalstring_new(ref);
369 }
370
371
372 /*
373  * Class:     sun/reflect/ConstantPool
374  * Method:    getUTF8At0
375  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
376  */
377 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)
378 {
379         utf *ref;
380         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
381
382         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
383
384         if (ref == NULL) {
385                 return NULL;
386         }
387
388         /* XXX: I hope literalstring_new is the right Function. */
389         return (java_lang_String*)literalstring_new(ref);
390 }