1 // natClass.cc - Implementation of java.lang.Class native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 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
16 #pragma implementation "Class.h"
20 #include <java-threads.h>
22 #include <java/lang/Class.h>
23 #include <java/lang/ClassLoader.h>
24 #include <java/lang/String.h>
25 #include <java/lang/reflect/Modifier.h>
26 #include <java/lang/reflect/Member.h>
27 #include <java/lang/reflect/Method.h>
28 #include <java/lang/reflect/Field.h>
29 #include <java/lang/reflect/Constructor.h>
30 #include <java/lang/AbstractMethodError.h>
31 #include <java/lang/ArrayStoreException.h>
32 #include <java/lang/ClassCastException.h>
33 #include <java/lang/ClassNotFoundException.h>
34 #include <java/lang/ExceptionInInitializerError.h>
35 #include <java/lang/IllegalAccessException.h>
36 #include <java/lang/IllegalAccessError.h>
37 #include <java/lang/IllegalArgumentException.h>
38 #include <java/lang/IncompatibleClassChangeError.h>
39 #include <java/lang/NoSuchFieldError.h>
40 #include <java/lang/ArrayIndexOutOfBoundsException.h>
41 #include <java/lang/InstantiationException.h>
42 #include <java/lang/NoClassDefFoundError.h>
43 #include <java/lang/NoSuchFieldException.h>
44 #include <java/lang/NoSuchMethodError.h>
45 #include <java/lang/NoSuchMethodException.h>
46 #include <java/lang/Thread.h>
47 #include <java/lang/NullPointerException.h>
48 #include <java/lang/RuntimePermission.h>
49 #include <java/lang/System.h>
50 #include <java/lang/SecurityManager.h>
51 #include <java/lang/StringBuffer.h>
52 #include <java/lang/VMClassLoader.h>
53 #include <gnu/gcj/runtime/StackTrace.h>
54 #include <gcj/method.h>
55 #include <gnu/gcj/runtime/MethodRef.h>
56 #include <gnu/gcj/RawData.h>
58 #include <java-cpool.h>
65 java::lang::Class::forName (jstring className, jboolean initialize,
66 java::lang::ClassLoader *loader)
69 throw new java::lang::NullPointerException;
71 jsize length = _Jv_GetStringUTFLength (className);
73 _Jv_GetStringUTFRegion (className, 0, length, buffer);
75 _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
77 if (! _Jv_VerifyClassName (name))
78 throw new java::lang::ClassNotFoundException (className);
80 jclass klass = (buffer[0] == '['
81 ? _Jv_FindClassFromSignature (name->data, loader)
82 : _Jv_FindClass (name, loader));
85 throw new java::lang::ClassNotFoundException (className);
88 _Jv_InitClass (klass);
94 java::lang::Class::forName (jstring className)
96 java::lang::ClassLoader *loader = NULL;
97 gnu::gcj::runtime::StackTrace *t
98 = new gnu::gcj::runtime::StackTrace(4);
99 java::lang::Class *klass = NULL;
102 for (int i = 1; !klass; i++)
104 klass = t->classAt (i);
106 loader = klass->getClassLoaderInternal();
108 catch (::java::lang::ArrayIndexOutOfBoundsException *e)
112 return forName (className, true, loader);
115 java::lang::ClassLoader *
116 java::lang::Class::getClassLoader (void)
118 java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
121 gnu::gcj::runtime::StackTrace *t
122 = new gnu::gcj::runtime::StackTrace(4);
123 Class *caller = NULL;
124 ClassLoader *caller_loader = NULL;
127 for (int i = 1; !caller; i++)
129 caller = t->classAt (i);
131 caller_loader = caller->getClassLoaderInternal();
133 catch (::java::lang::ArrayIndexOutOfBoundsException *e)
137 // If the caller has a non-null class loader, and that loader
138 // is not this class' loader or an ancestor thereof, then do a
140 if (caller_loader != NULL && ! caller_loader->isAncestorOf(loader))
141 s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
144 // The spec requires us to return `null' for primitive classes. In
145 // other cases we have the option of returning `null' for classes
146 // loaded with the bootstrap loader. All gcj-compiled classes which
147 // are linked into the application used to return `null' here, but
148 // that confuses some poorly-written applications. It is a useful
149 // and apparently harmless compatibility hack to simply never return
153 return loader ? loader : ClassLoader::getSystemClassLoader ();
156 java::lang::reflect::Constructor *
157 java::lang::Class::getConstructor (JArray<jclass> *param_types)
159 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
161 jstring partial_sig = getSignature (param_types, true);
162 jint hash = partial_sig->hashCode ();
164 int i = isPrimitive () ? 0 : method_count;
167 if (_Jv_equalUtf8Consts (methods[i].name, init_name)
168 && _Jv_equal (methods[i].signature, partial_sig, hash))
170 // Found it. For getConstructor, the constructor must be
172 using namespace java::lang::reflect;
173 if (! Modifier::isPublic(methods[i].accflags))
175 Constructor *cons = new Constructor ();
176 cons->offset = (char *) (&methods[i]) - (char *) methods;
177 cons->declaringClass = this;
181 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
184 JArray<java::lang::reflect::Constructor *> *
185 java::lang::Class::_getConstructors (jboolean declared)
187 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
189 int numConstructors = 0;
190 int max = isPrimitive () ? 0 : method_count;
192 for (i = max; --i >= 0; )
194 _Jv_Method *method = &methods[i];
195 if (method->name == NULL
196 || ! _Jv_equalUtf8Consts (method->name, init_name))
199 && ! java::lang::reflect::Modifier::isPublic(method->accflags))
203 JArray<java::lang::reflect::Constructor *> *result
204 = (JArray<java::lang::reflect::Constructor *> *)
205 JvNewObjectArray (numConstructors,
206 &java::lang::reflect::Constructor::class$,
208 java::lang::reflect::Constructor** cptr = elements (result);
209 for (i = 0; i < max; i++)
211 _Jv_Method *method = &methods[i];
212 if (method->name == NULL
213 || ! _Jv_equalUtf8Consts (method->name, init_name))
216 && ! java::lang::reflect::Modifier::isPublic(method->accflags))
218 java::lang::reflect::Constructor *cons
219 = new java::lang::reflect::Constructor ();
220 cons->offset = (char *) method - (char *) methods;
221 cons->declaringClass = this;
227 java::lang::reflect::Constructor *
228 java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
230 memberAccessCheck(java::lang::reflect::Member::DECLARED);
232 jstring partial_sig = getSignature (param_types, true);
233 jint hash = partial_sig->hashCode ();
235 int i = isPrimitive () ? 0 : method_count;
238 if (_Jv_equalUtf8Consts (methods[i].name, init_name)
239 && _Jv_equal (methods[i].signature, partial_sig, hash))
242 using namespace java::lang::reflect;
243 Constructor *cons = new Constructor ();
244 cons->offset = (char *) (&methods[i]) - (char *) methods;
245 cons->declaringClass = this;
249 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
252 java::lang::reflect::Field *
253 java::lang::Class::getField (jstring name, jint hash)
255 java::lang::reflect::Field* rfield;
256 for (int i = 0; i < field_count; i++)
258 _Jv_Field *field = &fields[i];
259 if (! _Jv_equal (field->name, name, hash))
261 if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
263 rfield = new java::lang::reflect::Field ();
264 rfield->offset = (char*) field - (char*) fields;
265 rfield->declaringClass = this;
269 jclass superclass = getSuperclass();
270 if (superclass == NULL)
272 rfield = superclass->getField(name, hash);
273 for (int i = 0; i < interface_count && rfield == NULL; ++i)
274 rfield = interfaces[i]->getField (name, hash);
278 java::lang::reflect::Field *
279 java::lang::Class::getDeclaredField (jstring name)
281 memberAccessCheck(java::lang::reflect::Member::DECLARED);
282 int hash = name->hashCode();
283 for (int i = 0; i < field_count; i++)
285 _Jv_Field *field = &fields[i];
286 if (! _Jv_equal (field->name, name, hash))
288 java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
289 rfield->offset = (char*) field - (char*) fields;
290 rfield->declaringClass = this;
294 throw new java::lang::NoSuchFieldException (name);
297 JArray<java::lang::reflect::Field *> *
298 java::lang::Class::getDeclaredFields (void)
300 memberAccessCheck(java::lang::reflect::Member::DECLARED);
301 JArray<java::lang::reflect::Field *> *result
302 = (JArray<java::lang::reflect::Field *> *)
303 JvNewObjectArray (field_count, &java::lang::reflect::Field::class$, NULL);
304 java::lang::reflect::Field** fptr = elements (result);
305 for (int i = 0; i < field_count; i++)
307 _Jv_Field *field = &fields[i];
308 java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
309 rfield->offset = (char*) field - (char*) fields;
310 rfield->declaringClass = this;
317 java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
320 buffer->append((jchar) method_count);
323 jstring name = getName();
324 if (name->charAt(0) != '[')
325 buffer->append((jchar) 'L');
326 buffer->append(name);
327 if (name->charAt(0) != '[')
328 buffer->append((jchar) ';');
332 // This doesn't have to be native. It is an implementation detail
333 // only called from the C++ code, though, so maybe this is clearer.
335 java::lang::Class::getSignature (JArray<jclass> *param_types,
336 jboolean is_constructor)
338 java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
339 buf->append((jchar) '(');
340 // A NULL param_types means "no parameters".
341 if (param_types != NULL)
343 jclass *v = elements (param_types);
344 for (int i = 0; i < param_types->length; ++i)
345 v[i]->getSignature(buf);
347 buf->append((jchar) ')');
349 buf->append((jchar) 'V');
350 return buf->toString();
353 java::lang::reflect::Method *
354 java::lang::Class::_getDeclaredMethod (jstring name,
355 JArray<jclass> *param_types)
357 jstring partial_sig = getSignature (param_types, false);
358 jint p_len = partial_sig->length();
359 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
360 int i = isPrimitive () ? 0 : method_count;
363 if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
364 && _Jv_equaln (methods[i].signature, partial_sig, p_len)
365 && (methods[i].accflags
366 & java::lang::reflect::Modifier::INVISIBLE) == 0)
369 using namespace java::lang::reflect;
370 Method *rmethod = new Method ();
371 rmethod->offset = (char*) (&methods[i]) - (char*) methods;
372 rmethod->declaringClass = this;
379 JArray<java::lang::reflect::Method *> *
380 java::lang::Class::getDeclaredMethods (void)
382 memberAccessCheck(java::lang::reflect::Member::DECLARED);
385 int max = isPrimitive () ? 0 : method_count;
387 for (i = max; --i >= 0; )
389 _Jv_Method *method = &methods[i];
390 if (method->name == NULL
391 || _Jv_equalUtf8Consts (method->name, clinit_name)
392 || _Jv_equalUtf8Consts (method->name, init_name)
393 || _Jv_equalUtf8Consts (method->name, finit_name)
394 || (methods[i].accflags
395 & java::lang::reflect::Modifier::INVISIBLE) != 0)
399 JArray<java::lang::reflect::Method *> *result
400 = (JArray<java::lang::reflect::Method *> *)
401 JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);
402 java::lang::reflect::Method** mptr = elements (result);
403 for (i = 0; i < max; i++)
405 _Jv_Method *method = &methods[i];
406 if (method->name == NULL
407 || _Jv_equalUtf8Consts (method->name, clinit_name)
408 || _Jv_equalUtf8Consts (method->name, init_name)
409 || _Jv_equalUtf8Consts (method->name, finit_name)
410 || (methods[i].accflags
411 & java::lang::reflect::Modifier::INVISIBLE) != 0)
413 java::lang::reflect::Method* rmethod
414 = new java::lang::reflect::Method ();
415 rmethod->offset = (char*) method - (char*) methods;
416 rmethod->declaringClass = this;
423 java::lang::Class::getName (void)
425 char buffer[name->length + 1];
426 memcpy (buffer, name->data, name->length);
427 buffer[name->length] = '\0';
428 return _Jv_NewStringUTF (buffer);
432 java::lang::Class::getClasses (void)
434 // FIXME: security checking.
436 // Until we have inner classes, it always makes sense to return an
438 JArray<jclass> *result
439 = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
445 java::lang::Class::getDeclaredClasses (void)
447 memberAccessCheck (java::lang::reflect::Member::DECLARED);
448 // Until we have inner classes, it always makes sense to return an
450 JArray<jclass> *result
451 = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
457 java::lang::Class::getDeclaringClass (void)
459 // Until we have inner classes, it makes sense to always return
465 java::lang::Class::_getFields (JArray<java::lang::reflect::Field *> *result,
469 for (int i = 0; i < field_count; i++)
471 _Jv_Field *field = &fields[i];
472 if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
478 java::lang::reflect::Field *rfield
479 = new java::lang::reflect::Field ();
480 rfield->offset = (char *) field - (char *) fields;
481 rfield->declaringClass = this;
482 rfield->name = _Jv_NewStringUtf8Const (field->name);
483 (elements (result))[offset++] = rfield;
486 jclass superclass = getSuperclass();
487 if (superclass != NULL)
489 int s_count = superclass->_getFields (result, offset);
493 for (int i = 0; i < interface_count; ++i)
495 int f_count = interfaces[i]->_getFields (result, offset);
502 JArray<java::lang::reflect::Field *> *
503 java::lang::Class::getFields (void)
505 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
507 int count = _getFields (NULL, 0);
509 JArray<java::lang::reflect::Field *> *result
510 = ((JArray<java::lang::reflect::Field *> *)
511 JvNewObjectArray (count, &java::lang::reflect::Field::class$, NULL));
513 _getFields (result, 0);
519 java::lang::Class::getInterfaces (void)
521 jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
522 jobject *data = elements (r);
523 for (int i = 0; i < interface_count; ++i)
524 data[i] = interfaces[i];
525 return reinterpret_cast<JArray<jclass> *> (r);
528 java::lang::reflect::Method *
529 java::lang::Class::_getMethod (jstring name, JArray<jclass> *param_types)
531 jstring partial_sig = getSignature (param_types, false);
532 jint p_len = partial_sig->length();
533 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
534 for (Class *klass = this; klass; klass = klass->getSuperclass())
536 int i = klass->isPrimitive () ? 0 : klass->method_count;
539 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
540 && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len)
541 && (klass->methods[i].accflags
542 & java::lang::reflect::Modifier::INVISIBLE) == 0)
545 using namespace java::lang::reflect;
547 // Method must be public.
548 if (! Modifier::isPublic (klass->methods[i].accflags))
551 Method *rmethod = new Method ();
552 rmethod->offset = ((char *) (&klass->methods[i])
553 - (char *) klass->methods);
554 rmethod->declaringClass = klass;
560 // If we haven't found a match, and this class is an interface, then
561 // check all the superinterfaces.
564 for (int i = 0; i < interface_count; ++i)
566 using namespace java::lang::reflect;
567 Method *rmethod = interfaces[i]->_getMethod (name, param_types);
576 // This is a very slow implementation, since it re-scans all the
577 // methods we've already listed to make sure we haven't duplicated a
578 // method. It also over-estimates the required size, so we have to
579 // shrink the result array later.
581 java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
586 // First examine all local methods
587 for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
589 _Jv_Method *method = &methods[i];
590 if (method->name == NULL
591 || _Jv_equalUtf8Consts (method->name, clinit_name)
592 || _Jv_equalUtf8Consts (method->name, init_name)
593 || _Jv_equalUtf8Consts (method->name, finit_name)
595 & java::lang::reflect::Modifier::INVISIBLE) != 0)
597 // Only want public methods.
598 if (! java::lang::reflect::Modifier::isPublic (method->accflags))
601 // This is where we over-count the slots required if we aren't
602 // filling the result for real.
606 java::lang::reflect::Method **mp = elements (result);
607 // If we already have a method with this name and signature,
608 // then ignore this one. This can happen with virtual
610 for (int j = 0; j < offset; ++j)
612 _Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
613 if (_Jv_equalUtf8Consts (method->name, meth_2->name)
614 && _Jv_equalUtf8Consts (method->signature,
627 using namespace java::lang::reflect;
628 Method *rmethod = new Method ();
629 rmethod->offset = (char *) method - (char *) methods;
630 rmethod->declaringClass = this;
631 Method **mp = elements (result);
632 mp[offset + count] = rmethod;
638 // Now examine superclasses.
639 if (getSuperclass () != NULL)
641 jint s_count = getSuperclass()->_getMethods (result, offset);
646 // Finally, examine interfaces.
647 for (int i = 0; i < interface_count; ++i)
649 int f_count = interfaces[i]->_getMethods (result, offset);
657 JArray<java::lang::reflect::Method *> *
658 java::lang::Class::getMethods (void)
660 using namespace java::lang::reflect;
662 memberAccessCheck(Member::PUBLIC);
664 // This will overestimate the size we need.
665 jint count = _getMethods (NULL, 0);
667 JArray<Method *> *result
668 = ((JArray<Method *> *) JvNewObjectArray (count,
672 // When filling the array for real, we get the actual count. Then
673 // we resize the array.
674 jint real_count = _getMethods (result, 0);
676 if (real_count != count)
679 = ((JArray<Method *> *) JvNewObjectArray (real_count,
683 Method **destp = elements (r2);
684 Method **srcp = elements (result);
686 for (int i = 0; i < real_count; ++i)
696 java::lang::Class::isAssignableFrom (jclass klass)
698 // Arguments may not have been initialized, given ".class" syntax.
699 _Jv_InitClass (this);
700 _Jv_InitClass (klass);
701 return _Jv_IsAssignableFrom (this, klass);
705 java::lang::Class::isInstance (jobject obj)
709 _Jv_InitClass (this);
710 return _Jv_IsAssignableFrom (this, JV_CLASS (obj));
714 java::lang::Class::newInstance (void)
716 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
721 || java::lang::reflect::Modifier::isAbstract(accflags))
722 throw new java::lang::InstantiationException (getName ());
724 _Jv_InitClass (this);
726 _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
728 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
730 jobject r = JvAllocObject (this);
731 ((void (*) (jobject)) meth->ncode) (r);
736 java::lang::Class::finalize (void)
739 JvAssert (_Jv_IsInterpretedClass (this));
740 _Jv_UnregisterClass (this);
744 // This implements the initialization process for a class. From Spec
747 java::lang::Class::initializeClass (void)
749 // short-circuit to avoid needless locking.
750 if (state == JV_STATE_DONE)
754 _Jv_MonitorEnter (this);
756 if (state < JV_STATE_LINKED)
759 if (_Jv_IsInterpretedClass (this))
761 // this can throw exceptions, so exit the monitor as a precaution.
762 _Jv_MonitorExit (this);
763 java::lang::VMClassLoader::resolveClass (this);
764 _Jv_MonitorEnter (this);
769 _Jv_PrepareCompiledClass (this);
774 java::lang::Thread *self = java::lang::Thread::currentThread();
775 // FIXME: `self' can be null at startup. Hence this nasty trick.
776 self = (java::lang::Thread *) ((long) self | 1);
777 while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
781 if (state == JV_STATE_DONE)
783 _Jv_MonitorExit (this);
786 if (state == JV_STATE_IN_PROGRESS)
788 _Jv_MonitorExit (this);
790 /* Initialization in progress. The class is linked now,
791 so ensure internal tables are built. */
792 _Jv_PrepareConstantTimeTables (this);
793 _Jv_MakeVTable(this);
794 _Jv_LinkSymbolTable(this);
800 if (state == JV_STATE_ERROR)
802 _Jv_MonitorExit (this);
803 throw new java::lang::NoClassDefFoundError (getName());
808 state = JV_STATE_IN_PROGRESS;
809 _Jv_MonitorExit (this);
812 if (! isInterface () && superclass)
816 _Jv_InitClass (superclass);
818 catch (java::lang::Throwable *except)
820 // Caught an exception.
821 _Jv_MonitorEnter (this);
822 state = JV_STATE_ERROR;
824 _Jv_MonitorExit (this);
829 _Jv_PrepareConstantTimeTables (this);
832 _Jv_MakeVTable(this);
834 if (otable || atable)
835 _Jv_LinkSymbolTable(this);
837 // Steps 8, 9, 10, 11.
840 _Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name,
843 ((void (*) (void)) meth->ncode) ();
845 catch (java::lang::Throwable *except)
847 if (! java::lang::Error::class$.isInstance(except))
851 except = new ExceptionInInitializerError (except);
853 catch (java::lang::Throwable *t)
858 _Jv_MonitorEnter (this);
859 state = JV_STATE_ERROR;
861 _Jv_MonitorExit (this);
865 _Jv_MonitorEnter (this);
866 state = JV_STATE_DONE;
868 _Jv_MonitorExit (this);
874 // Some class-related convenience functions.
877 // Find a method declared in the class. If it is not declared locally
878 // (or if it is inherited), return NULL.
880 _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
881 _Jv_Utf8Const *signature)
883 for (int i = 0; i < klass->method_count; ++i)
885 if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
886 && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
887 return &klass->methods[i];
893 _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
894 _Jv_Utf8Const *signature)
896 for (; klass; klass = klass->getSuperclass())
898 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
907 // NOTE: MCACHE_SIZE should be a power of 2 minus one.
908 #define MCACHE_SIZE 1023
916 static _Jv_mcache method_cache[MCACHE_SIZE + 1];
919 _Jv_FindMethodInCache (jclass klass,
921 _Jv_Utf8Const *signature)
923 int index = name->hash & MCACHE_SIZE;
924 _Jv_mcache *mc = method_cache + index;
925 _Jv_Method *m = mc->method;
927 if (mc->klass == klass
928 && m != NULL // thread safe check
929 && _Jv_equalUtf8Consts (m->name, name)
930 && _Jv_equalUtf8Consts (m->signature, signature))
931 return mc->method->ncode;
936 _Jv_AddMethodToCache (jclass klass,
939 _Jv_MonitorEnter (&java::lang::Class::class$);
941 int index = method->name->hash & MCACHE_SIZE;
943 method_cache[index].method = method;
944 method_cache[index].klass = klass;
946 _Jv_MonitorExit (&java::lang::Class::class$);
950 _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
951 _Jv_Utf8Const *signature)
953 using namespace java::lang::reflect;
955 void *ncode = _Jv_FindMethodInCache (klass, name, signature);
959 for (; klass; klass = klass->getSuperclass())
961 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
965 if (Modifier::isStatic(meth->accflags))
966 throw new java::lang::IncompatibleClassChangeError
967 (_Jv_GetMethodString (klass, meth->name));
968 if (Modifier::isAbstract(meth->accflags))
969 throw new java::lang::AbstractMethodError
970 (_Jv_GetMethodString (klass, meth->name));
971 if (! Modifier::isPublic(meth->accflags))
972 throw new java::lang::IllegalAccessError
973 (_Jv_GetMethodString (klass, meth->name));
975 _Jv_AddMethodToCache (klass, meth);
979 throw new java::lang::IncompatibleClassChangeError;
982 // Fast interface method lookup by index.
984 _Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
986 _Jv_IDispatchTable *cldt = klass->idt;
987 int idx = iface->idt->iface.ioffsets[cldt->cls.iindex] + method_idx;
988 return cldt->cls.itable[idx];
992 _Jv_IsAssignableFrom (jclass target, jclass source)
994 if (source == target)
997 // If target is array, so must source be.
998 while (target->isArray ())
1000 if (! source->isArray())
1002 target = target->getComponentType();
1003 source = source->getComponentType();
1006 if (target->isInterface())
1008 // Abstract classes have no IDT, and IDTs provide no way to check
1009 // two interfaces for assignability.
1010 if (__builtin_expect
1011 (source->idt == NULL || source->isInterface(), false))
1012 return _Jv_InterfaceAssignableFrom (target, source);
1014 _Jv_IDispatchTable *cl_idt = source->idt;
1015 _Jv_IDispatchTable *if_idt = target->idt;
1017 if (__builtin_expect ((if_idt == NULL), false))
1018 return false; // No class implementing TARGET has been loaded.
1019 jshort cl_iindex = cl_idt->cls.iindex;
1020 if (cl_iindex < if_idt->iface.ioffsets[0])
1022 jshort offset = if_idt->iface.ioffsets[cl_iindex];
1023 if (offset != -1 && offset < cl_idt->cls.itable_length
1024 && cl_idt->cls.itable[offset] == target)
1030 // Primitive TYPE classes are only assignable to themselves.
1031 if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
1034 if (target == &java::lang::Object::class$)
1036 else if (source->ancestors == NULL || target->ancestors == NULL)
1038 // We need this case when either SOURCE or TARGET has not has
1039 // its constant-time tables prepared.
1041 // At this point we know that TARGET can't be Object, so it is
1042 // safe to use that as the termination point.
1043 while (source && source != &java::lang::Object::class$)
1045 if (source == target)
1047 source = source->getSuperclass();
1050 else if (source->depth >= target->depth
1051 && source->ancestors[source->depth - target->depth] == target)
1057 // Interface type checking, the slow way. Returns TRUE if IFACE is a
1058 // superinterface of SOURCE. This is used when SOURCE is also an interface,
1059 // or a class with no interface dispatch table.
1061 _Jv_InterfaceAssignableFrom (jclass iface, jclass source)
1063 for (int i = 0; i < source->interface_count; i++)
1065 jclass interface = source->interfaces[i];
1066 if (iface == interface
1067 || _Jv_InterfaceAssignableFrom (iface, interface))
1071 if (!source->isInterface()
1072 && source->superclass
1073 && _Jv_InterfaceAssignableFrom (iface, source->superclass))
1080 _Jv_IsInstanceOf(jobject obj, jclass cl)
1082 if (__builtin_expect (!obj, false))
1084 return (_Jv_IsAssignableFrom (cl, JV_CLASS (obj)));
1088 _Jv_CheckCast (jclass c, jobject obj)
1090 if (__builtin_expect
1091 (obj != NULL && ! _Jv_IsAssignableFrom(c, JV_CLASS (obj)), false))
1092 throw new java::lang::ClassCastException
1093 ((new java::lang::StringBuffer
1094 (obj->getClass()->getName()))->append
1095 (JvNewStringUTF(" cannot be cast to "))->append
1096 (c->getName())->toString());
1102 _Jv_CheckArrayStore (jobject arr, jobject obj)
1106 JvAssert (arr != NULL);
1107 jclass elt_class = (JV_CLASS (arr))->getComponentType();
1108 if (elt_class == &java::lang::Object::class$)
1110 jclass obj_class = JV_CLASS (obj);
1111 if (__builtin_expect
1112 (! _Jv_IsAssignableFrom (elt_class, obj_class), false))
1113 throw new java::lang::ArrayStoreException
1114 ((new java::lang::StringBuffer
1115 (JvNewStringUTF("Cannot store ")))->append
1116 (obj_class->getName())->append
1117 (JvNewStringUTF(" in array of type "))->append
1118 (elt_class->getName())->toString());
1122 #define INITIAL_IOFFSETS_LEN 4
1123 #define INITIAL_IFACES_LEN 4
1125 static _Jv_IDispatchTable null_idt = { {SHRT_MAX, 0, NULL} };
1127 // Generate tables for constant-time assignment testing and interface
1128 // method lookup. This implements the technique described by Per Bothner
1129 // <per@bothner.com> on the java-discuss mailing list on 1999-09-02:
1130 // http://gcc.gnu.org/ml/java/1999-q3/msg00377.html
1132 _Jv_PrepareConstantTimeTables (jclass klass)
1134 if (klass->isPrimitive () || klass->isInterface ())
1137 // Short-circuit in case we've been called already.
1138 if ((klass->idt != NULL) || klass->depth != 0)
1141 // Calculate the class depth and ancestor table. The depth of a class
1142 // is how many "extends" it is removed from Object. Thus the depth of
1143 // java.lang.Object is 0, but the depth of java.io.FilterOutputStream
1144 // is 2. Depth is defined for all regular and array classes, but not
1145 // interfaces or primitive types.
1147 jclass klass0 = klass;
1148 jboolean has_interfaces = 0;
1149 while (klass0 != &java::lang::Object::class$)
1151 has_interfaces += klass0->interface_count;
1152 klass0 = klass0->superclass;
1156 // We do class member testing in constant time by using a small table
1157 // of all the ancestor classes within each class. The first element is
1158 // a pointer to the current class, and the rest are pointers to the
1159 // classes ancestors, ordered from the current class down by decreasing
1160 // depth. We do not include java.lang.Object in the table of ancestors,
1161 // since it is redundant.
1163 klass->ancestors = (jclass *) _Jv_Malloc (klass->depth * sizeof (jclass));
1165 for (int index = 0; index < klass->depth; index++)
1167 klass->ancestors[index] = klass0;
1168 klass0 = klass0->superclass;
1171 if (java::lang::reflect::Modifier::isAbstract (klass->accflags))
1174 // Optimization: If class implements no interfaces, use a common
1175 // predefined interface table.
1176 if (!has_interfaces)
1178 klass->idt = &null_idt;
1183 (_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
1188 ifaces.len = INITIAL_IFACES_LEN;
1189 ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
1191 int itable_size = _Jv_GetInterfaces (klass, &ifaces);
1193 if (ifaces.count > 0)
1195 klass->idt->cls.itable =
1196 (void **) _Jv_Malloc (itable_size * sizeof (void *));
1197 klass->idt->cls.itable_length = itable_size;
1199 jshort *itable_offsets =
1200 (jshort *) _Jv_Malloc (ifaces.count * sizeof (jshort));
1202 _Jv_GenerateITable (klass, &ifaces, itable_offsets);
1205 _Jv_FindIIndex (ifaces.list, itable_offsets, ifaces.count);
1207 for (int i=0; i < ifaces.count; i++)
1209 ifaces.list[i]->idt->iface.ioffsets[cls_iindex] =
1213 klass->idt->cls.iindex = cls_iindex;
1215 _Jv_Free (ifaces.list);
1216 _Jv_Free (itable_offsets);
1220 klass->idt->cls.iindex = SHRT_MAX;
1224 // Return index of item in list, or -1 if item is not present.
1226 _Jv_IndexOf (void *item, void **list, jshort list_len)
1228 for (int i=0; i < list_len; i++)
1230 if (list[i] == item)
1236 // Find all unique interfaces directly or indirectly implemented by klass.
1237 // Returns the size of the interface dispatch table (itable) for klass, which
1238 // is the number of unique interfaces plus the total number of methods that
1239 // those interfaces declare. May extend ifaces if required.
1241 _Jv_GetInterfaces (jclass klass, _Jv_ifaces *ifaces)
1245 for (int i=0; i < klass->interface_count; i++)
1247 jclass iface = klass->interfaces[i];
1249 /* Make sure interface is linked. */
1250 _Jv_WaitForState(iface, JV_STATE_LINKED);
1252 if (_Jv_IndexOf (iface, (void **) ifaces->list, ifaces->count) == -1)
1254 if (ifaces->count + 1 >= ifaces->len)
1256 /* Resize ifaces list */
1257 ifaces->len = ifaces->len * 2;
1258 ifaces->list = (jclass *) _Jv_Realloc (ifaces->list,
1259 ifaces->len * sizeof(jclass));
1261 ifaces->list[ifaces->count] = iface;
1264 result += _Jv_GetInterfaces (klass->interfaces[i], ifaces);
1268 if (klass->isInterface())
1270 result += klass->method_count + 1;
1274 if (klass->superclass)
1276 result += _Jv_GetInterfaces (klass->superclass, ifaces);
1282 // Fill out itable in klass, resolving method declarations in each ifaces.
1283 // itable_offsets is filled out with the position of each iface in itable,
1284 // such that itable[itable_offsets[n]] == ifaces.list[n].
1286 _Jv_GenerateITable (jclass klass, _Jv_ifaces *ifaces, jshort *itable_offsets)
1288 void **itable = klass->idt->cls.itable;
1289 jshort itable_pos = 0;
1291 for (int i=0; i < ifaces->count; i++)
1293 jclass iface = ifaces->list[i];
1294 itable_offsets[i] = itable_pos;
1295 itable_pos = _Jv_AppendPartialITable (klass, iface, itable, itable_pos);
1297 /* Create interface dispatch table for iface */
1298 if (iface->idt == NULL)
1301 (_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
1303 // The first element of ioffsets is its length (itself included).
1305 (jshort *) _Jv_Malloc (INITIAL_IOFFSETS_LEN * sizeof (jshort));
1306 ioffsets[0] = INITIAL_IOFFSETS_LEN;
1307 for (int i=1; i < INITIAL_IOFFSETS_LEN; i++)
1310 iface->idt->iface.ioffsets = ioffsets;
1315 // Format method name for use in error messages.
1317 _Jv_GetMethodString (jclass klass, _Jv_Utf8Const *name)
1319 jstring r = JvNewStringUTF (klass->name->data);
1320 r = r->concat (JvNewStringUTF ("."));
1321 r = r->concat (JvNewStringUTF (name->data));
1326 _Jv_ThrowNoSuchMethodError ()
1328 throw new java::lang::NoSuchMethodError;
1331 // Each superinterface of a class (i.e. each interface that the class
1332 // directly or indirectly implements) has a corresponding "Partial
1333 // Interface Dispatch Table" whose size is (number of methods + 1) words.
1334 // The first word is a pointer to the interface (i.e. the java.lang.Class
1335 // instance for that interface). The remaining words are pointers to the
1336 // actual methods that implement the methods declared in the interface,
1337 // in order of declaration.
1339 // Append partial interface dispatch table for "iface" to "itable", at
1340 // position itable_pos.
1341 // Returns the offset at which the next partial ITable should be appended.
1343 _Jv_AppendPartialITable (jclass klass, jclass iface, void **itable,
1346 using namespace java::lang::reflect;
1348 itable[pos++] = (void *) iface;
1351 for (int j=0; j < iface->method_count; j++)
1354 for (jclass cl = klass; cl; cl = cl->getSuperclass())
1356 meth = _Jv_GetMethodLocal (cl, iface->methods[j].name,
1357 iface->methods[j].signature);
1363 if (meth && (meth->name->data[0] == '<'))
1365 // leave a placeholder in the itable for hidden init methods.
1370 if (Modifier::isStatic(meth->accflags))
1371 throw new java::lang::IncompatibleClassChangeError
1372 (_Jv_GetMethodString (klass, meth->name));
1373 if (Modifier::isAbstract(meth->accflags))
1374 throw new java::lang::AbstractMethodError
1375 (_Jv_GetMethodString (klass, meth->name));
1376 if (! Modifier::isPublic(meth->accflags))
1377 throw new java::lang::IllegalAccessError
1378 (_Jv_GetMethodString (klass, meth->name));
1380 itable[pos] = meth->ncode;
1384 // The method doesn't exist in klass. Binary compatibility rules
1385 // permit this, so we delay the error until runtime using a pointer
1386 // to a method which throws an exception.
1387 itable[pos] = (void *) _Jv_ThrowNoSuchMethodError;
1395 static _Jv_Mutex_t iindex_mutex;
1396 static bool iindex_mutex_initialized = false;
1398 // We need to find the correct offset in the Class Interface Dispatch
1399 // Table for a given interface. Once we have that, invoking an interface
1400 // method just requires combining the Method's index in the interface
1401 // (known at compile time) to get the correct method. Doing a type test
1402 // (cast or instanceof) is the same problem: Once we have a possible Partial
1403 // Interface Dispatch Table, we just compare the first element to see if it
1404 // matches the desired interface. So how can we find the correct offset?
1405 // Our solution is to keep a vector of candiate offsets in each interface
1406 // (idt->iface.ioffsets), and in each class we have an index
1407 // (idt->cls.iindex) used to select the correct offset from ioffsets.
1409 // Calculate and return iindex for a new class.
1410 // ifaces is a vector of num interfaces that the class implements.
1411 // offsets[j] is the offset in the interface dispatch table for the
1412 // interface corresponding to ifaces[j].
1413 // May extend the interface ioffsets if required.
1415 _Jv_FindIIndex (jclass *ifaces, jshort *offsets, jshort num)
1420 // Acquire a global lock to prevent itable corruption in case of multiple
1421 // classes that implement an intersecting set of interfaces being linked
1422 // simultaneously. We can assume that the mutex will be initialized
1424 if (! iindex_mutex_initialized)
1426 _Jv_MutexInit (&iindex_mutex);
1427 iindex_mutex_initialized = true;
1430 _Jv_MutexLock (&iindex_mutex);
1432 for (i=1;; i++) /* each potential position in ioffsets */
1434 for (j=0;; j++) /* each iface */
1438 if (i >= ifaces[j]->idt->iface.ioffsets[0])
1440 int ioffset = ifaces[j]->idt->iface.ioffsets[i];
1441 /* We can potentially share this position with another class. */
1442 if (ioffset >= 0 && ioffset != offsets[j])
1443 break; /* Nope. Try next i. */
1447 for (j = 0; j < num; j++)
1449 int len = ifaces[j]->idt->iface.ioffsets[0];
1452 /* Resize ioffsets. */
1453 int newlen = 2 * len;
1456 jshort *old_ioffsets = ifaces[j]->idt->iface.ioffsets;
1457 jshort *new_ioffsets = (jshort *) _Jv_Realloc (old_ioffsets,
1458 newlen * sizeof(jshort));
1459 new_ioffsets[0] = newlen;
1461 while (len < newlen)
1462 new_ioffsets[len++] = -1;
1464 ifaces[j]->idt->iface.ioffsets = new_ioffsets;
1466 ifaces[j]->idt->iface.ioffsets[i] = offsets[j];
1469 _Jv_MutexUnlock (&iindex_mutex);
1474 // Only used by serialization
1475 java::lang::reflect::Field *
1476 java::lang::Class::getPrivateField (jstring name)
1478 int hash = name->hashCode ();
1480 java::lang::reflect::Field* rfield;
1481 for (int i = 0; i < field_count; i++)
1483 _Jv_Field *field = &fields[i];
1484 if (! _Jv_equal (field->name, name, hash))
1486 rfield = new java::lang::reflect::Field ();
1487 rfield->offset = (char*) field - (char*) fields;
1488 rfield->declaringClass = this;
1489 rfield->name = name;
1492 jclass superclass = getSuperclass();
1493 if (superclass == NULL)
1495 rfield = superclass->getPrivateField(name);
1496 for (int i = 0; i < interface_count && rfield == NULL; ++i)
1497 rfield = interfaces[i]->getPrivateField (name);
1501 // Only used by serialization
1502 java::lang::reflect::Method *
1503 java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
1505 jstring partial_sig = getSignature (param_types, false);
1506 jint p_len = partial_sig->length();
1507 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
1508 for (Class *klass = this; klass; klass = klass->getSuperclass())
1510 int i = klass->isPrimitive () ? 0 : klass->method_count;
1513 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
1514 && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
1517 using namespace java::lang::reflect;
1519 Method *rmethod = new Method ();
1520 rmethod->offset = ((char *) (&klass->methods[i])
1521 - (char *) klass->methods);
1522 rmethod->declaringClass = klass;
1527 throw new java::lang::NoSuchMethodException (name);
1530 // Private accessor method for Java code to retrieve the protection domain.
1531 java::security::ProtectionDomain *
1532 java::lang::Class::getProtectionDomain0 ()
1534 return protectionDomain;
1538 java::lang::Class::getSigners()
1544 java::lang::Class::setSigners(JArray<jobject> *s)
1549 // Functions for indirect dispatch (symbolic virtual binding) support.
1551 // There are two tables, atable and otable. atable is an array of
1552 // addresses, and otable is an array of offsets, and these are used
1553 // for static and virtual members respectively.
1555 // {a,o}table_syms is an array of _Jv_MethodSymbols. Each such symbol
1556 // is a tuple of {classname, member name, signature}.
1557 // _Jv_LinkSymbolTable() scans these two arrays and fills in the
1558 // corresponding atable and otable with the addresses of static
1559 // members and the offsets of virtual members.
1561 // The offset (in bytes) for each resolved method or field is placed
1562 // at the corresponding position in the virtual method offset table
1565 // The same otable and atable may be shared by many classes.
1568 _Jv_LinkSymbolTable(jclass klass)
1570 //// FIXME: Need to lock the tables ////
1573 _Jv_MethodSymbol sym;
1574 if (klass->otable == NULL
1575 || klass->otable->state != 0)
1578 klass->otable->state = 1;
1580 for (index = 0; sym = klass->otable_syms[index], sym.name != NULL; index++)
1582 jclass target_class = _Jv_FindClass (sym.class_name, NULL);
1583 _Jv_Method *meth = NULL;
1585 const _Jv_Utf8Const *signature = sym.signature;
1587 // FIXME: This should be special index for ThrowNoSuchMethod().
1588 klass->otable->offsets[index] = -1;
1590 if (target_class == NULL)
1593 if (target_class->isInterface())
1595 // FIXME: This does not yet fully conform to binary compatibility
1596 // rules. It will break if a declaration is moved into a
1598 for (jclass cls = target_class; cls != 0; cls = cls->getSuperclass ())
1600 for (int i=0; i < cls->method_count; i++)
1602 meth = &cls->methods[i];
1603 if (_Jv_equalUtf8Consts (sym.name, meth->name)
1604 && _Jv_equalUtf8Consts (signature, meth->signature))
1606 klass->otable->offsets[index] = i + 1;
1616 // We're looking for a field or a method, and we can tell
1617 // which is needed by looking at the signature.
1618 if (signature->length >= 2
1619 && signature->data[0] == '(')
1621 // If the target class does not have a vtable_method_count yet,
1622 // then we can't tell the offsets for its methods, so we must lay
1624 if (target_class->vtable_method_count == -1)
1626 JvSynchronize sync (target_class);
1627 _Jv_LayoutVTableMethods (target_class);
1630 meth = _Jv_LookupDeclaredMethod(target_class, sym.name,
1635 klass->otable->offsets[index] =
1636 _Jv_VTable::idx_to_offset (meth->index);
1644 _Jv_Field *the_field = NULL;
1646 for (jclass cls = target_class; cls != 0; cls = cls->getSuperclass ())
1648 for (int i = 0; i < cls->field_count; i++)
1650 _Jv_Field *field = &cls->fields[i];
1651 if (! _Jv_equalUtf8Consts (field->name, sym.name))
1654 // FIXME: What access checks should we perform here?
1655 // if (_Jv_CheckAccess (klass, cls, field->flags))
1658 if (!field->isResolved ())
1659 _Jv_ResolveField (field, cls->loader);
1661 // if (field_type != 0 && field->type != field_type)
1662 // throw new java::lang::LinkageError
1663 // (JvNewStringLatin1
1664 // ("field type mismatch with different loaders"));
1667 goto end_of_field_search;
1670 end_of_field_search:
1671 if (the_field != NULL)
1673 if (the_field->flags & 0x0008 /* Modifier::STATIC */)
1675 throw new java::lang::IncompatibleClassChangeError;
1679 klass->otable->offsets[index] = the_field->u.boffset;
1684 throw new java::lang::NoSuchFieldError
1685 (_Jv_NewStringUtf8Const (sym.name));
1691 if (klass->atable == NULL
1692 || klass->atable->state != 0)
1695 klass->atable->state = 1;
1697 for (index = 0; sym = klass->atable_syms[index], sym.name != NULL; index++)
1699 jclass target_class = _Jv_FindClass (sym.class_name, NULL);
1700 _Jv_Method *meth = NULL;
1701 const _Jv_Utf8Const *signature = sym.signature;
1703 // ??? Setting this pointer to null will at least get us a
1704 // NullPointerException
1705 klass->atable->addresses[index] = NULL;
1707 if (target_class == NULL)
1710 // We're looking for a static field or a static method, and we
1711 // can tell which is needed by looking at the signature.
1712 if (signature->length >= 2
1713 && signature->data[0] == '(')
1715 // If the target class does not have a vtable_method_count yet,
1716 // then we can't tell the offsets for its methods, so we must lay
1718 if (target_class->vtable_method_count == -1)
1720 JvSynchronize sync (target_class);
1721 _Jv_LayoutVTableMethods (target_class);
1724 meth = _Jv_LookupDeclaredMethod(target_class, sym.name,
1728 klass->atable->addresses[index] = meth->ncode;
1730 klass->atable->addresses[index] = (void *)_Jv_ThrowNoSuchMethodError;
1737 _Jv_Field *the_field = NULL;
1739 for (jclass cls = target_class; cls != 0; cls = cls->getSuperclass ())
1741 for (int i = 0; i < cls->field_count; i++)
1743 _Jv_Field *field = &cls->fields[i];
1744 if (! _Jv_equalUtf8Consts (field->name, sym.name))
1747 // FIXME: What access checks should we perform here?
1748 // if (_Jv_CheckAccess (klass, cls, field->flags))
1751 if (!field->isResolved ())
1752 _Jv_ResolveField (field, cls->loader);
1754 // if (field_type != 0 && field->type != field_type)
1755 // throw new java::lang::LinkageError
1756 // (JvNewStringLatin1
1757 // ("field type mismatch with different loaders"));
1760 goto end_of_static_field_search;
1763 end_of_static_field_search:
1764 if (the_field != NULL)
1766 if (the_field->flags & 0x0008 /* Modifier::STATIC */)
1768 klass->atable->addresses[index] = the_field->u.addr;
1772 throw new java::lang::IncompatibleClassChangeError;
1777 throw new java::lang::NoSuchFieldError
1778 (_Jv_NewStringUtf8Const (sym.name));
1784 // Returns true if METH should get an entry in a VTable.
1786 isVirtualMethod (_Jv_Method *meth)
1788 using namespace java::lang::reflect;
1789 return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
1790 && meth->name->data[0] != '<');
1793 // This is put in empty vtable slots.
1795 _Jv_abstractMethodError (void)
1797 throw new java::lang::AbstractMethodError();
1800 // Prepare virtual method declarations in KLASS, and any superclasses as
1801 // required, by determining their vtable index, setting method->index, and
1802 // finally setting the class's vtable_method_count. Must be called with the
1803 // lock for KLASS held.
1805 _Jv_LayoutVTableMethods (jclass klass)
1807 if (klass->vtable != NULL || klass->isInterface()
1808 || klass->vtable_method_count != -1)
1811 jclass superclass = klass->superclass;
1813 if (superclass != NULL && superclass->vtable_method_count == -1)
1815 JvSynchronize sync (superclass);
1816 _Jv_LayoutVTableMethods (superclass);
1819 int index = (superclass == NULL ? 0 : superclass->vtable_method_count);
1821 for (int i = 0; i < klass->method_count; ++i)
1823 _Jv_Method *meth = &klass->methods[i];
1824 _Jv_Method *super_meth = NULL;
1826 if (! isVirtualMethod (meth))
1829 if (superclass != NULL)
1831 super_meth = _Jv_LookupDeclaredMethod (superclass, meth->name,
1836 meth->index = super_meth->index;
1837 else if (! (meth->accflags & java::lang::reflect::Modifier::FINAL)
1838 && ! (klass->accflags & java::lang::reflect::Modifier::FINAL))
1839 meth->index = index++;
1842 klass->vtable_method_count = index;
1845 // Set entries in VTABLE for virtual methods declared in KLASS. If
1846 // KLASS has an immediate abstract parent, recursively do its methods
1847 // first. FLAGS is used to determine which slots we've actually set.
1849 _Jv_SetVTableEntries (jclass klass, _Jv_VTable *vtable, jboolean *flags)
1851 using namespace java::lang::reflect;
1853 jclass superclass = klass->getSuperclass();
1855 if (superclass != NULL && (superclass->getModifiers() & Modifier::ABSTRACT))
1856 _Jv_SetVTableEntries (superclass, vtable, flags);
1858 for (int i = klass->method_count - 1; i >= 0; i--)
1860 _Jv_Method *meth = &klass->methods[i];
1861 if (meth->index == (_Jv_ushort) -1)
1863 if ((meth->accflags & Modifier::ABSTRACT))
1865 vtable->set_method(meth->index, (void *) &_Jv_abstractMethodError);
1866 flags[meth->index] = false;
1870 vtable->set_method(meth->index, meth->ncode);
1871 flags[meth->index] = true;
1876 // Allocate and lay out the virtual method table for KLASS. This will also
1877 // cause vtables to be generated for any non-abstract superclasses, and
1878 // virtual method layout to occur for any abstract superclasses. Must be
1879 // called with monitor lock for KLASS held.
1881 _Jv_MakeVTable (jclass klass)
1883 using namespace java::lang::reflect;
1885 if (klass->vtable != NULL || klass->isInterface()
1886 || (klass->accflags & Modifier::ABSTRACT))
1889 // out before we can create a vtable.
1890 if (klass->vtable_method_count == -1)
1891 _Jv_LayoutVTableMethods (klass);
1893 // Allocate the new vtable.
1894 _Jv_VTable *vtable = _Jv_VTable::new_vtable (klass->vtable_method_count);
1895 klass->vtable = vtable;
1897 jboolean flags[klass->vtable_method_count];
1898 for (int i = 0; i < klass->vtable_method_count; ++i)
1901 // Copy the vtable of the closest non-abstract superclass.
1902 jclass superclass = klass->superclass;
1903 if (superclass != NULL)
1905 while ((superclass->accflags & Modifier::ABSTRACT) != 0)
1906 superclass = superclass->superclass;
1908 if (superclass->vtable == NULL)
1910 JvSynchronize sync (superclass);
1911 _Jv_MakeVTable (superclass);
1914 for (int i = 0; i < superclass->vtable_method_count; ++i)
1916 vtable->set_method (i, superclass->vtable->get_method (i));
1921 // Set the class pointer and GC descriptor.
1922 vtable->clas = klass;
1923 vtable->gc_descr = _Jv_BuildGCDescr (klass);
1925 // For each virtual declared in klass and any immediate abstract
1926 // superclasses, set new vtable entry or override an old one.
1927 _Jv_SetVTableEntries (klass, vtable, flags);
1929 // It is an error to have an abstract method in a concrete class.
1930 if (! (klass->accflags & Modifier::ABSTRACT))
1932 for (int i = 0; i < klass->vtable_method_count; ++i)
1935 using namespace java::lang;
1936 while (klass != NULL)
1938 for (int j = 0; j < klass->method_count; ++j)
1940 if (klass->methods[i].index == i)
1942 StringBuffer *buf = new StringBuffer ();
1943 buf->append (_Jv_NewStringUtf8Const (klass->methods[i].name));
1944 buf->append ((jchar) ' ');
1945 buf->append (_Jv_NewStringUtf8Const (klass->methods[i].signature));
1946 throw new AbstractMethodError (buf->toString ());
1949 klass = klass->getSuperclass ();
1951 // Couldn't find the name, which is weird.
1952 // But we still must throw the error.
1953 throw new AbstractMethodError ();