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 7723 2007-04-16 18:03:08Z michi $
37 #include "mm/memory.h"
39 #include "native/jni.h"
40 #include "native/native.h"
41 #include "native/include/java_lang_Class.h"
42 #include "native/include/java_lang_String.h"
43 #include "native/include/java_security_ProtectionDomain.h" /* required by... */
44 #include "native/include/java_lang_ClassLoader.h"
45 #include "native/include/java_util_Vector.h"
47 #include "native/vm/java_lang_ClassLoader.h"
49 #include "toolbox/logging.h"
51 #include "vm/builtin.h"
52 #include "vm/exceptions.h"
53 #include "vm/initialize.h"
54 #include "vm/stringlocal.h"
57 #include "vm/jit/asmpart.h"
59 #include "vmcore/class.h"
60 #include "vmcore/classcache.h"
61 #include "vmcore/linker.h"
62 #include "vmcore/loader.h"
63 #include "vmcore/options.h"
64 #include "vmcore/statistics.h"
65 #include "vmcore/suck.h"
66 #include "vmcore/zip.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)
80 return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
85 * Class: java/lang/VMClassLoader
86 * Method: getPrimitiveClass
87 * Signature: (C)Ljava/lang/Class;
89 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
94 /* get primitive class */
98 index = PRIMITIVETYPE_INT;
101 index = PRIMITIVETYPE_LONG;
104 index = PRIMITIVETYPE_FLOAT;
107 index = PRIMITIVETYPE_DOUBLE;
110 index = PRIMITIVETYPE_BYTE;
113 index = PRIMITIVETYPE_CHAR;
116 index = PRIMITIVETYPE_SHORT;
119 index = PRIMITIVETYPE_BOOLEAN;
122 index = PRIMITIVETYPE_VOID;
125 exceptions_throw_noclassdeffounderror(utf_null);
129 c = primitivetype_table[index].class_primitive;
131 return (java_lang_Class *) c;
136 * Class: java/lang/VMClassLoader
137 * Method: resolveClass
138 * Signature: (Ljava/lang/Class;)V
140 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
144 ci = (classinfo *) c;
147 exceptions_throw_nullpointerexception();
153 if (!(ci->state & CLASS_LINKED))
154 (void) link_class(ci);
161 * Class: java/lang/VMClassLoader
163 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
165 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, jboolean resolve)
169 java_objectheader *xptr;
172 exceptions_throw_nullpointerexception();
176 /* create utf string in which '.' is replaced by '/' */
178 u = javastring_toutf((java_objectheader *) name, true);
182 c = load_class_bootstrap(u);
187 /* resolve class -- if requested */
193 return (java_lang_Class *) c;
196 xptr = exceptions_get_exception();
198 c = xptr->vftbl->class;
200 /* if the exception is a NoClassDefFoundError, we replace it with a
201 ClassNotFoundException, otherwise return the exception */
203 if (c == class_java_lang_NoClassDefFoundError) {
204 /* clear exceptionptr, because builtin_new checks for
205 ExceptionInInitializerError */
206 exceptions_clear_exception();
208 exceptions_throw_classnotfoundexception(u);
216 * Class: java/lang/VMClassLoader
217 * Method: nativeGetResources
218 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
220 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
222 java_objectheader *o; /* vector being created */
223 methodinfo *m; /* "add" method of vector */
224 java_objectheader *path; /* path to be added */
225 list_classpath_entry *lce; /* classpath entry */
226 utf *utfname; /* utf to look for */
227 char *buffer; /* char buffer */
228 char *namestart; /* start of name to use */
229 char *tmppath; /* temporary buffer */
230 s4 namelen; /* length of name to use */
231 s4 searchlen; /* length of name to search */
232 s4 bufsize; /* size of buffer allocated */
233 s4 pathlen; /* name of path to assemble */
234 struct stat buf; /* buffer for stat */
235 jboolean ret; /* return value of "add" */
237 /* get the resource name as utf string */
239 utfname = javastring_toutf((java_objectheader *) name, false);
244 /* copy it to a char buffer */
246 namelen = utf_bytes(utfname);
248 bufsize = namelen + strlen("0");
249 buffer = MNEW(char, bufsize);
251 utf_copy(buffer, utfname);
254 /* skip leading '/' */
256 if (namestart[0] == '/') {
262 /* remove trailing `.class' */
264 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
268 /* create a new needle to look for, if necessary */
270 if (searchlen != bufsize-1) {
271 utfname = utf_new(namestart, searchlen);
278 o = native_new_and_init(class_java_util_Vector);
283 /* get Vector.add() method */
285 m = class_resolveclassmethod(class_java_util_Vector,
287 utf_new_char("(Ljava/lang/Object;)Z"),
294 /* iterate over all classpath entries */
296 for (lce = list_first(list_classpath_entries); lce != NULL;
297 lce = list_next(list_classpath_entries, lce)) {
298 /* clear path pointer */
301 #if defined(ENABLE_ZLIB)
302 if (lce->type == CLASSPATH_ARCHIVE) {
304 if (zip_find(lce, utfname)) {
305 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
306 namelen + strlen("0");
308 tmppath = MNEW(char, pathlen);
310 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
311 path = javastring_new_from_utf_string(tmppath),
313 MFREE(tmppath, char, pathlen);
317 #endif /* defined(ENABLE_ZLIB) */
318 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
320 tmppath = MNEW(char, pathlen);
322 sprintf(tmppath, "file://%s%s", lce->path, namestart);
324 /* Does this file exist? */
326 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
327 if (!S_ISDIR(buf.st_mode))
328 path = javastring_new_from_utf_string(tmppath);
330 MFREE(tmppath, char, pathlen);
331 #if defined(ENABLE_ZLIB)
335 /* if a resource was found, add it to the vector */
338 ret = vm_call_method_int(m, o, path);
340 if (exceptions_get_exception() != NULL)
348 MFREE(buffer, char, bufsize);
350 return (java_util_Vector *) o;
353 MFREE(buffer, char, bufsize);
360 * Class: java/lang/VMClassLoader
361 * Method: defaultAssertionStatus
364 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
366 return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
371 * Class: java/lang/VMClassLoader
372 * Method: findLoadedClass
373 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
375 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
381 cl = (classloader *) loader;
383 /* replace `.' by `/', this is required by the classcache */
385 u = javastring_toutf((java_objectheader *) name, true);
387 /* lookup for defining classloader */
389 c = classcache_lookup_defined(cl, u);
391 /* if not found, lookup for initiating classloader */
394 c = classcache_lookup(cl, u);
396 return (java_lang_Class *) c;
401 * These are local overrides for various environment variables in Emacs.
402 * Please do not remove this and leave it at the end of the file, where
403 * Emacs will automagically detect them.
404 * ---------------------------------------------------------------------
407 * indent-tabs-mode: t
411 * vim:noexpandtab:sw=4:ts=4: