1 /* src/native/vm/VMClassLoader.c - java/lang/VMClassLoader
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Roman Obermaiser
29 Changes: Joseph Wenninger
33 $Id: java_lang_VMClassLoader.c 6213 2006-12-18 17:36:06Z twisti $
44 #include "mm/memory.h"
45 #include "native/jni.h"
46 #include "native/native.h"
47 #include "native/include/java_lang_Class.h"
48 #include "native/include/java_lang_String.h"
49 #include "native/include/java_lang_ClassLoader.h"
50 #include "native/include/java_security_ProtectionDomain.h"
51 #include "native/include/java_util_Vector.h"
52 #include "toolbox/logging.h"
53 #include "vm/builtin.h"
55 #include "vm/classcache.h"
56 #include "vm/exceptions.h"
57 #include "vm/initialize.h"
58 #include "vm/linker.h"
59 #include "vm/loader.h"
60 #include "vm/options.h"
61 #include "vm/statistics.h"
62 #include "vm/stringlocal.h"
66 #include "vm/jit/asmpart.h"
68 #if defined(ENABLE_JVMTI)
69 #include "native/jvmti/cacaodbg.h"
74 * Class: java/lang/VMClassLoader
76 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
78 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_defineClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name, java_bytearray *data, s4 offset, s4 len, java_security_ProtectionDomain *pd)
84 #if defined(ENABLE_JVMTI)
85 jint new_class_data_len = 0;
86 unsigned char* new_class_data = NULL;
89 /* check if data was passed */
92 exceptions_throw_nullpointerexception();
96 /* check the indexes passed */
98 if ((offset < 0) || (len < 0) || ((offset + len) > data->header.size)) {
99 exceptions_throw_arrayindexoutofboundsexception();
104 /* convert '.' to '/' in java string */
106 utfname = javastring_toutf(name, true);
108 /* check if this class has already been defined */
110 c = classcache_lookup_defined_or_initiated((java_objectheader *) cl, utfname);
113 exceptions_new_linkageerror("duplicate class definition: ",c);
122 #if defined(ENABLE_JVMTI)
123 /* fire Class File Load Hook JVMTI event */
124 if (jvmti) jvmti_ClassFileLoadHook(utfname, len, (unsigned char*)data->data,
125 (java_objectheader *)cl, (java_objectheader *)pd,
126 &new_class_data_len, &new_class_data);
130 /* create a new classinfo struct */
132 c = class_create_classinfo(utfname);
134 #if defined(ENABLE_STATISTICS)
137 if (opt_getloadingtime)
141 /* build a classbuffer with the given data */
143 cb = NEW(classbuffer);
145 #if defined(ENABLE_JVMTI)
146 /* check if the JVMTI wants to modify the class */
147 if (new_class_data == NULL) {
150 cb->data = (u1 *) &data->data[offset];
151 #if defined(ENABLE_JVMTI)
153 cb->size = new_class_data_len;
154 cb->data = (u1 *) new_class_data;
159 /* preset the defining classloader */
161 c->classloader = (java_objectheader *) cl;
163 /* load the class from this buffer */
165 r = load_class_from_classbuffer(cb);
169 FREE(cb, classbuffer);
171 #if defined(ENABLE_STATISTICS)
174 if (opt_getloadingtime)
179 /* If return value is NULL, we had a problem and the class is not */
181 /* now free the allocated memory, otherwise we could run into a DOS */
188 /* set ProtectionDomain */
192 /* Store the newly defined class in the class cache. This call also */
193 /* checks whether a class of the same name has already been defined by */
194 /* the same defining loader, and if so, replaces the newly created class */
195 /* by the one defined earlier. */
196 /* Important: The classinfo given to classcache_store must be */
197 /* fully prepared because another thread may return this */
198 /* pointer after the lookup at to top of this function */
199 /* directly after the class cache lock has been released. */
201 c = classcache_store((java_objectheader *)cl,c,true);
203 return (java_lang_Class *) c;
208 * Class: java/lang/VMClassLoader
209 * Method: getPrimitiveClass
210 * Signature: (C)Ljava/lang/Class;
212 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
216 /* get primitive class */
220 c = primitivetype_table[PRIMITIVETYPE_INT].class_primitive;
223 c = primitivetype_table[PRIMITIVETYPE_LONG].class_primitive;
226 c = primitivetype_table[PRIMITIVETYPE_FLOAT].class_primitive;
229 c = primitivetype_table[PRIMITIVETYPE_DOUBLE].class_primitive;
232 c = primitivetype_table[PRIMITIVETYPE_BYTE].class_primitive;
235 c = primitivetype_table[PRIMITIVETYPE_CHAR].class_primitive;
238 c = primitivetype_table[PRIMITIVETYPE_SHORT].class_primitive;
241 c = primitivetype_table[PRIMITIVETYPE_BOOLEAN].class_primitive;
244 c = primitivetype_table[PRIMITIVETYPE_VOID].class_primitive;
247 *exceptionptr = new_exception(string_java_lang_ClassNotFoundException);
251 return (java_lang_Class *) c;
256 * Class: java/lang/VMClassLoader
257 * Method: resolveClass
258 * Signature: (Ljava/lang/Class;)V
260 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
264 ci = (classinfo *) c;
267 exceptions_throw_nullpointerexception();
273 if (!(ci->state & CLASS_LINKED))
274 (void) link_class(ci);
281 * Class: java/lang/VMClassLoader
283 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
285 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, jboolean resolve)
291 exceptions_throw_nullpointerexception();
295 /* create utf string in which '.' is replaced by '/' */
297 u = javastring_toutf(name, true);
301 if (!(c = load_class_bootstrap(u)))
304 /* resolve class -- if requested */
310 return (java_lang_Class *) c;
313 c = (*exceptionptr)->vftbl->class;
315 /* if the exception is a NoClassDefFoundError, we replace it with a
316 ClassNotFoundException, otherwise return the exception */
318 if (c == class_java_lang_NoClassDefFoundError) {
319 /* clear exceptionptr, because builtin_new checks for
320 ExceptionInInitializerError */
321 *exceptionptr = NULL;
324 new_exception_javastring(string_java_lang_ClassNotFoundException, name);
332 * Class: java/lang/VMClassLoader
333 * Method: nativeGetResources
334 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
336 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
338 jobject o; /* vector being created */
339 methodinfo *m; /* "add" method of vector */
340 java_lang_String *path; /* path to be added */
341 list_classpath_entry *lce; /* classpath entry */
342 utf *utfname; /* utf to look for */
343 char *buffer; /* char buffer */
344 char *namestart; /* start of name to use */
345 char *tmppath; /* temporary buffer */
346 s4 namelen; /* length of name to use */
347 s4 searchlen; /* length of name to search */
348 s4 bufsize; /* size of buffer allocated */
349 s4 pathlen; /* name of path to assemble */
350 struct stat buf; /* buffer for stat */
351 jboolean ret; /* return value of "add" */
353 /* get the resource name as utf string */
355 utfname = javastring_toutf(name, false);
359 /* copy it to a char buffer */
361 namelen = utf_bytes(utfname);
363 bufsize = namelen + strlen("0");
364 buffer = MNEW(char, bufsize);
366 utf_copy(buffer, utfname);
369 /* skip leading '/' */
371 if (namestart[0] == '/') {
377 /* remove trailing `.class' */
379 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
383 /* create a new needle to look for, if necessary */
385 if (searchlen != bufsize-1) {
386 utfname = utf_new(namestart, searchlen);
393 o = native_new_and_init(class_java_util_Vector);
398 /* get Vector.add() method */
400 m = class_resolveclassmethod(class_java_util_Vector,
402 utf_new_char("(Ljava/lang/Object;)Z"),
409 /* iterate over all classpath entries */
411 for (lce = list_first(list_classpath_entries); lce != NULL;
412 lce = list_next(list_classpath_entries, lce)) {
413 /* clear path pointer */
416 #if defined(ENABLE_ZLIB)
417 if (lce->type == CLASSPATH_ARCHIVE) {
419 if (zip_find(lce, utfname)) {
420 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
421 namelen + strlen("0");
423 tmppath = MNEW(char, pathlen);
425 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
426 path = javastring_new_from_utf_string(tmppath),
428 MFREE(tmppath, char, pathlen);
432 #endif /* defined(ENABLE_ZLIB) */
433 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
435 tmppath = MNEW(char, pathlen);
437 sprintf(tmppath, "file://%s%s", lce->path, namestart);
439 /* Does this file exist? */
441 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
442 if (!S_ISDIR(buf.st_mode))
443 path = javastring_new_from_utf_string(tmppath);
445 MFREE(tmppath, char, pathlen);
446 #if defined(ENABLE_ZLIB)
450 /* if a resource was found, add it to the vector */
453 ret = vm_call_method_int(m, o, path);
463 MFREE(buffer, char, bufsize);
465 return (java_util_Vector *) o;
468 MFREE(buffer, char, bufsize);
475 * Class: java/lang/VMClassLoader
476 * Method: defaultAssertionStatus
479 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
481 return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
486 * Class: java/lang/VMClassLoader
487 * Method: findLoadedClass
488 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
490 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name)
495 /* replace `.' by `/', this is required by the classcache */
497 u = javastring_toutf(name, true);
499 /* lookup for defining classloader */
501 c = classcache_lookup_defined((classloader *) cl, u);
503 /* if not found, lookup for initiating classloader */
506 c = classcache_lookup((classloader *) cl, u);
508 return (java_lang_Class *) c;
513 * These are local overrides for various environment variables in Emacs.
514 * Please do not remove this and leave it at the end of the file, where
515 * Emacs will automagically detect them.
516 * ---------------------------------------------------------------------
519 * indent-tabs-mode: t
523 * vim:noexpandtab:sw=4:ts=4: