49d459da799f5b20e1c3626e4e38a8a9c440745c
[cacao.git] / src / native / vm / cldc1.1 / java_lang_Class.cpp
1 /* src/native/vm/cldc1.1/java_lang_Class.cpp
2
3    Copyright (C) 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "native/jni.h"
31 #include "native/llni.h"
32 #include "native/native.h"
33
34 #if defined(ENABLE_JNI_HEADERS)
35 # include "native/include/java_lang_Class.h"
36 #endif
37
38 #include "vm/exceptions.hpp"
39 #include "vm/initialize.h"
40
41
42 // Native functions are exported as C functions.
43 extern "C" {
44
45 /*
46  * Class:     java/lang/Class
47  * Method:    forName
48  * Signature: (Ljava/lang/String;)Ljava/lang/Class;
49  */
50 JNIEXPORT jclass JNICALL Java_java_lang_Class_forName(JNIEnv *env, jclass clazz, jstring name)
51 {
52         utf       *ufile;
53         utf       *uname;
54         classinfo *c;
55         char*      pos;
56         int32_t    i;
57
58         /* illegal argument */
59
60         if (name == NULL) {
61                 exceptions_throw_nullpointerexception();
62                 return NULL;
63         }
64
65         /* create utf string in which '.' is replaced by '/' */
66
67         ufile = javastring_toutf((java_handle_t *) name, true);
68         uname = javastring_toutf((java_handle_t *) name, false);
69
70         /* name must not contain '/' (mauve test) */
71
72         // FIXME Move this check into a function.
73         for (i = 0, pos = uname->text; i < uname->blength; i++, pos++) {
74                 if (*pos == '/') {
75                         exceptions_throw_classnotfoundexception(uname);
76                         return NULL;
77                 }
78         }
79
80         /* try to load, ... */
81
82         c = load_class_bootstrap(ufile);
83
84         if (c == NULL)
85             return NULL;
86
87         /* link, ... */
88
89         if (!link_class(c))
90                 return NULL;
91         
92         /* ...and initialize it. */
93
94         if (!initialize_class(c))
95                 return NULL;
96
97         return (jclass) LLNI_classinfo_wrap(c);
98 }
99
100
101 /*
102  * Class:     java/lang/Class
103  * Method:    newInstance
104  * Signature: ()Ljava/lang/Object;
105  */
106 JNIEXPORT jobject JNICALL Java_java_lang_Class_newInstance(JNIEnv *env, jclass _this)
107 {
108         classinfo     *c;
109         java_handle_t *o;
110
111         c = LLNI_classinfo_unwrap(_this);
112
113         o = native_new_and_init(c);
114
115         return (jobject) o;
116 }
117
118
119 /*
120  * Class:     java/lang/Class
121  * Method:    isInstance
122  * Signature: (Ljava/lang/Object;)Z
123  */
124 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isInstance(JNIEnv *env, jclass _this, jobject obj)
125 {
126         classinfo     *c;
127         java_handle_t *h;
128
129         c = LLNI_classinfo_unwrap(_this);
130         h = (java_handle_t *) obj;
131
132         return class_is_instance(c, h);
133 }
134
135
136 /*
137  * Class:     java/lang/Class
138  * Method:    isAssignableFrom
139  * Signature: (Ljava/lang/Class;)Z
140  */
141 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jclass _this, jclass cls)
142 {
143         classinfo *to;
144         classinfo *from;
145
146         to   = LLNI_classinfo_unwrap(_this);
147         from = LLNI_classinfo_unwrap(cls);
148
149         if (from == NULL) {
150                 exceptions_throw_nullpointerexception();
151                 return 0;
152         }
153
154         return class_is_assignable_from(to, from);
155 }
156
157
158 /*
159  * Class:     java/lang/Class
160  * Method:    isInterface
161  * Signature: ()Z
162  */
163 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isInterface(JNIEnv *env, jclass _this)
164 {
165         classinfo *c;
166
167         c = LLNI_classinfo_unwrap(_this);
168
169         return class_is_interface(c);
170 }
171
172
173 /*
174  * Class:     java/lang/Class
175  * Method:    isArray
176  * Signature: ()Z
177  */
178 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isArray(JNIEnv *env, jclass _this)
179 {
180         classinfo *c;
181
182         c = LLNI_classinfo_unwrap(_this);
183
184         return class_is_array(c);
185 }
186
187
188 /*
189  * Class:     java/lang/Class
190  * Method:    getName
191  * Signature: ()Ljava/lang/String;
192  */
193 JNIEXPORT jstring JNICALL Java_java_lang_Class_getName(JNIEnv *env, jclass _this)
194 {
195         classinfo *c;
196
197         c = LLNI_classinfo_unwrap(_this);
198
199         return (jstring) class_get_classname(c);
200 }
201
202 } // extern "C"
203
204
205 /* native methods implemented by this file ************************************/
206  
207 static JNINativeMethod methods[] = {
208         { (char*) "forName",          (char*) "(Ljava/lang/String;)Ljava/lang/Class;",(void*) (uintptr_t) &Java_java_lang_Class_forName          },
209         { (char*) "newInstance",      (char*) "()Ljava/lang/Object;",                 (void*) (uintptr_t) &Java_java_lang_Class_newInstance      },
210         { (char*) "isInstance",       (char*) "(Ljava/lang/Object;)Z",                (void*) (uintptr_t) &Java_java_lang_Class_isInstance       },
211         { (char*) "isAssignableFrom", (char*) "(Ljava/lang/Class;)Z",                 (void*) (uintptr_t) &Java_java_lang_Class_isAssignableFrom },
212         { (char*) "isInterface",      (char*) "()Z",                                  (void*) (uintptr_t) &Java_java_lang_Class_isInterface      },
213         { (char*) "isArray",          (char*) "()Z",                                  (void*) (uintptr_t) &Java_java_lang_Class_isArray          },
214         { (char*) "getName",          (char*) "()Ljava/lang/String;",                 (void*) (uintptr_t) &Java_java_lang_Class_getName          },
215 };
216
217 /* _Jv_java_lang_Class_init ****************************************************
218  
219    Register native functions.
220  
221 *******************************************************************************/
222
223 // FIXME
224 extern "C" { 
225 void _Jv_java_lang_Class_init(void)
226 {
227         utf *u;
228  
229         u = utf_new_char("java/lang/Class");
230  
231         native_method_register(u, methods, NATIVE_METHODS_COUNT);
232 }
233 }
234
235
236 /*
237  * These are local overrides for various environment variables in Emacs.
238  * Please do not remove this and leave it at the end of the file, where
239  * Emacs will automagically detect them.
240  * ---------------------------------------------------------------------
241  * Local variables:
242  * mode: c++
243  * indent-tabs-mode: t
244  * c-basic-offset: 4
245  * tab-width: 4
246  * End:
247  * vim:noexpandtab:sw=4:ts=4:
248  */