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> */
22 #include <java-threads.h>
23 #include <java-interp.h>
25 #include <java/lang/Character.h>
26 #include <java/lang/Thread.h>
27 #include <java/lang/ClassLoader.h>
28 #include <gnu/gcj/runtime/VMClassLoader.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/VirtualMachineError.h>
37 #include <java/lang/VMClassLoader.h>
38 #include <java/lang/reflect/Modifier.h>
39 #include <java/lang/Runtime.h>
40 #include <java/lang/StringBuffer.h>
41 #include <java/io/Serializable.h>
42 #include <java/lang/Cloneable.h>
45 _Jv_WaitForState (jclass klass
, int state
)
47 if (klass
->state
>= state
)
50 _Jv_MonitorEnter (klass
) ;
52 if (klass
->state
== JV_STATE_COMPILED
)
54 klass
->state
= JV_STATE_LOADED
;
55 if (gcj::verbose_class_flag
)
56 fprintf (stderr
, "[Loaded (pre-compiled) %s]\n", klass
->name
->chars());
58 if (state
== JV_STATE_LINKED
)
60 // Must call _Jv_PrepareCompiledClass while holding the class
63 if (_Jv_IsInterpretedClass (klass
))
64 _Jv_PrepareClass (klass
);
66 _Jv_PrepareCompiledClass (klass
);
67 _Jv_MonitorExit (klass
);
71 java::lang::Thread
*self
= java::lang::Thread::currentThread();
73 // this is similar to the strategy for class initialization.
74 // if we already hold the lock, just leave.
75 while (klass
->state
<= state
77 && klass
->thread
!= self
)
80 _Jv_MonitorExit (klass
);
82 if (klass
->state
== JV_STATE_ERROR
)
83 throw new java::lang::LinkageError
;
86 typedef unsigned int uaddr
__attribute__ ((mode (pointer
)));
88 /** This function does class-preparation for compiled classes.
89 NOTE: It contains replicated functionality from
90 _Jv_ResolvePoolEntry, and this is intentional, since that function
91 lives in resolve.cc which is entirely conditionally compiled.
94 _Jv_PrepareCompiledClass (jclass klass
)
96 jint state
= klass
->state
;
97 if (state
>= JV_STATE_LINKED
)
100 // Short-circuit, so that mutually dependent classes are ok.
101 klass
->state
= JV_STATE_LINKED
;
103 _Jv_Constants
*pool
= &klass
->constants
;
105 // Resolve class constants first, since other constant pool
106 // entries may rely on these.
107 for (int index
= 1; index
< pool
->size
; ++index
)
109 if (pool
->tags
[index
] == JV_CONSTANT_Class
)
111 _Jv_Utf8Const
*name
= pool
->data
[index
].utf8
;
114 if (name
->first() == '[')
115 found
= _Jv_FindClassFromSignature (name
->chars(),
118 found
= _Jv_FindClass (name
, klass
->loader
);
122 jstring str
= name
->toString();
123 throw new java::lang::NoClassDefFoundError (str
);
126 pool
->data
[index
].clazz
= found
;
127 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
131 // If superclass looks like a constant pool entry,
133 if ((uaddr
) klass
->superclass
< pool
->size
)
134 klass
->superclass
= pool
->data
[(uaddr
) klass
->superclass
].clazz
;
136 // Likewise for interfaces.
137 for (int i
= 0; i
< klass
->interface_count
; i
++)
138 if ((uaddr
) klass
->interfaces
[i
] < pool
->size
)
139 klass
->interfaces
[i
] = pool
->data
[(uaddr
) klass
->interfaces
[i
]].clazz
;
141 // Resolve the remaining constant pool entries.
142 for (int index
= 1; index
< pool
->size
; ++index
)
144 if (pool
->tags
[index
] == JV_CONSTANT_String
)
148 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
149 pool
->data
[index
].o
= str
;
150 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
155 // FIXME: although the comment up top says that this function is
156 // only called for compiled classes, it is actually called for every
158 if (! _Jv_IsInterpretedClass (klass
))
160 #endif /* INTERPRETER */
161 jfieldID f
= JvGetFirstStaticField (klass
);
162 for (int n
= JvNumStaticFields (klass
); n
> 0; --n
)
164 int mod
= f
->getModifiers ();
165 // If we have a static String field with a non-null initial
166 // value, we know it points to a Utf8Const.
167 _Jv_ResolveField(f
, klass
->loader
);
168 if (f
->getClass () == &java::lang::String::class$
169 && java::lang::reflect::Modifier::isStatic (mod
))
171 jstring
*strp
= (jstring
*) f
->u
.addr
;
173 *strp
= _Jv_NewStringUtf8Const ((_Jv_Utf8Const
*) *strp
);
175 f
= f
->getNextField ();
179 #endif /* INTERPRETER */
181 if (klass
->isInterface ())
182 _Jv_LayoutInterfaceMethods (klass
);
184 if (state
== JV_STATE_COMPILED
&& gcj::verbose_class_flag
)
185 fprintf (stderr
, "[Loaded (pre-compiled) %s]\n",
186 klass
->name
->chars());
190 _Jv_PushClass (klass
);
195 // A single class can have many "initiating" class loaders,
196 // and a single "defining" class loader. The Defining
197 // class loader is what is returned from Class.getClassLoader()
198 // and is used when loading dependent classes during resolution.
199 // The set of initiating class loaders are used to ensure
200 // safety of linking, and is maintained in the hash table
201 // "initiated_classes". A defining classloader is by definition also
202 // initiating, so we only store classes in this table if they have more
203 // than one class loader associated.
207 // Size of local hash table.
208 #define HASH_LEN 1013
210 // Hash function for Utf8Consts.
211 #define HASH_UTF(Utf) ((Utf)->hash16() % HASH_LEN)
213 struct _Jv_LoaderInfo
215 _Jv_LoaderInfo
*next
;
216 java::lang::Class
*klass
;
217 java::lang::ClassLoader
*loader
;
220 static _Jv_LoaderInfo
*initiated_classes
[HASH_LEN
];
221 static jclass loaded_classes
[HASH_LEN
];
223 // This is the root of a linked list of classes
228 _Jv_FindClassInCache (_Jv_Utf8Const
*name
, java::lang::ClassLoader
*loader
)
230 JvSynchronize
sync (&java::lang::Class::class$
);
231 jint hash
= HASH_UTF (name
);
233 if (loader
&& loader
== java::lang::ClassLoader::getSystemClassLoader())
236 // first, if LOADER is a defining loader, then it is also initiating
238 for (klass
= loaded_classes
[hash
]; klass
; klass
= klass
->next
)
240 if (loader
== klass
->loader
&& _Jv_equalUtf8Consts (name
, klass
->name
))
244 // otherwise, it may be that the class in question was defined
245 // by some other loader, but that the loading was initiated by
246 // the loader in question.
249 _Jv_LoaderInfo
*info
;
250 for (info
= initiated_classes
[hash
]; info
; info
= info
->next
)
252 if (loader
== info
->loader
253 && _Jv_equalUtf8Consts (name
, info
->klass
->name
))
265 _Jv_UnregisterClass (jclass the_class
)
267 JvSynchronize
sync (&java::lang::Class::class$
);
268 jint hash
= HASH_UTF(the_class
->name
);
270 jclass
*klass
= &(loaded_classes
[hash
]);
271 for ( ; *klass
; klass
= &((*klass
)->next
))
273 if (*klass
== the_class
)
275 *klass
= (*klass
)->next
;
280 _Jv_LoaderInfo
**info
= &(initiated_classes
[hash
]);
281 for ( ; ; info
= &((*info
)->next
))
283 while (*info
&& (*info
)->klass
== the_class
)
285 _Jv_LoaderInfo
*old
= *info
;
286 *info
= (*info
)->next
;
296 _Jv_RegisterInitiatingLoader (jclass klass
, java::lang::ClassLoader
*loader
)
298 if (loader
&& loader
== java::lang::ClassLoader::getSystemClassLoader())
301 // This information can't be visible to the GC.
303 = (_Jv_LoaderInfo
*) _Jv_Malloc (sizeof(_Jv_LoaderInfo
));
304 jint hash
= HASH_UTF(klass
->name
);
306 JvSynchronize
sync (&java::lang::Class::class$
);
307 info
->loader
= loader
;
309 info
->next
= initiated_classes
[hash
];
310 initiated_classes
[hash
] = info
;
313 // This function is called many times during startup, before main() is
314 // run. At that point in time we know for certain we are running
315 // single-threaded, so we don't need to lock when adding classes to the
316 // class chain. At all other times, the caller should synchronize on
319 _Jv_RegisterClasses (const jclass
*classes
)
321 for (; *classes
; ++classes
)
323 jclass klass
= *classes
;
325 (*_Jv_RegisterClassHook
) (klass
);
327 // registering a compiled class causes
328 // it to be immediately "prepared".
329 if (klass
->state
== JV_STATE_NOTHING
)
330 klass
->state
= JV_STATE_COMPILED
;
334 // This is a version of _Jv_RegisterClasses that takes a count.
336 _Jv_RegisterClasses_Counted (const jclass
* classes
, size_t count
)
339 for (i
= 0; i
< count
; i
++)
341 jclass klass
= classes
[i
];
343 (*_Jv_RegisterClassHook
) (klass
);
345 // registering a compiled class causes
346 // it to be immediately "prepared".
347 if (klass
->state
== JV_STATE_NOTHING
)
348 klass
->state
= JV_STATE_COMPILED
;
353 _Jv_RegisterClassHookDefault (jclass klass
)
355 jint hash
= HASH_UTF (klass
->name
);
357 jclass check_class
= loaded_classes
[hash
];
359 // If the class is already registered, don't re-register it.
360 while (check_class
!= NULL
)
362 if (check_class
== klass
)
364 // If you get this, it means you have the same class in two
365 // different libraries.
366 #define TEXT "Duplicate class registration: "
367 // We size-limit MESSAGE so that you can't trash the stack.
369 strcpy (message
, TEXT
);
370 strncpy (message
+ sizeof (TEXT
) - 1, klass
->name
->chars(),
371 sizeof (message
) - sizeof (TEXT
));
372 message
[sizeof (message
) - 1] = '\0';
373 if (! gcj::runtimeInitialized
)
377 java::lang::String
*str
= JvNewStringLatin1 (message
);
378 throw new java::lang::VirtualMachineError (str
);
382 check_class
= check_class
->next
;
385 klass
->next
= loaded_classes
[hash
];
386 loaded_classes
[hash
] = klass
;
389 // A pointer to a function that actually registers a class.
390 // Normally _Jv_RegisterClassHookDefault, but could be some other function
391 // that registers the class in e.g. a ClassLoader-local table.
392 // Should synchronize on Class:class$ while setting/restore this variable.
394 void (*_Jv_RegisterClassHook
) (jclass cl
) = _Jv_RegisterClassHookDefault
;
397 _Jv_RegisterClass (jclass klass
)
402 _Jv_RegisterClasses (classes
);
406 _Jv_FindClass (_Jv_Utf8Const
*name
, java::lang::ClassLoader
*loader
)
408 jclass klass
= _Jv_FindClassInCache (name
, loader
);
412 jstring sname
= name
->toString();
414 java::lang::ClassLoader
*sys
415 = java::lang::ClassLoader::getSystemClassLoader ();
419 // Load using a user-defined loader, jvmspec 5.3.2
420 klass
= loader
->loadClass(sname
, false);
422 // If "loader" delegated the loadClass operation to another
423 // loader, explicitly register that it is also an initiating
424 // loader of the given class.
425 java::lang::ClassLoader
*delegate
= (loader
== sys
428 if (klass
&& klass
->getClassLoaderInternal () != delegate
)
429 _Jv_RegisterInitiatingLoader (klass
, loader
);
433 // Load using the bootstrap loader jvmspec 5.3.1.
434 klass
= sys
->loadClass (sname
, false);
436 // Register that we're an initiating loader.
438 _Jv_RegisterInitiatingLoader (klass
, 0);
443 // we need classes to be in the hash while
444 // we're loading, so that they can refer to themselves.
445 _Jv_WaitForState (klass
, JV_STATE_LOADED
);
452 _Jv_NewClass (_Jv_Utf8Const
*name
, jclass superclass
,
453 java::lang::ClassLoader
*loader
)
455 jclass ret
= (jclass
) _Jv_AllocObject (&java::lang::Class::class$
);
457 ret
->superclass
= superclass
;
458 ret
->loader
= loader
;
460 _Jv_RegisterClass (ret
);
465 static _Jv_IDispatchTable
*array_idt
= NULL
;
466 static jshort array_depth
= 0;
467 static jclass
*array_ancestors
= NULL
;
469 // Create a class representing an array of ELEMENT and store a pointer to it
470 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
471 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
472 // array class. This parameter is optional.
474 _Jv_NewArrayClass (jclass element
, java::lang::ClassLoader
*loader
,
475 _Jv_VTable
*array_vtable
)
477 JvSynchronize
sync (element
);
479 _Jv_Utf8Const
*array_name
;
482 if (element
->arrayclass
)
485 if (element
->isPrimitive())
487 if (element
== JvPrimClass (void))
488 throw new java::lang::ClassNotFoundException ();
492 len
= element
->name
->len() + 5;
497 signature
[index
++] = '[';
498 // Compute name of array class.
499 if (element
->isPrimitive())
501 signature
[index
++] = (char) element
->method_count
;
505 size_t length
= element
->name
->len();
506 const char *const name
= element
->name
->chars();
508 signature
[index
++] = 'L';
509 memcpy (&signature
[index
], name
, length
);
512 signature
[index
++] = ';';
514 array_name
= _Jv_makeUtf8Const (signature
, index
);
517 // Create new array class.
518 jclass array_class
= _Jv_NewClass (array_name
, &java::lang::Object::class$
,
521 // Note that `vtable_method_count' doesn't include the initial
523 JvAssert (java::lang::Object::class$
.vtable_method_count
524 == NUM_OBJECT_METHODS
);
525 int dm_count
= java::lang::Object::class$
.vtable_method_count
;
527 // Create a new vtable by copying Object's vtable.
530 vtable
= array_vtable
;
532 vtable
= _Jv_VTable::new_vtable (dm_count
);
533 vtable
->clas
= array_class
;
534 vtable
->gc_descr
= java::lang::Object::class$
.vtable
->gc_descr
;
535 for (int i
= 0; i
< dm_count
; ++i
)
536 vtable
->set_method (i
, java::lang::Object::class$
.vtable
->get_method (i
));
538 array_class
->vtable
= vtable
;
539 array_class
->vtable_method_count
540 = java::lang::Object::class$
.vtable_method_count
;
542 // Stash the pointer to the element type.
543 array_class
->methods
= (_Jv_Method
*) element
;
545 // Register our interfaces.
546 static jclass interfaces
[] =
548 &java::lang::Cloneable::class$
,
549 &java::io::Serializable::class$
551 array_class
->interfaces
= interfaces
;
552 array_class
->interface_count
= sizeof interfaces
/ sizeof interfaces
[0];
554 // Since all array classes have the same interface dispatch table, we can
555 // cache one and reuse it. It is not necessary to synchronize this.
558 _Jv_PrepareConstantTimeTables (array_class
);
559 array_idt
= array_class
->idt
;
560 array_depth
= array_class
->depth
;
561 array_ancestors
= array_class
->ancestors
;
565 array_class
->idt
= array_idt
;
566 array_class
->depth
= array_depth
;
567 array_class
->ancestors
= array_ancestors
;
570 using namespace java::lang::reflect
;
572 // Array classes are "abstract final"...
573 _Jv_ushort accflags
= Modifier::FINAL
| Modifier::ABSTRACT
;
574 // ... and inherit accessibility from element type, per vmspec 5.3.3.2
575 accflags
|= (element
->accflags
& Modifier::PUBLIC
);
576 accflags
|= (element
->accflags
& Modifier::PROTECTED
);
577 accflags
|= (element
->accflags
& Modifier::PRIVATE
);
578 array_class
->accflags
= accflags
;
581 // An array class has no visible instance fields. "length" is invisible to
584 // say this class is initialized and ready to go!
585 array_class
->state
= JV_STATE_DONE
;
587 // vmspec, section 5.3.3 describes this
588 if (element
->loader
!= loader
)
589 _Jv_RegisterInitiatingLoader (array_class
, loader
);
591 element
->arrayclass
= array_class
;
594 static jclass stack_head
;
596 // These two functions form a stack of classes. When a class is loaded
597 // it is pushed onto the stack by the class loader; this is so that
598 // StackTrace can quickly determine which classes have been loaded.
603 JvSynchronize
sync (&java::lang::Class::class$
);
606 jclass tmp
= stack_head
;
607 stack_head
= tmp
->chain
;
614 _Jv_PushClass (jclass k
)
616 JvSynchronize
sync (&java::lang::Class::class$
);
617 jclass tmp
= stack_head
;