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