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 8341 2007-08-17 21:32:01Z michi $
36 #include "mm/memory.h"
38 #include "native/jni.h"
39 #include "native/llni.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/include/java_lang_VMClassLoader.h"
49 #include "native/vm/java_lang_ClassLoader.h"
51 #include "toolbox/logging.h"
53 #include "vm/builtin.h"
54 #include "vm/exceptions.h"
55 #include "vm/initialize.h"
56 #include "vm/primitive.h"
57 #include "vm/stringlocal.h"
60 #include "vm/jit/asmpart.h"
62 #include "vmcore/class.h"
63 #include "vmcore/classcache.h"
64 #include "vmcore/linker.h"
65 #include "vmcore/loader.h"
66 #include "vmcore/options.h"
67 #include "vmcore/statistics.h"
68 #include "vmcore/suck.h"
69 #include "vmcore/zip.h"
71 #if defined(ENABLE_JVMTI)
72 #include "native/jvmti/cacaodbg.h"
76 /* native methods implemented by this file ************************************/
78 static JNINativeMethod methods[] = {
79 { "defineClass", "(Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_defineClass },
80 { "getPrimitiveClass", "(C)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_getPrimitiveClass },
81 { "resolveClass", "(Ljava/lang/Class;)V", (void *) (ptrint) &Java_java_lang_VMClassLoader_resolveClass },
82 { "loadClass", "(Ljava/lang/String;Z)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_loadClass },
83 { "nativeGetResources", "(Ljava/lang/String;)Ljava/util/Vector;", (void *) (ptrint) &Java_java_lang_VMClassLoader_nativeGetResources },
84 { "defaultAssertionStatus", "()Z", (void *) (ptrint) &Java_java_lang_VMClassLoader_defaultAssertionStatus },
85 { "findLoadedClass", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_findLoadedClass },
89 /* _Jv_java_lang_VMClassLoader_init ********************************************
91 Register native functions.
93 *******************************************************************************/
95 void _Jv_java_lang_VMClassLoader_init(void)
99 u = utf_new_char("java/lang/VMClassLoader");
101 native_method_register(u, methods, NATIVE_METHODS_COUNT);
106 * Class: java/lang/VMClassLoader
107 * Method: defineClass
108 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
110 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, s4 offset, s4 len, java_security_ProtectionDomain *pd)
112 return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
117 * Class: java/lang/VMClassLoader
118 * Method: getPrimitiveClass
119 * Signature: (C)Ljava/lang/Class;
121 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
125 c = primitive_class_get_by_char(type);
128 exceptions_throw_classnotfoundexception(utf_null);
132 return LLNI_classinfo_wrap(c);
137 * Class: java/lang/VMClassLoader
138 * Method: resolveClass
139 * Signature: (Ljava/lang/Class;)V
141 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
145 ci = LLNI_classinfo_unwrap(c);
148 exceptions_throw_nullpointerexception();
154 if (!(ci->state & CLASS_LINKED))
155 (void) link_class(ci);
162 * Class: java/lang/VMClassLoader
164 * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
166 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, s4 resolve)
172 exceptions_throw_nullpointerexception();
176 /* create utf string in which '.' is replaced by '/' */
178 u = javastring_toutf((java_handle_t *) name, true);
182 c = load_class_bootstrap(u);
187 /* resolve class -- if requested */
193 return LLNI_classinfo_wrap(c);
198 * Class: java/lang/VMClassLoader
199 * Method: nativeGetResources
200 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
202 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
204 java_handle_t *o; /* vector being created */
205 methodinfo *m; /* "add" method of vector */
206 java_handle_t *path; /* path to be added */
207 list_classpath_entry *lce; /* classpath entry */
208 utf *utfname; /* utf to look for */
209 char *buffer; /* char buffer */
210 char *namestart; /* start of name to use */
211 char *tmppath; /* temporary buffer */
212 s4 namelen; /* length of name to use */
213 s4 searchlen; /* length of name to search */
214 s4 bufsize; /* size of buffer allocated */
215 s4 pathlen; /* name of path to assemble */
216 struct stat buf; /* buffer for stat */
217 jboolean ret; /* return value of "add" */
219 /* get the resource name as utf string */
221 utfname = javastring_toutf((java_handle_t *) name, false);
226 /* copy it to a char buffer */
228 namelen = utf_bytes(utfname);
230 bufsize = namelen + strlen("0");
231 buffer = MNEW(char, bufsize);
233 utf_copy(buffer, utfname);
236 /* skip leading '/' */
238 if (namestart[0] == '/') {
244 /* remove trailing `.class' */
246 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
250 /* create a new needle to look for, if necessary */
252 if (searchlen != bufsize-1) {
253 utfname = utf_new(namestart, searchlen);
260 o = native_new_and_init(class_java_util_Vector);
265 /* get Vector.add() method */
267 m = class_resolveclassmethod(class_java_util_Vector,
269 utf_new_char("(Ljava/lang/Object;)Z"),
276 /* iterate over all classpath entries */
278 for (lce = list_first(list_classpath_entries); lce != NULL;
279 lce = list_next(list_classpath_entries, lce)) {
280 /* clear path pointer */
283 #if defined(ENABLE_ZLIB)
284 if (lce->type == CLASSPATH_ARCHIVE) {
286 if (zip_find(lce, utfname)) {
287 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
288 namelen + strlen("0");
290 tmppath = MNEW(char, pathlen);
292 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
293 path = javastring_new_from_utf_string(tmppath),
295 MFREE(tmppath, char, pathlen);
299 #endif /* defined(ENABLE_ZLIB) */
300 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
302 tmppath = MNEW(char, pathlen);
304 sprintf(tmppath, "file://%s%s", lce->path, namestart);
306 /* Does this file exist? */
308 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
309 if (!S_ISDIR(buf.st_mode))
310 path = javastring_new_from_utf_string(tmppath);
312 MFREE(tmppath, char, pathlen);
313 #if defined(ENABLE_ZLIB)
317 /* if a resource was found, add it to the vector */
320 ret = vm_call_method_int(m, o, path);
322 if (exceptions_get_exception() != NULL)
330 MFREE(buffer, char, bufsize);
332 return (java_util_Vector *) o;
335 MFREE(buffer, char, bufsize);
342 * Class: java/lang/VMClassLoader
343 * Method: defaultAssertionStatus
346 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
348 return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
353 * Class: java/lang/VMClassLoader
354 * Method: findLoadedClass
355 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
357 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
363 cl = (classloader *) loader;
365 /* replace `.' by `/', this is required by the classcache */
367 u = javastring_toutf((java_handle_t *) name, true);
369 /* lookup for defining classloader */
371 c = classcache_lookup_defined(cl, u);
373 /* if not found, lookup for initiating classloader */
376 c = classcache_lookup(cl, u);
378 return LLNI_classinfo_wrap(c);
383 * These are local overrides for various environment variables in Emacs.
384 * Please do not remove this and leave it at the end of the file, where
385 * Emacs will automagically detect them.
386 * ---------------------------------------------------------------------
389 * indent-tabs-mode: t
393 * vim:noexpandtab:sw=4:ts=4: