1 /* src/native/vm/gnu/VMClassLoader.c
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: java_lang_VMClassLoader.c 7246 2007-01-29 18:49:05Z twisti $
36 #include "mm/memory.h"
38 #include "native/jni.h"
39 #include "native/native.h"
40 #include "native/include/java_lang_Class.h"
41 #include "native/include/java_lang_String.h"
42 #include "native/include/java_security_ProtectionDomain.h" /* required by... */
43 #include "native/include/java_lang_ClassLoader.h"
44 #include "native/include/java_util_Vector.h"
46 #include "toolbox/logging.h"
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/initialize.h"
51 #include "vm/stringlocal.h"
54 #include "vm/jit/asmpart.h"
56 #include "vmcore/class.h"
57 #include "vmcore/classcache.h"
58 #include "vmcore/linker.h"
59 #include "vmcore/loader.h"
60 #include "vmcore/options.h"
61 #include "vmcore/statistics.h"
62 #include "vmcore/suck.h"
63 #include "vmcore/zip.h"
65 #if defined(ENABLE_JVMTI)
66 #include "native/jvmti/cacaodbg.h"
71 * Class: java/lang/VMClassLoader
73 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
75 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)
82 #if defined(ENABLE_JVMTI)
83 jint new_class_data_len = 0;
84 unsigned char* new_class_data = NULL;
87 /* check if data was passed */
90 exceptions_throw_nullpointerexception();
94 /* check the indexes passed */
96 if ((offset < 0) || (len < 0) || ((offset + len) > data->header.size)) {
97 exceptions_throw_arrayindexoutofboundsexception();
102 /* convert '.' to '/' in java string */
104 utfname = javastring_toutf(name, true);
106 /* check if this class has already been defined */
108 c = classcache_lookup_defined_or_initiated((java_objectheader *) cl, utfname);
110 exceptions_throw_linkageerror("duplicate class definition: ", c);
119 #if defined(ENABLE_JVMTI)
120 /* fire Class File Load Hook JVMTI event */
121 if (jvmti) jvmti_ClassFileLoadHook(utfname, len, (unsigned char*)data->data,
122 (java_objectheader *)cl, (java_objectheader *)pd,
123 &new_class_data_len, &new_class_data);
127 /* create a new classinfo struct */
129 c = class_create_classinfo(utfname);
131 #if defined(ENABLE_STATISTICS)
134 if (opt_getloadingtime)
138 /* build a classbuffer with the given data */
140 cb = NEW(classbuffer);
142 #if defined(ENABLE_JVMTI)
143 /* check if the JVMTI wants to modify the class */
144 if (new_class_data == NULL) {
147 cb->data = (u1 *) &data->data[offset];
148 #if defined(ENABLE_JVMTI)
150 cb->size = new_class_data_len;
151 cb->data = (u1 *) new_class_data;
156 /* preset the defining classloader */
158 c->classloader = (java_objectheader *) cl;
160 /* load the class from this buffer */
162 r = load_class_from_classbuffer(cb);
166 FREE(cb, classbuffer);
168 #if defined(ENABLE_STATISTICS)
171 if (opt_getloadingtime)
176 /* If return value is NULL, we had a problem and the class is
177 not loaded. Now free the allocated memory, otherwise we
178 could run into a DOS. */
185 /* set ProtectionDomain */
187 co = (java_lang_Class *) c;
191 /* Store the newly defined class in the class cache. This call also */
192 /* checks whether a class of the same name has already been defined by */
193 /* the same defining loader, and if so, replaces the newly created class */
194 /* by the one defined earlier. */
195 /* Important: The classinfo given to classcache_store must be */
196 /* fully prepared because another thread may return this */
197 /* pointer after the lookup at to top of this function */
198 /* directly after the class cache lock has been released. */
200 c = classcache_store((java_objectheader *) cl, c, true);
202 return (java_lang_Class *) c;
207 * Class: java/lang/VMClassLoader
208 * Method: getPrimitiveClass
209 * Signature: (C)Ljava/lang/Class;
211 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
216 /* get primitive class */
220 index = PRIMITIVETYPE_INT;
223 index = PRIMITIVETYPE_LONG;
226 index = PRIMITIVETYPE_FLOAT;
229 index = PRIMITIVETYPE_DOUBLE;
232 index = PRIMITIVETYPE_BYTE;
235 index = PRIMITIVETYPE_CHAR;
238 index = PRIMITIVETYPE_SHORT;
241 index = PRIMITIVETYPE_BOOLEAN;
244 index = PRIMITIVETYPE_VOID;
247 exceptions_throw_noclassdeffounderror(utf_null);
251 c = primitivetype_table[index].class_primitive;
253 return (java_lang_Class *) c;
258 * Class: java/lang/VMClassLoader
259 * Method: resolveClass
260 * Signature: (Ljava/lang/Class;)V
262 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
266 ci = (classinfo *) c;
269 exceptions_throw_nullpointerexception();
275 if (!(ci->state & CLASS_LINKED))
276 (void) link_class(ci);
283 * Class: java/lang/VMClassLoader
285 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
287 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, jboolean resolve)
291 java_objectheader *xptr;
294 exceptions_throw_nullpointerexception();
298 /* create utf string in which '.' is replaced by '/' */
300 u = javastring_toutf(name, true);
304 c = load_class_bootstrap(u);
309 /* resolve class -- if requested */
315 return (java_lang_Class *) c;
318 xptr = exceptions_get_exception();
320 c = xptr->vftbl->class;
322 /* if the exception is a NoClassDefFoundError, we replace it with a
323 ClassNotFoundException, otherwise return the exception */
325 if (c == class_java_lang_NoClassDefFoundError) {
326 /* clear exceptionptr, because builtin_new checks for
327 ExceptionInInitializerError */
328 exceptions_clear_exception();
330 exceptions_throw_classnotfoundexception(u);
338 * Class: java/lang/VMClassLoader
339 * Method: nativeGetResources
340 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
342 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
344 jobject o; /* vector being created */
345 methodinfo *m; /* "add" method of vector */
346 java_lang_String *path; /* path to be added */
347 list_classpath_entry *lce; /* classpath entry */
348 utf *utfname; /* utf to look for */
349 char *buffer; /* char buffer */
350 char *namestart; /* start of name to use */
351 char *tmppath; /* temporary buffer */
352 s4 namelen; /* length of name to use */
353 s4 searchlen; /* length of name to search */
354 s4 bufsize; /* size of buffer allocated */
355 s4 pathlen; /* name of path to assemble */
356 struct stat buf; /* buffer for stat */
357 jboolean ret; /* return value of "add" */
359 /* get the resource name as utf string */
361 utfname = javastring_toutf(name, false);
365 /* copy it to a char buffer */
367 namelen = utf_bytes(utfname);
369 bufsize = namelen + strlen("0");
370 buffer = MNEW(char, bufsize);
372 utf_copy(buffer, utfname);
375 /* skip leading '/' */
377 if (namestart[0] == '/') {
383 /* remove trailing `.class' */
385 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
389 /* create a new needle to look for, if necessary */
391 if (searchlen != bufsize-1) {
392 utfname = utf_new(namestart, searchlen);
399 o = native_new_and_init(class_java_util_Vector);
404 /* get Vector.add() method */
406 m = class_resolveclassmethod(class_java_util_Vector,
408 utf_new_char("(Ljava/lang/Object;)Z"),
415 /* iterate over all classpath entries */
417 for (lce = list_first(list_classpath_entries); lce != NULL;
418 lce = list_next(list_classpath_entries, lce)) {
419 /* clear path pointer */
422 #if defined(ENABLE_ZLIB)
423 if (lce->type == CLASSPATH_ARCHIVE) {
425 if (zip_find(lce, utfname)) {
426 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
427 namelen + strlen("0");
429 tmppath = MNEW(char, pathlen);
431 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
432 path = javastring_new_from_utf_string(tmppath),
434 MFREE(tmppath, char, pathlen);
438 #endif /* defined(ENABLE_ZLIB) */
439 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
441 tmppath = MNEW(char, pathlen);
443 sprintf(tmppath, "file://%s%s", lce->path, namestart);
445 /* Does this file exist? */
447 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
448 if (!S_ISDIR(buf.st_mode))
449 path = javastring_new_from_utf_string(tmppath);
451 MFREE(tmppath, char, pathlen);
452 #if defined(ENABLE_ZLIB)
456 /* if a resource was found, add it to the vector */
459 ret = vm_call_method_int(m, o, path);
461 if (exceptions_get_exception() != NULL)
469 MFREE(buffer, char, bufsize);
471 return (java_util_Vector *) o;
474 MFREE(buffer, char, bufsize);
481 * Class: java/lang/VMClassLoader
482 * Method: defaultAssertionStatus
485 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
487 return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
492 * Class: java/lang/VMClassLoader
493 * Method: findLoadedClass
494 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
496 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name)
501 /* replace `.' by `/', this is required by the classcache */
503 u = javastring_toutf(name, true);
505 /* lookup for defining classloader */
507 c = classcache_lookup_defined((classloader *) cl, u);
509 /* if not found, lookup for initiating classloader */
512 c = classcache_lookup((classloader *) cl, u);
514 return (java_lang_Class *) c;
519 * These are local overrides for various environment variables in Emacs.
520 * Please do not remove this and leave it at the end of the file, where
521 * Emacs will automagically detect them.
522 * ---------------------------------------------------------------------
525 * indent-tabs-mode: t
529 * vim:noexpandtab:sw=4:ts=4: