1 // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 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 <java-threads.h>
22 #include <java-interp.h>
24 #include <java/lang/Character.h>
25 #include <java/lang/Thread.h>
26 #include <java/lang/ClassLoader.h>
27 #include <gnu/gcj/runtime/VMClassLoader.h>
28 #include <java/lang/InternalError.h>
29 #include <java/lang/IllegalAccessError.h>
30 #include <java/lang/LinkageError.h>
31 #include <java/lang/NoClassDefFoundError.h>
32 #include <java/lang/ClassNotFoundException.h>
33 #include <java/lang/ClassCircularityError.h>
34 #include <java/lang/IncompatibleClassChangeError.h>
35 #include <java/lang/VirtualMachineError.h>
36 #include <java/lang/VMClassLoader.h>
37 #include <java/lang/reflect/Modifier.h>
38 #include <java/lang/Runtime.h>
39 #include <java/lang/StringBuffer.h>
40 #include <java/io/Serializable.h>
41 #include <java/lang/Cloneable.h>
44 _Jv_WaitForState (jclass klass, int state)
46 if (klass->state >= state)
49 _Jv_MonitorEnter (klass) ;
51 if (state == JV_STATE_LINKED)
53 // Must call _Jv_PrepareCompiledClass while holding the class
56 if (_Jv_IsInterpretedClass (klass))
57 _Jv_PrepareClass (klass);
59 _Jv_PrepareCompiledClass (klass);
60 _Jv_MonitorExit (klass);
64 java::lang::Thread *self = java::lang::Thread::currentThread();
66 // this is similar to the strategy for class initialization.
67 // if we already hold the lock, just leave.
68 while (klass->state <= state
70 && klass->thread != self)
73 _Jv_MonitorExit (klass);
75 if (klass->state == JV_STATE_ERROR)
76 throw new java::lang::LinkageError;
79 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
81 /** This function does class-preparation for compiled classes.
82 NOTE: It contains replicated functionality from
83 _Jv_ResolvePoolEntry, and this is intentional, since that function
84 lives in resolve.cc which is entirely conditionally compiled.
87 _Jv_PrepareCompiledClass (jclass klass)
89 if (klass->state >= JV_STATE_LINKED)
92 // Short-circuit, so that mutually dependent classes are ok.
93 klass->state = JV_STATE_LINKED;
95 _Jv_Constants *pool = &klass->constants;
97 // Resolve class constants first, since other constant pool
98 // entries may rely on these.
99 for (int index = 1; index < pool->size; ++index)
101 if (pool->tags[index] == JV_CONSTANT_Class)
103 _Jv_Utf8Const *name = pool->data[index].utf8;
106 if (name->data[0] == '[')
107 found = _Jv_FindClassFromSignature (&name->data[0],
110 found = _Jv_FindClass (name, klass->loader);
114 jstring str = _Jv_NewStringUTF (name->data);
115 throw new java::lang::NoClassDefFoundError (str);
118 pool->data[index].clazz = found;
119 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
123 // If superclass looks like a constant pool entry,
125 if ((uaddr) klass->superclass < pool->size)
126 klass->superclass = pool->data[(int) klass->superclass].clazz;
128 // Likewise for interfaces.
129 for (int i = 0; i < klass->interface_count; i++)
130 if ((uaddr) klass->interfaces[i] < pool->size)
131 klass->interfaces[i] = pool->data[(int) klass->interfaces[i]].clazz;
133 // Resolve the remaining constant pool entries.
134 for (int index = 1; index < pool->size; ++index)
136 if (pool->tags[index] == JV_CONSTANT_String)
140 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
141 pool->data[index].o = str;
142 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
147 // FIXME: although the comment up top says that this function is
148 // only called for compiled classes, it is actually called for every
150 if (! _Jv_IsInterpretedClass (klass))
152 #endif /* INTERPRETER */
153 jfieldID f = JvGetFirstStaticField (klass);
154 for (int n = JvNumStaticFields (klass); n > 0; --n)
156 int mod = f->getModifiers ();
157 // If we have a static String field with a non-null initial
158 // value, we know it points to a Utf8Const.
159 _Jv_ResolveField(f, klass->loader);
160 if (f->getClass () == &java::lang::String::class$
161 && java::lang::reflect::Modifier::isStatic (mod))
163 jstring *strp = (jstring *) f->u.addr;
165 *strp = _Jv_NewStringUtf8Const ((_Jv_Utf8Const *) *strp);
167 f = f->getNextField ();
171 #endif /* INTERPRETER */
175 _Jv_PushClass (klass);
180 // A single class can have many "initiating" class loaders,
181 // and a single "defining" class loader. The Defining
182 // class loader is what is returned from Class.getClassLoader()
183 // and is used when loading dependent classes during resolution.
184 // The set of initiating class loaders are used to ensure
185 // safety of linking, and is maintained in the hash table
186 // "initiated_classes". A defining classloader is by definition also
187 // initiating, so we only store classes in this table if they have more
188 // than one class loader associated.
192 // Size of local hash table.
193 #define HASH_LEN 1013
195 // Hash function for Utf8Consts.
196 #define HASH_UTF(Utf) (((Utf)->hash) % HASH_LEN)
198 struct _Jv_LoaderInfo
200 _Jv_LoaderInfo *next;
201 java::lang::Class *klass;
202 java::lang::ClassLoader *loader;
205 static _Jv_LoaderInfo *initiated_classes[HASH_LEN];
206 static jclass loaded_classes[HASH_LEN];
208 // This is the root of a linked list of classes
213 _Jv_FindClassInCache (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
215 JvSynchronize sync (&java::lang::Class::class$);
216 jint hash = HASH_UTF (name);
218 if (loader && loader == java::lang::ClassLoader::getSystemClassLoader())
221 // first, if LOADER is a defining loader, then it is also initiating
223 for (klass = loaded_classes[hash]; klass; klass = klass->next)
225 if (loader == klass->loader && _Jv_equalUtf8Consts (name, klass->name))
229 // otherwise, it may be that the class in question was defined
230 // by some other loader, but that the loading was initiated by
231 // the loader in question.
234 _Jv_LoaderInfo *info;
235 for (info = initiated_classes[hash]; info; info = info->next)
237 if (loader == info->loader
238 && _Jv_equalUtf8Consts (name, info->klass->name))
250 _Jv_UnregisterClass (jclass the_class)
252 JvSynchronize sync (&java::lang::Class::class$);
253 jint hash = HASH_UTF(the_class->name);
255 jclass *klass = &(loaded_classes[hash]);
256 for ( ; *klass; klass = &((*klass)->next))
258 if (*klass == the_class)
260 *klass = (*klass)->next;
265 _Jv_LoaderInfo **info = &(initiated_classes[hash]);
266 for ( ; ; info = &((*info)->next))
268 while (*info && (*info)->klass == the_class)
270 _Jv_LoaderInfo *old = *info;
271 *info = (*info)->next;
281 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
283 if (loader && loader == java::lang::ClassLoader::getSystemClassLoader())
286 // This information can't be visible to the GC.
288 = (_Jv_LoaderInfo *) _Jv_Malloc (sizeof(_Jv_LoaderInfo));
289 jint hash = HASH_UTF(klass->name);
291 JvSynchronize sync (&java::lang::Class::class$);
292 info->loader = loader;
294 info->next = initiated_classes[hash];
295 initiated_classes[hash] = info;
298 // This function is called many times during startup, before main() is
299 // run. At that point in time we know for certain we are running
300 // single-threaded, so we don't need to lock when adding classes to the
301 // class chain. At all other times, the caller should synchronize on
304 _Jv_RegisterClasses (jclass *classes)
306 for (; *classes; ++classes)
308 jclass klass = *classes;
310 (*_Jv_RegisterClassHook) (klass);
312 // registering a compiled class causes
313 // it to be immediately "prepared".
314 if (klass->state == JV_STATE_NOTHING)
315 klass->state = JV_STATE_COMPILED;
320 _Jv_RegisterClassHookDefault (jclass klass)
322 jint hash = HASH_UTF (klass->name);
324 jclass check_class = loaded_classes[hash];
326 // If the class is already registered, don't re-register it.
327 while (check_class != NULL)
329 if (check_class == klass)
331 // If you get this, it means you have the same class in two
332 // different libraries.
333 #define TEXT "Duplicate class registration: "
334 // We size-limit MESSAGE so that you can't trash the stack.
336 strcpy (message, TEXT);
337 strncpy (message + sizeof (TEXT) - 1, klass->name->data,
338 sizeof (message) - sizeof (TEXT));
339 message[sizeof (message) - 1] = '\0';
340 if (! gcj::runtimeInitialized)
344 java::lang::String *str = JvNewStringLatin1 (message);
345 throw new java::lang::VirtualMachineError (str);
349 check_class = check_class->next;
352 klass->next = loaded_classes[hash];
353 loaded_classes[hash] = klass;
356 // A pointer to a function that actually registers a class.
357 // Normally _Jv_RegisterClassHookDefault, but could be some other function
358 // that registers the class in e.g. a ClassLoader-local table.
359 // Should synchronize on Class:class$ while setting/restore this variable.
361 void (*_Jv_RegisterClassHook) (jclass cl) = _Jv_RegisterClassHookDefault;
364 _Jv_RegisterClass (jclass klass)
369 _Jv_RegisterClasses (classes);
373 _Jv_FindClass (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
375 jclass klass = _Jv_FindClassInCache (name, loader);
379 jstring sname = _Jv_NewStringUTF (name->data);
381 java::lang::ClassLoader *sys
382 = java::lang::ClassLoader::getSystemClassLoader ();
386 // Load using a user-defined loader, jvmspec 5.3.2
387 klass = loader->loadClass(sname, false);
389 // If "loader" delegated the loadClass operation to another
390 // loader, explicitly register that it is also an initiating
391 // loader of the given class.
392 java::lang::ClassLoader *delegate = (loader == sys
395 if (klass && klass->getClassLoaderInternal () != delegate)
396 _Jv_RegisterInitiatingLoader (klass, loader);
400 // Load using the bootstrap loader jvmspec 5.3.1.
401 klass = sys->loadClass (sname, false);
403 // Register that we're an initiating loader.
405 _Jv_RegisterInitiatingLoader (klass, 0);
410 // we need classes to be in the hash while
411 // we're loading, so that they can refer to themselves.
412 _Jv_WaitForState (klass, JV_STATE_LOADED);
419 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
420 java::lang::ClassLoader *loader)
422 jclass ret = (jclass) JvAllocObject (&java::lang::Class::class$);
424 ret->superclass = superclass;
425 ret->loader = loader;
427 _Jv_RegisterClass (ret);
432 static _Jv_IDispatchTable *array_idt = NULL;
433 static jshort array_depth = 0;
434 static jclass *array_ancestors = NULL;
436 // Create a class representing an array of ELEMENT and store a pointer to it
437 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
438 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
439 // array class. This parameter is optional.
441 _Jv_NewArrayClass (jclass element, java::lang::ClassLoader *loader,
442 _Jv_VTable *array_vtable)
444 JvSynchronize sync (element);
446 _Jv_Utf8Const *array_name;
449 if (element->arrayclass)
452 if (element->isPrimitive())
454 if (element == JvPrimClass (void))
455 throw new java::lang::ClassNotFoundException ();
459 len = element->name->length + 5;
464 signature[index++] = '[';
465 // Compute name of array class.
466 if (element->isPrimitive())
468 signature[index++] = (char) element->method_count;
472 size_t length = element->name->length;
473 const char *const name = element->name->data;
475 signature[index++] = 'L';
476 memcpy (&signature[index], name, length);
479 signature[index++] = ';';
481 array_name = _Jv_makeUtf8Const (signature, index);
484 // Create new array class.
485 jclass array_class = _Jv_NewClass (array_name, &java::lang::Object::class$,
488 // Note that `vtable_method_count' doesn't include the initial
490 JvAssert (java::lang::Object::class$.vtable_method_count
491 == NUM_OBJECT_METHODS);
492 int dm_count = java::lang::Object::class$.vtable_method_count;
494 // Create a new vtable by copying Object's vtable.
497 vtable = array_vtable;
499 vtable = _Jv_VTable::new_vtable (dm_count);
500 vtable->clas = array_class;
501 vtable->gc_descr = java::lang::Object::class$.vtable->gc_descr;
502 for (int i = 0; i < dm_count; ++i)
503 vtable->set_method (i, java::lang::Object::class$.vtable->get_method (i));
505 array_class->vtable = vtable;
506 array_class->vtable_method_count
507 = java::lang::Object::class$.vtable_method_count;
509 // Stash the pointer to the element type.
510 array_class->methods = (_Jv_Method *) element;
512 // Register our interfaces.
513 static jclass interfaces[] =
515 &java::lang::Cloneable::class$,
516 &java::io::Serializable::class$
518 array_class->interfaces = interfaces;
519 array_class->interface_count = sizeof interfaces / sizeof interfaces[0];
521 // Since all array classes have the same interface dispatch table, we can
522 // cache one and reuse it. It is not necessary to synchronize this.
525 _Jv_PrepareConstantTimeTables (array_class);
526 array_idt = array_class->idt;
527 array_depth = array_class->depth;
528 array_ancestors = array_class->ancestors;
532 array_class->idt = array_idt;
533 array_class->depth = array_depth;
534 array_class->ancestors = array_ancestors;
537 using namespace java::lang::reflect;
539 // Array classes are "abstract final"...
540 _Jv_ushort accflags = Modifier::FINAL | Modifier::ABSTRACT;
541 // ... and inherit accessibility from element type, per vmspec 5.3.3.2
542 accflags |= (element->accflags & Modifier::PUBLIC);
543 accflags |= (element->accflags & Modifier::PROTECTED);
544 accflags |= (element->accflags & Modifier::PRIVATE);
545 array_class->accflags = accflags;
548 // An array class has no visible instance fields. "length" is invisible to
551 // say this class is initialized and ready to go!
552 array_class->state = JV_STATE_DONE;
554 // vmspec, section 5.3.3 describes this
555 if (element->loader != loader)
556 _Jv_RegisterInitiatingLoader (array_class, loader);
558 element->arrayclass = array_class;
561 static jclass stack_head;
563 // These two functions form a stack of classes. When a class is loaded
564 // it is pushed onto the stack by the class loader; this is so that
565 // StackTrace can quickly determine which classes have been loaded.
570 JvSynchronize sync (&java::lang::Class::class$);
573 jclass tmp = stack_head;
574 stack_head = tmp->chain;
581 _Jv_PushClass (jclass k)
583 JvSynchronize sync (&java::lang::Class::class$);
584 jclass tmp = stack_head;