Arghhh, small type in #if.
[cacao.git] / src / native / vm / Method.c
1 /* 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
31    $Id: Method.c 1801 2004-12-21 20:19:19Z jowenn $
32
33 */
34
35
36 #include "types.h"
37 #include "native/jni.h"
38 #include "native/native.h"
39 #include "native/include/java_lang_Object.h"
40 #include "native/include/java_lang_Class.h"
41 #include "native/include/java_lang_reflect_Method.h"
42 #include "toolbox/logging.h"
43 #include "vm/global.h"
44 #include "vm/builtin.h"
45 #include "vm/jit/stacktrace.h"
46 #include "vm/exceptions.h"
47
48 /*
49  * Class:     java_lang_reflect_Method
50  * Method:    getModifiers
51  * Signature: ()I
52  */
53 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiers(JNIEnv *env, java_lang_reflect_Method *this)
54 {
55         classinfo *c = (classinfo *) this->declaringClass;
56
57         if (this->slot < 0 || this->slot >= c->methodscount)
58                 panic("error illegal slot for method in class (getReturnType)");
59         
60         return (c->methods[this->slot]).flags &
61                 (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_ABSTRACT | ACC_STATIC | ACC_FINAL |
62                  ACC_SYNCHRONIZED | ACC_NATIVE | ACC_STRICT);
63 }
64
65
66 /*
67  * Class:     java_lang_reflect_Method
68  * Method:    getReturnType
69  * Signature: ()Ljava/lang/Class;
70  */
71 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(JNIEnv *env, java_lang_reflect_Method *this)
72 {
73         classinfo *c = (classinfo *) this->declaringClass;
74
75         if (this->slot < 0 || this->slot >= c->methodscount)
76                 panic("error illegal slot for method in class (getReturnType)");
77
78         return (java_lang_Class *) get_returntype(&(c->methods[this->slot]));
79 }
80
81
82 /*
83  * Class:     java_lang_reflect_Method
84  * Method:    getParameterTypes
85  * Signature: ()[Ljava/lang/Class;
86  */
87 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
88 {
89         classinfo *c = (classinfo *) this->declaringClass;
90
91         if (this->slot < 0 || this->slot >= c->methodscount)
92                 panic("error illegal slot for method in class(getParameterTypes)");
93
94         return get_parametertypes(&(c->methods[this->slot]));
95 }
96
97
98 /*
99  * Class:     java_lang_reflect_Method
100  * Method:    getExceptionTypes
101  * Signature: ()[Ljava/lang/Class;
102  */
103 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
104 {
105         classinfo *c = (classinfo *) this->declaringClass;
106
107         if (this->slot < 0 || this->slot >= c->methodscount)
108                 panic("error illegal slot for method in class(getExceptionTypes)");
109
110         return get_exceptiontypes(&(c->methods[this->slot]));
111
112 }
113
114
115 /*
116  * Class:     java_lang_reflect_Method
117  * Method:    invokeNative
118  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
119  */
120 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Method_invokeNative(JNIEnv *env, java_lang_reflect_Method *this, java_lang_Object *obj, java_objectarray *params, java_lang_Class *declaringClass, s4 slot)
121 {
122         struct methodinfo *mi;
123
124         classinfo *c = (classinfo *) declaringClass;
125
126         if (slot < 0 || slot >= c->methodscount) {
127                 panic("error illegal slot for method in class(getParameterTypes)");
128         }
129         mi = &(c->methods[slot]);
130
131 #if (defined(__ALPHA__) || defined(__I386__))
132         /*log_text("Checking access rights");*/
133         if (!(getField(this,jboolean,getFieldID_critical(env,this->header.vftbl->class,"flag","Z")))) {
134                 int throwAccess=0;
135                 struct methodinfo *callingMethod;
136
137                 if ((mi->flags & ACC_PUBLIC)==0) {
138                         callingMethod=cacao_callingMethod();
139                         if ((mi->flags & ACC_PRIVATE)!=0) {
140                                 if (c!=callingMethod->class) {
141                                         throwAccess=1;
142                                 }
143                         } else {
144                                 if ((mi->flags & ACC_PROTECTED)!=0) {
145                                         if (!builtin_isanysubclass(callingMethod->class, c)) {
146                                                 throwAccess=1;
147                                         } 
148                                 } else {
149                                         /* default visibility*/
150                                         if (c->packagename!=callingMethod->class->packagename) {
151                                                 throwAccess=1;
152                                         }
153                                 }
154                         }
155                 }
156                 if (throwAccess) {
157                         *exceptionptr=0;
158                         *exceptionptr = new_exception(string_java_lang_IllegalAccessException);
159                         return 0;
160                 }
161
162         }
163
164 #endif
165
166     return (java_lang_Object *) jni_method_invokeNativeHelper(env, mi, (jobject) obj, params);
167 }
168
169
170 /*
171  * These are local overrides for various environment variables in Emacs.
172  * Please do not remove this and leave it at the end of the file, where
173  * Emacs will automagically detect them.
174  * ---------------------------------------------------------------------
175  * Local variables:
176  * mode: c
177  * indent-tabs-mode: t
178  * c-basic-offset: 4
179  * tab-width: 4
180  * End:
181  */