* src/vm/jit/i386/darwin/md-asm.h: Repaired --enable-cycles-stats.
[cacao.git] / src / native / vm / java_lang_ClassLoader.c
1 /* src/native/vm/java_lang_ClassLoader.c - java/lang/ClassLoader
2
3    Copyright (C) 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 <string.h>
32
33 #include "vm/types.h"
34
35 #include "mm/memory.h"
36
37 #include "vm/global.h"                          /* required by native headers */
38
39 #include "native/jni.h"
40 #include "native/llni.h"
41
42 /* keep this order of the native includes */
43
44 #include "native/include/java_lang_Object.h"
45
46 #if defined(ENABLE_JAVASE)
47 # include "native/include/java_lang_String.h"            /* required by j.l.C */
48
49 # if defined(WITH_CLASSPATH_SUN)
50 #  include "native/include/java_nio_ByteBuffer.h"       /* required by j.l.CL */
51 # endif
52
53 # include "native/include/java_lang_ClassLoader.h"       /* required by j.l.C */
54 # include "native/include/java_lang_Class.h"
55 # include "native/include/java_security_ProtectionDomain.h"
56 #endif
57
58 #include "vm/exceptions.h"
59 #include "vm/stringlocal.h"
60
61 #include "vmcore/class.h"
62 #include "vmcore/classcache.h"
63 #include "vmcore/options.h"
64
65 #if defined(ENABLE_STATISTICS)
66 # include "vmcore/statistics.h"
67 #endif
68
69
70 /*
71  * Class:     java/lang/ClassLoader
72  * Method:    defineClass
73  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
74  */
75 java_lang_Class *_Jv_java_lang_ClassLoader_defineClass(java_lang_ClassLoader *cl, java_lang_String *name, java_handle_bytearray_t *data, s4 offset, s4 len, java_security_ProtectionDomain *pd)
76 {
77         utf             *utfname;
78         classinfo       *c;
79         classloader     *loader;
80         java_lang_Class *o;
81
82 #if defined(ENABLE_JVMTI)
83         jint new_class_data_len = 0;
84         unsigned char* new_class_data = NULL;
85 #endif
86
87         /* check if data was passed */
88
89         if (data == NULL) {
90                 exceptions_throw_nullpointerexception();
91                 return NULL;
92         }
93
94         /* check the indexes passed */
95
96         if ((offset < 0) || (len < 0) || ((offset + len) > LLNI_array_size(data))) {
97                 exceptions_throw_arrayindexoutofboundsexception();
98                 return NULL;
99         }
100
101         /* add classloader to classloader hashtable */
102
103         loader = loader_hashtable_classloader_add((java_handle_t *) cl);
104
105         if (name != NULL) {
106                 /* convert '.' to '/' in java string */
107
108                 utfname = javastring_toutf((java_handle_t *) name, true);
109         } 
110         else {
111                 utfname = NULL;
112         }
113
114 #if defined(ENABLE_JVMTI)
115         /* XXX again this will not work because of the indirection cell for classloaders */
116         assert(0);
117         /* fire Class File Load Hook JVMTI event */
118
119         if (jvmti)
120                 jvmti_ClassFileLoadHook(utfname, len, (unsigned char *) data->data, 
121                                                                 loader, (java_handle_t *) pd, 
122                                                                 &new_class_data_len, &new_class_data);
123 #endif
124
125         /* define the class */
126
127 #if defined(ENABLE_JVMTI)
128         /* check if the JVMTI wants to modify the class */
129
130         if (new_class_data == NULL)
131                 c = class_define(utfname, loader, new_class_data_len, new_class_data, pd); 
132         else
133 #endif
134                 c = class_define(utfname, loader, len, (const uint8_t *) &LLNI_array_direct(data, offset), pd);
135
136         if (c == NULL)
137                 return NULL;
138
139         /* for convenience */
140
141         o = LLNI_classinfo_wrap(c);
142
143 #if defined(WITH_CLASSPATH_GNU)
144         /* set ProtectionDomain */
145
146         LLNI_field_set_ref(o, pd, pd);
147 #endif
148
149         return o;
150 }
151
152
153 /*
154  * These are local overrides for various environment variables in Emacs.
155  * Please do not remove this and leave it at the end of the file, where
156  * Emacs will automagically detect them.
157  * ---------------------------------------------------------------------
158  * Local variables:
159  * mode: c
160  * indent-tabs-mode: t
161  * c-basic-offset: 4
162  * tab-width: 4
163  * End:
164  * vim:noexpandtab:sw=4:ts=4:
165  */