- added gnu header
[cacao.git] / src / native / vm / Constructor.c
1 /* nat/Constructor.c -
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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: Constructor.c 835 2004-01-04 23:39:36Z twisti $
32
33 */
34
35
36 #include <string.h>
37 #include "jni.h"
38 #include "builtin.h"
39 #include "loader.h"
40 #include "native.h"
41 #include "tables.h"
42 #include "asmpart.h"
43 #include "toolbox/loging.h"
44 #include "java_lang_Object.h"
45 #include "java_lang_Class.h"
46 #include "java_lang_reflect_Constructor.h"
47
48
49 /*
50  * Class:     java/lang/reflect/Constructor
51  * Method:    newInstance
52  * Signature: ([Ljava/lang/Object;)Ljava/lang/Object;
53  */
54 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_Constructor_constructNative(JNIEnv *env, struct java_lang_reflect_Constructor* this, java_objectarray* parameters, struct java_lang_Class* clazz, s4 par3)
55 {
56
57 #warning fix me for parameters float/double and long long  parameters
58
59         methodinfo *m;
60         java_objectheader *o;
61
62         
63         /*      log_text("Java_java_lang_reflect_Constructor_constructNative called");
64         log_plain_utf(((struct classinfo*)clazz)->name);*/
65 /*      log_plain("\n"); */
66
67         /* find initializer */
68
69         if (!parameters) {
70                 if (this->parameterTypes->header.size != 0) {
71                         (*env)->ThrowNew(env, loader_load(utf_new_char("java/lang/IllegalArgumentException")), "wrong number of arguments");
72                         return 0;
73                 }
74
75         } else {
76                 if (this->parameterTypes->header.size != parameters->header.size) {
77                         (*env)->ThrowNew(env, loader_load(utf_new_char("java/lang/IllegalArgumentException")), "wrong number of arguments");
78                         return 0;
79                 }
80         }
81
82         if (this->slot >= ((classinfo *) clazz)->methodscount) {
83                 log_text("illegal index in methods table");
84                 return 0;
85         }
86
87         o = builtin_new((classinfo *) clazz);         /*          create object */
88         if (!o) {
89                 log_text("Objet instance could not be created");
90                 return NULL;
91         }
92         
93         /*      log_text("o!=NULL\n");*/
94
95
96         m = &((classinfo *)clazz)->methods[this->slot];
97         if (!((m->name == utf_new_char("<init>"))))
98                 /* && 
99                    (m->descriptor == create_methodsig(this->parameterTypes,"V"))))*/
100                 {
101                         if (verbose) {
102                                 char logtext[MAXLOGTEXT];
103                                 sprintf(logtext, "Warning: class has no instance-initializer of specified type: ");
104                                 utf_sprint(logtext + strlen(logtext), ((classinfo *) clazz)->name);
105                                 log_text(logtext);
106                                 log_plain_utf( create_methodsig(this->parameterTypes,"V"));
107                                 log_plain("\n");
108                                 class_showconstantpool((classinfo *) clazz);
109                         }
110 #warning throw an exception here, although this should never happen
111                         return (java_lang_Object *) o;
112                 }
113
114         /*      log_text("calling initializer");*/
115         /* call initializer */
116 #if 0
117         switch (this->parameterTypes->header.size) {
118         case 0: exceptionptr=asm_calljavamethod (m, o, NULL, NULL, NULL);
119                 break;
120         case 1: exceptionptr=asm_calljavamethod (m, o, parameters->data[0], NULL, NULL);
121                 break;
122         case 2: exceptionptr=asm_calljavamethod (m, o, parameters->data[0], parameters->data[1], NULL);
123                 break;
124         case 3: exceptionptr=asm_calljavamethod (m, o, parameters->data[0], parameters->data[1], 
125                                                                                          parameters->data[2]);
126         break;
127         default:
128                 log_text("Not supported number of arguments in Java_java_lang_reflect_Constructor");
129         }
130 #endif
131         /*log_plain_utf(m->descriptor);
132           log_text("calling constructor");*/
133         (void) jni_method_invokeNativeHelper(env, m ,o, parameters); 
134
135         return (java_lang_Object *) o;
136 }
137
138
139 /*
140  * Class:     java_lang_reflect_Constructor
141  * Method:    getModifiers
142  * Signature: ()I
143  */
144 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Constructor_getModifiers(JNIEnv *env, struct java_lang_reflect_Constructor* this)
145 {
146         /*      log_text("Java_java_lang_reflect_Constructor_getModifiers called");*/
147         classinfo *c = (classinfo *) (this->clazz);
148
149         if ((this->slot < 0) || (this->slot >= c->methodscount))
150                 panic("error illegal slot for method in class (getReturnType)");
151
152         return (c->methods[this->slot]).flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED);
153 }
154
155
156 /*
157  * These are local overrides for various environment variables in Emacs.
158  * Please do not remove this and leave it at the end of the file, where
159  * Emacs will automagically detect them.
160  * ---------------------------------------------------------------------
161  * Local variables:
162  * mode: c
163  * indent-tabs-mode: t
164  * c-basic-offset: 4
165  * tab-width: 4
166  * End:
167  */