* src/vm/javaobjects.cpp (java_lang_management_MemoryUsage): Added new
[cacao.git] / src / vm / javaobjects.cpp
1 /* src/vm/javaobjects.cpp - functions to create and access Java objects
2
3    Copyright (C) 2008, 2009 Theobroma Systems Ltd.
4
5    This file is part of CACAO.
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21
22 */
23
24
25 #include "config.h"
26
27 #include <stdint.h>
28
29 #include "native/vm/reflection.hpp"
30
31 #include "vm/access.hpp"
32 #include "vm/jit/builtin.hpp"
33 #include "vm/global.h"
34 #include "vm/globals.hpp"
35 #include "vm/initialize.hpp"
36 #include "vm/javaobjects.hpp"
37
38
39 #if defined(ENABLE_JAVASE)
40
41 /**
42  * Invokes the static Java method getSystemClassLoader().
43  *
44  * @return Return value of the invocation or NULL in
45  * case of an exception.
46  */
47 java_handle_t* java_lang_ClassLoader::invoke_getSystemClassLoader()
48 {
49         methodinfo    *m;
50         java_handle_t *clo;
51         classloader_t *cl;
52
53         assert(class_java_lang_Object);
54         assert(class_java_lang_ClassLoader);
55         assert(class_java_lang_ClassLoader->state & CLASS_LINKED);
56
57         m = class_resolveclassmethod(class_java_lang_ClassLoader,
58                                                                  utf_getSystemClassLoader,
59                                                                  utf_void__java_lang_ClassLoader,
60                                                                  class_java_lang_Object,
61                                                                  false);
62
63         if (m == NULL)
64                 return NULL;
65
66         clo = vm_call_method(m, NULL);
67
68         if (clo == NULL)
69                 return NULL;
70
71         cl = loader_hashtable_classloader_add(clo);
72
73         return cl;
74 }
75
76
77 /**
78  * Constructs a new instance of the class by calling the
79  * appropriate Java initializer.
80  */
81 java_lang_management_MemoryUsage::java_lang_management_MemoryUsage(int64_t init, int64_t used, int64_t commited, int64_t maximum)
82 {
83         // Load the class.
84         // XXX Maybe this should be made global at some points.
85         classinfo* class_java_lang_management_MemoryUsage;
86         if (!(class_java_lang_management_MemoryUsage = load_class_bootstrap(utf_new_char("java/lang/management/MemoryUsage"))))
87                 return;
88
89         // Find the appropriate initializer.
90         // XXX Maybe this should be made global at some points.
91         methodinfo* m = class_findmethod(class_java_lang_management_MemoryUsage,
92                                                                          utf_init,
93                                                                          utf_new_char("(JJJJ)V"));
94
95         if (m == NULL)
96                 return;
97
98         // Instantiate a new object.
99         _handle = builtin_new(class_java_lang_management_MemoryUsage);
100
101         if (is_null())
102                 return;
103
104         // Call initializer.
105         (void) vm_call_method(m, _handle, init, used, commited, maximum);
106 }
107
108
109 /**
110  * Constructs a Java object with the given
111  * java.lang.reflect.Constructor.
112  *
113  * @param args     Constructor arguments.
114  *
115  * @return Handle to Java object.
116  */
117 java_handle_t* java_lang_reflect_Constructor::new_instance(java_handle_objectarray_t* args)
118 {
119         methodinfo* m = get_method();
120
121         // Should we bypass security the checks (AccessibleObject)?
122         if (get_override() == false) {
123                 /* This method is always called like this:
124                        [0] java.lang.reflect.Constructor.constructNative (Native Method)
125                        [1] java.lang.reflect.Constructor.newInstance
126                        [2] <caller>
127                 */
128
129                 if (!access_check_method(m, 2))
130                         return NULL;
131         }
132
133         // Create a Java object.
134         java_handle_t* h = builtin_new(m->clazz);
135
136         if (h == NULL)
137                 return NULL;
138
139         // Call initializer.
140         (void) Reflection::invoke(m, h, args);
141
142         return h;
143 }
144
145
146 /**
147  * Invokes the given method.
148  *
149  * @param args Method arguments.
150  *
151  * @return return value of the method
152  */
153 java_handle_t* java_lang_reflect_Method::invoke(java_handle_t* o, java_handle_objectarray_t* args)
154 {
155         methodinfo* m = get_method();
156
157         // Should we bypass security the checks (AccessibleObject)?
158         if (get_override() == false) {
159 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
160                 /* This method is always called like this:
161                        [0] java.lang.reflect.Method.invokeNative (Native Method)
162                        [1] java.lang.reflect.Method.invoke (Method.java:329)
163                        [2] <caller>
164                 */
165
166                 if (!access_check_method(m, 2))
167                         return NULL;
168 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
169                 /* We only pass 1 here as stacktrace_get_caller_class, which
170                    is called from access_check_method, skips
171                    java.lang.reflect.Method.invoke(). */
172
173                 if (!access_check_method(m, 1))
174                         return NULL;
175 #else
176 # error unknown classpath configuration
177 #endif
178         }
179
180         // Check if method class is initialized.
181         if (!(m->clazz->state & CLASS_INITIALIZED))
182                 if (!initialize_class(m->clazz))
183                         return NULL;
184
185         // Call the Java method.
186         java_handle_t* result = Reflection::invoke(m, o, args);
187
188         return result;
189 }
190
191 #endif // ENABLE_JAVASE
192
193
194 /*
195  * These are local overrides for various environment variables in Emacs.
196  * Please do not remove this and leave it at the end of the file, where
197  * Emacs will automagically detect them.
198  * ---------------------------------------------------------------------
199  * Local variables:
200  * mode: c++
201  * indent-tabs-mode: t
202  * c-basic-offset: 4
203  * tab-width: 4
204  * End:
205  */