1 // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods.
3 /* Copyright (C) 1999 Cygnus Solutions
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> */
20 #include <java/lang/Character.h>
21 #include <java/lang/Thread.h>
22 #include <java/lang/ClassLoader.h>
23 #include <gnu/gcj/runtime/VMClassLoader.h>
24 #include <java/lang/InternalError.h>
25 #include <java/lang/IllegalAccessError.h>
26 #include <java/lang/LinkageError.h>
27 #include <java/lang/ClassFormatError.h>
28 #include <java/lang/NoClassDefFoundError.h>
29 #include <java/lang/ClassNotFoundException.h>
30 #include <java/lang/ClassCircularityError.h>
31 #include <java/lang/IncompatibleClassChangeError.h>
32 #include <java/lang/reflect/Modifier.h>
34 #include <java-interp.h>
36 #define CloneableClass _CL_Q34java4lang9Cloneable
37 extern java::lang::Class CloneableClass;
38 #define ObjectClass _CL_Q34java4lang6Object
39 extern java::lang::Class ObjectClass;
40 #define ClassClass _CL_Q34java4lang5Class
41 extern java::lang::Class ClassClass;
42 #define VMClassLoaderClass _CL_Q34java4lang17VMClassLoader
43 extern java::lang::Class VMClassLoader;
44 #define ClassLoaderClass _CL_Q34java4lang11ClassLoader
45 extern java::lang::Class ClassLoaderClass;
47 /////////// java.lang.ClassLoader native methods ////////////
50 gnu::gcj::runtime::VMClassLoader *redirect = 0;
53 java::lang::ClassLoader*
54 java::lang::ClassLoader::getVMClassLoader0 ()
58 redirect = new gnu::gcj::runtime::VMClassLoader;
66 java::lang::ClassLoader::defineClass2 (jclass klass, jbyteArray data,
67 jint offset, jint length)
70 _Jv_DefineClass (klass, data, offset, length);
75 java::lang::ClassLoader::defineClass0 (jstring name,
82 klass = (jclass) JvAllocObject (&ClassClass, sizeof (_Jv_InterpClass));
84 // synchronize on the class, so that it is not
85 // attempted initialized until we're done loading.
86 _Jv_MonitorEnter (klass);
88 // record which is the defining loader
91 // register that we are the initiating loader...
94 _Jv_Utf8Const * name2 = _Jv_makeUtf8Const (name);
96 _Jv_VerifyClassName (name2);
101 // this will do the magic. loadInto also operates
102 // as an exception trampoline for now...
103 Throwable *ex = defineClass1 (klass, data, offset, length);
105 if (ex) // we failed to load it
107 klass->state = JV_STATE_ERROR;
110 _Jv_UnregisterClass (klass);
112 _Jv_MonitorExit (klass);
114 // FIXME: Here we may want to test that EX does
115 // indeed represent a valid exception. That is,
116 // anything but ClassNotFoundException,
117 // or some kind of Error.
122 // if everything proceeded sucessfully, we're loaded.
123 JvAssert (klass->state == JV_STATE_LOADED);
125 // if an exception is generated, this is initially missed.
126 // however, we come back here in handleException0 below...
127 _Jv_MonitorExit (klass);
138 _Jv_WaitForState (jclass klass, int state)
140 if (klass->state >= state)
143 _Jv_MonitorEnter (klass) ;
145 if (state == JV_STATE_LINKED)
147 _Jv_MonitorExit (klass);
148 _Jv_PrepareCompiledClass (klass);
152 java::lang::Thread *self = java::lang::Thread::currentThread();
154 // this is similar to the strategy for class initialization.
155 // if we already hold the lock, just leave.
156 while (klass->state <= state
158 && klass->thread != self)
161 _Jv_MonitorExit (klass);
163 if (klass->state == JV_STATE_ERROR)
165 _Jv_Throw (new java::lang::LinkageError ());
169 // Finish linking a class. Only called from ClassLoader::resolveClass.
171 java::lang::ClassLoader::linkClass0 (java::lang::Class *klass)
173 if (klass->state >= JV_STATE_LINKED)
177 if (_Jv_IsInterpretedClass (klass))
179 _Jv_PrepareClass (klass);
183 _Jv_PrepareCompiledClass (klass);
187 java::lang::ClassLoader::markClassErrorState0 (java::lang::Class *klass)
189 klass->state = JV_STATE_ERROR;
194 /** this is the only native method in VMClassLoader, so
195 we define it here. */
197 gnu::gcj::runtime::VMClassLoader::findSystemClass (jstring name)
199 return _Jv_FindClassInCache (_Jv_makeUtf8Const (name), 0);
203 java::lang::ClassLoader::findLoadedClass (jstring name)
205 return _Jv_FindClassInCache (_Jv_makeUtf8Const (name), this);
208 static const int PUBLIC = 0x001;
209 static const int PRIVATE = 0x002;
210 static const int PROTECTED = 0x004;
211 static const int STATIC = 0x008;
212 static const int FINAL = 0x010;
213 static const int SYNCHRONIZED = 0x020;
214 static const int VOLATILE = 0x040;
215 static const int TRANSIENT = 0x080;
216 static const int NATIVE = 0x100;
217 static const int INTERFACE = 0x200;
218 static const int ABSTRACT = 0x400;
219 static const int ALL_FLAGS = 0x7FF;
222 /** This function does class-preparation for compiled classes.
223 NOTE: It contains replicated functionality from
224 _Jv_ResolvePoolEntry, and this is intentional, since that function
225 lives in resolve.cc which is entirely conditionally compiled.
228 _Jv_PrepareCompiledClass(jclass klass)
230 if (klass->state >= JV_STATE_LINKED)
233 // short-circuit, so that mutually dependent classes are ok
234 klass->state = JV_STATE_LINKED;
236 _Jv_Constants *pool = &klass->constants;
237 for (int index = 1; index < pool->size; ++index)
239 if (pool->tags[index] == JV_CONSTANT_Class)
241 _Jv_Utf8Const *name = pool->data[index].utf8;
244 if (name->data[0] == '[')
245 found = _Jv_FindClassFromSignature (&name->data[0],
248 found = _Jv_FindClass (name, klass->loader);
252 jstring str = _Jv_NewStringUTF (name->data);
253 JvThrow (new java::lang::ClassNotFoundException (str));
256 pool->data[index].clazz = found;
257 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
260 else if (pool->tags[index] == JV_CONSTANT_String)
263 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
264 pool->data[index].o = str;
265 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
274 // A single class can have many "initiating" class loaders,
275 // and a single "defining" class loader. The Defining
276 // class loader is what is returned from Class.getClassLoader()
277 // and is used when loading dependent classes during resolution.
278 // The set of initiating class loaders are used to ensure
279 // safety of linking, and is maintained in the hash table
280 // "initiated_classes". A defining classloader is by definition also
281 // initiating, so we only store classes in this table, if they have more
282 // than one class loader associated.
286 // Size of local hash table.
287 #define HASH_LEN 1013
289 // Hash function for Utf8Consts.
290 #define HASH_UTF(Utf) (((Utf)->hash) % HASH_LEN)
292 struct _Jv_LoaderInfo {
293 _Jv_LoaderInfo *next;
294 java::lang::Class *klass;
295 java::lang::ClassLoader *loader;
298 _Jv_LoaderInfo *initiated_classes[HASH_LEN];
299 jclass loaded_classes[HASH_LEN];
302 _Jv_FindClassInCache (_Jv_Utf8Const *name, java::lang::ClassLoader *loader)
304 _Jv_MonitorEnter (&ClassClass);
305 jint hash = HASH_UTF (name);
307 // first, if LOADER is a defining loader, then it is also initiating
309 for (klass = loaded_classes[hash]; klass; klass = klass->next)
311 if (loader == klass->loader && _Jv_equalUtf8Consts (name, klass->name))
315 // otherwise, it may be that the class in question was defined
316 // by some other loader, but that the loading was initiated by
317 // the loader in question.
320 _Jv_LoaderInfo *info;
321 for (info = initiated_classes[hash]; info; info = info->next)
323 if (loader == info->loader
324 && _Jv_equalUtf8Consts (name, info->klass->name))
332 _Jv_MonitorExit (&ClassClass);
338 _Jv_UnregisterClass (jclass the_class)
340 _Jv_MonitorEnter (&ClassClass);
341 jint hash = HASH_UTF(the_class->name);
343 jclass *klass = &(loaded_classes[hash]);
344 for ( ; *klass; klass = &((*klass)->next))
346 if (*klass == the_class)
348 *klass = (*klass)->next;
353 _Jv_LoaderInfo **info = &(initiated_classes[hash]);
354 for ( ; *info; info = &((*info)->next))
356 while ((*info)->klass == the_class)
358 *info = (*info)->next;
362 _Jv_MonitorExit (&ClassClass);
366 _Jv_RegisterInitiatingLoader (jclass klass, java::lang::ClassLoader *loader)
368 _Jv_LoaderInfo *info = new _Jv_LoaderInfo; // non-gc alloc!
369 jint hash = HASH_UTF(klass->name);
371 _Jv_MonitorEnter (&ClassClass);
372 info->loader = loader;
374 info->next = initiated_classes[hash];
375 initiated_classes[hash] = info;
376 _Jv_MonitorExit (&ClassClass);
380 // This function is called many times during startup, before main() is
381 // run. We do our runtime initialization here the very first time we
382 // are called. At that point in time we know for certain we are
383 // running single-threaded, so we don't need to lock when modifying
384 // `init'. CLASSES is NULL-terminated.
386 _Jv_RegisterClasses (jclass *classes)
388 static bool init = false;
395 _Jv_InitializeSyncMutex ();
398 JvSynchronize sync (&ClassClass);
399 for (; *classes; ++classes)
401 jclass klass = *classes;
402 jint hash = HASH_UTF (klass->name);
403 klass->next = loaded_classes[hash];
404 loaded_classes[hash] = klass;
406 // registering a compiled class causes
407 // it to be immediately "prepared".
408 if (klass->state == JV_STATE_NOTHING)
409 klass->state = JV_STATE_COMPILED;
414 _Jv_RegisterClass (jclass klass)
419 _Jv_RegisterClasses (classes);
423 // NOTE: this one is out of date with the new loader stuff...
425 _Jv_FindClassInCache (jstring name, java::lang::ClassLoader *loader)
427 JvSynchronize sync (&ClassClass);
428 jint hash = name->hashCode();
429 jclass klass = loaded_classes[(_Jv_ushort) hash % HASH_LEN];
430 for ( ; klass; klass = klass->next)
432 if (loader == klass->loader
433 && _Jv_equal (klass->name, name, hash))
436 _Jv_MonitorExit (&ClassClass);
441 jclass _Jv_FindClass (_Jv_Utf8Const *name,
442 java::lang::ClassLoader *loader)
444 jclass klass = _Jv_FindClassInCache (name, loader);
449 jstring sname = _Jv_NewStringUTF (name->data);
453 // Load using a user-defined loader, jvmspec 5.3.2
454 klass = loader->loadClass(sname, false);
456 // if "loader" delegateted the loadClass operation
457 // to another loader, register explicitly
458 // that he is also an initiating loader of the
461 if (klass && (klass->getClassLoader () != loader))
462 _Jv_RegisterInitiatingLoader (klass, 0);
466 if (redirect == NULL)
468 _Jv_InitClass (&ClassLoaderClass);
469 java::lang::ClassLoader::getSystemClassLoader ();
472 // Load using the bootstrap loader jmspec 5.3.1
473 klass = redirect -> loadClass (sname, false);
475 // register that we're an initiating loader
478 _Jv_RegisterInitiatingLoader (klass, 0);
484 // we need classes to be in the hash while
485 // we're loading, so that they can refer to themselves.
486 _Jv_WaitForState (klass, JV_STATE_LOADED);
494 // NOTE: this one is out of date with the new class loader stuff...
496 _Jv_FindClass (jstring name, java::lang::ClassLoader *loader)
498 jclass klass = _Jv_FindClassInCache (name, loader);
503 klass = loader->loadClass(name);
509 // delegate to the system loader
510 klass = java::lang::ClassLoader::system.loadClass (sname);
512 // register that we're an initiating loader
514 _Jv_RegisterInitiatingLoader (klass, 0);
519 _Jv_WaitForState (klass, JV_STATE_LOADED);
527 _Jv_NewClass (_Jv_Utf8Const *name, jclass superclass,
528 java::lang::ClassLoader *loader)
530 jclass ret = (jclass) JvAllocObject (&ClassClass);
535 ret->superclass = superclass;
536 ret->constants.size = 0;
537 ret->constants.tags = NULL;
538 ret->constants.data = NULL;
540 ret->method_count = 0;
541 ret->vtable_method_count = 0;
543 ret->size_in_bytes = 0;
544 ret->field_count = 0;
545 ret->static_field_count = 0;
547 ret->interfaces = NULL;
548 ret->loader = loader;
549 ret->interface_count = 0;
550 ret->state = JV_STATE_NOTHING;
553 _Jv_RegisterClass (ret);
559 _Jv_FindArrayClass (jclass element, java::lang::ClassLoader *loader)
561 _Jv_Utf8Const *array_name;
563 if (element->isPrimitive())
565 // For primitive types the array is cached in the class.
566 jclass ret = (jclass) element->methods;
572 len = element->name->length + 5;
577 signature[index++] = '[';
578 // Compute name of array class to see if we've already cached it.
579 if (element->isPrimitive())
581 signature[index++] = (char) element->method_count;
585 size_t length = element->name->length;
586 const char *const name = element->name->data;
588 signature[index++] = 'L';
589 memcpy (&signature[index], name, length);
592 signature[index++] = ';';
594 array_name = _Jv_makeUtf8Const (signature, index);
597 jclass array_class = _Jv_FindClassInCache (array_name, element->loader);
601 // Create new array class.
602 array_class = _Jv_NewClass (array_name, &ObjectClass, element->loader);
604 // Note that `vtable_method_count' doesn't include the initial
606 int dm_count = ObjectClass.vtable_method_count + 1;
608 // Create a new vtable by copying Object's vtable (except the
609 // class pointer, of course). Note that we allocate this as
610 // unscanned memory -- the vtables are handled specially by the
612 int size = (sizeof (_Jv_VTable) +
613 ((dm_count - 1) * sizeof (void *)));
614 _Jv_VTable *vtable = (_Jv_VTable *) _Jv_AllocBytes (size);
615 vtable->clas = array_class;
616 memcpy (vtable->method, ObjectClass.vtable->method,
617 dm_count * sizeof (void *));
618 array_class->vtable = vtable;
619 array_class->vtable_method_count = ObjectClass.vtable_method_count;
621 // Stash the pointer to the element type.
622 array_class->methods = (_Jv_Method *) element;
624 // Register our interfaces.
625 // FIXME: for JDK 1.2 we need Serializable.
626 static jclass interfaces[] = { &CloneableClass };
627 array_class->interfaces = interfaces;
628 array_class->interface_count = 1;
630 // as per vmspec 5.3.3.2
631 array_class->accflags = element->accflags;
633 // FIXME: initialize other Class instance variables,
636 // say this class is initialized and ready to go!
637 array_class->state = JV_STATE_DONE;
639 // vmspec, section 5.3.3 describes this
640 if (element->loader != loader)
641 _Jv_RegisterInitiatingLoader (array_class, loader);
644 // For primitive types, point back at this array.
645 if (element->isPrimitive())
646 element->methods = (_Jv_Method *) array_class;