* src/vmcore/method.c (method_returntype_get): New function.
[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 8063 2007-06-11 14:44:58Z 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 #include "vmcore/method.h"
54
55
56 /* native methods implemented by this file ************************************/
57
58 static JNINativeMethod methods[] = {
59         { "getModifiersInternal", "()I",                                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getModifiersInternal },
60         { "getReturnType",        "()Ljava/lang/Class;",                                                         (void *) (ptrint) &Java_java_lang_reflect_Method_getReturnType        },
61         { "getParameterTypes",    "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getParameterTypes    },
62         { "getExceptionTypes",    "()[Ljava/lang/Class;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getExceptionTypes    },
63         { "invokeNative",         "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;", (void *) (ptrint) &Java_java_lang_reflect_Method_invokeNative         },
64         { "getSignature",         "()Ljava/lang/String;",                                                        (void *) (ptrint) &Java_java_lang_reflect_Method_getSignature         },
65 };
66
67
68 /* _Jv_java_lang_reflect_Method_init *******************************************
69
70    Register native functions.
71
72 *******************************************************************************/
73
74 void _Jv_java_lang_reflect_Method_init(void)
75 {
76         utf *u;
77
78         u = utf_new_char("java/lang/reflect/Method");
79
80         native_method_register(u, methods, NATIVE_METHODS_COUNT);
81 }
82
83
84 /*
85  * Class:     java/lang/reflect/Method
86  * Method:    getModifiersInternal
87  * Signature: ()I
88  */
89 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Method_getModifiersInternal(JNIEnv *env, java_lang_reflect_Method *this)
90 {
91         classinfo  *c;
92         methodinfo *m;
93
94         c = (classinfo *) this->declaringClass;
95         m = &(c->methods[this->slot]);
96
97         return m->flags;
98 }
99
100
101 /*
102  * Class:     java/lang/reflect/Method
103  * Method:    getReturnType
104  * Signature: ()Ljava/lang/Class;
105  */
106 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_reflect_Method_getReturnType(JNIEnv *env, java_lang_reflect_Method *this)
107 {
108         classinfo  *c;
109         methodinfo *m;
110         classinfo  *result;
111
112         c = (classinfo *) this->declaringClass;
113         m = &(c->methods[this->slot]);
114
115         result = method_returntype_get(m);
116
117         return (java_lang_Class *) result;
118 }
119
120
121 /*
122  * Class:     java/lang/reflect/Method
123  * Method:    getParameterTypes
124  * Signature: ()[Ljava/lang/Class;
125  */
126 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getParameterTypes(JNIEnv *env, java_lang_reflect_Method *this)
127 {
128         classinfo  *c;
129         methodinfo *m;
130
131         c = (classinfo *) this->declaringClass;
132         m = &(c->methods[this->slot]);
133
134         return method_get_parametertypearray(m);
135 }
136
137
138 /*
139  * Class:     java/lang/reflect/Method
140  * Method:    getExceptionTypes
141  * Signature: ()[Ljava/lang/Class;
142  */
143 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Method_getExceptionTypes(JNIEnv *env, java_lang_reflect_Method *this)
144 {
145         classinfo  *c;
146         methodinfo *m;
147
148         c = (classinfo *) this->declaringClass;
149         m = &(c->methods[this->slot]);
150
151         return method_get_exceptionarray(m);
152 }
153
154
155 /*
156  * Class:     java/lang/reflect/Method
157  * Method:    invokeNative
158  * Signature: (Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;I)Ljava/lang/Object;
159  */
160 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)
161 {
162         classinfo        *c;
163         methodinfo       *m;
164
165         c = (classinfo *) declaringClass;
166         m = &(c->methods[slot]);
167
168         /* check method access */
169
170         /* check if we should bypass security checks (AccessibleObject) */
171
172         if (this->flag == false) {
173                 if (!access_check_method(m, 1))
174                         return NULL;
175         }
176
177         /* check if method class is initialized */
178
179         if (!(c->state & CLASS_INITIALIZED))
180                 if (!initialize_class(c))
181                         return NULL;
182
183         /* call the Java method via a helper function */
184
185         return (java_lang_Object *) _Jv_jni_invokeNative(m, (jobject) o, args);
186 }
187
188
189 /*
190  * Class:     java/lang/reflect/Method
191  * Method:    getSignature
192  * Signature: ()Ljava/lang/String;
193  */
194 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Method_getSignature(JNIEnv *env, java_lang_reflect_Method* this)
195 {
196         classinfo         *c;
197         methodinfo        *m;
198         java_objectheader *o;
199
200         c = (classinfo *) this->declaringClass;
201         m = &(c->methods[this->slot]);
202
203         if (m->signature == NULL)
204                 return NULL;
205
206         o = javastring_new(m->signature);
207
208         /* in error case o is NULL */
209
210         return (java_lang_String *) o;
211 }
212
213
214 /*
215  * These are local overrides for various environment variables in Emacs.
216  * Please do not remove this and leave it at the end of the file, where
217  * Emacs will automagically detect them.
218  * ---------------------------------------------------------------------
219  * Local variables:
220  * mode: c
221  * indent-tabs-mode: t
222  * c-basic-offset: 4
223  * tab-width: 4
224  * End:
225  */