1 /* src/native/vm/reflect.c - helper functions for java/lang/reflect
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, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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
25 $Id: reflect.c 8305 2007-08-15 13:49:26Z panzi $
34 #if defined(ENABLE_ANNOTATIONS)
35 #include "mm/memory.h"
37 #if defined(WITH_CLASSPATH_GNU)
40 #include "native/include/sun_reflect_ConstantPool.h"
44 #include "native/jni.h"
45 #include "native/llni.h"
46 #include "native/native.h"
48 /* keep this order of the native includes */
50 #include "native/include/java_lang_String.h"
52 #if defined(WITH_CLASSPATH_SUN)
53 # include "native/include/java_nio_ByteBuffer.h" /* required by j.l.CL */
55 #include "native/include/java_lang_ClassLoader.h"
57 #include "native/include/java_lang_Object.h"
58 #include "native/include/java_lang_Class.h"
59 #include "native/include/java_lang_reflect_Constructor.h"
60 #include "native/include/java_lang_reflect_Field.h"
61 #include "native/include/java_lang_reflect_Method.h"
63 #include "native/vm/java_lang_String.h"
64 #include "native/vm/reflect.h"
66 #include "vm/builtin.h"
67 #include "vm/global.h"
68 #include "vm/stringlocal.h"
70 #include "vmcore/method.h"
73 /* reflect_constructor_new *****************************************************
75 Allocates a new java.lang.reflect.Constructor object and
76 initializes the fields with the method passed.
78 *******************************************************************************/
80 java_lang_reflect_Constructor *reflect_constructor_new(methodinfo *m)
84 java_lang_reflect_Constructor *rc;
86 java_bytearray *annotations = NULL;
87 java_bytearray *parameterAnnotations = NULL;
88 annotation_bytearray_t *ba = NULL;
90 #if defined(ENABLE_ANNOTATIONS)
92 ba = method_get_annotations(m);
95 annotations = builtin_newarray_byte(ba->size);
97 if (annotations == NULL)
100 MCOPY(annotations->data, ba->data, uint8_t, ba->size);
103 /* get parameter annotations */
104 ba = method_get_parameterannotations(m);
107 parameterAnnotations = builtin_newarray_byte(ba->size);
109 if (parameterAnnotations == NULL)
112 MCOPY(parameterAnnotations->data, ba->data, uint8_t, ba->size);
116 /* get declaring class */
118 c = (classinfo *) m->class;
120 /* allocate a new object */
122 o = builtin_new(class_java_lang_reflect_Constructor);
127 /* initialize instance fields */
129 rc = (java_lang_reflect_Constructor *) o;
131 /* calculate the slot */
133 slot = m - c->methods;
135 #if defined(WITH_CLASSPATH_GNU)
137 LLNI_field_set_cls(rc, clazz , c);
138 LLNI_field_set_val(rc, slot , slot);
139 LLNI_field_set_ref(rc, annotations , annotations);
140 LLNI_field_set_ref(rc, parameterAnnotations, parameterAnnotations);
142 #elif defined(WITH_CLASSPATH_SUN)
144 LLNI_field_set_cls(rc, clazz , c);
145 LLNI_field_set_ref(rc, parameterTypes , method_get_parametertypearray(m));
146 LLNI_field_set_ref(rc, exceptionTypes , method_get_exceptionarray(m));
147 LLNI_field_set_val(rc, modifiers , m->flags & ACC_CLASS_REFLECT_MASK);
148 LLNI_field_set_val(rc, slot , slot);
149 LLNI_field_set_ref(rc, signature , m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
150 LLNI_field_set_ref(rc, annotations , annotations);
151 LLNI_field_set_ref(rc, parameterAnnotations, parameterAnnotations);
154 # error unknown classpath configuration
161 /* reflect_field_new ***********************************************************
163 Allocates a new java.lang.reflect.Field object and initializes the
164 fields with the field passed.
166 *******************************************************************************/
168 java_lang_reflect_Field *reflect_field_new(fieldinfo *f)
172 java_lang_reflect_Field *rf;
174 java_bytearray *annotations = NULL;
175 annotation_bytearray_t *ba = NULL;
177 #if defined(ENABLE_ANNOTATIONS)
178 /* get annotations */
179 ba = field_get_annotations(f);
182 annotations = builtin_newarray_byte(ba->size);
184 if (annotations == NULL)
187 MCOPY(annotations->data, ba->data, uint8_t, ba->size);
191 /* get declaring class */
193 c = (classinfo *) f->class;
195 /* allocate a new object */
197 o = builtin_new(class_java_lang_reflect_Field);
202 /* initialize instance fields */
204 rf = (java_lang_reflect_Field *) o;
206 /* calculate the slot */
208 slot = f - c->fields;
210 #if defined(WITH_CLASSPATH_GNU)
212 LLNI_field_set_cls(rf, clazz , c);
214 /* The name needs to be interned */
215 /* XXX implement me better! */
217 LLNI_field_set_ref(rf, name , _Jv_java_lang_String_intern((java_lang_String *) javastring_new(f->name)));
218 LLNI_field_set_val(rf, slot , slot);
219 LLNI_field_set_ref(rf, annotations , annotations);
221 #elif defined(WITH_CLASSPATH_SUN)
223 LLNI_field_set_cls(rf, clazz , c);
225 /* The name needs to be interned */
226 /* XXX implement me better! */
228 LLNI_field_set_ref(rf, name , _Jv_java_lang_String_intern((java_lang_String *) javastring_new(f->name)));
229 LLNI_field_set_cls(rf, type , (java_lang_Class *) field_get_type(f));
230 LLNI_field_set_val(rf, modifiers , f->flags);
231 LLNI_field_set_val(rf, slot , slot);
232 LLNI_field_set_ref(rf, signature , f->signature ? (java_lang_String *) javastring_new(f->signature) : NULL);
233 LLNI_field_set_ref(rf, annotations , annotations);
236 # error unknown classpath configuration
243 /* reflect_method_new **********************************************************
245 Allocates a new java.lang.reflect.Method object and initializes the
246 fields with the method passed.
248 *******************************************************************************/
250 java_lang_reflect_Method *reflect_method_new(methodinfo *m)
254 java_lang_reflect_Method *rm;
256 java_bytearray *annotations = NULL;
257 java_bytearray *parameterAnnotations = NULL;
258 java_bytearray *annotationDefault = NULL;
259 annotation_bytearray_t *ba = NULL;
261 #if defined(ENABLE_ANNOTATIONS)
262 /* get annotations */
263 ba = method_get_annotations(m);
266 annotations = builtin_newarray_byte(ba->size);
268 if (annotations == NULL)
271 MCOPY(annotations->data, ba->data, uint8_t, ba->size);
274 /* get parameter annotations */
275 ba = method_get_parameterannotations(m);
278 parameterAnnotations = builtin_newarray_byte(ba->size);
280 if (parameterAnnotations == NULL)
283 MCOPY(parameterAnnotations->data, ba->data, uint8_t, ba->size);
286 /* get annotation default value */
287 ba = method_get_annotationdefault(m);
290 annotationDefault = builtin_newarray_byte(ba->size);
292 if (annotationDefault == NULL)
295 MCOPY(annotationDefault->data, ba->data, uint8_t, ba->size);
299 /* get declaring class */
301 c = (classinfo *) m->class;
303 /* allocate a new object */
305 o = builtin_new(class_java_lang_reflect_Method);
310 /* initialize instance fields */
312 rm = (java_lang_reflect_Method *) o;
314 /* calculate the slot */
316 slot = m - c->methods;
318 #if defined(WITH_CLASSPATH_GNU)
320 LLNI_field_set_cls(rm, clazz , m->class);
322 /* The name needs to be interned */
323 /* XXX implement me better! */
325 LLNI_field_set_ref(rm, name , _Jv_java_lang_String_intern((java_lang_String *) javastring_new(m->name)));
326 LLNI_field_set_val(rm, slot , slot);
327 LLNI_field_set_ref(rm, annotations , annotations);
328 LLNI_field_set_ref(rm, parameterAnnotations, parameterAnnotations);
329 LLNI_field_set_ref(rm, annotationDefault , annotationDefault);
331 #elif defined(WITH_CLASSPATH_SUN)
333 LLNI_field_set_cls(rm, clazz , (java_lang_Class *) m->class);
335 /* The name needs to be interned */
336 /* XXX implement me better! */
338 rm->name = _Jv_java_lang_String_intern((java_lang_String *) javastring_new(m->name));
339 rm->parameterTypes = method_get_parametertypearray(m);
340 rm->returnType = (java_lang_Class *) method_returntype_get(m);
341 rm->exceptionTypes = method_get_exceptionarray(m);
342 rm->modifiers = m->flags & ACC_CLASS_REFLECT_MASK;
344 rm->signature = m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL;
345 rm->annotations = annotations;
346 rm->parameterAnnotations = parameterAnnotations;
347 rm->annotationDefault = annotationDefault;
350 # error unknown classpath configuration
357 #if defined(WITH_CLASSPATH_GNU) && defined(ENABLE_ANNOTATIONS)
358 /* reflect_get_declaredannotatios *********************************************
360 Returns a java.util.Map<Class, Annotation> of the declared
363 *******************************************************************************/
365 struct java_util_Map* reflect_get_declaredannotatios(
366 java_bytearray *annotations,
367 java_lang_Class *declaringClass,
370 static methodinfo *m_parseAnnotations = NULL;
371 utf *utf_parseAnnotations = NULL;
372 utf *utf_desc = NULL;
373 sun_reflect_ConstantPool *constantPool = NULL;
374 java_lang_Object *constantPoolOop = (java_lang_Object*)declaringClass;
377 (sun_reflect_ConstantPool*)native_new_and_init(
378 class_sun_reflect_ConstantPool);
380 if(constantPool == NULL) {
385 LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
387 /* only resolve the method the first time */
388 if (m_parseAnnotations == NULL) {
389 utf_parseAnnotations = utf_new_char("parseAnnotations");
390 utf_desc = utf_new_char(
391 "([BLsun/reflect/ConstantPool;Ljava/lang/Class;)"
394 if (utf_parseAnnotations == NULL || utf_desc == NULL) {
399 m_parseAnnotations = class_resolveclassmethod(
400 class_sun_reflect_annotation_AnnotationParser,
401 utf_parseAnnotations,
406 if (m_parseAnnotations == NULL) {
407 /* method not found */
412 return (struct java_util_Map*)vm_call_method(
413 m_parseAnnotations, NULL, annotations,
414 constantPool, declaringClass);
418 /* reflect_get_parameterannotations *******************************************
420 Parses and returns the parameter annotations of a method.
422 *******************************************************************************/
424 java_objectarray* reflect_get_parameterannotations(
425 java_handle_t *parameterAnnotations,
427 java_lang_Class *declaringClass,
430 /* This method in java would be basically the following.
431 * We don't do it in java because we don't want to make a
432 * public method with wich you can get a ConstantPool, because
433 * with that you could read any kind of constants (even private
436 * ConstantPool constPool = new ConstantPool();
437 * constPool.constantPoolOop = getDeclaringClass();
438 * return sun.reflect.AnnotationParser.parseParameterAnnotations(
439 * parameterAnnotations,
441 * getDeclaringClass(),
442 * getParameterTypes().length);
444 static methodinfo *m_parseParameterAnnotations = NULL;
445 utf *utf_parseParameterAnnotations = NULL;
446 utf *utf_desc = NULL;
447 sun_reflect_ConstantPool *constantPool = NULL;
448 java_lang_Object *constantPoolOop = (java_lang_Object*)declaringClass;
450 methodinfo *m = NULL;
451 int32_t numParameters = -1;
453 /* get parameter count */
455 c = (classinfo *)declaringClass;
456 m = &(c->methods[slot]);
458 numParameters = method_get_parametercount(m);
460 if (numParameters < 0) {
461 /* error parsing descriptor */
465 /* get ConstantPool */
468 (sun_reflect_ConstantPool*)native_new_and_init(
469 class_sun_reflect_ConstantPool);
471 if(constantPool == NULL) {
476 LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
478 /* only resolve the method the first time */
479 if (m_parseParameterAnnotations == NULL) {
480 utf_parseParameterAnnotations = utf_new_char("parseParameterAnnotations");
481 utf_desc = utf_new_char(
482 "([BLsun/reflect/ConstantPool;Ljava/lang/Class;I)"
483 "[[Ljava/lang/annotation/Annotation;");
485 if (utf_parseParameterAnnotations == NULL || utf_desc == NULL) {
490 /* get parser method */
492 m_parseParameterAnnotations = class_resolveclassmethod(
493 class_sun_reflect_annotation_AnnotationParser,
494 utf_parseParameterAnnotations,
499 if (m_parseParameterAnnotations == NULL)
501 /* method not found */
506 return (java_objectarray*)vm_call_method(
507 m_parseParameterAnnotations, NULL, parameterAnnotations,
508 constantPool, declaringClass, numParameters);
514 * These are local overrides for various environment variables in Emacs.
515 * Please do not remove this and leave it at the end of the file, where
516 * Emacs will automagically detect them.
517 * ---------------------------------------------------------------------
520 * indent-tabs-mode: t
524 * vim:noexpandtab:sw=4:ts=4: