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
34 #include "mm/memory.h"
36 #include "native/jni.h"
37 #include "native/llni.h"
38 #include "native/native.h"
39 #include "native/include/java_lang_Class.h"
40 #include "native/include/java_lang_String.h"
41 #include "native/include/java_security_ProtectionDomain.h" /* required by... */
42 #include "native/include/java_lang_ClassLoader.h"
43 #include "native/include/java_util_Vector.h"
45 #include "native/include/java_lang_VMClassLoader.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/primitive.h"
55 #include "vm/stringlocal.h"
58 #include "vm/jit/asmpart.h"
60 #include "vmcore/class.h"
61 #include "vmcore/classcache.h"
62 #include "vmcore/linker.h"
63 #include "vmcore/loader.h"
64 #include "vmcore/options.h"
65 #include "vmcore/statistics.h"
66 #include "vmcore/suck.h"
67 #include "vmcore/zip.h"
69 #if defined(ENABLE_JVMTI)
70 #include "native/jvmti/cacaodbg.h"
74 /* native methods implemented by this file ************************************/
76 static JNINativeMethod methods[] = {
77 { "defineClass", "(Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_defineClass },
78 { "getPrimitiveClass", "(C)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_getPrimitiveClass },
79 { "resolveClass", "(Ljava/lang/Class;)V", (void *) (ptrint) &Java_java_lang_VMClassLoader_resolveClass },
80 { "loadClass", "(Ljava/lang/String;Z)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_loadClass },
81 { "nativeGetResources", "(Ljava/lang/String;)Ljava/util/Vector;", (void *) (ptrint) &Java_java_lang_VMClassLoader_nativeGetResources },
82 { "defaultAssertionStatus", "()Z", (void *) (ptrint) &Java_java_lang_VMClassLoader_defaultAssertionStatus },
83 { "findLoadedClass", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_findLoadedClass },
87 /* _Jv_java_lang_VMClassLoader_init ********************************************
89 Register native functions.
91 *******************************************************************************/
93 void _Jv_java_lang_VMClassLoader_init(void)
97 u = utf_new_char("java/lang/VMClassLoader");
99 native_method_register(u, methods, NATIVE_METHODS_COUNT);
104 * Class: java/lang/VMClassLoader
105 * Method: defineClass
106 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
108 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)
110 return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
115 * Class: java/lang/VMClassLoader
116 * Method: getPrimitiveClass
117 * Signature: (C)Ljava/lang/Class;
119 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
123 c = primitive_class_get_by_char(type);
126 exceptions_throw_classnotfoundexception(utf_null);
130 return LLNI_classinfo_wrap(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 = LLNI_classinfo_unwrap(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, s4 resolve)
170 exceptions_throw_nullpointerexception();
174 /* create utf string in which '.' is replaced by '/' */
176 u = javastring_toutf((java_handle_t *) name, true);
180 c = load_class_bootstrap(u);
185 /* resolve class -- if requested */
191 return LLNI_classinfo_wrap(c);
196 * Class: java/lang/VMClassLoader
197 * Method: nativeGetResources
198 * Signature: (Ljava/lang/String;)Ljava/util/Vector;
200 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
202 java_handle_t *o; /* vector being created */
203 methodinfo *m; /* "add" method of vector */
204 java_handle_t *path; /* path to be added */
205 list_classpath_entry *lce; /* classpath entry */
206 utf *utfname; /* utf to look for */
207 char *buffer; /* char buffer */
208 char *namestart; /* start of name to use */
209 char *tmppath; /* temporary buffer */
210 s4 namelen; /* length of name to use */
211 s4 searchlen; /* length of name to search */
212 s4 bufsize; /* size of buffer allocated */
213 s4 pathlen; /* name of path to assemble */
214 struct stat buf; /* buffer for stat */
215 jboolean ret; /* return value of "add" */
217 /* get the resource name as utf string */
219 utfname = javastring_toutf((java_handle_t *) name, false);
224 /* copy it to a char buffer */
226 namelen = utf_bytes(utfname);
228 bufsize = namelen + strlen("0");
229 buffer = MNEW(char, bufsize);
231 utf_copy(buffer, utfname);
234 /* skip leading '/' */
236 if (namestart[0] == '/') {
242 /* remove trailing `.class' */
244 if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
248 /* create a new needle to look for, if necessary */
250 if (searchlen != bufsize-1) {
251 utfname = utf_new(namestart, searchlen);
258 o = native_new_and_init(class_java_util_Vector);
263 /* get Vector.add() method */
265 m = class_resolveclassmethod(class_java_util_Vector,
267 utf_new_char("(Ljava/lang/Object;)Z"),
274 /* iterate over all classpath entries */
276 for (lce = list_first(list_classpath_entries); lce != NULL;
277 lce = list_next(list_classpath_entries, lce)) {
278 /* clear path pointer */
281 #if defined(ENABLE_ZLIB)
282 if (lce->type == CLASSPATH_ARCHIVE) {
284 if (zip_find(lce, utfname)) {
285 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
286 namelen + strlen("0");
288 tmppath = MNEW(char, pathlen);
290 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
291 path = javastring_new_from_utf_string(tmppath),
293 MFREE(tmppath, char, pathlen);
297 #endif /* defined(ENABLE_ZLIB) */
298 pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
300 tmppath = MNEW(char, pathlen);
302 sprintf(tmppath, "file://%s%s", lce->path, namestart);
304 /* Does this file exist? */
306 if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
307 if (!S_ISDIR(buf.st_mode))
308 path = javastring_new_from_utf_string(tmppath);
310 MFREE(tmppath, char, pathlen);
311 #if defined(ENABLE_ZLIB)
315 /* if a resource was found, add it to the vector */
318 ret = vm_call_method_int(m, o, path);
320 if (exceptions_get_exception() != NULL)
328 MFREE(buffer, char, bufsize);
330 return (java_util_Vector *) o;
333 MFREE(buffer, char, bufsize);
340 * Class: java/lang/VMClassLoader
341 * Method: defaultAssertionStatus
344 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
346 return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
351 * Class: java/lang/VMClassLoader
352 * Method: findLoadedClass
353 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
355 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
361 cl = (classloader *) loader;
363 /* replace `.' by `/', this is required by the classcache */
365 u = javastring_toutf((java_handle_t *) name, true);
367 /* lookup for defining classloader */
369 c = classcache_lookup_defined(cl, u);
371 /* if not found, lookup for initiating classloader */
374 c = classcache_lookup(cl, u);
376 return LLNI_classinfo_wrap(c);
381 * These are local overrides for various environment variables in Emacs.
382 * Please do not remove this and leave it at the end of the file, where
383 * Emacs will automagically detect them.
384 * ---------------------------------------------------------------------
387 * indent-tabs-mode: t
391 * vim:noexpandtab:sw=4:ts=4: