CACAO
sun_reflect_ConstantPool.cpp
Go to the documentation of this file.
1 /* src/native/vm/gnuclasspath/sun_reflect_ConstantPool.cpp
2 
3  Copyright (C) 2007-2013
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.hpp"
44 
45 #include "native/jni.hpp"
46 #include "native/llni.hpp"
47 #include "native/native.hpp"
48 
49 // FIXME
50 //#include "native/include/sun_reflect_ConstantPool.h"
51 
52 #include "native/vm/reflection.hpp"
53 
54 #include "toolbox/logging.hpp"
55 
56 #include "vm/class.hpp"
57 #include "vm/exceptions.hpp"
58 #include "vm/javaobjects.hpp"
59 #include "vm/resolve.hpp"
60 #include "vm/string.hpp"
61 #include "vm/utf8.hpp"
62 #include "vm/vm.hpp"
63 
64 
65 // Native functions are exported as C functions.
66 extern "C" {
67 
68 /*
69  * Class: sun/reflect/ConstantPool
70  * Method: getSize0
71  * Signature: (Ljava/lang/Object;)I
72  */
73 JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, jobject _this, jobject jcpool)
74 {
75  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
76  return cls->cpcount;
77 }
78 
79 
80 /*
81  * Class: sun/reflect/ConstantPool
82  * Method: getClassAt0
83  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
84  */
85 JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
86 {
87  constant_classref *ref;
88  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
89 
91  cls, index, CONSTANT_Class);
92 
93  if (ref == NULL) {
95  return NULL;
96  }
97 
99 }
100 
101 
102 /*
103  * Class: sun/reflect/ConstantPool
104  * Method: getClassAtIfLoaded0
105  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
106  */
108 {
109  constant_classref *ref;
110  classinfo *c = NULL;
111  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
112 
114  cls, index, CONSTANT_Class);
115 
116  if (ref == NULL) {
118  return NULL;
119  }
120 
121  if (!resolve_classref(NULL,ref,resolveLazy,true,true,&c)) {
122  return NULL;
123  }
124 
125  if (c == NULL || !(c->state & CLASS_LOADED)) {
126  return NULL;
127  }
128 
129  return (jclass) LLNI_classinfo_wrap(c);
130 }
131 
132 
133 /*
134  * Class: sun/reflect/ConstantPool
135  * Method: getMethodAt0
136  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
137  */
138 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
139 {
140  constant_FMIref *ref;
141  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
142 
144  cls, index, CONSTANT_Methodref);
145 
146  if (ref == NULL) {
148  return NULL;
149  }
150 
151  /* XXX: is that right? or do I have to use resolve_method_*? */
152  java_lang_reflect_Method jlrm(ref->p.method);
153 
154  return (jobject) jlrm.get_handle();
155 }
156 
157 
158 /*
159  * Class: sun/reflect/ConstantPool
160  * Method: getMethodAtIfLoaded0
161  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
162  */
164 {
165  constant_FMIref *ref;
166  classinfo *c = NULL;
167  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
168 
170  cls, index, CONSTANT_Methodref);
171 
172  if (ref == NULL) {
174  return NULL;
175  }
176 
177  if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
178  return NULL;
179  }
180 
181  if (c == NULL || !(c->state & CLASS_LOADED)) {
182  return NULL;
183  }
184 
185  java_lang_reflect_Method jlrm(ref->p.method);
186 
187  return (jobject) jlrm.get_handle();
188 }
189 
190 
191 /*
192  * Class: sun/reflect/ConstantPool
193  * Method: getFieldAt0
194  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
195  */
196 JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
197 {
198  constant_FMIref *ref;
199  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
200 
202 
203  if (ref == NULL) {
205  return NULL;
206  }
207 
208  // Create a new java.lang.reflect.Field Java object.
209  java_lang_reflect_Field jlrf(ref->p.field);
210 
211  return (jobject) jlrf.get_handle();
212 }
213 
214 
215 /*
216  * Class: sun/reflect/ConstantPool
217  * Method: getFieldAtIfLoaded0
218  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
219  */
221 {
222  constant_FMIref *ref;
223  classinfo *c;
224  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
225 
227 
228  if (ref == NULL) {
230  return NULL;
231  }
232 
233  if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
234  return NULL;
235  }
236 
237  if (c == NULL || !(c->state & CLASS_LOADED)) {
238  return NULL;
239  }
240 
241  // Create a new java.lang.reflect.Field Java object.
242  java_lang_reflect_Field jlrf(ref->p.field);
243 
244  return (jobject) jlrf.get_handle();
245 }
246 
247 
248 /*
249  * Class: sun/reflect/ConstantPool
250  * Method: getMemberRefInfoAt0
251  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
252  */
254 {
255  log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(env=%p, jcpool=%p, index=%d): IMPLEMENT ME!", env, jcpool, index);
256  return NULL;
257 }
258 
259 
260 /*
261  * Class: sun/reflect/ConstantPool
262  * Method: getIntAt0
263  * Signature: (Ljava/lang/Object;I)I
264  */
265 JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
266 {
267  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
268 
269  int32_t *ref = (int32_t*) class_getconstant(cls, index, CONSTANT_Integer);
270 
271  if (ref == NULL) {
273  return 0;
274  }
275 
276  return *ref;
277 }
278 
279 
280 /*
281  * Class: sun/reflect/ConstantPool
282  * Method: getLongAt0
283  * Signature: (Ljava/lang/Object;I)J
284  */
285 JNIEXPORT jlong JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
286 {
287  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
288 
289  int64_t *ref = (int64_t*) class_getconstant(cls, index, CONSTANT_Long);
290 
291  if (ref == NULL) {
293  return 0;
294  }
295 
296  return *ref;
297 }
298 
299 
300 /*
301  * Class: sun/reflect/ConstantPool
302  * Method: getFloatAt0
303  * Signature: (Ljava/lang/Object;I)F
304  */
305 JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
306 {
307  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
308 
309  float *ref = (float*) class_getconstant( cls, index, CONSTANT_Float);
310 
311  if (ref == NULL) {
313  return 0;
314  }
315 
316  return *ref;
317 }
318 
319 
320 /*
321  * Class: sun/reflect/ConstantPool
322  * Method: getDoubleAt0
323  * Signature: (Ljava/lang/Object;I)D
324  */
325 JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
326 {
327  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
328 
329  double *ref = (double*) class_getconstant(cls, index, CONSTANT_Double);
330 
331  if (ref == NULL) {
333  return 0;
334  }
335 
336  return *ref;
337 }
338 
339 
340 /*
341  * Class: sun/reflect/ConstantPool
342  * Method: getStringAt0
343  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
344  */
345 JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
346 {
347  Utf8String ref;
348  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
349 
350  ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
351 
352  if (ref == NULL) {
354  return NULL;
355  }
356 
357  /* XXX: I hope literalstring_new is the right Function. */
358  return (jstring) JavaString::literal(ref);
359 }
360 
361 
362 /*
363  * Class: sun/reflect/ConstantPool
364  * Method: getUTF8At0
365  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
366  */
367 JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
368 {
369  Utf8String ref;
370  classinfo *cls = LLNI_classinfo_unwrap(jcpool);
371 
372  ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
373 
374  if (ref == NULL) {
376  return NULL;
377  }
378 
379  /* XXX: I hope literalstring_new is the right Function. */
380  return (jstring) JavaString::literal(ref);
381 }
382 
383 } // extern "C"
384 
385 
386 /* native methods implemented by this file ************************************/
387 
388 static JNINativeMethod methods[] = {
389  { (char*) "getSize0", (char*) "(Ljava/lang/Object;I)I", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getSize0 },
390  { (char*) "getClassAt0", (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAt0 },
391  { (char*) "getClassAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0 },
392  { (char*) "getMethodAt0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0 },
393  { (char*) "getMethodAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
394  { (char*) "getFieldAt0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0 },
395  { (char*) "getFieldAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0 },
396  { (char*) "getMemberRefInfoAt0", (char*) "(Ljava/lang/Object;I)[Ljava/lang/String;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0 },
397  { (char*) "getIntAt0", (char*) "(Ljava/lang/Object;I)I", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getIntAt0 },
398  { (char*) "getLongAt0", (char*) "(Ljava/lang/Object;I)J", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getLongAt0 },
399  { (char*) "getFloatAt0", (char*) "(Ljava/lang/Object;I)F", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0 },
400  { (char*) "getDoubleAt0", (char*) "(Ljava/lang/Object;I)D", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0 },
401  { (char*) "getStringAt0", (char*) "(Ljava/lang/Object;I)Ljava/lang/String;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getStringAt0 },
402  { (char*) "getUTF8At0", (char*) "(Ljava/lang/Object;I)Ljava/lang/String;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0 },
403 };
404 
405 
406 /* _Jv_sun_reflect_ConstantPool_init ******************************************
407 
408  Register native functions.
409 
410 *******************************************************************************/
411 
413 {
414  Utf8String u = Utf8String::from_utf8("sun/reflect/ConstantPool");
415 
418 }
419 
420 
421 /*
422  * These are local overrides for various environment variables in Emacs.
423  * Please do not remove this and leave it at the end of the file, where
424  * Emacs will automagically detect them.
425  * ---------------------------------------------------------------------
426  * Local variables:
427  * mode: c++
428  * indent-tabs-mode: t
429  * c-basic-offset: 4
430  * tab-width: 4
431  * End:
432  * vim:noexpandtab:sw=4:ts=4:
433  */
void exceptions_throw_illegalargumentexception(void)
std::size_t index
official tags from JVM spec
Definition: global.hpp:164
virtual java_handle_t * get_handle() const
void * class_getconstant(classinfo *c, u4 pos, ConstantPoolTag ctype)
Definition: class.cpp:679
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
s4 state
Definition: class.hpp:115
bool resolve_classref(methodinfo *refmethod, constant_classref *ref, resolve_mode_t mode, bool checkaccess, bool link, classinfo **result)
Definition: resolve.cpp:309
GNU Classpath java/lang/reflect/Field.
union constant_FMIref::@26 p
JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
methodinfo * method
Definition: references.hpp:101
void log_println(const char *text,...)
Definition: logging.cpp:193
#define LLNI_classinfo_wrap(classinfo)
Definition: llni.hpp:110
JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
GNU Classpath java/lang/reflect/Method.
JNIEXPORT jobjectArray JNICALL Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
Fieldref, Methodref and InterfaceMethodref.
Definition: references.hpp:86
JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
s4 cpcount
Definition: class.hpp:94
JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
JNIEXPORT jlong JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
static JNINativeMethod methods[]
constant_classref * classref
Definition: references.hpp:97
JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
classinfo * resolve_classref_eager(constant_classref *ref)
Definition: resolve.cpp:961
#define LLNI_classinfo_unwrap(clazz)
Definition: llni.hpp:113
fieldinfo * field
Definition: references.hpp:100
static JavaString literal(Utf8String)
Definition: string.cpp:257
void _Jv_sun_reflect_ConstantPool_init(void)
JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, jobject _this, jobject jcpool)
static VM * get_current()
Definition: vm.hpp:99