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