svn merge -r108665:108708 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / libjava / java / lang / natClassLoader.cc
blob43016bf5e63e6b6ef0020cd34a63672bef4c4cb2
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 <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>
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 records classes which will be registered with the system class
56 // loader when it is initialized.
57 static jclass system_class_list;
59 // This is used as the value of system_class_list after we have
60 // initialized the system class loader; it lets us know that we should
61 // no longer pay attention to the system abi flag.
62 #define SYSTEM_LOADER_INITIALIZED ((jclass) -1)
64 // This is the root of a linked list of classes
65 static jclass stack_head;
67 // While bootstrapping we keep a list of classes we found, so that we
68 // can register their packages. There aren't many of these so we
69 // just keep a small buffer here and abort if we overflow.
70 #define BOOTSTRAP_CLASS_LIST_SIZE 20
71 static jclass bootstrap_class_list[BOOTSTRAP_CLASS_LIST_SIZE];
72 static int bootstrap_index;
77 jclass
78 java::lang::ClassLoader::loadClassFromSig(jstring name)
80 int len = _Jv_GetStringUTFLength (name);
81 char sig[len + 1];
82 _Jv_GetStringUTFRegion (name, 0, name->length(), sig);
83 return _Jv_FindClassFromSignature(sig, this);
88 // This tries to find a class in our built-in cache. This cache is
89 // used only for classes which are linked in to the executable or
90 // loaded via dlopen().
91 jclass
92 _Jv_FindClassInCache (_Jv_Utf8Const *name)
94 JvSynchronize sync (&java::lang::Class::class$);
95 jint hash = HASH_UTF (name);
97 jclass klass;
98 for (klass = loaded_classes[hash]; klass; klass = klass->next_or_version)
100 if (_Jv_equalUtf8Consts (name, klass->name))
101 break;
104 return klass;
107 void
108 _Jv_UnregisterClass (jclass the_class)
110 // This can happen if the class could not be defined properly.
111 if (! the_class->name)
112 return;
114 JvSynchronize sync (&java::lang::Class::class$);
115 jint hash = HASH_UTF(the_class->name);
117 jclass *klass = &(loaded_classes[hash]);
118 for ( ; *klass; klass = &((*klass)->next_or_version))
120 if (*klass == the_class)
122 *klass = (*klass)->next_or_version;
123 break;
128 // Register an initiating class loader for a given class.
129 void
130 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
132 if (! loader)
133 loader = java::lang::VMClassLoader::bootLoader;
134 if (! loader)
136 // Very early in the bootstrap process, the Bootstrap classloader may
137 // not exist yet.
138 // FIXME: We could maintain a list of these and come back and register
139 // them later.
140 return;
142 loader->loadedClasses->put(klass->name->toString(), klass);
145 // If we found an error while defining an interpreted class, we must
146 // go back and unregister it.
147 void
148 _Jv_UnregisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
150 if (! loader)
151 loader = java::lang::VMClassLoader::bootLoader;
152 loader->loadedClasses->remove(klass->name->toString());
155 // This function is called many times during startup, before main() is
156 // run. At that point in time we know for certain we are running
157 // single-threaded, so we don't need to lock when adding classes to the
158 // class chain. At all other times, the caller should synchronize on
159 // Class::class$.
160 void
161 _Jv_RegisterClasses (const jclass *classes)
163 for (; *classes; ++classes)
165 jclass klass = *classes;
167 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
168 (*_Jv_RegisterClassHook) (klass);
172 // This is a version of _Jv_RegisterClasses that takes a count.
173 void
174 _Jv_RegisterClasses_Counted (const jclass * classes, size_t count)
176 size_t i;
177 for (i = 0; i < count; i++)
179 jclass klass = classes[i];
181 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
182 (*_Jv_RegisterClassHook) (klass);
186 void
187 _Jv_RegisterClassHookDefault (jclass klass)
189 // This is bogus, but there doesn't seem to be a better place to do
190 // it.
191 if (! klass->engine)
192 klass->engine = &_Jv_soleCompiledEngine;
194 if (system_class_list != SYSTEM_LOADER_INITIALIZED)
196 unsigned long abi = (unsigned long) klass->next_or_version;
197 if (! _Jv_ClassForBootstrapLoader (abi))
199 klass->next_or_version = system_class_list;
200 system_class_list = klass;
201 return;
205 jint hash = HASH_UTF (klass->name);
207 // If the class is already registered, don't re-register it.
208 for (jclass check_class = loaded_classes[hash];
209 check_class != NULL;
210 check_class = check_class->next_or_version)
212 if (check_class == klass)
214 // If you get this, it means you have the same class in two
215 // different libraries.
216 #define TEXT "Duplicate class registration: "
217 // We size-limit MESSAGE so that you can't trash the stack.
218 char message[200];
219 strcpy (message, TEXT);
220 strncpy (message + sizeof (TEXT) - 1, klass->name->chars(),
221 sizeof (message) - sizeof (TEXT));
222 message[sizeof (message) - 1] = '\0';
223 if (! gcj::runtimeInitialized)
224 JvFail (message);
225 else
227 java::lang::String *str = JvNewStringLatin1 (message);
228 throw new java::lang::VirtualMachineError (str);
233 klass->next_or_version = loaded_classes[hash];
234 loaded_classes[hash] = klass;
237 // A pointer to a function that actually registers a class.
238 // Normally _Jv_RegisterClassHookDefault, but could be some other function
239 // that registers the class in e.g. a ClassLoader-local table.
240 // Should synchronize on Class:class$ while setting/restore this variable.
242 void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
244 void
245 _Jv_RegisterClass (jclass klass)
247 jclass classes[2];
248 classes[0] = klass;
249 classes[1] = NULL;
250 _Jv_RegisterClasses (classes);
253 // This is used during initialization to register all compiled-in
254 // classes that are not part of the core with the system class loader.
255 void
256 _Jv_CopyClassesToSystemLoader (java::lang::ClassLoader *loader)
258 for (jclass klass = system_class_list;
259 klass;
260 klass = klass->next_or_version)
262 klass->loader = loader;
263 loader->loadedClasses->put(klass->name->toString(), klass);
265 system_class_list = SYSTEM_LOADER_INITIALIZED;
268 jclass
269 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
271 // See if the class was already loaded by this loader. This handles
272 // initiating loader checks, as we register classes with their
273 // initiating loaders.
275 java::lang::ClassLoader *boot = java::lang::VMClassLoader::bootLoader;
276 java::lang::ClassLoader *real = loader;
277 if (! real)
278 real = boot;
279 jstring sname = name->toString();
280 // We might still be bootstrapping the VM, in which case there
281 // won't be a bootstrap class loader yet.
282 jclass klass = real ? real->findLoadedClass (sname) : NULL;
284 if (! klass)
286 if (loader)
288 // Load using a user-defined loader, jvmspec 5.3.2.
289 // Note that we explicitly must call the single-argument form.
290 klass = loader->loadClass(sname);
292 // If "loader" delegated the loadClass operation to another
293 // loader, explicitly register that it is also an initiating
294 // loader of the given class.
295 java::lang::ClassLoader *delegate = (loader == boot
296 ? NULL
297 : loader);
298 if (klass && klass->getClassLoaderInternal () != delegate)
299 _Jv_RegisterInitiatingLoader (klass, loader);
301 else if (boot)
303 // Load using the bootstrap loader jvmspec 5.3.1.
304 klass = java::lang::VMClassLoader::loadClass (sname, false);
306 // Register that we're an initiating loader.
307 if (klass)
308 _Jv_RegisterInitiatingLoader (klass, 0);
310 else
312 // Not even a bootstrap loader, try the built-in cache.
313 klass = _Jv_FindClassInCache (name);
315 if (klass)
317 bool found = false;
318 for (int i = 0; i < bootstrap_index; ++i)
320 if (bootstrap_class_list[i] == klass)
322 found = true;
323 break;
326 if (! found)
328 if (bootstrap_index == BOOTSTRAP_CLASS_LIST_SIZE)
329 abort ();
330 bootstrap_class_list[bootstrap_index++] = klass;
336 return klass;
339 void
340 _Jv_RegisterBootstrapPackages ()
342 for (int i = 0; i < bootstrap_index; ++i)
343 java::lang::VMClassLoader::definePackageForNative(bootstrap_class_list[i]->getName());
346 jclass
347 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
348 java::lang::ClassLoader *loader)
350 jclass ret = (jclass) _Jv_AllocObject (&java::lang::Class::class$);
351 ret->name = name;
352 ret->superclass = superclass;
353 ret->loader = loader;
355 _Jv_RegisterInitiatingLoader (ret, loader);
357 return ret;
360 static _Jv_IDispatchTable *array_idt = NULL;
361 static jshort array_depth = 0;
362 static jclass *array_ancestors = NULL;
364 // Create a class representing an array of ELEMENT and store a pointer to it
365 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
366 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
367 // array class. This parameter is optional.
368 void
369 _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
370 _Jv_VTable *array_vtable)
372 JvSynchronize sync (element);
374 _Jv_Utf8Const *array_name;
375 int len;
377 if (element->arrayclass)
378 return;
380 if (element->isPrimitive())
382 if (element == JvPrimClass (void))
383 throw new java::lang::ClassNotFoundException ();
384 len = 3;
386 else
387 len = element->name->len() + 5;
390 char signature[len];
391 int index = 0;
392 signature[index++] = '[';
393 // Compute name of array class.
394 if (element->isPrimitive())
396 signature[index++] = (char) element->method_count;
398 else
400 size_t length = element->name->len();
401 const char *const name = element->name->chars();
402 if (name[0] != '[')
403 signature[index++] = 'L';
404 memcpy (&signature[index], name, length);
405 index += length;
406 if (name[0] != '[')
407 signature[index++] = ';';
409 array_name = _Jv_makeUtf8Const (signature, index);
412 // Create new array class.
413 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
414 element->loader);
416 // Note that `vtable_method_count' doesn't include the initial
417 // gc_descr slot.
418 JvAssert (java::lang::Object::class$.vtable_method_count
419 == NUM_OBJECT_METHODS);
420 int dm_count = java::lang::Object::class$.vtable_method_count;
422 // Create a new vtable by copying Object's vtable.
423 _Jv_VTable *vtable;
424 if (array_vtable)
425 vtable = array_vtable;
426 else
427 vtable = _Jv_VTable::new_vtable (dm_count);
428 vtable->clas = array_class;
429 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
430 for (int i = 0; i < dm_count; ++i)
431 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
433 array_class->vtable = vtable;
434 array_class->vtable_method_count
435 = java::lang::Object::class$.vtable_method_count;
437 // Stash the pointer to the element type.
438 array_class->methods = (_Jv_Method *) element;
440 // Register our interfaces.
441 static jclass interfaces[] =
443 &java::lang::Cloneable::class$,
444 &java::io::Serializable::class$
446 array_class->interfaces = interfaces;
447 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
449 // Since all array classes have the same interface dispatch table, we can
450 // cache one and reuse it. It is not necessary to synchronize this.
451 if (!array_idt)
453 _Jv_Linker::wait_for_state(array_class, JV_STATE_PREPARED);
454 array_idt = array_class->idt;
455 array_depth = array_class->depth;
456 array_ancestors = array_class->ancestors;
458 else
460 array_class->idt = array_idt;
461 array_class->depth = array_depth;
462 array_class->ancestors = array_ancestors;
465 using namespace java::lang::reflect;
467 // Array classes are "abstract final" and inherit accessibility
468 // from element type, per vmspec 5.3.3.2
469 _Jv_ushort accflags = (Modifier::FINAL | Modifier::ABSTRACT
470 | (element->accflags
471 & (Modifier::PUBLIC | Modifier::PROTECTED
472 | Modifier::PRIVATE)));
473 array_class->accflags = accflags;
476 // An array class has no visible instance fields. "length" is invisible to
477 // reflection.
479 // Say this class is initialized and ready to go!
480 array_class->state = JV_STATE_DONE;
482 // vmspec, section 5.3.3 describes this
483 if (element->loader != loader)
484 _Jv_RegisterInitiatingLoader (array_class, loader);
486 element->arrayclass = array_class;
489 // These two functions form a stack of classes. When a class is loaded
490 // it is pushed onto the stack by the class loader; this is so that
491 // StackTrace can quickly determine which classes have been loaded.
493 jclass
494 _Jv_PopClass (void)
496 JvSynchronize sync (&java::lang::Class::class$);
497 if (stack_head)
499 jclass tmp = stack_head;
500 stack_head = tmp->chain;
501 return tmp;
503 return NULL;
506 void
507 _Jv_PushClass (jclass k)
509 JvSynchronize sync (&java::lang::Class::class$);
510 jclass tmp = stack_head;
511 stack_head = k;
512 k->chain = tmp;