1 /* src/vmcore/javaobjects.cpp - functions to create and access Java objects
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
31 #include "native/vm/reflection.hpp"
33 #include "vm/access.h"
34 #include "vm/builtin.h"
35 #include "vm/global.h"
37 #include "vmcore/globals.hpp"
38 #include "vmcore/javaobjects.hpp"
42 * Allocates a new java.lang.reflect.Constructor object and
43 * initializes the fields with the method passed.
45 java_handle_t* java_lang_reflect_Constructor::create(methodinfo *m)
49 /* Allocate a java.lang.reflect.Constructor object. */
51 h = builtin_new(class_java_lang_reflect_Constructor);
56 java_lang_reflect_Constructor rc(h);
58 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
60 /* Allocate a java.lang.reflect.VMConstructor object. */
62 h = builtin_new(class_java_lang_reflect_VMConstructor);
67 java_lang_reflect_VMConstructor rvmc(h, m);
69 // Link the two Java objects.
71 rc.set_cons(rvmc.get_handle());
72 rvmc.set_cons(rc.get_handle());
74 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
76 /* Calculate the slot. */
78 int slot = m - m->clazz->methods;
80 /* Set Java object instance fields. */
82 LLNI_field_set_cls(rc, clazz , m->clazz);
83 LLNI_field_set_ref(rc, parameterTypes , method_get_parametertypearray(m));
84 LLNI_field_set_ref(rc, exceptionTypes , method_get_exceptionarray(m));
85 LLNI_field_set_val(rc, modifiers , m->flags & ACC_CLASS_REFLECT_MASK);
86 LLNI_field_set_val(rc, slot , slot);
87 LLNI_field_set_ref(rc, signature , m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
88 LLNI_field_set_ref(rc, annotations , method_get_annotations(m));
89 LLNI_field_set_ref(rc, parameterAnnotations, method_get_parameterannotations(m));
92 # error unknown classpath configuration
95 return rc.get_handle();
100 * Constructs a Java object with the given
101 * java.lang.reflect.Constructor.
103 * @param m Method structure of the constructor.
104 * @param args Constructor arguments.
105 * @param override Override security checks.
107 * @return Handle to Java object.
109 java_handle_t* java_lang_reflect_Constructor::new_instance(methodinfo* m, java_handle_objectarray_t* args, bool override)
113 // Should we bypass security the checks (AccessibleObject)?
114 if (override == false) {
115 /* This method is always called like this:
116 [0] java.lang.reflect.Constructor.constructNative (Native Method)
117 [1] java.lang.reflect.Constructor.newInstance
121 if (!access_check_method(m, 2))
125 // Create a Java object.
126 h = builtin_new(m->clazz);
132 (void) Reflection::invoke(m, h, args);
139 * Creates a java.lang.reflect.Field object on the GC heap and
140 * intializes it with the given field.
142 * @param f Field structure.
144 * @return Handle to Java object.
146 java_handle_t* java_lang_reflect_Field::create(fieldinfo* f)
150 /* Allocate a java.lang.reflect.Field object. */
152 h = builtin_new(class_java_lang_reflect_Field);
157 java_lang_reflect_Field rf(h);
159 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
161 // Allocate a java.lang.reflect.VMField object.
163 h = builtin_new(class_java_lang_reflect_VMField);
168 java_lang_reflect_VMField rvmf(h, f);
170 // Link the two Java objects.
172 rf.set_f(rvmf.get_handle());
173 rvmf.set_f(rf.get_handle());
175 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
177 /* Calculate the slot. */
179 int slot = f - f->clazz->fields;
181 /* Set the Java object fields. */
183 LLNI_field_set_cls(rf, clazz, f->clazz);
185 /* The name needs to be interned */
186 /* XXX implement me better! */
188 LLNI_field_set_ref(rf, name, (java_lang_String *) javastring_intern(javastring_new(f->name)));
189 LLNI_field_set_cls(rf, type, (java_lang_Class *) field_get_type(f));
190 LLNI_field_set_val(rf, modifiers, f->flags);
191 LLNI_field_set_val(rf, slot, slot);
192 LLNI_field_set_ref(rf, signature, f->signature ? (java_lang_String *) javastring_new(f->signature) : NULL);
193 LLNI_field_set_ref(rf, annotations, field_get_annotations(f));
196 # error unknown classpath configuration
199 return rf.get_handle();
204 * Allocates a new java.lang.reflect.Method object and initializes the
205 * fields with the method passed.
207 * @param m Method structure.
209 * @return Handle to Java object.
211 java_handle_t* java_lang_reflect_Method::create(methodinfo *m)
215 /* Allocate a java.lang.reflect.Method object. */
217 h = builtin_new(class_java_lang_reflect_Method);
222 java_lang_reflect_Method rm(h);
224 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
226 /* Allocate a java.lang.reflect.VMMethod object. */
228 h = builtin_new(class_java_lang_reflect_VMMethod);
233 java_lang_reflect_VMMethod rvmm(h, m);
235 // Link the two Java objects.
237 rm.set_m(rvmm.get_handle());
238 rvmm.set_m(rm.get_handle());
240 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
242 /* Calculate the slot. */
244 int slot = m - m->clazz->methods;
246 LLNI_field_set_cls(rm, clazz, m->clazz);
248 /* The name needs to be interned */
249 /* XXX implement me better! */
251 LLNI_field_set_ref(rm, name, (java_lang_String *) javastring_intern(javastring_new(m->name)));
252 LLNI_field_set_ref(rm, parameterTypes, method_get_parametertypearray(m));
253 LLNI_field_set_cls(rm, returnType, (java_lang_Class *) method_returntype_get(m));
254 LLNI_field_set_ref(rm, exceptionTypes, method_get_exceptionarray(m));
255 LLNI_field_set_val(rm, modifiers, m->flags & ACC_CLASS_REFLECT_MASK);
256 LLNI_field_set_val(rm, slot, slot);
257 LLNI_field_set_ref(rm, signature, m->signature ? (java_lang_String *) javastring_new(m->signature) : NULL);
258 LLNI_field_set_ref(rm, annotations, method_get_annotations(m));
259 LLNI_field_set_ref(rm, parameterAnnotations, method_get_parameterannotations(m));
260 LLNI_field_set_ref(rm, annotationDefault, method_get_annotationdefault(m));
263 # error unknown classpath configuration
266 return rm.get_handle();
270 // Legacy C interface.
273 java_handle_t* java_lang_reflect_Constructor_create(methodinfo* m) { return java_lang_reflect_Constructor::create(m); }
274 java_handle_t* java_lang_reflect_Field_create(fieldinfo* f) { return java_lang_reflect_Field::create(f); }
275 java_handle_t* java_lang_reflect_Method_create(methodinfo* m) { return java_lang_reflect_Method::create(m); }
280 * These are local overrides for various environment variables in Emacs.
281 * Please do not remove this and leave it at the end of the file, where
282 * Emacs will automagically detect them.
283 * ---------------------------------------------------------------------
286 * indent-tabs-mode: t