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 7408 2007-02-26 22:11:38Z 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 "native/vm/java_lang_ClassLoader.h"
48 #include "toolbox/logging.h"
50 #include "vm/builtin.h"
51 #include "vm/exceptions.h"
52 #include "vm/initialize.h"
53 #include "vm/stringlocal.h"
56 #include "vm/jit/asmpart.h"
58 #include "vmcore/class.h"
59 #include "vmcore/classcache.h"
60 #include "vmcore/linker.h"
61 #include "vmcore/loader.h"
62 #include "vmcore/options.h"
63 #include "vmcore/statistics.h"
64 #include "vmcore/suck.h"
65 #include "vmcore/zip.h"
67 #if defined(ENABLE_JVMTI)
68 #include "native/jvmti/cacaodbg.h"
73 * Class: java/lang/VMClassLoader
75 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
77 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)
79 return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
84 * Class: java/lang/VMClassLoader
85 * Method: getPrimitiveClass
86 * Signature: (C)Ljava/lang/Class;
88 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
93 /* get primitive class */
97 index = PRIMITIVETYPE_INT;
100 index = PRIMITIVETYPE_LONG;
103 index = PRIMITIVETYPE_FLOAT;
106 index = PRIMITIVETYPE_DOUBLE;
109 index = PRIMITIVETYPE_BYTE;
112 index = PRIMITIVETYPE_CHAR;
115 index = PRIMITIVETYPE_SHORT;
118 index = PRIMITIVETYPE_BOOLEAN;
121 index = PRIMITIVETYPE_VOID;
124 exceptions_throw_noclassdeffounderror(utf_null);
128 c = primitivetype_table[index].class_primitive;
130 return (java_lang_Class *) c;
135 * Class: java/lang/VMClassLoader
136 * Method: resolveClass
137 * Signature: (Ljava/lang/Class;)V
139 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
143 ci = (classinfo *) c;
146 exceptions_throw_nullpointerexception();
152 if (!(ci->state & CLASS_LINKED))
153 (void) link_class(ci);
160 * Class: java/lang/VMClassLoader
162 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
164 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, jboolean resolve)
168 java_objectheader *xptr;
171 exceptions_throw_nullpointerexception();
175 /* create utf string in which '.' is replaced by '/' */
177 u = javastring_toutf(name, true);
181 c = load_class_bootstrap(u);
186 /* resolve class -- if requested */
192 return (java_lang_Class *) c;
195 xptr = exceptions_get_exception();
197 c = xptr->vftbl->class;
199 /* if the exception is a NoClassDefFoundError, we replace it with a
200 ClassNotFoundException, otherwise return the exception */
202 if (c == class_java_lang_NoClassDefFoundError) {
203 /* clear exceptionptr, because builtin_new checks for
204 ExceptionInInitializerError */
205 exceptions_clear_exception();
207 exceptions_throw_classnotfoundexception(u);
215 * Class: java/lang/VMClassLoader
216 * Method: nativeGetResources
217 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
219 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
221 jobject o; /* vector being created */
222 methodinfo *m; /* "add" method of vector */
223 java_lang_String *path; /* path to be added */
224 list_classpath_entry *lce; /* classpath entry */
225 utf *utfname; /* utf to look for */
226 char *buffer; /* char buffer */
227 char *namestart; /* start of name to use */
228 char *tmppath; /* temporary buffer */
229 s4 namelen; /* length of name to use */
230 s4 searchlen; /* length of name to search */
231 s4 bufsize; /* size of buffer allocated */
232 s4 pathlen; /* name of path to assemble */
233 struct stat buf; /* buffer for stat */
234 jboolean ret; /* return value of "add" */
236 /* get the resource name as utf string */
238 utfname = javastring_toutf(name, false);
242 /* copy it to a char buffer */
244 namelen = utf_bytes(utfname);
246 bufsize = namelen + strlen("0");
247 buffer = MNEW(char, bufsize);
249 utf_copy(buffer, utfname);
252 /* skip leading '/' */
254 if (namestart[0] == '/') {
260 /* remove trailing `.class' */
262 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
266 /* create a new needle to look for, if necessary */
268 if (searchlen != bufsize-1) {
269 utfname = utf_new(namestart, searchlen);
276 o = native_new_and_init(class_java_util_Vector);
281 /* get Vector.add() method */
283 m = class_resolveclassmethod(class_java_util_Vector,
285 utf_new_char("(Ljava/lang/Object;)Z"),
292 /* iterate over all classpath entries */
294 for (lce = list_first(list_classpath_entries); lce != NULL;
295 lce = list_next(list_classpath_entries, lce)) {
296 /* clear path pointer */
299 #if defined(ENABLE_ZLIB)
300 if (lce->type == CLASSPATH_ARCHIVE) {
302 if (zip_find(lce, utfname)) {
303 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
304 namelen + strlen("0");
306 tmppath = MNEW(char, pathlen);
308 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
309 path = javastring_new_from_utf_string(tmppath),
311 MFREE(tmppath, char, pathlen);
315 #endif /* defined(ENABLE_ZLIB) */
316 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
318 tmppath = MNEW(char, pathlen);
320 sprintf(tmppath, "file://%s%s", lce->path, namestart);
322 /* Does this file exist? */
324 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
325 if (!S_ISDIR(buf.st_mode))
326 path = javastring_new_from_utf_string(tmppath);
328 MFREE(tmppath, char, pathlen);
329 #if defined(ENABLE_ZLIB)
333 /* if a resource was found, add it to the vector */
336 ret = vm_call_method_int(m, o, path);
338 if (exceptions_get_exception() != NULL)
346 MFREE(buffer, char, bufsize);
348 return (java_util_Vector *) o;
351 MFREE(buffer, char, bufsize);
358 * Class: java/lang/VMClassLoader
359 * Method: defaultAssertionStatus
362 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
364 return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
369 * Class: java/lang/VMClassLoader
370 * Method: findLoadedClass
371 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
373 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *cl, java_lang_String *name)
378 /* replace `.' by `/', this is required by the classcache */
380 u = javastring_toutf(name, true);
382 /* lookup for defining classloader */
384 c = classcache_lookup_defined((classloader *) cl, u);
386 /* if not found, lookup for initiating classloader */
389 c = classcache_lookup((classloader *) cl, u);
391 return (java_lang_Class *) c;
396 * These are local overrides for various environment variables in Emacs.
397 * Please do not remove this and leave it at the end of the file, where
398 * Emacs will automagically detect them.
399 * ---------------------------------------------------------------------
402 * indent-tabs-mode: t
406 * vim:noexpandtab:sw=4:ts=4: