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