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 8393 2007-08-22 01:10:09Z 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/reflect.h"
65 #include "vm/builtin.h"
66 #include "vm/global.h"
67 #include "vm/stringlocal.h"
69 #include "vmcore/method.h"
72 /* reflect_constructor_new *****************************************************
74 Allocates a new java.lang.reflect.Constructor object and
75 initializes the fields with the method passed.
77 *******************************************************************************/
79 java_lang_reflect_Constructor *reflect_constructor_new(methodinfo *m)
83 java_lang_reflect_Constructor *rc;
86 /* get declaring class */
90 /* allocate a new object */
92 o = builtin_new(class_java_lang_reflect_Constructor);
97 /* initialize instance fields */
99 rc = (java_lang_reflect_Constructor *) o;
101 /* calculate the slot */
103 slot = m - c->methods;
105 #if defined(WITH_CLASSPATH_GNU)
107 LLNI_field_set_cls(rc, clazz , c);
108 LLNI_field_set_val(rc, slot , slot);
109 LLNI_field_set_ref(rc, annotations , method_get_annotations(m));
110 LLNI_field_set_ref(rc, parameterAnnotations, method_get_parameterannotations(m));
112 #elif defined(WITH_CLASSPATH_SUN)
114 LLNI_field_set_cls(rc, clazz , c);
115 LLNI_field_set_ref(rc, parameterTypes , method_get_parametertypearray(m));
116 LLNI_field_set_ref(rc, exceptionTypes , method_get_exceptionarray(m));
117 LLNI_field_set_val(rc, modifiers , m->flags & ACC_CLASS_REFLECT_MASK);
118 LLNI_field_set_val(rc, slot , slot);
119 LLNI_field_set_ref(rc, signature , m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
120 LLNI_field_set_ref(rc, annotations , method_get_annotations(m));
121 LLNI_field_set_ref(rc, parameterAnnotations, method_get_parameterannotations(m));
124 # error unknown classpath configuration
131 /* reflect_field_new ***********************************************************
133 Allocates a new java.lang.reflect.Field object and initializes the
134 fields with the field passed.
136 *******************************************************************************/
138 java_lang_reflect_Field *reflect_field_new(fieldinfo *f)
142 java_lang_reflect_Field *rf;
145 /* get declaring class */
149 /* allocate a new object */
151 o = builtin_new(class_java_lang_reflect_Field);
156 /* initialize instance fields */
158 rf = (java_lang_reflect_Field *) o;
160 /* calculate the slot */
162 slot = f - c->fields;
164 #if defined(WITH_CLASSPATH_GNU)
166 LLNI_field_set_cls(rf, clazz , c);
168 /* The name needs to be interned */
169 /* XXX implement me better! */
171 LLNI_field_set_ref(rf, name , javastring_intern((java_lang_String *) javastring_new(f->name)));
172 LLNI_field_set_val(rf, slot , slot);
173 LLNI_field_set_ref(rf, annotations , field_get_annotations(f));
175 #elif defined(WITH_CLASSPATH_SUN)
177 LLNI_field_set_cls(rf, clazz , c);
179 /* The name needs to be interned */
180 /* XXX implement me better! */
182 LLNI_field_set_ref(rf, name , javastring_intern((java_lang_String *) javastring_new(f->name)));
183 LLNI_field_set_cls(rf, type , (java_lang_Class *) field_get_type(f));
184 LLNI_field_set_val(rf, modifiers , f->flags);
185 LLNI_field_set_val(rf, slot , slot);
186 LLNI_field_set_ref(rf, signature , f->signature ? (java_lang_String *) javastring_new(f->signature) : NULL);
187 LLNI_field_set_ref(rf, annotations , field_get_annotations(f));
190 # error unknown classpath configuration
197 /* reflect_method_new **********************************************************
199 Allocates a new java.lang.reflect.Method object and initializes the
200 fields with the method passed.
202 *******************************************************************************/
204 java_lang_reflect_Method *reflect_method_new(methodinfo *m)
208 java_lang_reflect_Method *rm;
211 /* get declaring class */
215 /* allocate a new object */
217 o = builtin_new(class_java_lang_reflect_Method);
222 /* initialize instance fields */
224 rm = (java_lang_reflect_Method *) o;
226 /* calculate the slot */
228 slot = m - c->methods;
230 #if defined(WITH_CLASSPATH_GNU)
232 LLNI_field_set_cls(rm, clazz , m->class);
234 /* The name needs to be interned */
235 /* XXX implement me better! */
237 LLNI_field_set_ref(rm, name , javastring_intern((java_lang_String *) javastring_new(m->name)));
238 LLNI_field_set_val(rm, slot , slot);
239 LLNI_field_set_ref(rm, annotations , method_get_annotations(m));
240 LLNI_field_set_ref(rm, parameterAnnotations, method_get_parameterannotations(m));
241 LLNI_field_set_ref(rm, annotationDefault , method_get_annotationdefault(m));
243 #elif defined(WITH_CLASSPATH_SUN)
245 LLNI_field_set_cls(rm, clazz , m->class);
247 /* The name needs to be interned */
248 /* XXX implement me better! */
250 LLNI_field_set_ref(rm, name , javastring_intern((java_lang_String *) javastring_new(m->name)));
251 LLNI_field_set_ref(rm, parameterTypes , method_get_parametertypearray(m));
252 LLNI_field_set_cls(rm, returnType , (java_lang_Class *) method_returntype_get(m));
253 LLNI_field_set_ref(rm, exceptionTypes , method_get_exceptionarray(m));
254 LLNI_field_set_val(rm, modifiers , m->flags & ACC_CLASS_REFLECT_MASK);
255 LLNI_field_set_val(rm, slot , slot);
256 LLNI_field_set_ref(rm, signature , m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
257 LLNI_field_set_ref(rm, annotations , method_get_annotations(m));
258 LLNI_field_set_ref(rm, parameterAnnotations, method_get_parameterannotations(m));
259 LLNI_field_set_ref(rm, annotationDefault , method_get_annotationdefault(m));
262 # error unknown classpath configuration
269 #if defined(WITH_CLASSPATH_GNU) && defined(ENABLE_ANNOTATIONS)
270 /* reflect_get_declaredannotatios *********************************************
272 Returns a java.util.Map<Class<? extends Annotation>, Annotation>
273 of the declared annotations.
275 *******************************************************************************/
277 struct java_util_Map* reflect_get_declaredannotatios(
278 java_handle_bytearray_t *annotations,
279 java_lang_Class *declaringClass,
282 static methodinfo *m_parseAnnotations = NULL;
283 utf *utf_parseAnnotations = NULL;
284 utf *utf_desc = NULL;
285 sun_reflect_ConstantPool *constantPool = NULL;
286 java_lang_Object *constantPoolOop = (java_lang_Object*)declaringClass;
289 (sun_reflect_ConstantPool*)native_new_and_init(
290 class_sun_reflect_ConstantPool);
292 if(constantPool == NULL) {
297 LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
299 /* only resolve the method the first time */
300 if (m_parseAnnotations == NULL) {
301 utf_parseAnnotations = utf_new_char("parseAnnotations");
302 utf_desc = utf_new_char(
303 "([BLsun/reflect/ConstantPool;Ljava/lang/Class;)"
306 if (utf_parseAnnotations == NULL || utf_desc == NULL) {
311 m_parseAnnotations = class_resolveclassmethod(
312 class_sun_reflect_annotation_AnnotationParser,
313 utf_parseAnnotations,
318 if (m_parseAnnotations == NULL) {
319 /* method not found */
324 return (struct java_util_Map*)vm_call_method(
325 m_parseAnnotations, NULL, annotations,
326 constantPool, declaringClass);
330 /* reflect_get_parameterannotations *******************************************
332 Parses and returns the parameter annotations of a method.
334 *******************************************************************************/
336 java_handle_objectarray_t* reflect_get_parameterannotations(
337 java_handle_t *parameterAnnotations,
339 java_lang_Class *declaringClass,
342 /* This method in java would be basically the following.
343 * We don't do it in java because we don't want to make a
344 * public method with wich you can get a ConstantPool, because
345 * with that you could read any kind of constants (even private
348 * ConstantPool constPool = new ConstantPool();
349 * constPool.constantPoolOop = getDeclaringClass();
350 * return sun.reflect.AnnotationParser.parseParameterAnnotations(
351 * parameterAnnotations,
353 * getDeclaringClass(),
354 * getParameterTypes().length);
356 static methodinfo *m_parseParameterAnnotations = NULL;
357 utf *utf_parseParameterAnnotations = NULL;
358 utf *utf_desc = NULL;
359 sun_reflect_ConstantPool *constantPool = NULL;
360 java_lang_Object *constantPoolOop = (java_lang_Object*)declaringClass;
362 methodinfo *m = NULL;
363 int32_t numParameters = -1;
365 /* get parameter count */
367 c = LLNI_classinfo_unwrap(declaringClass);
368 m = &(c->methods[slot]);
370 numParameters = method_get_parametercount(m);
372 if (numParameters < 0) {
373 /* error parsing descriptor */
377 /* get ConstantPool */
380 (sun_reflect_ConstantPool*)native_new_and_init(
381 class_sun_reflect_ConstantPool);
383 if(constantPool == NULL) {
388 LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
390 /* only resolve the method the first time */
391 if (m_parseParameterAnnotations == NULL) {
392 utf_parseParameterAnnotations = utf_new_char("parseParameterAnnotations");
393 utf_desc = utf_new_char(
394 "([BLsun/reflect/ConstantPool;Ljava/lang/Class;I)"
395 "[[Ljava/lang/annotation/Annotation;");
397 if (utf_parseParameterAnnotations == NULL || utf_desc == NULL) {
402 /* get parser method */
404 m_parseParameterAnnotations = class_resolveclassmethod(
405 class_sun_reflect_annotation_AnnotationParser,
406 utf_parseParameterAnnotations,
411 if (m_parseParameterAnnotations == NULL)
413 /* method not found */
418 return (java_handle_objectarray_t*)vm_call_method(
419 m_parseParameterAnnotations, NULL, parameterAnnotations,
420 constantPool, declaringClass, numParameters);
426 * These are local overrides for various environment variables in Emacs.
427 * Please do not remove this and leave it at the end of the file, where
428 * Emacs will automagically detect them.
429 * ---------------------------------------------------------------------
432 * indent-tabs-mode: t
436 * vim:noexpandtab:sw=4:ts=4: