eac72274226b94d55353d1c4badaa225cdcbba89
[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    $Id: java_lang_VMClassLoader.c 7723 2007-04-16 18:03:08Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <sys/stat.h>
34
35 #include "vm/types.h"
36
37 #include "mm/memory.h"
38
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"
46
47 #include "native/vm/java_lang_ClassLoader.h"
48
49 #include "toolbox/logging.h"
50
51 #include "vm/builtin.h"
52 #include "vm/exceptions.h"
53 #include "vm/initialize.h"
54 #include "vm/stringlocal.h"
55 #include "vm/vm.h"
56
57 #include "vm/jit/asmpart.h"
58
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"
67
68 #if defined(ENABLE_JVMTI)
69 #include "native/jvmti/cacaodbg.h"
70 #endif
71
72
73 /*
74  * Class:     java/lang/VMClassLoader
75  * Method:    defineClass
76  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
77  */
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)
79 {
80         return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
81 }
82
83
84 /*
85  * Class:     java/lang/VMClassLoader
86  * Method:    getPrimitiveClass
87  * Signature: (C)Ljava/lang/Class;
88  */
89 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
90 {
91         classinfo *c;
92         s4         index;
93
94         /* get primitive class */
95
96         switch (type) {
97         case 'I':
98                 index = PRIMITIVETYPE_INT;
99                 break;
100         case 'J':
101                 index = PRIMITIVETYPE_LONG;
102                 break;
103         case 'F':
104                 index = PRIMITIVETYPE_FLOAT;
105                 break;
106         case 'D':
107                 index = PRIMITIVETYPE_DOUBLE;
108                 break;
109         case 'B':
110                 index = PRIMITIVETYPE_BYTE;
111                 break;
112         case 'C':
113                 index = PRIMITIVETYPE_CHAR;
114                 break;
115         case 'S':
116                 index = PRIMITIVETYPE_SHORT;
117                 break;
118         case 'Z':
119                 index = PRIMITIVETYPE_BOOLEAN;
120                 break;
121         case 'V':
122                 index = PRIMITIVETYPE_VOID;
123                 break;
124         default:
125                 exceptions_throw_noclassdeffounderror(utf_null);
126                 c = NULL;
127         }
128
129         c = primitivetype_table[index].class_primitive;
130
131         return (java_lang_Class *) 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 = (classinfo *) 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, jboolean resolve)
166 {
167         classinfo         *c;
168         utf               *u;
169         java_objectheader *xptr;
170
171         if (name == NULL) {
172                 exceptions_throw_nullpointerexception();
173                 return NULL;
174         }
175
176         /* create utf string in which '.' is replaced by '/' */
177
178         u = javastring_toutf((java_objectheader *) name, true);
179
180         /* load class */
181
182         c = load_class_bootstrap(u);
183
184         if (c == NULL)
185                 goto exception;
186
187         /* resolve class -- if requested */
188
189 /*      if (resolve) */
190                 if (!link_class(c))
191                         goto exception;
192
193         return (java_lang_Class *) c;
194
195  exception:
196         xptr = exceptions_get_exception();
197
198         c = xptr->vftbl->class;
199         
200         /* if the exception is a NoClassDefFoundError, we replace it with a
201            ClassNotFoundException, otherwise return the exception */
202
203         if (c == class_java_lang_NoClassDefFoundError) {
204                 /* clear exceptionptr, because builtin_new checks for 
205                    ExceptionInInitializerError */
206                 exceptions_clear_exception();
207
208                 exceptions_throw_classnotfoundexception(u);
209         }
210
211         return NULL;
212 }
213
214
215 /*
216  * Class:     java/lang/VMClassLoader
217  * Method:    nativeGetResources
218  * Signature: (Ljava/lang/String;)Ljava/util/Vector;
219  */
220 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
221 {
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"    */
236
237         /* get the resource name as utf string */
238
239         utfname = javastring_toutf((java_objectheader *) name, false);
240
241         if (utfname == NULL)
242                 return NULL;
243
244         /* copy it to a char buffer */
245
246         namelen   = utf_bytes(utfname);
247         searchlen = namelen;
248         bufsize   = namelen + strlen("0");
249         buffer    = MNEW(char, bufsize);
250
251         utf_copy(buffer, utfname);
252         namestart = buffer;
253
254         /* skip leading '/' */
255
256         if (namestart[0] == '/') {
257                 namestart++;
258                 namelen--;
259                 searchlen--;
260         }
261
262         /* remove trailing `.class' */
263
264         if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
265                 searchlen -= 6;
266         }
267
268         /* create a new needle to look for, if necessary */
269
270         if (searchlen != bufsize-1) {
271                 utfname = utf_new(namestart, searchlen);
272                 if (utfname == NULL)
273                         goto return_NULL;
274         }
275                         
276         /* new Vector() */
277
278         o = native_new_and_init(class_java_util_Vector);
279
280         if (o == NULL)
281                 goto return_NULL;
282
283         /* get Vector.add() method */
284
285         m = class_resolveclassmethod(class_java_util_Vector,
286                                                                  utf_add,
287                                                                  utf_new_char("(Ljava/lang/Object;)Z"),
288                                                                  NULL,
289                                                                  true);
290
291         if (m == NULL)
292                 goto return_NULL;
293
294         /* iterate over all classpath entries */
295
296         for (lce = list_first(list_classpath_entries); lce != NULL;
297                  lce = list_next(list_classpath_entries, lce)) {
298                 /* clear path pointer */
299                 path = NULL;
300
301 #if defined(ENABLE_ZLIB)
302                 if (lce->type == CLASSPATH_ARCHIVE) {
303
304                         if (zip_find(lce, utfname)) {
305                                 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
306                                         namelen + strlen("0");
307
308                                 tmppath = MNEW(char, pathlen);
309
310                                 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
311                                 path = javastring_new_from_utf_string(tmppath),
312
313                                 MFREE(tmppath, char, pathlen);
314                         }
315
316                 } else {
317 #endif /* defined(ENABLE_ZLIB) */
318                         pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
319
320                         tmppath = MNEW(char, pathlen);
321
322                         sprintf(tmppath, "file://%s%s", lce->path, namestart);
323
324                         /* Does this file exist? */
325
326                         if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
327                                 if (!S_ISDIR(buf.st_mode))
328                                         path = javastring_new_from_utf_string(tmppath);
329
330                         MFREE(tmppath, char, pathlen);
331 #if defined(ENABLE_ZLIB)
332                 }
333 #endif
334
335                 /* if a resource was found, add it to the vector */
336
337                 if (path != NULL) {
338                         ret = vm_call_method_int(m, o, path);
339
340                         if (exceptions_get_exception() != NULL)
341                                 goto return_NULL;
342
343                         if (ret == 0) 
344                                 goto return_NULL;
345                 }
346         }
347
348         MFREE(buffer, char, bufsize);
349
350         return (java_util_Vector *) o;
351
352 return_NULL:
353         MFREE(buffer, char, bufsize);
354
355         return NULL;
356 }
357
358
359 /*
360  * Class:     java/lang/VMClassLoader
361  * Method:    defaultAssertionStatus
362  * Signature: ()Z
363  */
364 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
365 {
366         return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
367 }
368
369
370 /*
371  * Class:     java/lang/VMClassLoader
372  * Method:    findLoadedClass
373  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
374  */
375 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
376 {
377         classloader *cl;
378         classinfo   *c;
379         utf         *u;
380
381         cl = (classloader *) loader;
382
383         /* replace `.' by `/', this is required by the classcache */
384
385         u = javastring_toutf((java_objectheader *) name, true);
386
387         /* lookup for defining classloader */
388
389         c = classcache_lookup_defined(cl, u);
390
391         /* if not found, lookup for initiating classloader */
392
393         if (c == NULL)
394                 c = classcache_lookup(cl, u);
395
396         return (java_lang_Class *) c;
397 }
398
399
400 /*
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  * ---------------------------------------------------------------------
405  * Local variables:
406  * mode: c
407  * indent-tabs-mode: t
408  * c-basic-offset: 4
409  * tab-width: 4
410  * End:
411  * vim:noexpandtab:sw=4:ts=4:
412  */