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