ccdfc4f85070d874e7888a340e909d8523622273
[cacao.git] / src / vmcore / javaobjects.cpp
1 /* src/vmcore/javaobjects.cpp - functions to create and access Java objects
2
3    Copyright (C) 2008 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 // REMOVEME
30 #include "native/vm/reflection.hpp"
31
32 #include "vm/access.h"
33 #include "vm/builtin.h"
34 #include "vm/global.h"
35 #include "vm/initialize.h"
36
37 #include "vmcore/globals.hpp"
38 #include "vmcore/javaobjects.hpp"
39
40
41 #if defined(ENABLE_JAVASE)
42
43 /**
44  * Constructs a Java object with the given
45  * java.lang.reflect.Constructor.
46  *
47  * @param args     Constructor arguments.
48  *
49  * @return Handle to Java object.
50  */
51 java_handle_t* java_lang_reflect_Constructor::new_instance(java_handle_objectarray_t* args)
52 {
53         methodinfo* m = get_method();
54
55         // Should we bypass security the checks (AccessibleObject)?
56         if (get_override() == false) {
57                 /* This method is always called like this:
58                        [0] java.lang.reflect.Constructor.constructNative (Native Method)
59                        [1] java.lang.reflect.Constructor.newInstance
60                        [2] <caller>
61                 */
62
63                 if (!access_check_method(m, 2))
64                         return NULL;
65         }
66
67         // Create a Java object.
68         java_handle_t* h = builtin_new(m->clazz);
69
70         if (h == NULL)
71                 return NULL;
72         
73         // Call initializer.
74         (void) Reflection::invoke(m, h, args);
75
76         return h;
77 }
78
79
80 /**
81  * Invokes the given method.
82  *
83  * @param args Method arguments.
84  *
85  * @return return value of the method
86  */
87 java_handle_t* java_lang_reflect_Method::invoke(java_handle_t* o, java_handle_objectarray_t* args)
88 {
89         methodinfo* m = get_method();
90
91         // Should we bypass security the checks (AccessibleObject)?
92         if (get_override() == false) {
93 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
94                 /* This method is always called like this:
95                        [0] java.lang.reflect.Method.invokeNative (Native Method)
96                        [1] java.lang.reflect.Method.invoke (Method.java:329)
97                        [2] <caller>
98                 */
99
100                 if (!access_check_method(m, 2))
101                         return NULL;
102 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
103                 /* We only pass 1 here as stacktrace_get_caller_class, which
104                    is called from access_check_method, skips
105                    java.lang.reflect.Method.invoke(). */
106
107                 if (!access_check_method(m, 1))
108                         return NULL;
109 #else
110 # error unknown classpath configuration
111 #endif
112         }
113
114         // Check if method class is initialized.
115         if (!(m->clazz->state & CLASS_INITIALIZED))
116                 if (!initialize_class(m->clazz))
117                         return NULL;
118
119         // Call the Java method.
120         java_handle_t* result = Reflection::invoke(m, o, args);
121
122         return result;
123 }
124
125
126 // Legacy C interface.
127
128 extern "C" {
129         java_handle_t* java_lang_reflect_Constructor_create(methodinfo* m) { return java_lang_reflect_Constructor(m).get_handle(); }
130         java_handle_t* java_lang_reflect_Field_create(fieldinfo* f) { return java_lang_reflect_Field(f).get_handle(); }
131         java_handle_t* java_lang_reflect_Method_create(methodinfo* m) { return java_lang_reflect_Method(m).get_handle(); }
132 }
133
134 #endif // ENABLE_JAVASE
135
136
137 /*
138  * These are local overrides for various environment variables in Emacs.
139  * Please do not remove this and leave it at the end of the file, where
140  * Emacs will automagically detect them.
141  * ---------------------------------------------------------------------
142  * Local variables:
143  * mode: c++
144  * indent-tabs-mode: t
145  * c-basic-offset: 4
146  * tab-width: 4
147  * End:
148  */