* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / native / vm / Constructor.c
1 /* src/native/vm/Constructor.c - java/lang/reflect/Constructor
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
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: Constructor.c 4357 2006-01-22 23:33:38Z twisti $
33
34 */
35
36
37 #include "config.h"
38
39 #include <assert.h>
40 #include <stdlib.h>
41
42 #include "vm/types.h"
43
44 #include "native/jni.h"
45 #include "native/native.h"
46 #include "native/include/java_lang_Class.h"
47 #include "native/include/java_lang_Object.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/stringlocal.h"
54
55
56 /*
57  * Class:     java/lang/reflect/Constructor
58  * Method:    newInstance
59  * Signature: ([Ljava/lang/Object;)Ljava/lang/Object;
60  */
61 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)
62 {
63         classinfo         *c;
64         methodinfo        *m;
65         java_objectheader *o;
66
67         c = (classinfo *) declaringClass;
68
69 #if 0
70         /* find initializer */
71
72         if (!args) {
73                 if (this->parameterTypes->header.size != 0) {
74                         *exceptionptr =
75                                 new_exception_message(string_java_lang_IllegalArgumentException,
76                                                                           "wrong number of arguments");
77                         return NULL;
78                 }
79
80         } else {
81                 if (this->parameterTypes->header.size != args->header.size) {
82                         *exceptionptr =
83                                 new_exception_message(string_java_lang_IllegalArgumentException,
84                                                                           "wrong number of arguments");
85                         return NULL;
86                 }
87         }
88 #endif
89
90         if (this->slot >= c->methodscount) {
91                 log_text("illegal index in methods table");
92                 return NULL;
93         }
94
95         /* create object */
96
97         o = builtin_new(c);
98
99         if (!o)
100                 return NULL;
101         
102         m = &(c->methods[this->slot]);
103
104         if (m->name != utf_init) {
105                 /* XXX throw an exception here, although this should never happen */
106
107                 assert(0);
108         }
109
110         /* call initializer */
111
112         (void) _Jv_jni_invokeNative(m, o, args);
113
114         return (java_lang_Object *) o;
115 }
116
117
118 /*
119  * Class:     java/lang/reflect/Constructor
120  * Method:    getModifiers
121  * Signature: ()I
122  */
123 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Constructor_getModifiers(JNIEnv *env, java_lang_reflect_Constructor *this)
124 {
125         classinfo  *c;
126         methodinfo *m;
127
128         c = (classinfo *) (this->clazz);
129
130         if ((this->slot < 0) || (this->slot >= c->methodscount)) {
131                 log_text("error illegal slot for constructor in class");
132                 assert(0);
133         }
134
135         m = &(c->methods[this->slot]);
136
137         return (m->flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED));
138 }
139
140
141 /*
142  * Class:     java/lang/reflect/Constructor
143  * Method:    getParameterTypes
144  * Signature: ()[Ljava/lang/Class;
145  */
146 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Constructor_getParameterTypes(JNIEnv *env, java_lang_reflect_Constructor *this)
147 {
148         classinfo  *c;
149         methodinfo *m;
150
151         c = (classinfo *) this->clazz;
152
153         if ((this->slot < 0) || (this->slot >= c->methodscount)) {
154                 log_text("error illegal slot for constructor in class");
155                 assert(0);
156         }
157
158         m = &(c->methods[this->slot]);
159
160         return native_get_parametertypes(m);
161 }
162
163
164 /*
165  * Class:     java/lang/reflect/Constructor
166  * Method:    getExceptionTypes
167  * Signature: ()[Ljava/lang/Class;
168  */
169 JNIEXPORT java_objectarray* JNICALL Java_java_lang_reflect_Constructor_getExceptionTypes(JNIEnv *env, java_lang_reflect_Constructor *this)
170 {
171         classinfo  *c;
172         methodinfo *m;
173
174         c = (classinfo *) this->clazz;
175
176         if ((this->slot < 0) || (this->slot >= c->methodscount)) {
177                 log_text("error illegal slot for constructor in class");
178                 assert(0);
179         }
180
181         m = &(c->methods[this->slot]);
182
183         return native_get_exceptiontypes(m);
184 }
185
186
187 /*
188  * These are local overrides for various environment variables in Emacs.
189  * Please do not remove this and leave it at the end of the file, where
190  * Emacs will automagically detect them.
191  * ---------------------------------------------------------------------
192  * Local variables:
193  * mode: c
194  * indent-tabs-mode: t
195  * c-basic-offset: 4
196  * tab-width: 4
197  * End:
198  */