15f199493015464079a1d8cd724da97da31bb552
[cacao.git] / src / native / vm / Method.c
1 /* src/native/vm/Method.c - java/lang/reflect/Method
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
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.
14
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.
19
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: Method.c 4331 2006-01-20 14:49:53Z twisti $
33
34 */
35
36
37 #include <assert.h>
38
39 #include "vm/types.h"
40
41 #include "native/jni.h"
42 #include "native/native.h"
43 #include "native/include/java_lang_Object.h"
44 #include "native/include/java_lang_Class.h"
45 #include "native/include/java_lang_reflect_Method.h"
46 #include "toolbox/logging.h"
47 #include "vm/access.h"
48 #include "vm/global.h"
49 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/initialize.h"
52 #include "vm/stringlocal.h"
53 #include "vm/jit/stacktrace.h"
54
55
56 /*
57  * Class:     java/lang/reflect/Method
58  * Method:    getModifiers
59  * Signature: ()I
60  */
61 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiers(JNIEnv *env, java_lang_reflect_Method *this)
62 {
63         classinfo  *c;
64         methodinfo *m;
65
66         c = (classinfo *) this->declaringClass;
67         m = &(c->methods[this->slot]);
68
69         return (m->flags &
70                         (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_ABSTRACT |
71                          ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED | ACC_NATIVE |
72                          ACC_STRICT));
73 }
74
75
76 /*
77  * Class:     java/lang/reflect/Method
78  * Method:    getReturnType
79  * Signature: ()Ljava/lang/Class;
80  */
81 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(JNIEnv *env, java_lang_reflect_Method *this)
82 {
83         classinfo  *c;
84         methodinfo *m;
85
86         c = (classinfo *) this->declaringClass;
87         m = &(c->methods[this->slot]);
88
89         return (java_lang_Class *) native_get_returntype(m);
90 }
91
92
93 /*
94  * Class:     java/lang/reflect/Method
95  * Method:    getParameterTypes
96  * Signature: ()[Ljava/lang/Class;
97  */
98 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
99 {
100         classinfo  *c;
101         methodinfo *m;
102
103         c = (classinfo *) this->declaringClass;
104         m = &(c->methods[this->slot]);
105
106         return native_get_parametertypes(m);
107 }
108
109
110 /*
111  * Class:     java/lang/reflect/Method
112  * Method:    getExceptionTypes
113  * Signature: ()[Ljava/lang/Class;
114  */
115 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
116 {
117         classinfo  *c;
118         methodinfo *m;
119
120         c = (classinfo *) this->declaringClass;
121         m = &(c->methods[this->slot]);
122
123         return native_get_exceptiontypes(m);
124 }
125
126
127 /*
128  * Class:     java/lang/reflect/Method
129  * Method:    invokeNative
130  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
131  */
132 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Method_invokeNative(JNIEnv *env, java_lang_reflect_Method *this, java_lang_Object *o, java_objectarray *args, java_lang_Class *declaringClass, s4 slot)
133 {
134         classinfo        *c;
135         methodinfo       *m;
136         java_objectarray *oa;
137         classinfo        *callerclass;
138
139         c = (classinfo *) declaringClass;
140         m = &(c->methods[slot]);
141
142         /* check method access */
143
144         if (!(m->flags & ACC_PUBLIC) || !(c->flags & ACC_PUBLIC)) {
145                 /* check if we should bypass security checks (AccessibleObject) */
146
147                 if (this->flag == false) {
148                         /* get the calling class */
149
150                         oa = cacao_createClassContextArray();
151
152                         /* this function is always called like this:
153
154                                java.lang.reflect.Method.invokeNative (Native Method)
155                            [0] java.lang.reflect.Method.invoke (Method.java:329)
156                            [1] <caller>
157                         */
158
159                         callerclass = (classinfo *) oa->data[1];
160
161                         if (!access_is_accessible_class(callerclass,c)
162                                 || !access_is_accessible_member(callerclass, c, m->flags)) 
163                         {
164                                 *exceptionptr =
165                                         new_exception(string_java_lang_IllegalAccessException);
166                                 return NULL;
167                         }
168                 }
169         }
170
171         /* check if method class is initialized */
172
173         if (!(c->state & CLASS_INITIALIZED))
174                 if (!initialize_class(c))
175                         return NULL;
176
177         /* call the Java method via a helper function */
178
179         return (java_lang_Object *) _Jv_jni_invokeNative(m, (jobject) o, args);
180 }
181
182
183 /*
184  * These are local overrides for various environment variables in Emacs.
185  * Please do not remove this and leave it at the end of the file, where
186  * Emacs will automagically detect them.
187  * ---------------------------------------------------------------------
188  * Local variables:
189  * mode: c
190  * indent-tabs-mode: t
191  * c-basic-offset: 4
192  * tab-width: 4
193  * End:
194  */