* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / libjava / java / lang / natClassLoader.cc
blobfa6f201db468a2f98c1abd4e750b05abf381da67
1 // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 <java/lang/InternalError.h>
30 #include <java/lang/IllegalAccessError.h>
31 #include <java/lang/LinkageError.h>
32 #include <java/lang/NoClassDefFoundError.h>
33 #include <java/lang/ClassNotFoundException.h>
34 #include <java/lang/ClassCircularityError.h>
35 #include <java/lang/IncompatibleClassChangeError.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <java/lang/VirtualMachineError.h>
38 #include <java/lang/VMClassLoader.h>
39 #include <java/lang/reflect/Modifier.h>
40 #include <java/lang/Runtime.h>
41 #include <java/lang/StringBuffer.h>
42 #include <java/io/Serializable.h>
43 #include <java/lang/Cloneable.h>
44 #include <java/util/HashMap.h>
45 #include <gnu/gcj/runtime/BootClassLoader.h>
46 #include <gnu/gcj/runtime/SystemClassLoader.h>
48 // Size of local hash table.
49 #define HASH_LEN 1013
51 // Hash function for Utf8Consts.
52 #define HASH_UTF(Utf) ((Utf)->hash16() % HASH_LEN)
54 static jclass loaded_classes[HASH_LEN];
56 // This records classes which will be registered with the system class
57 // loader when it is initialized.
58 static jclass system_class_list;
60 // This is used as the value of system_class_list after we have
61 // initialized the system class loader; it lets us know that we should
62 // no longer pay attention to the system abi flag.
63 #define SYSTEM_LOADER_INITIALIZED ((jclass) -1)
65 // This is the root of a linked list of classes
66 static jclass stack_head;
68 // While bootstrapping we keep a list of classes we found, so that we
69 // can register their packages. There aren't many of these so we
70 // just keep a small buffer here and abort if we overflow.
71 #define BOOTSTRAP_CLASS_LIST_SIZE 20
72 static jclass bootstrap_class_list[BOOTSTRAP_CLASS_LIST_SIZE];
73 static int bootstrap_index;
78 jclass
79 java::lang::ClassLoader::loadClassFromSig(jstring name)
81 int len = _Jv_GetStringUTFLength (name);
82 char sig[len + 1];
83 _Jv_GetStringUTFRegion (name, 0, name->length(), sig);
84 return _Jv_FindClassFromSignature(sig, this);
89 // This tries to find a class in our built-in cache. This cache is
90 // used only for classes which are linked in to the executable or
91 // loaded via dlopen().
92 jclass
93 _Jv_FindClassInCache (_Jv_Utf8Const *name)
95 JvSynchronize sync (&java::lang::Class::class$);
96 jint hash = HASH_UTF (name);
98 jclass klass;
99 for (klass = loaded_classes[hash]; klass; klass = klass->next_or_version)
101 if (_Jv_equalUtf8Consts (name, klass->name))
102 break;
105 return klass;
108 void
109 _Jv_UnregisterClass (jclass the_class)
111 // This can happen if the class could not be defined properly.
112 if (! the_class->name)
113 return;
115 JvSynchronize sync (&java::lang::Class::class$);
116 jint hash = HASH_UTF(the_class->name);
118 jclass *klass = &(loaded_classes[hash]);
119 for ( ; *klass; klass = &((*klass)->next_or_version))
121 if (*klass == the_class)
123 *klass = (*klass)->next_or_version;
124 break;
129 // Register an initiating class loader for a given class.
130 void
131 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
133 if (! loader)
134 loader = java::lang::VMClassLoader::bootLoader;
135 if (! loader)
137 // Very early in the bootstrap process, the Bootstrap classloader may
138 // not exist yet.
139 // FIXME: We could maintain a list of these and come back and register
140 // them later.
141 return;
143 loader->loadedClasses->put(klass->name->toString(), klass);
146 // If we found an error while defining an interpreted class, we must
147 // go back and unregister it.
148 void
149 _Jv_UnregisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
151 if (! loader)
152 loader = java::lang::VMClassLoader::bootLoader;
153 loader->loadedClasses->remove(klass->name->toString());
156 // This function is called many times during startup, before main() is
157 // run. At that point in time we know for certain we are running
158 // single-threaded, so we don't need to lock when adding classes to the
159 // class chain. At all other times, the caller should synchronize on
160 // Class::class$.
161 void
162 _Jv_RegisterClasses (const jclass *classes)
164 for (; *classes; ++classes)
166 jclass klass = *classes;
168 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
169 (*_Jv_RegisterClassHook) (klass);
173 // This is a version of _Jv_RegisterClasses that takes a count.
174 void
175 _Jv_RegisterClasses_Counted (const jclass * classes, size_t count)
177 size_t i;
178 for (i = 0; i < count; i++)
180 jclass klass = classes[i];
182 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
183 (*_Jv_RegisterClassHook) (klass);
187 void
188 _Jv_RegisterClassHookDefault (jclass klass)
190 // This is bogus, but there doesn't seem to be a better place to do
191 // it.
192 if (! klass->engine)
193 klass->engine = &_Jv_soleCompiledEngine;
195 if (system_class_list != SYSTEM_LOADER_INITIALIZED)
197 unsigned long abi = (unsigned long) klass->next_or_version;
198 if (! _Jv_ClassForBootstrapLoader (abi))
200 klass->next_or_version = system_class_list;
201 system_class_list = klass;
202 return;
206 jint hash = HASH_UTF (klass->name);
208 // If the class is already registered, don't re-register it.
209 for (jclass check_class = loaded_classes[hash];
210 check_class != NULL;
211 check_class = check_class->next_or_version)
213 if (check_class == klass)
215 // If you get this, it means you have the same class in two
216 // different libraries.
217 #define TEXT "Duplicate class registration: "
218 // We size-limit MESSAGE so that you can't trash the stack.
219 char message[200];
220 strcpy (message, TEXT);
221 strncpy (message + sizeof (TEXT) - 1, klass->name->chars(),
222 sizeof (message) - sizeof (TEXT));
223 message[sizeof (message) - 1] = '\0';
224 if (! gcj::runtimeInitialized)
225 JvFail (message);
226 else
228 java::lang::String *str = JvNewStringLatin1 (message);
229 throw new java::lang::VirtualMachineError (str);
234 klass->next_or_version = loaded_classes[hash];
235 loaded_classes[hash] = klass;
238 // A pointer to a function that actually registers a class.
239 // Normally _Jv_RegisterClassHookDefault, but could be some other function
240 // that registers the class in e.g. a ClassLoader-local table.
241 // Should synchronize on Class:class$ while setting/restore this variable.
243 void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
245 void
246 _Jv_RegisterClass (jclass klass)
248 jclass classes[2];
249 classes[0] = klass;
250 classes[1] = NULL;
251 _Jv_RegisterClasses (classes);
254 // This is used during initialization to register all compiled-in
255 // classes that are not part of the core with the system class loader.
256 void
257 _Jv_CopyClassesToSystemLoader (gnu::gcj::runtime::SystemClassLoader *loader)
259 for (jclass klass = system_class_list;
260 klass;
261 klass = klass->next_or_version)
263 klass->loader = loader;
264 loader->addClass(klass);
266 system_class_list = SYSTEM_LOADER_INITIALIZED;
269 // An internal variant of _Jv_FindClass which simply swallows a
270 // NoClassDefFoundError or a ClassNotFoundException. This gives the
271 // caller a chance to evaluate the situation and behave accordingly.
272 jclass
273 _Jv_FindClassNoException (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
275 jclass klass;
279 klass = _Jv_FindClass(name, loader);
281 catch ( java::lang::NoClassDefFoundError *ncdfe )
283 return NULL;
285 catch ( java::lang::ClassNotFoundException *cnfe )
287 return NULL;
290 return klass;
293 jclass
294 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
296 // See if the class was already loaded by this loader. This handles
297 // initiating loader checks, as we register classes with their
298 // initiating loaders.
300 java::lang::ClassLoader *boot = java::lang::VMClassLoader::bootLoader;
301 java::lang::ClassLoader *real = loader;
302 if (! real)
303 real = boot;
304 jstring sname = name->toString();
305 // We might still be bootstrapping the VM, in which case there
306 // won't be a bootstrap class loader yet.
307 jclass klass = real ? real->findLoadedClass (sname) : NULL;
309 if (! klass)
311 if (loader)
313 // Load using a user-defined loader, jvmspec 5.3.2.
314 // Note that we explicitly must call the single-argument form.
315 klass = loader->loadClass(sname);
317 // If "loader" delegated the loadClass operation to another
318 // loader, explicitly register that it is also an initiating
319 // loader of the given class.
320 java::lang::ClassLoader *delegate = (loader == boot
321 ? NULL
322 : loader);
323 if (klass && klass->getClassLoaderInternal () != delegate)
324 _Jv_RegisterInitiatingLoader (klass, loader);
326 else if (boot)
328 // Load using the bootstrap loader jvmspec 5.3.1.
329 klass = java::lang::VMClassLoader::loadClass (sname, false);
331 // Register that we're an initiating loader.
332 if (klass)
333 _Jv_RegisterInitiatingLoader (klass, 0);
335 else
337 // Not even a bootstrap loader, try the built-in cache.
338 klass = _Jv_FindClassInCache (name);
340 if (klass)
342 bool found = false;
343 for (int i = 0; i < bootstrap_index; ++i)
345 if (bootstrap_class_list[i] == klass)
347 found = true;
348 break;
351 if (! found)
353 if (bootstrap_index == BOOTSTRAP_CLASS_LIST_SIZE)
354 abort ();
355 bootstrap_class_list[bootstrap_index++] = klass;
361 return klass;
364 void
365 _Jv_RegisterBootstrapPackages ()
367 for (int i = 0; i < bootstrap_index; ++i)
368 java::lang::VMClassLoader::definePackageForNative(bootstrap_class_list[i]->getName());
371 jclass
372 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
373 java::lang::ClassLoader *loader)
375 jclass ret = (jclass) _Jv_AllocObject (&java::lang::Class::class$);
376 ret->name = name;
377 ret->superclass = superclass;
378 ret->loader = loader;
380 _Jv_RegisterInitiatingLoader (ret, loader);
382 return ret;
385 static _Jv_IDispatchTable *array_idt = NULL;
386 static jshort array_depth = 0;
387 static jclass *array_ancestors = NULL;
389 // Create a class representing an array of ELEMENT and store a pointer to it
390 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
391 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
392 // array class. This parameter is optional.
393 void
394 _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
395 _Jv_VTable *array_vtable)
397 JvSynchronize sync (element);
399 _Jv_Utf8Const *array_name;
400 int len;
402 if (element->arrayclass)
403 return;
405 if (element->isPrimitive())
407 if (element == JvPrimClass (void))
408 throw new java::lang::ClassNotFoundException ();
409 len = 3;
411 else
412 len = element->name->len() + 5;
415 char signature[len];
416 int index = 0;
417 signature[index++] = '[';
418 // Compute name of array class.
419 if (element->isPrimitive())
421 signature[index++] = (char) element->method_count;
423 else
425 size_t length = element->name->len();
426 const char *const name = element->name->chars();
427 if (name[0] != '[')
428 signature[index++] = 'L';
429 memcpy (&signature[index], name, length);
430 index += length;
431 if (name[0] != '[')
432 signature[index++] = ';';
434 array_name = _Jv_makeUtf8Const (signature, index);
437 // Create new array class.
438 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
439 element->loader);
441 // Note that `vtable_method_count' doesn't include the initial
442 // gc_descr slot.
443 int dm_count = java::lang::Object::class$.vtable_method_count;
445 // Create a new vtable by copying Object's vtable.
446 _Jv_VTable *vtable;
447 if (array_vtable)
448 vtable = array_vtable;
449 else
450 vtable = _Jv_VTable::new_vtable (dm_count);
451 vtable->clas = array_class;
452 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
453 for (int i = 0; i < dm_count; ++i)
454 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
456 array_class->vtable = vtable;
457 array_class->vtable_method_count
458 = java::lang::Object::class$.vtable_method_count;
460 // Stash the pointer to the element type.
461 array_class->element_type = element;
463 // Register our interfaces.
464 static jclass interfaces[] =
466 &java::lang::Cloneable::class$,
467 &java::io::Serializable::class$
469 array_class->interfaces = interfaces;
470 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
472 // Since all array classes have the same interface dispatch table, we can
473 // cache one and reuse it. It is not necessary to synchronize this.
474 if (!array_idt)
476 _Jv_Linker::wait_for_state(array_class, JV_STATE_PREPARED);
477 array_idt = array_class->idt;
478 array_depth = array_class->depth;
479 array_ancestors = array_class->ancestors;
481 else
483 array_class->idt = array_idt;
484 array_class->depth = array_depth;
485 array_class->ancestors = array_ancestors;
488 using namespace java::lang::reflect;
490 // Array classes are "abstract final" and inherit accessibility
491 // from element type, per vmspec 5.3.3.2
492 _Jv_ushort accflags = (Modifier::FINAL | Modifier::ABSTRACT
493 | (element->accflags
494 & (Modifier::PUBLIC | Modifier::PROTECTED
495 | Modifier::PRIVATE)));
496 array_class->accflags = accflags;
499 // An array class has no visible instance fields. "length" is invisible to
500 // reflection.
502 // Say this class is initialized and ready to go!
503 array_class->state = JV_STATE_DONE;
505 // vmspec, section 5.3.3 describes this
506 if (element->loader != loader)
507 _Jv_RegisterInitiatingLoader (array_class, loader);
509 element->arrayclass = array_class;
512 // These two functions form a stack of classes. When a class is loaded
513 // it is pushed onto the stack by the class loader; this is so that
514 // StackTrace can quickly determine which classes have been loaded.
516 jclass
517 _Jv_PopClass (void)
519 JvSynchronize sync (&java::lang::Class::class$);
520 if (stack_head)
522 jclass tmp = stack_head;
523 stack_head = tmp->chain;
524 return tmp;
526 return NULL;
529 void
530 _Jv_PushClass (jclass k)
532 JvSynchronize sync (&java::lang::Class::class$);
533 jclass tmp = stack_head;
534 stack_head = k;
535 k->chain = tmp;