f7273977fb4918783488ab62435f3d1c4052f233
[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 8341 2007-08-17 21:32:01Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <sys/stat.h>
33
34 #include "vm/types.h"
35
36 #include "mm/memory.h"
37
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"
46
47 #include "native/include/java_lang_VMClassLoader.h"
48
49 #include "native/vm/java_lang_ClassLoader.h"
50
51 #include "toolbox/logging.h"
52
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"
58 #include "vm/vm.h"
59
60 #include "vm/jit/asmpart.h"
61
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"
70
71 #if defined(ENABLE_JVMTI)
72 #include "native/jvmti/cacaodbg.h"
73 #endif
74
75
76 /* native methods implemented by this file ************************************/
77
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        },
86 };
87
88
89 /* _Jv_java_lang_VMClassLoader_init ********************************************
90
91    Register native functions.
92
93 *******************************************************************************/
94
95 void _Jv_java_lang_VMClassLoader_init(void)
96 {
97         utf *u;
98
99         u = utf_new_char("java/lang/VMClassLoader");
100
101         native_method_register(u, methods, NATIVE_METHODS_COUNT);
102 }
103
104
105 /*
106  * Class:     java/lang/VMClassLoader
107  * Method:    defineClass
108  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
109  */
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)
111 {
112         return _Jv_java_lang_ClassLoader_defineClass(cl, name, data, offset, len, pd);
113 }
114
115
116 /*
117  * Class:     java/lang/VMClassLoader
118  * Method:    getPrimitiveClass
119  * Signature: (C)Ljava/lang/Class;
120  */
121 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_getPrimitiveClass(JNIEnv *env, jclass clazz, s4 type)
122 {
123         classinfo *c;
124
125         c = primitive_class_get_by_char(type);
126
127         if (c == NULL) {
128                 exceptions_throw_classnotfoundexception(utf_null);
129                 return NULL;
130         }
131
132         return LLNI_classinfo_wrap(c);
133 }
134
135
136 /*
137  * Class:     java/lang/VMClassLoader
138  * Method:    resolveClass
139  * Signature: (Ljava/lang/Class;)V
140  */
141 JNIEXPORT void JNICALL Java_java_lang_VMClassLoader_resolveClass(JNIEnv *env, jclass clazz, java_lang_Class *c)
142 {
143         classinfo *ci;
144
145         ci = LLNI_classinfo_unwrap(c);
146
147         if (!ci) {
148                 exceptions_throw_nullpointerexception();
149                 return;
150         }
151
152         /* link the class */
153
154         if (!(ci->state & CLASS_LINKED))
155                 (void) link_class(ci);
156
157         return;
158 }
159
160
161 /*
162  * Class:     java/lang/VMClassLoader
163  * Method:    loadClass
164  * Signature: (Ljava/lang/String;Z)Ljava/lang/Class;
165  */
166 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_loadClass(JNIEnv *env, jclass clazz, java_lang_String *name, s4 resolve)
167 {
168         classinfo *c;
169         utf       *u;
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_handle_t *) name, true);
179
180         /* load class */
181
182         c = load_class_bootstrap(u);
183
184         if (c == NULL)
185                 return NULL;
186
187         /* resolve class -- if requested */
188
189 /*      if (resolve) */
190                 if (!link_class(c))
191                         return NULL;
192
193         return LLNI_classinfo_wrap(c);
194 }
195
196
197 /*
198  * Class:     java/lang/VMClassLoader
199  * Method:    nativeGetResources
200  * Signature: (Ljava/lang/String;)Ljava/util/Vector;
201  */
202 JNIEXPORT java_util_Vector* JNICALL Java_java_lang_VMClassLoader_nativeGetResources(JNIEnv *env, jclass clazz, java_lang_String *name)
203 {
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"    */
218
219         /* get the resource name as utf string */
220
221         utfname = javastring_toutf((java_handle_t *) name, false);
222
223         if (utfname == NULL)
224                 return NULL;
225
226         /* copy it to a char buffer */
227
228         namelen   = utf_bytes(utfname);
229         searchlen = namelen;
230         bufsize   = namelen + strlen("0");
231         buffer    = MNEW(char, bufsize);
232
233         utf_copy(buffer, utfname);
234         namestart = buffer;
235
236         /* skip leading '/' */
237
238         if (namestart[0] == '/') {
239                 namestart++;
240                 namelen--;
241                 searchlen--;
242         }
243
244         /* remove trailing `.class' */
245
246         if (namelen >= 6 && strcmp(namestart + (namelen - 6), ".class") == 0) {
247                 searchlen -= 6;
248         }
249
250         /* create a new needle to look for, if necessary */
251
252         if (searchlen != bufsize-1) {
253                 utfname = utf_new(namestart, searchlen);
254                 if (utfname == NULL)
255                         goto return_NULL;
256         }
257                         
258         /* new Vector() */
259
260         o = native_new_and_init(class_java_util_Vector);
261
262         if (o == NULL)
263                 goto return_NULL;
264
265         /* get Vector.add() method */
266
267         m = class_resolveclassmethod(class_java_util_Vector,
268                                                                  utf_add,
269                                                                  utf_new_char("(Ljava/lang/Object;)Z"),
270                                                                  NULL,
271                                                                  true);
272
273         if (m == NULL)
274                 goto return_NULL;
275
276         /* iterate over all classpath entries */
277
278         for (lce = list_first(list_classpath_entries); lce != NULL;
279                  lce = list_next(list_classpath_entries, lce)) {
280                 /* clear path pointer */
281                 path = NULL;
282
283 #if defined(ENABLE_ZLIB)
284                 if (lce->type == CLASSPATH_ARCHIVE) {
285
286                         if (zip_find(lce, utfname)) {
287                                 pathlen = strlen("jar:file://") + lce->pathlen + strlen("!/") +
288                                         namelen + strlen("0");
289
290                                 tmppath = MNEW(char, pathlen);
291
292                                 sprintf(tmppath, "jar:file://%s!/%s", lce->path, namestart);
293                                 path = javastring_new_from_utf_string(tmppath),
294
295                                 MFREE(tmppath, char, pathlen);
296                         }
297
298                 } else {
299 #endif /* defined(ENABLE_ZLIB) */
300                         pathlen = strlen("file://") + lce->pathlen + namelen + strlen("0");
301
302                         tmppath = MNEW(char, pathlen);
303
304                         sprintf(tmppath, "file://%s%s", lce->path, namestart);
305
306                         /* Does this file exist? */
307
308                         if (stat(tmppath + strlen("file://") - 1, &buf) == 0)
309                                 if (!S_ISDIR(buf.st_mode))
310                                         path = javastring_new_from_utf_string(tmppath);
311
312                         MFREE(tmppath, char, pathlen);
313 #if defined(ENABLE_ZLIB)
314                 }
315 #endif
316
317                 /* if a resource was found, add it to the vector */
318
319                 if (path != NULL) {
320                         ret = vm_call_method_int(m, o, path);
321
322                         if (exceptions_get_exception() != NULL)
323                                 goto return_NULL;
324
325                         if (ret == 0) 
326                                 goto return_NULL;
327                 }
328         }
329
330         MFREE(buffer, char, bufsize);
331
332         return (java_util_Vector *) o;
333
334 return_NULL:
335         MFREE(buffer, char, bufsize);
336
337         return NULL;
338 }
339
340
341 /*
342  * Class:     java/lang/VMClassLoader
343  * Method:    defaultAssertionStatus
344  * Signature: ()Z
345  */
346 JNIEXPORT s4 JNICALL Java_java_lang_VMClassLoader_defaultAssertionStatus(JNIEnv *env, jclass clazz)
347 {
348         return _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus;
349 }
350
351
352 /*
353  * Class:     java/lang/VMClassLoader
354  * Method:    findLoadedClass
355  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;
356  */
357 JNIEXPORT java_lang_Class* JNICALL Java_java_lang_VMClassLoader_findLoadedClass(JNIEnv *env, jclass clazz, java_lang_ClassLoader *loader, java_lang_String *name)
358 {
359         classloader *cl;
360         classinfo   *c;
361         utf         *u;
362
363         cl = (classloader *) loader;
364
365         /* replace `.' by `/', this is required by the classcache */
366
367         u = javastring_toutf((java_handle_t *) name, true);
368
369         /* lookup for defining classloader */
370
371         c = classcache_lookup_defined(cl, u);
372
373         /* if not found, lookup for initiating classloader */
374
375         if (c == NULL)
376                 c = classcache_lookup(cl, u);
377
378         return LLNI_classinfo_wrap(c);
379 }
380
381
382 /*
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  * ---------------------------------------------------------------------
387  * Local variables:
388  * mode: c
389  * indent-tabs-mode: t
390  * c-basic-offset: 4
391  * tab-width: 4
392  * End:
393  * vim:noexpandtab:sw=4:ts=4:
394  */