* src/vm/access.c (string.h): Added.
[cacao.git] / src / native / vm / gnu / java_lang_reflect_Method.c
1 /* src/native/vm/gnu/java_lang_reflect_Method.c
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: java_lang_reflect_Method.c 7976 2007-05-29 12:22:55Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "native/jni.h"
37 #include "native/native.h"
38
39 #include "native/include/java_lang_Object.h"
40 #include "native/include/java_lang_Class.h"
41 #include "native/include/java_lang_String.h"
42
43 #include "native/include/java_lang_reflect_Method.h"
44
45 #include "vm/access.h"
46 #include "vm/global.h"
47 #include "vm/builtin.h"
48 #include "vm/exceptions.h"
49 #include "vm/initialize.h"
50 #include "vm/resolve.h"
51 #include "vm/stringlocal.h"
52
53
54 /* native methods implemented by this file ************************************/
55
56 static JNINativeMethod methods[] = {
57         { "getModifiersInternal", "()I",                                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getModifiersInternal },
58         { "getReturnType",        "()Ljava/lang/Class;",                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getReturnType        },
59         { "getParameterTypes",    "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterTypes    },
60         { "getExceptionTypes",    "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getExceptionTypes    },
61         { "invokeNative",         "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;", (void *) (ptrint) &Java_java_lang_reflect_Method_invokeNative         },
62         { "getSignature",         "()Ljava/lang/String;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getSignature         },
63 };
64
65
66 /* _Jv_java_lang_reflect_Method_init *******************************************
67
68    Register native functions.
69
70 *******************************************************************************/
71
72 void _Jv_java_lang_reflect_Method_init(void)
73 {
74         utf *u;
75
76         u = utf_new_char("java/lang/reflect/Method");
77
78         native_method_register(u, methods, NATIVE_METHODS_COUNT);
79 }
80
81
82 /*
83  * Class:     java/lang/reflect/Method
84  * Method:    getModifiersInternal
85  * Signature: ()I
86  */
87 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiersInternal(JNIEnv *env, java_lang_reflect_Method *this)
88 {
89         classinfo  *c;
90         methodinfo *m;
91
92         c = (classinfo *) this->declaringClass;
93         m = &(c->methods[this->slot]);
94
95         return m->flags;
96 }
97
98
99 /*
100  * Class:     java/lang/reflect/Method
101  * Method:    getReturnType
102  * Signature: ()Ljava/lang/Class;
103  */
104 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(JNIEnv *env, java_lang_reflect_Method *this)
105 {
106         classinfo  *c;
107         methodinfo *m;
108         typedesc   *td;
109
110         c = (classinfo *) this->declaringClass;
111         m = &(c->methods[this->slot]);
112
113         td = &(m->parseddesc->returntype);
114
115         if (!resolve_class_from_typedesc(td, true, false, &c))
116                 return NULL;
117
118         return (java_lang_Class *) c;
119 }
120
121
122 /*
123  * Class:     java/lang/reflect/Method
124  * Method:    getParameterTypes
125  * Signature: ()[Ljava/lang/Class;
126  */
127 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
128 {
129         classinfo  *c;
130         methodinfo *m;
131
132         c = (classinfo *) this->declaringClass;
133         m = &(c->methods[this->slot]);
134
135         return method_get_parametertypearray(m);
136 }
137
138
139 /*
140  * Class:     java/lang/reflect/Method
141  * Method:    getExceptionTypes
142  * Signature: ()[Ljava/lang/Class;
143  */
144 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
145 {
146         classinfo  *c;
147         methodinfo *m;
148
149         c = (classinfo *) this->declaringClass;
150         m = &(c->methods[this->slot]);
151
152         return method_get_exceptionarray(m);
153 }
154
155
156 /*
157  * Class:     java/lang/reflect/Method
158  * Method:    invokeNative
159  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
160  */
161 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)
162 {
163         classinfo        *c;
164         methodinfo       *m;
165
166         c = (classinfo *) declaringClass;
167         m = &(c->methods[slot]);
168
169         /* check method access */
170
171         /* check if we should bypass security checks (AccessibleObject) */
172
173         if (this->flag == false) {
174                 if (!access_check_method(m, 1))
175                         return NULL;
176         }
177
178         /* check if method class is initialized */
179
180         if (!(c->state & CLASS_INITIALIZED))
181                 if (!initialize_class(c))
182                         return NULL;
183
184         /* call the Java method via a helper function */
185
186         return (java_lang_Object *) _Jv_jni_invokeNative(m, (jobject) o, args);
187 }
188
189
190 /*
191  * Class:     java/lang/reflect/Method
192  * Method:    getSignature
193  * Signature: ()Ljava/lang/String;
194  */
195 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Method_getSignature(JNIEnv *env, java_lang_reflect_Method* this)
196 {
197         classinfo         *c;
198         methodinfo        *m;
199         java_objectheader *o;
200
201         c = (classinfo *) this->declaringClass;
202         m = &(c->methods[this->slot]);
203
204         if (m->signature == NULL)
205                 return NULL;
206
207         o = javastring_new(m->signature);
208
209         /* in error case o is NULL */
210
211         return (java_lang_String *) o;
212 }
213
214
215 /*
216  * These are local overrides for various environment variables in Emacs.
217  * Please do not remove this and leave it at the end of the file, where
218  * Emacs will automagically detect them.
219  * ---------------------------------------------------------------------
220  * Local variables:
221  * mode: c
222  * indent-tabs-mode: t
223  * c-basic-offset: 4
224  * tab-width: 4
225  * End:
226  */