1 /* src/vm/javaobjects.cpp - functions to create and access Java objects
3 Copyright (C) 2010, 2011
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008, 2009 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #include "native/vm/reflection.hpp"
33 #include "vm/access.hpp"
34 #include "vm/jit/builtin.hpp"
35 #include "vm/global.h"
36 #include "vm/globals.hpp"
37 #include "vm/initialize.hpp"
38 #include "vm/javaobjects.hpp"
42 #if defined(ENABLE_JAVASE)
45 * Invokes the static Java method getSystemClassLoader().
47 * @return Return value of the invocation or NULL in
48 * case of an exception.
50 java_handle_t* java_lang_ClassLoader::invoke_getSystemClassLoader()
56 assert(class_java_lang_Object);
57 assert(class_java_lang_ClassLoader);
58 assert(class_java_lang_ClassLoader->state & CLASS_LINKED);
60 m = class_resolveclassmethod(class_java_lang_ClassLoader,
61 utf_getSystemClassLoader,
62 utf_void__java_lang_ClassLoader,
63 class_java_lang_Object,
69 clo = vm_call_method(m, NULL);
74 cl = loader_hashtable_classloader_add(clo);
81 * Constructs a new instance of the class by calling the
82 * appropriate Java initializer.
84 java_lang_management_MemoryUsage::java_lang_management_MemoryUsage(int64_t init, int64_t used, int64_t commited, int64_t maximum)
87 // XXX Maybe this should be made global at some points.
88 classinfo* class_java_lang_management_MemoryUsage;
89 if (!(class_java_lang_management_MemoryUsage = load_class_bootstrap(utf_new_char("java/lang/management/MemoryUsage"))))
92 // Find the appropriate initializer.
93 // XXX Maybe this should be made global at some points.
94 methodinfo* m = class_findmethod(class_java_lang_management_MemoryUsage,
96 utf_new_char("(JJJJ)V"));
101 // Instantiate a new object.
102 _handle = builtin_new(class_java_lang_management_MemoryUsage);
108 (void) vm_call_method(m, _handle, init, used, commited, maximum);
113 * Constructs a Java object with the given
114 * java.lang.reflect.Constructor.
116 * @param args Constructor arguments.
118 * @return Handle to Java object.
120 java_handle_t* java_lang_reflect_Constructor::new_instance(java_handle_objectarray_t* args)
122 methodinfo* m = get_method();
124 // Should we bypass security the checks (AccessibleObject)?
125 if (get_override() == false) {
126 /* This method is always called like this:
127 [0] java.lang.reflect.Constructor.constructNative (Native Method)
128 [1] java.lang.reflect.Constructor.newInstance
132 if (!access_check_method(m, 2))
136 // Create a Java object.
137 java_handle_t* h = builtin_new(m->clazz);
143 (void) Reflection::invoke(m, h, args);
150 * Invokes the given method.
152 * @param args Method arguments.
154 * @return return value of the method
156 java_handle_t* java_lang_reflect_Method::invoke(java_handle_t* o, java_handle_objectarray_t* args)
158 methodinfo* m = get_method();
160 // Should we bypass security the checks (AccessibleObject)?
161 if (get_override() == false) {
162 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
163 /* This method is always called like this:
164 [0] java.lang.reflect.Method.invokeNative (Native Method)
165 [1] java.lang.reflect.Method.invoke (Method.java:329)
169 if (!access_check_method(m, 2))
171 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
172 /* We only pass 1 here as stacktrace_get_caller_class, which
173 is called from access_check_method, skips
174 java.lang.reflect.Method.invoke(). */
176 if (!access_check_method(m, 1))
179 # error unknown classpath configuration
183 // Check if method class is initialized.
184 if (!(m->clazz->state & CLASS_INITIALIZED))
185 if (!initialize_class(m->clazz))
188 // Call the Java method.
189 java_handle_t* result = Reflection::invoke(m, o, args);
194 struct DynOffsetEntry {
195 void (*setter)(int32_t);
199 typedef std::map<classinfo *, DynOffsetEntry *> RegisteredDynMap;
200 static RegisteredDynMap dynEntryMap;
202 static void register_dyn_entry_table(classinfo *c, DynOffsetEntry *entries)
204 dynEntryMap.insert(std::make_pair(c, entries));
207 static bool runAllSetters(classinfo *c, DynOffsetEntry entries[])
210 fieldinfo *fi = class_findfield_by_name(c, utf_new_char(entries->name));
213 entries->setter(fi->offset);
214 } while ((++entries)->setter);
218 bool jobjects_run_dynoffsets_hook(classinfo *c)
220 RegisteredDynMap::const_iterator it = dynEntryMap.find(c);
221 if (it == dynEntryMap.end())
224 if (!runAllSetters(c, it->second))
231 void jobjects_register_dyn_offsets()
235 #endif // ENABLE_JAVASE
239 * These are local overrides for various environment variables in Emacs.
240 * Please do not remove this and leave it at the end of the file, where
241 * Emacs will automagically detect them.
242 * ---------------------------------------------------------------------
245 * indent-tabs-mode: t
249 * vim:noexpandtab:sw=4:ts=4: