CACAO
java_lang_Class.cpp
Go to the documentation of this file.
1 /* src/native/vm/cldc1.1/java_lang_Class.cpp
2 
3  Copyright (C) 2006-2013
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.hpp"
31 #include "native/llni.hpp"
32 #include "native/native.hpp"
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.hpp"
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  */
51 {
52  Utf8String ufile;
53  Utf8String uname;
54  JavaString sname = name;
55  classinfo *c;
56  char* pos;
57  int32_t i;
58 
59  /* illegal argument */
60 
61  if (name == NULL) {
63  return NULL;
64  }
65 
66  /* create utf string in which '.' is replaced by '/' */
67 
68  ufile = sname.to_utf8_dot_to_slash();
69  uname = sname.to_utf8();
70 
71  /* name must not contain '/' (mauve test) */
72 
73  // FIXME Move this check into a function.
74  for (const char *it = uname.begin(), *end = uname.end(); it != end; ++it) {
75  if (*it == '/') {
77  return NULL;
78  }
79  }
80 
81  /* try to load, ... */
82 
83  c = load_class_bootstrap(ufile);
84 
85  if (c == NULL)
86  return NULL;
87 
88  /* link, ... */
89 
90  if (!link_class(c))
91  return NULL;
92 
93  /* ...and initialize it. */
94 
95  if (!initialize_class(c))
96  return NULL;
97 
98  return (jclass) LLNI_classinfo_wrap(c);
99 }
100 
101 
102 /*
103  * Class: java/lang/Class
104  * Method: newInstance
105  * Signature: ()Ljava/lang/Object;
106  */
108 {
109  classinfo *c;
110  java_handle_t *o;
111 
112  c = LLNI_classinfo_unwrap(_this);
113 
114  o = native_new_and_init(c);
115 
116  return (jobject) o;
117 }
118 
119 
120 /*
121  * Class: java/lang/Class
122  * Method: isInstance
123  * Signature: (Ljava/lang/Object;)Z
124  */
125 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isInstance(JNIEnv *env, jclass _this, jobject obj)
126 {
127  classinfo *c;
128  java_handle_t *h;
129 
130  c = LLNI_classinfo_unwrap(_this);
131  h = (java_handle_t *) obj;
132 
133  return class_is_instance(c, h);
134 }
135 
136 
137 /*
138  * Class: java/lang/Class
139  * Method: isAssignableFrom
140  * Signature: (Ljava/lang/Class;)Z
141  */
142 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jclass _this, jclass cls)
143 {
144  classinfo *to;
145  classinfo *from;
146 
147  to = LLNI_classinfo_unwrap(_this);
148  from = LLNI_classinfo_unwrap(cls);
149 
150  if (from == NULL) {
152  return 0;
153  }
154 
155  return class_is_assignable_from(to, from);
156 }
157 
158 
159 /*
160  * Class: java/lang/Class
161  * Method: isInterface
162  * Signature: ()Z
163  */
164 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isInterface(JNIEnv *env, jclass _this)
165 {
166  classinfo *c;
167 
168  c = LLNI_classinfo_unwrap(_this);
169 
170  return class_is_interface(c);
171 }
172 
173 
174 /*
175  * Class: java/lang/Class
176  * Method: isArray
177  * Signature: ()Z
178  */
179 JNIEXPORT jboolean JNICALL Java_java_lang_Class_isArray(JNIEnv *env, jclass _this)
180 {
181  classinfo *c;
182 
183  c = LLNI_classinfo_unwrap(_this);
184 
185  return class_is_array(c);
186 }
187 
188 
189 /*
190  * Class: java/lang/Class
191  * Method: getName
192  * Signature: ()Ljava/lang/String;
193  */
194 JNIEXPORT jstring JNICALL Java_java_lang_Class_getName(JNIEnv *env, jclass _this)
195 {
196  classinfo *c;
197 
198  c = LLNI_classinfo_unwrap(_this);
199 
200  return (jstring) class_get_classname(c);
201 }
202 
203 } // extern "C"
204 
205 
206 /* native methods implemented by this file ************************************/
207 
208 static JNINativeMethod methods[] = {
209  { (char*) "forName", (char*) "(Ljava/lang/String;)Ljava/lang/Class;",(void*) (uintptr_t) &Java_java_lang_Class_forName },
210  { (char*) "newInstance", (char*) "()Ljava/lang/Object;", (void*) (uintptr_t) &Java_java_lang_Class_newInstance },
211  { (char*) "isInstance", (char*) "(Ljava/lang/Object;)Z", (void*) (uintptr_t) &Java_java_lang_Class_isInstance },
212  { (char*) "isAssignableFrom", (char*) "(Ljava/lang/Class;)Z", (void*) (uintptr_t) &Java_java_lang_Class_isAssignableFrom },
213  { (char*) "isInterface", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_Class_isInterface },
214  { (char*) "isArray", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_Class_isArray },
215  { (char*) "getName", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_Class_getName },
216 };
217 
218 
219 /* _Jv_java_lang_Class_init ****************************************************
220 
221  Register native functions.
222 
223 *******************************************************************************/
224 
226 {
227  Utf8String u = Utf8String::from_utf8("java/lang/Class");
228 
231 }
232 
233 
234 /*
235  * These are local overrides for various environment variables in Emacs.
236  * Please do not remove this and leave it at the end of the file, where
237  * Emacs will automagically detect them.
238  * ---------------------------------------------------------------------
239  * Local variables:
240  * mode: c++
241  * indent-tabs-mode: t
242  * c-basic-offset: 4
243  * tab-width: 4
244  * End:
245  * vim:noexpandtab:sw=4:ts=4:
246  */
static JNINativeMethod methods[]
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
Utf8String to_utf8() const
Definition: string.cpp:437
argument_type from
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
void exceptions_throw_classnotfoundexception(Utf8String name)
Definition: exceptions.cpp:681
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
byte_iterator end() const
Definition: utf8.hpp:107
JNIEXPORT jstring JNICALL Java_java_lang_Class_getName(JNIEnv *env, jclass _this)
classinfo * load_class_bootstrap(Utf8String name)
Definition: loader.cpp:1276
bool class_is_instance(classinfo *c, java_handle_t *h)
Definition: class.cpp:1572
JNIEXPORT jboolean JNICALL Java_java_lang_Class_isArray(JNIEnv *env, jclass _this)
static bool class_is_array(classinfo *c)
Definition: class.hpp:341
JNIEXPORT jboolean JNICALL Java_java_lang_Class_isInterface(JNIEnv *env, jclass _this)
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
java_handle_t * class_get_classname(classinfo *c)
Returns the classname of the class, where slashes (&#39;/&#39;) are replaced by dots (&#39;.
Definition: class.cpp:86
#define LLNI_classinfo_wrap(classinfo)
Definition: llni.hpp:110
JNIEXPORT jclass JNICALL Java_java_lang_Class_forName(JNIEnv *env, jclass clazz, jstring name)
void exceptions_throw_nullpointerexception(void)
static bool class_is_interface(classinfo *c)
Definition: class.hpp:357
JNIEXPORT jboolean JNICALL Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jclass _this, jclass cls)
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
MIIterator i
JNIEXPORT jboolean JNICALL Java_java_lang_Class_isInstance(JNIEnv *env, jclass _this, jobject obj)
MIIterator pos
bool initialize_class(classinfo *c)
Definition: initialize.cpp:110
JNIEXPORT jobject JNICALL Java_java_lang_Class_newInstance(JNIEnv *env, jclass _this)
java_handle_t * native_new_and_init(classinfo *c)
Registers a new native agent by specified by it&#39;s library name and with an optional options string...
Definition: native.cpp:729
byte_iterator begin() const
Definition: utf8.hpp:106
void _Jv_java_lang_Class_init(void)
classinfo * link_class(classinfo *c)
Definition: linker.cpp:378
bool class_is_assignable_from(classinfo *to, classinfo *from)
Definition: class.cpp:1539
#define LLNI_classinfo_unwrap(clazz)
Definition: llni.hpp:113
static VM * get_current()
Definition: vm.hpp:99
Utf8String to_utf8_dot_to_slash() const
Definition: string.cpp:450