1 /* src/native/vm/gnuclasspath/java_lang_VMClassLoader.cpp
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
32 #include "mm/memory.h"
34 #include "native/jni.hpp"
35 #include "native/llni.h"
36 #include "native/native.h"
38 #if defined(ENABLE_JNI_HEADERS)
39 # include "native/vm/include/java_lang_VMClassLoader.h"
42 #include "toolbox/logging.h"
43 #include "toolbox/list.h"
45 #if defined(ENABLE_ASSERTION)
46 #include "vm/assertion.h"
49 #include "vm/jit/builtin.hpp"
51 #include "vm/classcache.h"
52 #include "vm/exceptions.hpp"
53 #include "vm/globals.hpp"
54 #include "vm/initialize.h"
55 #include "vm/javaobjects.hpp"
56 #include "vm/linker.h"
57 #include "vm/loader.hpp"
58 #include "vm/options.h"
59 #include "vm/primitive.hpp"
60 #include "vm/statistics.h"
61 #include "vm/string.hpp"
65 #include "vm/jit/asmpart.h"
67 #if defined(ENABLE_JVMTI)
68 #include "native/jvmti/cacaodbg.h"
72 // Native functions are exported as C functions.
76 * Class: java/lang/VMClassLoader
78 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
80 JNIEXPORT jclass JNICALL Java_java_lang_VMClassLoader_defineClass(JNIEnv *env, jclass clazz, jobject cl, jstring name, jbyteArray data, jint offset, jint len, jobject pd)
84 classloader_t *loader;
85 java_handle_bytearray_t* ba;
88 #if defined(ENABLE_JVMTI)
89 jint new_class_data_len = 0;
90 unsigned char* new_class_data = NULL;
93 /* check if data was passed */
96 exceptions_throw_nullpointerexception();
100 /* check the indexes passed */
102 if ((offset < 0) || (len < 0) || ((offset + len) > LLNI_array_size(data))) {
103 exceptions_throw_arrayindexoutofboundsexception();
107 /* add classloader to classloader hashtable */
109 loader = loader_hashtable_classloader_add((java_handle_t *) cl);
112 /* convert '.' to '/' in java string */
114 utfname = javastring_toutf((java_handle_t *) name, true);
120 #if defined(ENABLE_JVMTI)
121 /* XXX again this will not work because of the indirection cell for classloaders */
123 /* fire Class File Load Hook JVMTI event */
126 jvmti_ClassFileLoadHook(utfname, len, (unsigned char *) data->data,
127 loader, (java_handle_t *) pd,
128 &new_class_data_len, &new_class_data);
131 /* define the class */
133 #if defined(ENABLE_JVMTI)
134 /* check if the JVMTI wants to modify the class */
136 if (new_class_data == NULL)
137 c = class_define(utfname, loader, new_class_data_len, new_class_data, pd);
141 ba = (java_handle_bytearray_t*) data;
142 stream = (uint8_t *) &LLNI_array_direct(ba, offset);
143 c = class_define(utfname, loader, len, stream, (java_handle_t *) pd);
150 java_handle_t* h = LLNI_classinfo_wrap(c);
152 // Set ProtectionDomain.
153 java_lang_Class jlc(h);
156 return (jclass) jlc.get_handle();
161 * Class: java/lang/VMClassLoader
162 * Method: getPrimitiveClass
163 * Signature: (C)Ljava/lang/Class;
165 JNIEXPORT jclass JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, jchar type)
169 c = Primitive::get_class_by_char(type);
172 exceptions_throw_classnotfoundexception(utf_null);
176 return (jclass) LLNI_classinfo_wrap(c);
181 * Class: java/lang/VMClassLoader
182 * Method: resolveClass
183 * Signature: (Ljava/lang/Class;)V
185 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, jclass c)
189 ci = LLNI_classinfo_unwrap(c);
192 exceptions_throw_nullpointerexception();
198 if (!(ci->state & CLASS_LINKED))
199 (void) link_class(ci);
206 * Class: java/lang/VMClassLoader
208 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
210 JNIEXPORT jclass JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, jstring name, jboolean resolve)
216 exceptions_throw_nullpointerexception();
220 /* create utf string in which '.' is replaced by '/' */
222 u = javastring_toutf((java_handle_t *) name, true);
226 c = load_class_bootstrap(u);
231 /* resolve class -- if requested */
237 return (jclass) LLNI_classinfo_wrap(c);
242 * Class: java/lang/VMClassLoader
243 * Method: nativeGetResources
244 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
246 JNIEXPORT jobject JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, jstring name)
248 java_handle_t *o; /* vector being created */
249 methodinfo *m; /* "add" method of vector */
250 java_handle_t *path; /* path to be added */
251 list_classpath_entry *lce; /* classpath entry */
252 utf *utfname; /* utf to look for */
253 char *buffer; /* char buffer */
254 char *namestart; /* start of name to use */
255 char *tmppath; /* temporary buffer */
256 int32_t namelen; /* length of name to use */
257 int32_t searchlen; /* length of name to search */
258 int32_t bufsize; /* size of buffer allocated */
259 int32_t pathlen; /* name of path to assemble */
260 struct stat buf; /* buffer for stat */
261 jboolean ret; /* return value of "add" */
263 /* get the resource name as utf string */
265 utfname = javastring_toutf((java_handle_t *) name, false);
270 /* copy it to a char buffer */
272 namelen = utf_bytes(utfname);
274 bufsize = namelen + strlen("0");
275 buffer = MNEW(char, bufsize);
277 utf_copy(buffer, utfname);
280 /* skip leading '/' */
282 if (namestart[0] == '/') {
288 /* remove trailing `.class' */
290 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
294 /* create a new needle to look for, if necessary */
296 if (searchlen != bufsize-1) {
297 utfname = utf_new(namestart, searchlen);
304 o = native_new_and_init(class_java_util_Vector);
309 /* get Vector.add() method */
311 m = class_resolveclassmethod(class_java_util_Vector,
313 utf_new_char("(Ljava/lang/Object;)Z"),
320 /* iterate over all classpath entries */
322 for (lce = (list_classpath_entry*) list_first(list_classpath_entries); lce != NULL;
323 lce = (list_classpath_entry*) list_next(list_classpath_entries, lce)) {
324 /* clear path pointer */
327 #if defined(ENABLE_ZLIB)
328 if (lce->type == CLASSPATH_ARCHIVE) {
330 if (zip_find(lce, utfname)) {
331 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
332 namelen + strlen("0");
334 tmppath = MNEW(char, pathlen);
336 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
337 path = javastring_new_from_utf_string(tmppath),
339 MFREE(tmppath, char, pathlen);
343 #endif /* defined(ENABLE_ZLIB) */
344 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
346 tmppath = MNEW(char, pathlen);
348 sprintf(tmppath, "file://%s%s", lce->path, namestart);
350 /* Does this file exist? */
352 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
353 if (!S_ISDIR(buf.st_mode))
354 path = javastring_new_from_utf_string(tmppath);
356 MFREE(tmppath, char, pathlen);
357 #if defined(ENABLE_ZLIB)
361 /* if a resource was found, add it to the vector */
364 ret = vm_call_method_int(m, o, path);
366 if (exceptions_get_exception() != NULL)
374 MFREE(buffer, char, bufsize);
379 MFREE(buffer, char, bufsize);
386 * Class: java/lang/VMClassLoader
387 * Method: defaultAssertionStatus
390 JNIEXPORT jboolean JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
392 #if defined(ENABLE_ASSERTION)
393 return assertion_system_enabled;
400 * Class: java/lang/VMClassLoader
401 * Method: userAssertionStatus
404 JNIEXPORT jboolean JNICALL Java_java_lang_VMClassLoader_defaultUserAssertionStatus(JNIEnv *env, jclass clazz)
406 #if defined(ENABLE_ASSERTION)
407 return assertion_user_enabled;
414 * Class: java/lang/VMClassLoader
415 * Method: packageAssertionStatus
416 * Signature: (Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;
418 JNIEXPORT jobject JNICALL Java_java_lang_VMClassLoader_packageAssertionStatus0(JNIEnv *env, jclass clazz, jobject jtrue, jobject jfalse)
421 #if defined(ENABLE_ASSERTION)
424 assertion_name_t *item;
429 hm = native_new_and_init(class_java_util_HashMap);
434 #if defined(ENABLE_ASSERTION)
435 /* if nothing todo, return now */
437 if (assertion_package_count == 0) {
441 /* get HashMap.put method */
443 m = class_resolveclassmethod(class_java_util_HashMap,
445 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
453 item = (assertion_name_t *)list_first(list_assertion_names);
455 while (item != NULL) {
456 if (item->package == false) {
457 item = (assertion_name_t *)list_next(list_assertion_names, item);
461 if (strcmp(item->name, "") == 0) {
462 /* unnamed package wanted */
466 js = javastring_new_from_ascii(item->name);
472 if (item->enabled == true) {
473 vm_call_method(m, hm, js, jtrue);
476 vm_call_method(m, hm, js, jfalse);
479 item = (assertion_name_t *)list_next(list_assertion_names, item);
487 * Class: java/lang/VMClassLoader
488 * Method: classAssertionStatus
489 * Signature: (Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;
491 JNIEXPORT jobject JNICALL Java_java_lang_VMClassLoader_classAssertionStatus0(JNIEnv *env, jclass clazz, jobject jtrue, jobject jfalse)
494 #if defined(ENABLE_ASSERTION)
497 assertion_name_t *item;
502 hm = native_new_and_init(class_java_util_HashMap);
507 #if defined(ENABLE_ASSERTION)
508 /* if nothing todo, return now */
510 if (assertion_class_count == 0) {
514 /* get HashMap.put method */
516 m = class_resolveclassmethod(class_java_util_HashMap,
518 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
526 item = (assertion_name_t *)list_first(list_assertion_names);
528 while (item != NULL) {
529 if (item->package == true) {
530 item = (assertion_name_t *)list_next(list_assertion_names, item);
534 js = javastring_new_from_ascii(item->name);
539 if (item->enabled == true) {
540 vm_call_method(m, hm, js, jtrue);
543 vm_call_method(m, hm, js, jfalse);
546 item = (assertion_name_t *)list_next(list_assertion_names, item);
555 * Class: java/lang/VMClassLoader
556 * Method: findLoadedClass
557 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
559 JNIEXPORT jclass JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, jobject loader, jstring name)
565 /* XXX is it correct to add the classloader to the hashtable here? */
567 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
569 /* replace `.' by `/', this is required by the classcache */
571 u = javastring_toutf((java_handle_t *) name, true);
573 /* lookup for defining classloader */
575 c = classcache_lookup_defined(cl, u);
577 /* if not found, lookup for initiating classloader */
580 c = classcache_lookup(cl, u);
582 return (jclass) LLNI_classinfo_wrap(c);
588 /* native methods implemented by this file ************************************/
590 static JNINativeMethod methods[] = {
591 { (char*) "defineClass", (char*) "(Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_defineClass },
592 { (char*) "getPrimitiveClass", (char*) "(C)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_getPrimitiveClass },
593 { (char*) "resolveClass", (char*) "(Ljava/lang/Class;)V", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_resolveClass },
594 { (char*) "loadClass", (char*) "(Ljava/lang/String;Z)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_loadClass },
595 { (char*) "nativeGetResources", (char*) "(Ljava/lang/String;)Ljava/util/Vector;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_nativeGetResources },
596 { (char*) "defaultAssertionStatus", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_defaultAssertionStatus },
597 { (char*) "defaultUserAssertionStatus", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_defaultUserAssertionStatus },
598 { (char*) "packageAssertionStatus0", (char*) "(Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_packageAssertionStatus0 },
599 { (char*) "classAssertionStatus0", (char*) "(Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_classAssertionStatus0 },
600 { (char*) "findLoadedClass", (char*) "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_findLoadedClass },
604 /* _Jv_java_lang_VMClassLoader_init ********************************************
606 Register native functions.
608 *******************************************************************************/
612 void _Jv_java_lang_VMClassLoader_init(void)
616 u = utf_new_char("java/lang/VMClassLoader");
618 native_method_register(u, methods, NATIVE_METHODS_COUNT);
624 * These are local overrides for various environment variables in Emacs.
625 * Please do not remove this and leave it at the end of the file, where
626 * Emacs will automagically detect them.
627 * ---------------------------------------------------------------------
630 * indent-tabs-mode: t
634 * vim:noexpandtab:sw=4:ts=4: