* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / native / vm / gnu / java_lang_VMClassLoader.c
1 /* src/native/vm/gnu/VMClassLoader.c
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <sys/stat.h>
32
33 #include "vm/types.h"
34
35 #include "mm/memory.h"
36
37 #include "native/jni.h"
38 #include "native/llni.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"
45
46 #include "native/include/java_lang_VMClassLoader.h"
47
48 #include "native/vm/java_lang_ClassLoader.h"
49
50 #include "toolbox/logging.h"
51
52 #include "vm/builtin.h"
53 #include "vm/exceptions.h"
54 #include "vm/initialize.h"
55 #include "vm/primitive.h"
56 #include "vm/stringlocal.h"
57 #include "vm/vm.h"
58
59 #include "vm/jit/asmpart.h"
60
61 #include "vmcore/class.h"
62 #include "vmcore/classcache.h"
63 #include "vmcore/linker.h"
64 #include "vmcore/loader.h"
65 #include "vmcore/options.h"
66 #include "vmcore/statistics.h"
67 #include "vmcore/suck.h"
68 #include "vmcore/zip.h"
69
70 #if defined(ENABLE_JVMTI)
71 #include "native/jvmti/cacaodbg.h"
72 #endif
73
74
75 /* native methods implemented by this file ************************************/
76
77 static JNINativeMethod methods[] = {
78         { "defineClass",            "(Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;", (void *) (ptrint) &Java_java_lang_VMClassLoader_defineClass            },
79         { "getPrimitiveClass",      "(C)Ljava/lang/Class;",                                                                             (void *) (ptrint) &Java_java_lang_VMClassLoader_getPrimitiveClass      },
80         { "resolveClass",           "(Ljava/lang/Class;)V",                                                                             (void *) (ptrint) &Java_java_lang_VMClassLoader_resolveClass           },
81         { "loadClass",              "(Ljava/lang/String;Z)Ljava/lang/Class;",                                                           (void *) (ptrint) &Java_java_lang_VMClassLoader_loadClass              },
82         { "nativeGetResources",     "(Ljava/lang/String;)Ljava/util/Vector;",                                                           (void *) (ptrint) &Java_java_lang_VMClassLoader_nativeGetResources     },
83         { "defaultAssertionStatus", "()Z",                                                                                              (void *) (ptrint) &Java_java_lang_VMClassLoader_defaultAssertionStatus },
84         { "findLoadedClass",        "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;",                                     (void *) (ptrint) &Java_java_lang_VMClassLoader_findLoadedClass        },
85 };
86
87
88 /* _Jv_java_lang_VMClassLoader_init ********************************************
89
90    Register native functions.
91
92 *******************************************************************************/
93
94 void _Jv_java_lang_VMClassLoader_init(void)
95 {
96         utf *u;
97
98         u = utf_new_char("java/lang/VMClassLoader");
99
100         native_method_register(u, methods, NATIVE_METHODS_COUNT);
101 }
102
103
104 /*
105  * Class:     java/lang/VMClassLoader
106  * Method:    defineClass
107  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
108  */
109 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 {
111         return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
112 }
113
114
115 /*
116  * Class:     java/lang/VMClassLoader
117  * Method:    getPrimitiveClass
118  * Signature: (C)Ljava/lang/Class;
119  */
120 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
121 {
122         classinfo *c;
123
124         c = primitive_class_get_by_char(type);
125
126         if (c == NULL) {
127                 exceptions_throw_classnotfoundexception(utf_null);
128                 return NULL;
129         }
130
131         return LLNI_classinfo_wrap(c);
132 }
133
134
135 /*
136  * Class:     java/lang/VMClassLoader
137  * Method:    resolveClass
138  * Signature: (Ljava/lang/Class;)V
139  */
140 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
141 {
142         classinfo *ci;
143
144         ci = LLNI_classinfo_unwrap(c);
145
146         if (!ci) {
147                 exceptions_throw_nullpointerexception();
148                 return;
149         }
150
151         /* link the class */
152
153         if (!(ci->state & CLASS_LINKED))
154                 (void) link_class(ci);
155
156         return;
157 }
158
159
160 /*
161  * Class:     java/lang/VMClassLoader
162  * Method:    loadClass
163  * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
164  */
165 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, s4 resolve)
166 {
167         classinfo *c;
168         utf       *u;
169
170         if (name == NULL) {
171                 exceptions_throw_nullpointerexception();
172                 return NULL;
173         }
174
175         /* create utf string in which '.' is replaced by '/' */
176
177         u = javastring_toutf((java_handle_t *) name, true);
178
179         /* load class */
180
181         c = load_class_bootstrap(u);
182
183         if (c == NULL)
184                 return NULL;
185
186         /* resolve class -- if requested */
187
188 /*      if (resolve) */
189                 if (!link_class(c))
190                         return NULL;
191
192         return LLNI_classinfo_wrap(c);
193 }
194
195
196 /*
197  * Class:     java/lang/VMClassLoader
198  * Method:    nativeGetResources
199  * Signature: (Ljava/lang/String;)Ljava/util/Vector;
200  */
201 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
202 {
203         java_handle_t        *o;         /* vector being created     */
204         methodinfo           *m;         /* "add" method of vector   */
205         java_handle_t        *path;      /* path to be added         */
206         list_classpath_entry *lce;       /* classpath entry          */
207         utf                  *utfname;   /* utf to look for          */
208         char                 *buffer;    /* char buffer              */
209         char                 *namestart; /* start of name to use     */
210         char                 *tmppath;   /* temporary buffer         */
211         s4                    namelen;   /* length of name to use    */
212         s4                    searchlen; /* length of name to search */
213         s4                    bufsize;   /* size of buffer allocated */
214         s4                    pathlen;   /* name of path to assemble */
215         struct stat           buf;       /* buffer for stat          */
216         jboolean              ret;       /* return value of "add"    */
217
218         /* get the resource name as utf string */
219
220         utfname = javastring_toutf((java_handle_t *) name, false);
221
222         if (utfname == NULL)
223                 return NULL;
224
225         /* copy it to a char buffer */
226
227         namelen   = utf_bytes(utfname);
228         searchlen = namelen;
229         bufsize   = namelen + strlen("0");
230         buffer    = MNEW(char, bufsize);
231
232         utf_copy(buffer, utfname);
233         namestart = buffer;
234
235         /* skip leading '/' */
236
237         if (namestart[0] == '/') {
238                 namestart++;
239                 namelen--;
240                 searchlen--;
241         }
242
243         /* remove trailing `.class' */
244
245         if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
246                 searchlen -= 6;
247         }
248
249         /* create a new needle to look for, if necessary */
250
251         if (searchlen != bufsize-1) {
252                 utfname = utf_new(namestart, searchlen);
253                 if (utfname == NULL)
254                         goto return_NULL;
255         }
256                         
257         /* new Vector() */
258
259         o = native_new_and_init(class_java_util_Vector);
260
261         if (o == NULL)
262                 goto return_NULL;
263
264         /* get Vector.add() method */
265
266         m = class_resolveclassmethod(class_java_util_Vector,
267                                                                  utf_add,
268                                                                  utf_new_char("(Ljava/lang/Object;)Z"),
269                                                                  NULL,
270                                                                  true);
271
272         if (m == NULL)
273                 goto return_NULL;
274
275         /* iterate over all classpath entries */
276
277         for (lce = list_first(list_classpath_entries); lce != NULL;
278                  lce = list_next(list_classpath_entries, lce)) {
279                 /* clear path pointer */
280                 path = NULL;
281
282 #if defined(ENABLE_ZLIB)
283                 if (lce->type == CLASSPATH_ARCHIVE) {
284
285                         if (zip_find(lce, utfname)) {
286                                 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
287                                         namelen + strlen("0");
288
289                                 tmppath = MNEW(char, pathlen);
290
291                                 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
292                                 path = javastring_new_from_utf_string(tmppath),
293
294                                 MFREE(tmppath, char, pathlen);
295                         }
296
297                 } else {
298 #endif /* defined(ENABLE_ZLIB) */
299                         pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
300
301                         tmppath = MNEW(char, pathlen);
302
303                         sprintf(tmppath, "file://%s%s", lce->path, namestart);
304
305                         /* Does this file exist? */
306
307                         if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
308                                 if (!S_ISDIR(buf.st_mode))
309                                         path = javastring_new_from_utf_string(tmppath);
310
311                         MFREE(tmppath, char, pathlen);
312 #if defined(ENABLE_ZLIB)
313                 }
314 #endif
315
316                 /* if a resource was found, add it to the vector */
317
318                 if (path != NULL) {
319                         ret = vm_call_method_int(m, o, path);
320
321                         if (exceptions_get_exception() != NULL)
322                                 goto return_NULL;
323
324                         if (ret == 0) 
325                                 goto return_NULL;
326                 }
327         }
328
329         MFREE(buffer, char, bufsize);
330
331         return (java_util_Vector *) o;
332
333 return_NULL:
334         MFREE(buffer, char, bufsize);
335
336         return NULL;
337 }
338
339
340 /*
341  * Class:     java/lang/VMClassLoader
342  * Method:    defaultAssertionStatus
343  * Signature: ()Z
344  */
345 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
346 {
347         return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
348 }
349
350
351 /*
352  * Class:     java/lang/VMClassLoader
353  * Method:    findLoadedClass
354  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
355  */
356 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
357 {
358         classloader *cl;
359         classinfo   *c;
360         utf         *u;
361
362         cl = loader_hashtable_classloader_find((java_handle_t *) loader);
363
364         if (cl == NULL)
365                 vm_abort("unable to find classloader");
366
367         /* replace `.' by `/', this is required by the classcache */
368
369         u = javastring_toutf((java_handle_t *) name, true);
370
371         /* lookup for defining classloader */
372
373         c = classcache_lookup_defined(cl, u);
374
375         /* if not found, lookup for initiating classloader */
376
377         if (c == NULL)
378                 c = classcache_lookup(cl, u);
379
380         return LLNI_classinfo_wrap(c);
381 }
382
383
384 /*
385  * These are local overrides for various environment variables in Emacs.
386  * Please do not remove this and leave it at the end of the file, where
387  * Emacs will automagically detect them.
388  * ---------------------------------------------------------------------
389  * Local variables:
390  * mode: c
391  * indent-tabs-mode: t
392  * c-basic-offset: 4
393  * tab-width: 4
394  * End:
395  * vim:noexpandtab:sw=4:ts=4:
396  */