805153adae01ad4f3898c05424513c4b3e3319cb
[cacao.git] / src / native / vm / java_lang_reflect_Constructor.c
1 /* src/native/vm/java_lang_reflect_Constructor.c
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Roman Obermaiser
28             Joseph Wenninger
29             Christian Thalinger
30
31    $Id: java_lang_reflect_Constructor.c 6168 2006-12-11 00:28:17Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <stdlib.h>
40
41 #include "vm/types.h"
42
43 #include "native/jni.h"
44 #include "native/native.h"
45 #include "native/include/java_lang_Class.h"
46 #include "native/include/java_lang_Object.h"
47 #include "native/include/java_lang_String.h"
48 #include "native/include/java_lang_reflect_Constructor.h"
49 #include "toolbox/logging.h"
50 #include "vm/class.h"
51 #include "vm/exceptions.h"
52 #include "vm/method.h"
53 #include "vm/access.h"
54 #include "vm/stringlocal.h"
55
56
57 /*
58  * Class:     java/lang/reflect/Constructor
59  * Method:    getModifiersInternal
60  * Signature: ()I
61  */
62 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Constructor_getModifiersInternal(JNIEnv *env, java_lang_reflect_Constructor *this)
63 {
64         classinfo  *c;
65         methodinfo *m;
66
67         c = (classinfo *) (this->clazz);
68         m = &(c->methods[this->slot]);
69
70         return m->flags;
71 }
72
73
74 /*
75  * Class:     java/lang/reflect/Constructor
76  * Method:    getParameterTypes
77  * Signature: ()[Ljava/lang/Class;
78  */
79 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Constructor_getParameterTypes(JNIEnv *env, java_lang_reflect_Constructor *this)
80 {
81         classinfo  *c;
82         methodinfo *m;
83
84         c = (classinfo *) this->clazz;
85         m = &(c->methods[this->slot]);
86
87         return native_get_parametertypes(m);
88 }
89
90
91 /*
92  * Class:     java/lang/reflect/Constructor
93  * Method:    getExceptionTypes
94  * Signature: ()[Ljava/lang/Class;
95  */
96 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Constructor_getExceptionTypes(JNIEnv *env, java_lang_reflect_Constructor *this)
97 {
98         classinfo  *c;
99         methodinfo *m;
100
101         c = (classinfo *) this->clazz;
102         m = &(c->methods[this->slot]);
103
104         return native_get_exceptiontypes(m);
105 }
106
107
108 /*
109  * Class:     java/lang/reflect/Constructor
110  * Method:    constructNative
111  * Signature: ([Ljava/lang/Object;)Ljava/lang/Object;
112  */
113 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_reflect_Constructor_constructNative(JNIEnv *env, java_lang_reflect_Constructor *this, java_objectarray *args, java_lang_Class *declaringClass, s4 slot)
114 {
115         classinfo         *c;
116         methodinfo        *m;
117         java_objectheader *o;
118
119         c = (classinfo *) declaringClass;
120
121 #if 0
122         /* find initializer */
123
124         if (!args) {
125                 if (this->parameterTypes->header.size != 0) {
126                         *exceptionptr =
127                                 new_exception_message(string_java_lang_IllegalArgumentException,
128                                                                           "wrong number of arguments");
129                         return NULL;
130                 }
131
132         } else {
133                 if (this->parameterTypes->header.size != args->header.size) {
134                         *exceptionptr =
135                                 new_exception_message(string_java_lang_IllegalArgumentException,
136                                                                           "wrong number of arguments");
137                         return NULL;
138                 }
139         }
140 #endif
141
142         if (this->slot >= c->methodscount) {
143                 log_text("illegal index in methods table");
144                 return NULL;
145         }
146
147         m = &(c->methods[this->slot]);
148
149         if (m->name != utf_init) {
150                 /* XXX throw an exception here, although this should never happen */
151
152                 assert(0);
153         }
154
155         /* check method access */
156         /* check if we should bypass security checks (AccessibleObject) */
157
158         if (this->flag == false) {
159                 if (!access_check_caller(c, m->flags, 1))
160                         return NULL;
161         }
162
163         /* create object */
164
165         o = builtin_new(c);
166
167         if (!o)
168                 return NULL;
169         
170         /* call initializer */
171
172         (void) _Jv_jni_invokeNative(m, o, args);
173
174         return (java_lang_Object *) o;
175 }
176
177
178 /*
179  * Class:     java/lang/reflect/Constructor
180  * Method:    getSignature
181  * Signature: ()Ljava/lang/String;
182  */
183 JNIEXPORT java_lang_String* JNICALL Java_java_lang_reflect_Constructor_getSignature(JNIEnv *env, java_lang_reflect_Constructor *this)
184 {
185         classinfo        *c;
186         methodinfo       *m;
187         java_lang_String *s;
188
189         c = (classinfo *) this->clazz;
190         m = &(c->methods[this->slot]);
191
192         if (m->signature == NULL)
193                 return NULL;
194
195         s = javastring_new(m->signature);
196
197         /* in error case, s == NULL */
198
199         return s;
200 }
201
202
203 /*
204  * These are local overrides for various environment variables in Emacs.
205  * Please do not remove this and leave it at the end of the file, where
206  * Emacs will automagically detect them.
207  * ---------------------------------------------------------------------
208  * Local variables:
209  * mode: c
210  * indent-tabs-mode: t
211  * c-basic-offset: 4
212  * tab-width: 4
213  * End:
214  */