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 // If we found an error while defining an interpreted class, we must
114 // go back and unregister it.
116 _Jv_UnregisterInitiatingLoader (jclass klass
, java::lang::ClassLoader
*loader
)
119 loader
= java::lang::ClassLoader::getSystemClassLoader();
120 loader
->loadedClasses
->remove(klass
->name
->toString());
123 // This function is called many times during startup, before main() is
124 // run. At that point in time we know for certain we are running
125 // single-threaded, so we don't need to lock when adding classes to the
126 // class chain. At all other times, the caller should synchronize on
129 _Jv_RegisterClasses (const jclass
*classes
)
131 for (; *classes
; ++classes
)
133 jclass klass
= *classes
;
135 if (_Jv_CheckABIVersion ((unsigned long) klass
->next_or_version
))
136 (*_Jv_RegisterClassHook
) (klass
);
140 // This is a version of _Jv_RegisterClasses that takes a count.
142 _Jv_RegisterClasses_Counted (const jclass
* classes
, size_t count
)
145 for (i
= 0; i
< count
; i
++)
147 jclass klass
= classes
[i
];
149 if (_Jv_CheckABIVersion ((unsigned long) klass
->next_or_version
))
150 (*_Jv_RegisterClassHook
) (klass
);
155 _Jv_RegisterClassHookDefault (jclass klass
)
157 jint hash
= HASH_UTF (klass
->name
);
159 // If the class is already registered, don't re-register it.
160 for (jclass check_class
= loaded_classes
[hash
];
162 check_class
= check_class
->next_or_version
)
164 if (check_class
== klass
)
166 // If you get this, it means you have the same class in two
167 // different libraries.
168 #define TEXT "Duplicate class registration: "
169 // We size-limit MESSAGE so that you can't trash the stack.
171 strcpy (message
, TEXT
);
172 strncpy (message
+ sizeof (TEXT
) - 1, klass
->name
->chars(),
173 sizeof (message
) - sizeof (TEXT
));
174 message
[sizeof (message
) - 1] = '\0';
175 if (! gcj::runtimeInitialized
)
179 java::lang::String
*str
= JvNewStringLatin1 (message
);
180 throw new java::lang::VirtualMachineError (str
);
185 // FIXME: this is really bogus!
187 klass
->engine
= &_Jv_soleCompiledEngine
;
188 klass
->next_or_version
= loaded_classes
[hash
];
189 loaded_classes
[hash
] = klass
;
192 // A pointer to a function that actually registers a class.
193 // Normally _Jv_RegisterClassHookDefault, but could be some other function
194 // that registers the class in e.g. a ClassLoader-local table.
195 // Should synchronize on Class:class$ while setting/restore this variable.
197 void (*_Jv_RegisterClassHook
) (jclass cl
) = _Jv_RegisterClassHookDefault
;
200 _Jv_RegisterClass (jclass klass
)
205 _Jv_RegisterClasses (classes
);
209 _Jv_FindClass (_Jv_Utf8Const
*name
, java::lang::ClassLoader
*loader
)
211 // See if the class was already loaded by this loader. This handles
212 // initiating loader checks, as we register classes with their
213 // initiating loaders.
214 java::lang::ClassLoader
*sys
215 = java::lang::ClassLoader::getSystemClassLoader ();
216 java::lang::ClassLoader
*real
= loader
;
219 jstring sname
= name
->toString();
220 // We might still be bootstrapping the VM, in which case there
221 // won't be a system class loader yet.
222 jclass klass
= real
? real
->findLoadedClass (sname
) : NULL
;
228 // Load using a user-defined loader, jvmspec 5.3.2
229 klass
= loader
->loadClass(sname
, false);
231 // If "loader" delegated the loadClass operation to another
232 // loader, explicitly register that it is also an initiating
233 // loader of the given class.
234 java::lang::ClassLoader
*delegate
= (loader
== sys
237 if (klass
&& klass
->getClassLoaderInternal () != delegate
)
238 _Jv_RegisterInitiatingLoader (klass
, loader
);
242 // Load using the bootstrap loader jvmspec 5.3.1.
243 klass
= sys
->loadClass (sname
, false);
245 // Register that we're an initiating loader.
247 _Jv_RegisterInitiatingLoader (klass
, 0);
251 // Not even a bootstrap loader, try the built-in cache.
252 klass
= _Jv_FindClassInCache (name
);
254 if (bootstrap_index
== BOOTSTRAP_CLASS_LIST_SIZE
)
256 bootstrap_class_list
[bootstrap_index
++] = klass
;
261 // we need classes to be in the hash while
262 // we're loading, so that they can refer to themselves.
263 _Jv_Linker::wait_for_state (klass
, JV_STATE_LOADED
);
270 _Jv_RegisterBootstrapPackages ()
272 for (int i
= 0; i
< bootstrap_index
; ++i
)
273 java::lang::VMClassLoader::definePackageForNative(bootstrap_class_list
[i
]->getName());
277 _Jv_NewClass (_Jv_Utf8Const
*name
, jclass superclass
,
278 java::lang::ClassLoader
*loader
)
280 jclass ret
= (jclass
) _Jv_AllocObject (&java::lang::Class::class$
);
282 ret
->superclass
= superclass
;
283 ret
->loader
= loader
;
285 _Jv_RegisterClass (ret
);
290 static _Jv_IDispatchTable
*array_idt
= NULL
;
291 static jshort array_depth
= 0;
292 static jclass
*array_ancestors
= NULL
;
294 // Create a class representing an array of ELEMENT and store a pointer to it
295 // in element->arrayclass. LOADER is the ClassLoader which _initiated_ the
296 // instantiation of this array. ARRAY_VTABLE is the vtable to use for the new
297 // array class. This parameter is optional.
299 _Jv_NewArrayClass (jclass element
, java::lang::ClassLoader
*loader
,
300 _Jv_VTable
*array_vtable
)
302 JvSynchronize
sync (element
);
304 _Jv_Utf8Const
*array_name
;
307 if (element
->arrayclass
)
310 if (element
->isPrimitive())
312 if (element
== JvPrimClass (void))
313 throw new java::lang::ClassNotFoundException ();
317 len
= element
->name
->len() + 5;
322 signature
[index
++] = '[';
323 // Compute name of array class.
324 if (element
->isPrimitive())
326 signature
[index
++] = (char) element
->method_count
;
330 size_t length
= element
->name
->len();
331 const char *const name
= element
->name
->chars();
333 signature
[index
++] = 'L';
334 memcpy (&signature
[index
], name
, length
);
337 signature
[index
++] = ';';
339 array_name
= _Jv_makeUtf8Const (signature
, index
);
342 // Create new array class.
343 jclass array_class
= _Jv_NewClass (array_name
, &java::lang::Object::class$
,
346 // Note that `vtable_method_count' doesn't include the initial
348 JvAssert (java::lang::Object::class$
.vtable_method_count
349 == NUM_OBJECT_METHODS
);
350 int dm_count
= java::lang::Object::class$
.vtable_method_count
;
352 // Create a new vtable by copying Object's vtable.
355 vtable
= array_vtable
;
357 vtable
= _Jv_VTable::new_vtable (dm_count
);
358 vtable
->clas
= array_class
;
359 vtable
->gc_descr
= java::lang::Object::class$
.vtable
->gc_descr
;
360 for (int i
= 0; i
< dm_count
; ++i
)
361 vtable
->set_method (i
, java::lang::Object::class$
.vtable
->get_method (i
));
363 array_class
->vtable
= vtable
;
364 array_class
->vtable_method_count
365 = java::lang::Object::class$
.vtable_method_count
;
367 // Stash the pointer to the element type.
368 array_class
->methods
= (_Jv_Method
*) element
;
370 // Register our interfaces.
371 static jclass interfaces
[] =
373 &java::lang::Cloneable::class$
,
374 &java::io::Serializable::class$
376 array_class
->interfaces
= interfaces
;
377 array_class
->interface_count
= sizeof interfaces
/ sizeof interfaces
[0];
379 // Since all array classes have the same interface dispatch table, we can
380 // cache one and reuse it. It is not necessary to synchronize this.
383 _Jv_Linker::wait_for_state(array_class
, JV_STATE_PREPARED
);
384 array_idt
= array_class
->idt
;
385 array_depth
= array_class
->depth
;
386 array_ancestors
= array_class
->ancestors
;
390 array_class
->idt
= array_idt
;
391 array_class
->depth
= array_depth
;
392 array_class
->ancestors
= array_ancestors
;
395 using namespace java::lang::reflect
;
397 // Array classes are "abstract final" and inherit accessibility
398 // from element type, per vmspec 5.3.3.2
399 _Jv_ushort accflags
= (Modifier::FINAL
| Modifier::ABSTRACT
401 & (Modifier::PUBLIC
| Modifier::PROTECTED
402 | Modifier::PRIVATE
)));
403 array_class
->accflags
= accflags
;
406 // An array class has no visible instance fields. "length" is invisible to
409 // Say this class is initialized and ready to go!
410 array_class
->state
= JV_STATE_DONE
;
412 // vmspec, section 5.3.3 describes this
413 if (element
->loader
!= loader
)
414 _Jv_RegisterInitiatingLoader (array_class
, loader
);
416 element
->arrayclass
= array_class
;
419 // These two functions form a stack of classes. When a class is loaded
420 // it is pushed onto the stack by the class loader; this is so that
421 // StackTrace can quickly determine which classes have been loaded.
426 JvSynchronize
sync (&java::lang::Class::class$
);
429 jclass tmp
= stack_head
;
430 stack_head
= tmp
->chain
;
437 _Jv_PushClass (jclass k
)
439 JvSynchronize
sync (&java::lang::Class::class$
);
440 jclass tmp
= stack_head
;