* src/vmcore/linker.c (build_display): Removed superfluous recursion; return
[cacao.git] / src / native / vm / cldc1.1 / java_lang_Class.c
1 /* src/native/vm/cldc1.1/java_lang_Class.c
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 #include "vm/types.h"
28
29 #include "native/jni.h"
30 #include "native/llni.h"
31 #include "native/native.h"
32
33 #include "native/include/java_lang_String.h"             /* required by j.l.C */
34 #include "native/include/java_lang_Object.h"
35
36 #include "native/include/java_lang_Class.h"
37
38 #include "vm/exceptions.h"
39 #include "vm/initialize.h"
40
41
42 /* native methods implemented by this file ************************************/
43  
44 static JNINativeMethod methods[] = {
45         { "forName",          "(Ljava/lang/String;)Ljava/lang/Class;",(void *) (ptrint) &Java_java_lang_Class_forName          },
46         { "newInstance",      "()Ljava/lang/Object;",                 (void *) (ptrint) &Java_java_lang_Class_newInstance      },
47         { "isInstance",       "(Ljava/lang/Object;)Z",                (void *) (ptrint) &Java_java_lang_Class_isInstance       },
48         { "isAssignableFrom", "(Ljava/lang/Class;)Z",                 (void *) (ptrint) &Java_java_lang_Class_isAssignableFrom },
49         { "isInterface",      "()Z",                                  (void *) (ptrint) &Java_java_lang_Class_isInterface      },
50         { "isArray",          "()Z",                                  (void *) (ptrint) &Java_java_lang_Class_isArray          },
51         { "getName",          "()Ljava/lang/String;",                 (void *) (ptrint) &Java_java_lang_Class_getName          },
52 };
53
54 /* _Jv_java_lang_Class_init ****************************************************
55  
56    Register native functions.
57  
58 *******************************************************************************/
59  
60 void _Jv_java_lang_Class_init(void)
61 {
62         utf *u;
63  
64         u = utf_new_char("java/lang/Class");
65  
66         native_method_register(u, methods, NATIVE_METHODS_COUNT);
67 }
68
69
70 /*
71  * Class:     java/lang/Class
72  * Method:    forName
73  * Signature: (Ljava/lang/String;)Ljava/lang/Class;
74  */
75 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_Class_forName(JNIEnv *env, jclass clazz, java_lang_String *name)
76 {
77         utf       *ufile;
78         utf       *uname;
79         classinfo *c;
80         u2        *pos;
81         s4         i;
82
83         /* illegal argument */
84
85         if (name == NULL) {
86                 exceptions_throw_nullpointerexception();
87                 return NULL;
88         }
89
90         /* create utf string in which '.' is replaced by '/' */
91
92         ufile = javastring_toutf((java_handle_t *) name, true);
93         uname = javastring_toutf((java_handle_t *) name, false);
94
95         /* name must not contain '/' (mauve test) */
96
97         for (i = 0, pos = LLNI_field_direct(name, value)->data + LLNI_field_direct(name, offset); i < LLNI_field_direct(name, count); i++, pos++) {
98                 if (*pos == '/') {
99                         exceptions_throw_classnotfoundexception(uname);
100                         return NULL;
101                 }
102         }
103
104         /* try to load, ... */
105
106         c = load_class_bootstrap(ufile);
107
108         if (c == NULL)
109             return NULL;
110
111         /* link, ... */
112
113         if (!link_class(c))
114                 return NULL;
115         
116         /* ...and initialize it. */
117
118         if (!initialize_class(c))
119                 return NULL;
120
121         return LLNI_classinfo_wrap(c);
122 }
123
124
125 /*
126  * Class:     java/lang/Class
127  * Method:    newInstance
128  * Signature: ()Ljava/lang/Object;
129  */
130 JNIEXPORT java_lang_Object* JNICALL Java_java_lang_Class_newInstance(JNIEnv *env, java_lang_Class* this)
131 {
132         classinfo     *c;
133         java_handle_t *o;
134
135         c = LLNI_classinfo_unwrap(this);
136
137         o = native_new_and_init(c);
138
139         return (java_lang_Object *) o;
140 }
141
142
143 /*
144  * Class:     java/lang/Class
145  * Method:    isInstance
146  * Signature: (Ljava/lang/Object;)Z
147  */
148 JNIEXPORT int32_t JNICALL Java_java_lang_Class_isInstance(JNIEnv *env, java_lang_Class *this, java_lang_Object *obj)
149 {
150         classinfo     *c;
151         java_handle_t *h;
152
153         c = LLNI_classinfo_unwrap(this);
154         h = (java_handle_t *) obj;
155
156         return class_is_instance(c, h);
157 }
158
159
160 /*
161  * Class:     java/lang/Class
162  * Method:    isAssignableFrom
163  * Signature: (Ljava/lang/Class;)Z
164  */
165 JNIEXPORT int32_t JNICALL Java_java_lang_Class_isAssignableFrom(JNIEnv *env, java_lang_Class *this, java_lang_Class *cls)
166 {
167         classinfo *to;
168         classinfo *from;
169
170         to   = LLNI_classinfo_unwrap(this);
171         from = LLNI_classinfo_unwrap(cls);
172
173         if (from == NULL) {
174                 exceptions_throw_nullpointerexception();
175                 return 0;
176         }
177
178         return class_is_assignable_from(to, from);
179 }
180
181
182 /*
183  * Class:     java/lang/Class
184  * Method:    isInterface
185  * Signature: ()Z
186  */
187 JNIEXPORT int32_t JNICALL Java_java_lang_Class_isInterface(JNIEnv *env, java_lang_Class *this)
188 {
189         classinfo *c;
190
191         c = LLNI_classinfo_unwrap(this);
192
193         return class_is_interface(c);
194 }
195
196
197 /*
198  * Class:     java/lang/Class
199  * Method:    isArray
200  * Signature: ()Z
201  */
202 JNIEXPORT int32_t JNICALL Java_java_lang_Class_isArray(JNIEnv *env, java_lang_Class *this)
203 {
204         classinfo *c;
205
206         c = LLNI_classinfo_unwrap(this);
207
208         return class_is_array(c);
209 }
210
211
212 /*
213  * Class:     java/lang/Class
214  * Method:    getName
215  * Signature: ()Ljava/lang/String;
216  */
217 JNIEXPORT java_lang_String* JNICALL Java_java_lang_Class_getName(JNIEnv *env, java_lang_Class *this)
218 {
219         classinfo *c;
220
221         c = LLNI_classinfo_unwrap(this);
222
223         return (java_lang_String*) class_get_classname(c);
224 }
225
226
227 /*
228  * These are local overrides for various environment variables in Emacs.
229  * Please do not remove this and leave it at the end of the file, where
230  * Emacs will automagically detect them.
231  * ---------------------------------------------------------------------
232  * Local variables:
233  * mode: c
234  * indent-tabs-mode: t
235  * c-basic-offset: 4
236  * tab-width: 4
237  * End:
238  * vim:noexpandtab:sw=4:ts=4:
239  */