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