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