* src/native/vm/java_lang_ClassLoader.c [ENABLE_STATISTICS]
[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    $Id: java_lang_VMClass.c 6131 2006-12-06 22:15:57Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <string.h>
34
35 #include "vm/types.h"
36
37 #include "mm/memory.h"
38
39 #include "vm/global.h"                          /* required by native headers */
40
41 #include "native/jni.h"
42
43 #include "native/include/java_lang_Object.h"
44
45 #if defined(ENABLE_JAVASE)
46 # include "native/include/java_lang_String.h"/* required by java_lang_Class.h */
47 # include "native/include/java_lang_Class.h"
48 # include "native/include/java_lang_ClassLoader.h"
49 # include "native/include/java_security_ProtectionDomain.h"
50 #endif
51
52 #include "vm/exceptions.h"
53 #include "vm/stringlocal.h"
54
55 #include "vmcore/class.h"
56 #include "vmcore/classcache.h"
57 #include "vmcore/options.h"
58
59 #if defined(ENABLE_STATISTICS)
60 # include "vmcore/statistics.h"
61 #endif
62
63
64 /*
65  * Class:     java/lang/ClassLoader
66  * Method:    defineClass
67  * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BIILjava/security/ProtectionDomain;)Ljava/lang/Class;
68  */
69 java_lang_Class *_Jv_java_lang_ClassLoader_defineClass(java_lang_ClassLoader *cl, java_lang_String *name, java_bytearray *data, s4 offset, s4 len, java_security_ProtectionDomain *pd)
70 {
71         java_objectheader *loader;
72         utf               *utfname;
73         classinfo         *c;
74         classinfo         *r;
75         classbuffer       *cb;
76         java_lang_Class   *co;
77 #if defined(ENABLE_JVMTI)
78         jint new_class_data_len = 0;
79         unsigned char* new_class_data = NULL;
80 #endif
81
82         loader = (java_objectheader *) cl;
83
84         /* check if data was passed */
85
86         if (data == NULL) {
87                 exceptions_throw_nullpointerexception();
88                 return NULL;
89         }
90
91         /* check the indexes passed */
92
93         if ((offset < 0) || (len < 0) || ((offset + len) > data->header.size)) {
94                 exceptions_throw_arrayindexoutofboundsexception();
95                 return NULL;
96         }
97
98         if (name != NULL) {
99                 /* convert '.' to '/' in java string */
100
101                 utfname = javastring_toutf((java_objectheader *) name, true);
102                 
103                 /* check if this class has already been defined */
104
105                 c = classcache_lookup_defined_or_initiated(loader, utfname);
106
107                 if (c != NULL) {
108                         exceptions_throw_linkageerror("duplicate class definition: ", c);
109                         return NULL;
110                 }
111         } 
112         else {
113                 utfname = NULL;
114         }
115
116 #if defined(ENABLE_JVMTI)
117         /* fire Class File Load Hook JVMTI event */
118
119         if (jvmti)
120                 jvmti_ClassFileLoadHook(utfname, len, (unsigned char *) data->data, 
121                                                                 loader, (java_objectheader *) pd, 
122                                                                 &new_class_data_len, &new_class_data);
123 #endif
124
125         /* create a new classinfo struct */
126
127         c = class_create_classinfo(utfname);
128
129 #if defined(ENABLE_STATISTICS)
130         /* measure time */
131
132         if (opt_getloadingtime)
133                 loadingtime_start();
134 #endif
135
136         /* build a classbuffer with the given data */
137
138         cb = NEW(classbuffer);
139         cb->class = c;
140 #if defined(ENABLE_JVMTI)
141         /* check if the JVMTI wants to modify the class */
142         if (new_class_data == NULL) {
143 #endif
144         cb->size  = len;
145         cb->data  = (u1 *) &data->data[offset];
146 #if defined(ENABLE_JVMTI)
147         } else {
148                 cb->size  = new_class_data_len;
149                 cb->data  = (u1 *) new_class_data;
150         }
151 #endif
152         cb->pos   = cb->data;
153
154         /* preset the defining classloader */
155
156         c->classloader = loader;
157
158         /* load the class from this buffer */
159
160         r = load_class_from_classbuffer(cb);
161
162         /* free memory */
163
164         FREE(cb, classbuffer);
165
166 #if defined(ENABLE_STATISTICS)
167         /* measure time */
168
169         if (opt_getloadingtime)
170                 loadingtime_stop();
171 #endif
172
173         if (r == NULL) {
174                 /* If return value is NULL, we had a problem and the class is
175                    not loaded.  Now free the allocated memory, otherwise we
176                    could run into a DOS. */
177
178                 class_free(c);
179
180                 return NULL;
181         }
182
183         /* set ProtectionDomain */
184
185         co = (java_lang_Class *) c;
186
187         co->pd = pd;
188
189         /* Store the newly defined class in the class cache. This call also       */
190         /* checks whether a class of the same name has already been defined by    */
191         /* the same defining loader, and if so, replaces the newly created class  */
192         /* by the one defined earlier.                                            */
193         /* Important: The classinfo given to classcache_store must be             */
194         /*            fully prepared because another thread may return this       */
195         /*            pointer after the lookup at to top of this function         */
196         /*            directly after the class cache lock has been released.      */
197
198         c = classcache_store(loader, c, true);
199
200         return (java_lang_Class *) c;
201 }
202
203
204 /*
205  * These are local overrides for various environment variables in Emacs.
206  * Please do not remove this and leave it at the end of the file, where
207  * Emacs will automagically detect them.
208  * ---------------------------------------------------------------------
209  * Local variables:
210  * mode: c
211  * indent-tabs-mode: t
212  * c-basic-offset: 4
213  * tab-width: 4
214  * End:
215  * vim:noexpandtab:sw=4:ts=4:
216  */