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
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
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.
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().
72 _Jv_FindClassInCache (_Jv_Utf8Const *name)
74 JvSynchronize sync (&java::lang::Class::class$);
75 jint hash = HASH_UTF (name);
78 for (klass = loaded_classes[hash]; klass; klass = klass->next_or_version)
80 if (_Jv_equalUtf8Consts (name, klass->name))
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;
104 // Register an initiating class loader for a given class.
106 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
109 loader = java::lang::ClassLoader::getSystemClassLoader();
110 loader->loadedClasses->put(klass->name->toString(), klass);
113 // This function is called many times during startup, before main() is
114 // run. At that point in time we know for certain we are running
115 // single-threaded, so we don't need to lock when adding classes to the
116 // class chain. At all other times, the caller should synchronize on
119 _Jv_RegisterClasses (const jclass *classes)
121 for (; *classes; ++classes)
123 jclass klass = *classes;
125 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
126 (*_Jv_RegisterClassHook) (klass);
130 // This is a version of _Jv_RegisterClasses that takes a count.
132 _Jv_RegisterClasses_Counted (const jclass * classes, size_t count)
135 for (i = 0; i < count; i++)
137 jclass klass = classes[i];
139 if (_Jv_CheckABIVersion ((unsigned long) klass->next_or_version))
140 (*_Jv_RegisterClassHook) (klass);
145 _Jv_RegisterClassHookDefault (jclass klass)
147 jint hash = HASH_UTF (klass->name);
149 // If the class is already registered, don't re-register it.
150 for (jclass check_class = loaded_classes[hash];
152 check_class = check_class->next_or_version)
154 if (check_class == klass)
156 // If you get this, it means you have the same class in two
157 // different libraries.
158 #define TEXT "Duplicate class registration: "
159 // We size-limit MESSAGE so that you can't trash the stack.
161 strcpy (message, TEXT);
162 strncpy (message + sizeof (TEXT) - 1, klass->name->chars(),
163 sizeof (message) - sizeof (TEXT));
164 message[sizeof (message) - 1] = '\0';
165 if (! gcj::runtimeInitialized)
169 java::lang::String *str = JvNewStringLatin1 (message);
170 throw new java::lang::VirtualMachineError (str);
175 // FIXME: this is really bogus!
177 klass->engine = &_Jv_soleCompiledEngine;
178 klass->next_or_version = loaded_classes[hash];
179 loaded_classes[hash] = klass;
182 // A pointer to a function that actually registers a class.
183 // Normally _Jv_RegisterClassHookDefault, but could be some other function
184 // that registers the class in e.g. a ClassLoader-local table.
185 // Should synchronize on Class:class$ while setting/restore this variable.
187 void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
190 _Jv_RegisterClass (jclass klass)
195 _Jv_RegisterClasses (classes);
199 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
201 // See if the class was already loaded by this loader. This handles
202 // initiating loader checks, as we register classes with their
203 // initiating loaders.
204 java::lang::ClassLoader *sys
205 = java::lang::ClassLoader::getSystemClassLoader ();
206 java::lang::ClassLoader *real = loader;
209 jstring sname = name->toString();
210 // We might still be bootstrapping the VM, in which case there
211 // won't be a system class loader yet.
212 jclass klass = real ? real->findLoadedClass (sname) : NULL;
218 // Load using a user-defined loader, jvmspec 5.3.2
219 klass = loader->loadClass(sname, false);
221 // If "loader" delegated the loadClass operation to another
222 // loader, explicitly register that it is also an initiating
223 // loader of the given class.
224 java::lang::ClassLoader *delegate = (loader == sys
227 if (klass && klass->getClassLoaderInternal () != delegate)
228 _Jv_RegisterInitiatingLoader (klass, loader);
232 // Load using the bootstrap loader jvmspec 5.3.1.
233 klass = sys->loadClass (sname, false);
235 // Register that we're an initiating loader.
237 _Jv_RegisterInitiatingLoader (klass, 0);
241 // Not even a bootstrap loader, try the built-in cache.
242 klass = _Jv_FindClassInCache (name);
244 if (bootstrap_index == BOOTSTRAP_CLASS_LIST_SIZE)
246 bootstrap_class_list[bootstrap_index++] = klass;
251 // we need classes to be in the hash while
252 // we're loading, so that they can refer to themselves.
253 _Jv_Linker::wait_for_state (klass, JV_STATE_LOADED);
260 _Jv_RegisterBootstrapPackages ()
262 for (int i = 0; i < bootstrap_index; ++i)
263 java::lang::VMClassLoader::definePackageForNative(bootstrap_class_list[i]->getName());
267 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
268 java::lang::ClassLoader *loader)
270 jclass ret = (jclass) _Jv_AllocObject (&java::lang::Class::class$);
272 ret->superclass = superclass;
273 ret->loader = loader;
275 _Jv_RegisterClass (ret);
280 static _Jv_IDispatchTable *array_idt = NULL;
281 static jshort array_depth = 0;
282 static jclass *array_ancestors = NULL;
284 // Create a class representing an array of ELEMENT and store a pointer to it
285 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
286 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
287 // array class. This parameter is optional.
289 _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
290 _Jv_VTable *array_vtable)
292 JvSynchronize sync (element);
294 _Jv_Utf8Const *array_name;
297 if (element->arrayclass)
300 if (element->isPrimitive())
302 if (element == JvPrimClass (void))
303 throw new java::lang::ClassNotFoundException ();
307 len = element->name->len() + 5;
312 signature[index++] = '[';
313 // Compute name of array class.
314 if (element->isPrimitive())
316 signature[index++] = (char) element->method_count;
320 size_t length = element->name->len();
321 const char *const name = element->name->chars();
323 signature[index++] = 'L';
324 memcpy (&signature[index], name, length);
327 signature[index++] = ';';
329 array_name = _Jv_makeUtf8Const (signature, index);
332 // Create new array class.
333 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
336 // Note that `vtable_method_count' doesn't include the initial
338 JvAssert (java::lang::Object::class$.vtable_method_count
339 == NUM_OBJECT_METHODS);
340 int dm_count = java::lang::Object::class$.vtable_method_count;
342 // Create a new vtable by copying Object's vtable.
345 vtable = array_vtable;
347 vtable = _Jv_VTable::new_vtable (dm_count);
348 vtable->clas = array_class;
349 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
350 for (int i = 0; i < dm_count; ++i)
351 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
353 array_class->vtable = vtable;
354 array_class->vtable_method_count
355 = java::lang::Object::class$.vtable_method_count;
357 // Stash the pointer to the element type.
358 array_class->methods = (_Jv_Method *) element;
360 // Register our interfaces.
361 static jclass interfaces[] =
363 &java::lang::Cloneable::class$,
364 &java::io::Serializable::class$
366 array_class->interfaces = interfaces;
367 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
369 // Since all array classes have the same interface dispatch table, we can
370 // cache one and reuse it. It is not necessary to synchronize this.
373 _Jv_Linker::wait_for_state(array_class, JV_STATE_PREPARED);
374 array_idt = array_class->idt;
375 array_depth = array_class->depth;
376 array_ancestors = array_class->ancestors;
380 array_class->idt = array_idt;
381 array_class->depth = array_depth;
382 array_class->ancestors = array_ancestors;
385 using namespace java::lang::reflect;
387 // Array classes are "abstract final" and inherit accessibility
388 // from element type, per vmspec 5.3.3.2
389 _Jv_ushort accflags = (Modifier::FINAL | Modifier::ABSTRACT
391 & (Modifier::PUBLIC | Modifier::PROTECTED
392 | Modifier::PRIVATE)));
393 array_class->accflags = accflags;
396 // An array class has no visible instance fields. "length" is invisible to
399 // Say this class is initialized and ready to go!
400 array_class->state = JV_STATE_DONE;
402 // vmspec, section 5.3.3 describes this
403 if (element->loader != loader)
404 _Jv_RegisterInitiatingLoader (array_class, loader);
406 element->arrayclass = array_class;
409 // These two functions form a stack of classes. When a class is loaded
410 // it is pushed onto the stack by the class loader; this is so that
411 // StackTrace can quickly determine which classes have been loaded.
416 JvSynchronize sync (&java::lang::Class::class$);
419 jclass tmp = stack_head;
420 stack_head = tmp->chain;
427 _Jv_PushClass (jclass k)
429 JvSynchronize sync (&java::lang::Class::class$);
430 jclass tmp = stack_head;