Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / lang / natClassLoader.cc
blob17b2a8833869071c45a52dbc6ffefd50ff9eab95
1 // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
13 #include <config.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
19 #include <gcj/cni.h>
20 #include <jvm.h>
21 #include <execution.h>
23 #include <java-threads.h>
24 #include <java-interp.h>
26 #include <java/lang/Character.h>
27 #include <java/lang/Thread.h>
28 #include <java/lang/ClassLoader.h>
29 #include <gnu/gcj/runtime/VMClassLoader.h>
30 #include <java/lang/InternalError.h>
31 #include <java/lang/IllegalAccessError.h>
32 #include <java/lang/LinkageError.h>
33 #include <java/lang/NoClassDefFoundError.h>
34 #include <java/lang/ClassNotFoundException.h>
35 #include <java/lang/ClassCircularityError.h>
36 #include <java/lang/IncompatibleClassChangeError.h>
37 #include <java/lang/ClassFormatError.h>
38 #include <java/lang/VirtualMachineError.h>
39 #include <java/lang/VMClassLoader.h>
40 #include <java/lang/reflect/Modifier.h>
41 #include <java/lang/Runtime.h>
42 #include <java/lang/StringBuffer.h>
43 #include <java/io/Serializable.h>
44 #include <java/lang/Cloneable.h>
45 #include <java/util/HashMap.h>
47 // Size of local hash table.
48 #define HASH_LEN 1013
50 // Hash function for Utf8Consts.
51 #define HASH_UTF(Utf) ((Utf)->hash16() % HASH_LEN)
53 static jclass loaded_classes[HASH_LEN];
55 // This is the root of a linked list of classes
56 static jclass stack_head;
58 // While bootstrapping we keep a list of classes we found, so that we
59 // can register their packages. There aren't many of these so we
60 // just keep a small buffer here and abort if we overflow.
61 #define BOOTSTRAP_CLASS_LIST_SIZE 20
62 static jclass bootstrap_class_list[BOOTSTRAP_CLASS_LIST_SIZE];
63 static int bootstrap_index;
68 // This tries to find a class in our built-in cache. This cache is
69 // used only for classes which are linked in to the executable or
70 // loaded via dlopen().
71 jclass
72 _Jv_FindClassInCache (_Jv_Utf8Const *name)
74 JvSynchronize sync (&java::lang::Class::class$);
75 jint hash = HASH_UTF (name);
77 jclass klass;
78 for (klass = loaded_classes[hash]; klass; klass = klass->next_or_version)
80 if (_Jv_equalUtf8Consts (name, klass->name))
81 break;
84 return klass;
87 void
88 _Jv_UnregisterClass (jclass the_class)
90 JvSynchronize sync (&java::lang::Class::class$);
91 jint hash = HASH_UTF(the_class->name);
93 jclass *klass = &(loaded_classes[hash]);
94 for ( ; *klass; klass = &((*klass)->next_or_version))
96 if (*klass == the_class)
98 *klass = (*klass)->next_or_version;
99 break;
104 // Register an initiating class loader for a given class.
105 void
106 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
108 if (! loader)
109 loader = java::lang::ClassLoader::getSystemClassLoader();
110 loader->loadedClasses->put(klass->name->toString(), klass);
113 // If we found an error while defining an interpreted class, we must
114 // go back and unregister it.
115 void
116 _Jv_UnregisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
118 if (! loader)
119 loader = java::lang::ClassLoader::getSystemClassLoader();
120 loader->loadedClasses->remove(klass->name->toString());
123 // This function is called many times during startup, before main() is
124 // run. At that point in time we know for certain we are running
125 // single-threaded, so we don't need to lock when adding classes to the
126 // class chain. At all other times, the caller should synchronize on
127 // Class::class$.
128 void
129 _Jv_RegisterClasses (const jclass *classes)
131 for (; *classes; ++classes)
133 jclass klass = *classes;
135 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
136 (*_Jv_RegisterClassHook) (klass);
140 // This is a version of _Jv_RegisterClasses that takes a count.
141 void
142 _Jv_RegisterClasses_Counted (const jclass * classes, size_t count)
144 size_t i;
145 for (i = 0; i < count; i++)
147 jclass klass = classes[i];
149 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
150 (*_Jv_RegisterClassHook) (klass);
154 void
155 _Jv_RegisterClassHookDefault (jclass klass)
157 jint hash = HASH_UTF (klass->name);
159 // If the class is already registered, don't re-register it.
160 for (jclass check_class = loaded_classes[hash];
161 check_class != NULL;
162 check_class = check_class->next_or_version)
164 if (check_class == klass)
166 // If you get this, it means you have the same class in two
167 // different libraries.
168 #define TEXT "Duplicate class registration: "
169 // We size-limit MESSAGE so that you can't trash the stack.
170 char message[200];
171 strcpy (message, TEXT);
172 strncpy (message + sizeof (TEXT) - 1, klass->name->chars(),
173 sizeof (message) - sizeof (TEXT));
174 message[sizeof (message) - 1] = '\0';
175 if (! gcj::runtimeInitialized)
176 JvFail (message);
177 else
179 java::lang::String *str = JvNewStringLatin1 (message);
180 throw new java::lang::VirtualMachineError (str);
185 // FIXME: this is really bogus!
186 if (! klass->engine)
187 klass->engine = &_Jv_soleCompiledEngine;
188 klass->next_or_version = loaded_classes[hash];
189 loaded_classes[hash] = klass;
192 // A pointer to a function that actually registers a class.
193 // Normally _Jv_RegisterClassHookDefault, but could be some other function
194 // that registers the class in e.g. a ClassLoader-local table.
195 // Should synchronize on Class:class$ while setting/restore this variable.
197 void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
199 void
200 _Jv_RegisterClass (jclass klass)
202 jclass classes[2];
203 classes[0] = klass;
204 classes[1] = NULL;
205 _Jv_RegisterClasses (classes);
208 jclass
209 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
211 // See if the class was already loaded by this loader. This handles
212 // initiating loader checks, as we register classes with their
213 // initiating loaders.
214 java::lang::ClassLoader *sys
215 = java::lang::ClassLoader::getSystemClassLoader ();
216 java::lang::ClassLoader *real = loader;
217 if (! real)
218 real = sys;
219 jstring sname = name->toString();
220 // We might still be bootstrapping the VM, in which case there
221 // won't be a system class loader yet.
222 jclass klass = real ? real->findLoadedClass (sname) : NULL;
224 if (! klass)
226 if (loader)
228 // Load using a user-defined loader, jvmspec 5.3.2
229 klass = loader->loadClass(sname, false);
231 // If "loader" delegated the loadClass operation to another
232 // loader, explicitly register that it is also an initiating
233 // loader of the given class.
234 java::lang::ClassLoader *delegate = (loader == sys
235 ? NULL
236 : loader);
237 if (klass && klass->getClassLoaderInternal () != delegate)
238 _Jv_RegisterInitiatingLoader (klass, loader);
240 else if (sys)
242 // Load using the bootstrap loader jvmspec 5.3.1.
243 klass = sys->loadClass (sname, false);
245 // Register that we're an initiating loader.
246 if (klass)
247 _Jv_RegisterInitiatingLoader (klass, 0);
249 else
251 // Not even a bootstrap loader, try the built-in cache.
252 klass = _Jv_FindClassInCache (name);
254 if (bootstrap_index == BOOTSTRAP_CLASS_LIST_SIZE)
255 abort ();
256 bootstrap_class_list[bootstrap_index++] = klass;
259 else
261 // we need classes to be in the hash while
262 // we're loading, so that they can refer to themselves.
263 _Jv_Linker::wait_for_state (klass, JV_STATE_LOADED);
266 return klass;
269 void
270 _Jv_RegisterBootstrapPackages ()
272 for (int i = 0; i < bootstrap_index; ++i)
273 java::lang::VMClassLoader::definePackageForNative(bootstrap_class_list[i]->getName());
276 jclass
277 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
278 java::lang::ClassLoader *loader)
280 jclass ret = (jclass) _Jv_AllocObject (&java::lang::Class::class$);
281 ret->name = name;
282 ret->superclass = superclass;
283 ret->loader = loader;
285 _Jv_RegisterClass (ret);
287 return ret;
290 static _Jv_IDispatchTable *array_idt = NULL;
291 static jshort array_depth = 0;
292 static jclass *array_ancestors = NULL;
294 // Create a class representing an array of ELEMENT and store a pointer to it
295 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
296 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
297 // array class. This parameter is optional.
298 void
299 _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
300 _Jv_VTable *array_vtable)
302 JvSynchronize sync (element);
304 _Jv_Utf8Const *array_name;
305 int len;
307 if (element->arrayclass)
308 return;
310 if (element->isPrimitive())
312 if (element == JvPrimClass (void))
313 throw new java::lang::ClassNotFoundException ();
314 len = 3;
316 else
317 len = element->name->len() + 5;
320 char signature[len];
321 int index = 0;
322 signature[index++] = '[';
323 // Compute name of array class.
324 if (element->isPrimitive())
326 signature[index++] = (char) element->method_count;
328 else
330 size_t length = element->name->len();
331 const char *const name = element->name->chars();
332 if (name[0] != '[')
333 signature[index++] = 'L';
334 memcpy (&signature[index], name, length);
335 index += length;
336 if (name[0] != '[')
337 signature[index++] = ';';
339 array_name = _Jv_makeUtf8Const (signature, index);
342 // Create new array class.
343 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
344 element->loader);
346 // Note that `vtable_method_count' doesn't include the initial
347 // gc_descr slot.
348 JvAssert (java::lang::Object::class$.vtable_method_count
349 == NUM_OBJECT_METHODS);
350 int dm_count = java::lang::Object::class$.vtable_method_count;
352 // Create a new vtable by copying Object's vtable.
353 _Jv_VTable *vtable;
354 if (array_vtable)
355 vtable = array_vtable;
356 else
357 vtable = _Jv_VTable::new_vtable (dm_count);
358 vtable->clas = array_class;
359 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
360 for (int i = 0; i < dm_count; ++i)
361 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
363 array_class->vtable = vtable;
364 array_class->vtable_method_count
365 = java::lang::Object::class$.vtable_method_count;
367 // Stash the pointer to the element type.
368 array_class->methods = (_Jv_Method *) element;
370 // Register our interfaces.
371 static jclass interfaces[] =
373 &java::lang::Cloneable::class$,
374 &java::io::Serializable::class$
376 array_class->interfaces = interfaces;
377 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
379 // Since all array classes have the same interface dispatch table, we can
380 // cache one and reuse it. It is not necessary to synchronize this.
381 if (!array_idt)
383 _Jv_Linker::wait_for_state(array_class, JV_STATE_PREPARED);
384 array_idt = array_class->idt;
385 array_depth = array_class->depth;
386 array_ancestors = array_class->ancestors;
388 else
390 array_class->idt = array_idt;
391 array_class->depth = array_depth;
392 array_class->ancestors = array_ancestors;
395 using namespace java::lang::reflect;
397 // Array classes are "abstract final" and inherit accessibility
398 // from element type, per vmspec 5.3.3.2
399 _Jv_ushort accflags = (Modifier::FINAL | Modifier::ABSTRACT
400 | (element->accflags
401 & (Modifier::PUBLIC | Modifier::PROTECTED
402 | Modifier::PRIVATE)));
403 array_class->accflags = accflags;
406 // An array class has no visible instance fields. "length" is invisible to
407 // reflection.
409 // Say this class is initialized and ready to go!
410 array_class->state = JV_STATE_DONE;
412 // vmspec, section 5.3.3 describes this
413 if (element->loader != loader)
414 _Jv_RegisterInitiatingLoader (array_class, loader);
416 element->arrayclass = array_class;
419 // These two functions form a stack of classes. When a class is loaded
420 // it is pushed onto the stack by the class loader; this is so that
421 // StackTrace can quickly determine which classes have been loaded.
423 jclass
424 _Jv_PopClass (void)
426 JvSynchronize sync (&java::lang::Class::class$);
427 if (stack_head)
429 jclass tmp = stack_head;
430 stack_head = tmp->chain;
431 return tmp;
433 return NULL;
436 void
437 _Jv_PushClass (jclass k)
439 JvSynchronize sync (&java::lang::Class::class$);
440 jclass tmp = stack_head;
441 stack_head = k;
442 k->chain = tmp;