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.h"
35 #include "native/llni.h"
36 #include "native/native.h"
37 #include "native/include/java_lang_Class.h"
38 #include "native/include/java_lang_String.h"
39 #include "native/include/java_security_ProtectionDomain.h" /* required by... */
40 #include "native/include/java_lang_ClassLoader.h"
41 #include "native/include/java_util_HashMap.h"
42 #include "native/include/java_util_Map.h"
43 #include "native/include/java_lang_Boolean.h"
47 #include "native/include/java_lang_VMClassLoader.h"
50 #include "toolbox/logging.h"
51 #include "toolbox/list.h"
53 #if defined(ENABLE_ASSERTION)
54 #include "vm/assertion.h"
57 #include "vm/builtin.h"
58 #include "vm/exceptions.hpp"
59 #include "vm/initialize.h"
60 #include "vm/primitive.hpp"
61 #include "vm/stringlocal.h"
64 #include "vm/jit/asmpart.h"
66 #include "vmcore/class.h"
67 #include "vmcore/classcache.h"
68 #include "vmcore/globals.hpp"
69 #include "vmcore/linker.h"
70 #include "vmcore/loader.h"
71 #include "vmcore/options.h"
72 #include "vmcore/statistics.h"
73 #include "vmcore/suck.h"
74 #include "vmcore/zip.h"
76 #if defined(ENABLE_JVMTI)
77 #include "native/jvmti/cacaodbg.h"
81 // Native functions are exported as C functions.
85 * Class: java/lang/VMClassLoader
87 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
89 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_defineClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name, java_handle_bytearray_t *data, int32_t offset, int32_t len, java_security_ProtectionDomain *pd)
93 classloader_t *loader;
96 #if defined(ENABLE_JVMTI)
97 jint new_class_data_len = 0;
98 unsigned char* new_class_data = NULL;
101 /* check if data was passed */
104 exceptions_throw_nullpointerexception();
108 /* check the indexes passed */
110 if ((offset < 0) || (len < 0) || ((offset + len) > LLNI_array_size(data))) {
111 exceptions_throw_arrayindexoutofboundsexception();
115 /* add classloader to classloader hashtable */
117 loader = loader_hashtable_classloader_add((java_handle_t *) cl);
120 /* convert '.' to '/' in java string */
122 utfname = javastring_toutf((java_handle_t *) name, true);
128 #if defined(ENABLE_JVMTI)
129 /* XXX again this will not work because of the indirection cell for classloaders */
131 /* fire Class File Load Hook JVMTI event */
134 jvmti_ClassFileLoadHook(utfname, len, (unsigned char *) data->data,
135 loader, (java_handle_t *) pd,
136 &new_class_data_len, &new_class_data);
139 /* define the class */
141 #if defined(ENABLE_JVMTI)
142 /* check if the JVMTI wants to modify the class */
144 if (new_class_data == NULL)
145 c = class_define(utfname, loader, new_class_data_len, new_class_data, pd);
148 c = class_define(utfname, loader, len, (uint8_t *) &LLNI_array_direct(data, offset), (java_handle_t *) pd);
153 /* for convenience */
155 o = LLNI_classinfo_wrap(c);
157 /* set ProtectionDomain */
159 LLNI_field_set_ref(o, pd, pd);
166 * Class: java/lang/VMClassLoader
167 * Method: getPrimitiveClass
168 * Signature: (C)Ljava/lang/Class;
170 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, int32_t type)
174 c = Primitive::get_class_by_char(type);
177 exceptions_throw_classnotfoundexception(utf_null);
181 return LLNI_classinfo_wrap(c);
186 * Class: java/lang/VMClassLoader
187 * Method: resolveClass
188 * Signature: (Ljava/lang/Class;)V
190 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
194 ci = LLNI_classinfo_unwrap(c);
197 exceptions_throw_nullpointerexception();
203 if (!(ci->state & CLASS_LINKED))
204 (void) link_class(ci);
211 * Class: java/lang/VMClassLoader
213 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
215 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, int32_t resolve)
221 exceptions_throw_nullpointerexception();
225 /* create utf string in which '.' is replaced by '/' */
227 u = javastring_toutf((java_handle_t *) name, true);
231 c = load_class_bootstrap(u);
236 /* resolve class -- if requested */
242 return LLNI_classinfo_wrap(c);
247 * Class: java/lang/VMClassLoader
248 * Method: nativeGetResources
249 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
251 JNIEXPORT struct java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
253 java_handle_t *o; /* vector being created */
254 methodinfo *m; /* "add" method of vector */
255 java_handle_t *path; /* path to be added */
256 list_classpath_entry *lce; /* classpath entry */
257 utf *utfname; /* utf to look for */
258 char *buffer; /* char buffer */
259 char *namestart; /* start of name to use */
260 char *tmppath; /* temporary buffer */
261 int32_t namelen; /* length of name to use */
262 int32_t searchlen; /* length of name to search */
263 int32_t bufsize; /* size of buffer allocated */
264 int32_t pathlen; /* name of path to assemble */
265 struct stat buf; /* buffer for stat */
266 jboolean ret; /* return value of "add" */
268 /* get the resource name as utf string */
270 utfname = javastring_toutf((java_handle_t *) name, false);
275 /* copy it to a char buffer */
277 namelen = utf_bytes(utfname);
279 bufsize = namelen + strlen("0");
280 buffer = MNEW(char, bufsize);
282 utf_copy(buffer, utfname);
285 /* skip leading '/' */
287 if (namestart[0] == '/') {
293 /* remove trailing `.class' */
295 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
299 /* create a new needle to look for, if necessary */
301 if (searchlen != bufsize-1) {
302 utfname = utf_new(namestart, searchlen);
309 o = native_new_and_init(class_java_util_Vector);
314 /* get Vector.add() method */
316 m = class_resolveclassmethod(class_java_util_Vector,
318 utf_new_char("(Ljava/lang/Object;)Z"),
325 /* iterate over all classpath entries */
327 for (lce = (list_classpath_entry*) list_first(list_classpath_entries); lce != NULL;
328 lce = (list_classpath_entry*) list_next(list_classpath_entries, lce)) {
329 /* clear path pointer */
332 #if defined(ENABLE_ZLIB)
333 if (lce->type == CLASSPATH_ARCHIVE) {
335 if (zip_find(lce, utfname)) {
336 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
337 namelen + strlen("0");
339 tmppath = MNEW(char, pathlen);
341 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
342 path = javastring_new_from_utf_string(tmppath),
344 MFREE(tmppath, char, pathlen);
348 #endif /* defined(ENABLE_ZLIB) */
349 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
351 tmppath = MNEW(char, pathlen);
353 sprintf(tmppath, "file://%s%s", lce->path, namestart);
355 /* Does this file exist? */
357 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
358 if (!S_ISDIR(buf.st_mode))
359 path = javastring_new_from_utf_string(tmppath);
361 MFREE(tmppath, char, pathlen);
362 #if defined(ENABLE_ZLIB)
366 /* if a resource was found, add it to the vector */
369 ret = vm_call_method_int(m, o, path);
371 if (exceptions_get_exception() != NULL)
379 MFREE(buffer, char, bufsize);
381 return (struct java_util_Vector *) o;
384 MFREE(buffer, char, bufsize);
391 * Class: java/lang/VMClassLoader
392 * Method: defaultAssertionStatus
395 JNIEXPORT int32_t JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
397 #if defined(ENABLE_ASSERTION)
398 return assertion_system_enabled;
405 * Class: java/lang/VMClassLoader
406 * Method: userAssertionStatus
409 JNIEXPORT int32_t JNICALL Java_java_lang_VMClassLoader_defaultUserAssertionStatus(JNIEnv *env, jclass clazz)
411 #if defined(ENABLE_ASSERTION)
412 return assertion_user_enabled;
419 * Class: java/lang/VMClassLoader
420 * Method: packageAssertionStatus
421 * Signature: ()Ljava_util_Map;
423 JNIEXPORT java_util_Map* JNICALL Java_java_lang_VMClassLoader_packageAssertionStatus0(JNIEnv *env, jclass clazz, java_lang_Boolean *jtrue, java_lang_Boolean *jfalse)
426 #if defined(ENABLE_ASSERTION)
429 assertion_name_t *item;
434 hm = native_new_and_init(class_java_util_HashMap);
439 #if defined(ENABLE_ASSERTION)
440 /* if nothing todo, return now */
442 if (assertion_package_count == 0) {
443 return (java_util_Map *) hm;
446 /* get HashMap.put method */
448 m = class_resolveclassmethod(class_java_util_HashMap,
450 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
458 item = (assertion_name_t *)list_first(list_assertion_names);
460 while (item != NULL) {
461 if (item->package == false) {
462 item = (assertion_name_t *)list_next(list_assertion_names, item);
466 if (strcmp(item->name, "") == 0) {
467 /* unnamed package wanted */
471 js = javastring_new_from_ascii(item->name);
477 if (item->enabled == true) {
478 vm_call_method(m, hm, js, jtrue);
481 vm_call_method(m, hm, js, jfalse);
484 item = (assertion_name_t *)list_next(list_assertion_names, item);
488 return (java_util_Map *) hm;
492 * Class: java/lang/VMClassLoader
493 * Method: classAssertionStatus
494 * Signature: ()Ljava_util_Map;
496 JNIEXPORT java_util_Map* JNICALL Java_java_lang_VMClassLoader_classAssertionStatus0(JNIEnv *env, jclass clazz, java_lang_Boolean *jtrue, java_lang_Boolean *jfalse)
499 #if defined(ENABLE_ASSERTION)
502 assertion_name_t *item;
507 hm = native_new_and_init(class_java_util_HashMap);
512 #if defined(ENABLE_ASSERTION)
513 /* if nothing todo, return now */
515 if (assertion_class_count == 0) {
516 return (java_util_Map *) hm;
519 /* get HashMap.put method */
521 m = class_resolveclassmethod(class_java_util_HashMap,
523 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
531 item = (assertion_name_t *)list_first(list_assertion_names);
533 while (item != NULL) {
534 if (item->package == true) {
535 item = (assertion_name_t *)list_next(list_assertion_names, item);
539 js = javastring_new_from_ascii(item->name);
544 if (item->enabled == true) {
545 vm_call_method(m, hm, js, jtrue);
548 vm_call_method(m, hm, js, jfalse);
551 item = (assertion_name_t *)list_next(list_assertion_names, item);
555 return (java_util_Map *) hm;
560 * Class: java/lang/VMClassLoader
561 * Method: findLoadedClass
562 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
564 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
570 /* XXX is it correct to add the classloader to the hashtable here? */
572 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
574 /* replace `.' by `/', this is required by the classcache */
576 u = javastring_toutf((java_handle_t *) name, true);
578 /* lookup for defining classloader */
580 c = classcache_lookup_defined(cl, u);
582 /* if not found, lookup for initiating classloader */
585 c = classcache_lookup(cl, u);
587 return LLNI_classinfo_wrap(c);
593 /* native methods implemented by this file ************************************/
595 static JNINativeMethod methods[] = {
596 { (char*) "defineClass", (char*) "(Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_defineClass },
597 { (char*) "getPrimitiveClass", (char*) "(C)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_getPrimitiveClass },
598 { (char*) "resolveClass", (char*) "(Ljava/lang/Class;)V", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_resolveClass },
599 { (char*) "loadClass", (char*) "(Ljava/lang/String;Z)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_loadClass },
600 { (char*) "nativeGetResources", (char*) "(Ljava/lang/String;)Ljava/util/Vector;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_nativeGetResources },
601 { (char*) "defaultAssertionStatus", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_defaultAssertionStatus },
602 { (char*) "defaultUserAssertionStatus", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_defaultUserAssertionStatus },
603 { (char*) "packageAssertionStatus0", (char*) "(Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_packageAssertionStatus0 },
604 { (char*) "classAssertionStatus0", (char*) "(Ljava/lang/Boolean;Ljava/lang/Boolean;)Ljava/util/Map;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_classAssertionStatus0 },
605 { (char*) "findLoadedClass", (char*) "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", (void*) (uintptr_t) &Java_java_lang_VMClassLoader_findLoadedClass },
609 /* _Jv_java_lang_VMClassLoader_init ********************************************
611 Register native functions.
613 *******************************************************************************/
617 void _Jv_java_lang_VMClassLoader_init(void)
621 u = utf_new_char("java/lang/VMClassLoader");
623 native_method_register(u, methods, NATIVE_METHODS_COUNT);
629 * These are local overrides for various environment variables in Emacs.
630 * Please do not remove this and leave it at the end of the file, where
631 * Emacs will automagically detect them.
632 * ---------------------------------------------------------------------
635 * indent-tabs-mode: t
639 * vim:noexpandtab:sw=4:ts=4: