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