1 // natClass.cc - Implementation of java.lang.Class native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
19 #pragma implementation "Class.h"
23 #include <java-threads.h>
25 #include <java/lang/Class.h>
26 #include <java/lang/ClassLoader.h>
27 #include <java/lang/String.h>
28 #include <java/lang/reflect/Modifier.h>
29 #include <java/lang/reflect/Member.h>
30 #include <java/lang/reflect/Method.h>
31 #include <java/lang/reflect/Field.h>
32 #include <java/lang/reflect/Constructor.h>
33 #include <java/lang/AbstractMethodError.h>
34 #include <java/lang/ArrayStoreException.h>
35 #include <java/lang/ClassCastException.h>
36 #include <java/lang/ClassNotFoundException.h>
37 #include <java/lang/ExceptionInInitializerError.h>
38 #include <java/lang/IllegalAccessException.h>
39 #include <java/lang/IllegalAccessError.h>
40 #include <java/lang/IllegalArgumentException.h>
41 #include <java/lang/IncompatibleClassChangeError.h>
42 #include <java/lang/NoSuchFieldError.h>
43 #include <java/lang/ArrayIndexOutOfBoundsException.h>
44 #include <java/lang/InstantiationException.h>
45 #include <java/lang/NoClassDefFoundError.h>
46 #include <java/lang/NoSuchFieldException.h>
47 #include <java/lang/NoSuchMethodError.h>
48 #include <java/lang/NoSuchMethodException.h>
49 #include <java/lang/Thread.h>
50 #include <java/lang/NullPointerException.h>
51 #include <java/lang/RuntimePermission.h>
52 #include <java/lang/System.h>
53 #include <java/lang/SecurityManager.h>
54 #include <java/lang/StringBuffer.h>
55 #include <java/lang/VMClassLoader.h>
56 #include <gnu/gcj/runtime/StackTrace.h>
57 #include <gcj/method.h>
58 #include <gnu/gcj/runtime/MethodRef.h>
59 #include <gnu/gcj/RawData.h>
60 #include <java/lang/VerifyError.h>
62 #include <java-cpool.h>
63 #include <java-interp.h>
64 #include <java-assert.h>
65 #include <execution.h>
72 java::lang::Class::forName (jstring className, jboolean initialize,
73 java::lang::ClassLoader *loader)
76 throw new java::lang::NullPointerException;
78 jsize length = _Jv_GetStringUTFLength (className);
80 _Jv_GetStringUTFRegion (className, 0, className->length(), buffer);
82 _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
84 if (! _Jv_VerifyClassName (name))
85 throw new java::lang::ClassNotFoundException (className);
87 jclass klass = (buffer[0] == '['
88 ? _Jv_FindClassFromSignature (name->chars(), loader)
89 : _Jv_FindClass (name, loader));
92 throw new java::lang::ClassNotFoundException (className);
95 _Jv_InitClass (klass);
101 java::lang::Class::forName (jstring className)
103 java::lang::ClassLoader *loader = NULL;
104 gnu::gcj::runtime::StackTrace *t
105 = new gnu::gcj::runtime::StackTrace(4);
106 java::lang::Class *klass = NULL;
109 for (int i = 1; !klass; i++)
111 klass = t->classAt (i);
113 loader = klass->getClassLoaderInternal();
115 catch (::java::lang::ArrayIndexOutOfBoundsException *e)
119 return forName (className, true, loader);
122 java::lang::ClassLoader *
123 java::lang::Class::getClassLoader (void)
125 java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
128 gnu::gcj::runtime::StackTrace *t
129 = new gnu::gcj::runtime::StackTrace(4);
130 Class *caller = NULL;
131 ClassLoader *caller_loader = NULL;
134 for (int i = 1; !caller; i++)
136 caller = t->classAt (i);
138 caller_loader = caller->getClassLoaderInternal();
140 catch (::java::lang::ArrayIndexOutOfBoundsException *e)
144 // If the caller has a non-null class loader, and that loader
145 // is not this class' loader or an ancestor thereof, then do a
147 if (caller_loader != NULL && ! caller_loader->isAncestorOf(loader))
148 s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
151 // The spec requires us to return `null' for primitive classes. In
152 // other cases we have the option of returning `null' for classes
153 // loaded with the bootstrap loader. All gcj-compiled classes which
154 // are linked into the application used to return `null' here, but
155 // that confuses some poorly-written applications. It is a useful
156 // and apparently harmless compatibility hack to simply never return
160 return loader ? loader : ClassLoader::systemClassLoader;
163 java::lang::reflect::Constructor *
164 java::lang::Class::getConstructor (JArray<jclass> *param_types)
166 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
168 jstring partial_sig = getSignature (param_types, true);
169 jint hash = partial_sig->hashCode ();
171 int i = isPrimitive () ? 0 : method_count;
174 if (_Jv_equalUtf8Consts (methods[i].name, init_name)
175 && _Jv_equal (methods[i].signature, partial_sig, hash))
177 // Found it. For getConstructor, the constructor must be
179 using namespace java::lang::reflect;
180 if (! Modifier::isPublic(methods[i].accflags))
182 Constructor *cons = new Constructor ();
183 cons->offset = (char *) (&methods[i]) - (char *) methods;
184 cons->declaringClass = this;
188 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
191 JArray<java::lang::reflect::Constructor *> *
192 java::lang::Class::_getConstructors (jboolean declared)
194 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
196 int numConstructors = 0;
197 int max = isPrimitive () ? 0 : method_count;
199 for (i = max; --i >= 0; )
201 _Jv_Method *method = &methods[i];
202 if (method->name == NULL
203 || ! _Jv_equalUtf8Consts (method->name, init_name))
206 && ! java::lang::reflect::Modifier::isPublic(method->accflags))
210 JArray<java::lang::reflect::Constructor *> *result
211 = (JArray<java::lang::reflect::Constructor *> *)
212 JvNewObjectArray (numConstructors,
213 &java::lang::reflect::Constructor::class$,
215 java::lang::reflect::Constructor** cptr = elements (result);
216 for (i = 0; i < max; i++)
218 _Jv_Method *method = &methods[i];
219 if (method->name == NULL
220 || ! _Jv_equalUtf8Consts (method->name, init_name))
223 && ! java::lang::reflect::Modifier::isPublic(method->accflags))
225 java::lang::reflect::Constructor *cons
226 = new java::lang::reflect::Constructor ();
227 cons->offset = (char *) method - (char *) methods;
228 cons->declaringClass = this;
234 java::lang::reflect::Constructor *
235 java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
237 memberAccessCheck(java::lang::reflect::Member::DECLARED);
239 jstring partial_sig = getSignature (param_types, true);
240 jint hash = partial_sig->hashCode ();
242 int i = isPrimitive () ? 0 : method_count;
245 if (_Jv_equalUtf8Consts (methods[i].name, init_name)
246 && _Jv_equal (methods[i].signature, partial_sig, hash))
249 using namespace java::lang::reflect;
250 Constructor *cons = new Constructor ();
251 cons->offset = (char *) (&methods[i]) - (char *) methods;
252 cons->declaringClass = this;
256 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
259 java::lang::reflect::Field *
260 java::lang::Class::getField (jstring name, jint hash)
262 java::lang::reflect::Field* rfield;
263 for (int i = 0; i < field_count; i++)
265 _Jv_Field *field = &fields[i];
266 if (! _Jv_equal (field->name, name, hash))
268 if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
270 rfield = new java::lang::reflect::Field ();
271 rfield->offset = (char*) field - (char*) fields;
272 rfield->declaringClass = this;
276 jclass superclass = getSuperclass();
277 if (superclass == NULL)
279 rfield = superclass->getField(name, hash);
280 for (int i = 0; i < interface_count && rfield == NULL; ++i)
281 rfield = interfaces[i]->getField (name, hash);
285 java::lang::reflect::Field *
286 java::lang::Class::getDeclaredField (jstring name)
288 memberAccessCheck(java::lang::reflect::Member::DECLARED);
289 int hash = name->hashCode();
290 for (int i = 0; i < field_count; i++)
292 _Jv_Field *field = &fields[i];
293 if (! _Jv_equal (field->name, name, hash))
295 java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
296 rfield->offset = (char*) field - (char*) fields;
297 rfield->declaringClass = this;
301 throw new java::lang::NoSuchFieldException (name);
304 JArray<java::lang::reflect::Field *> *
305 java::lang::Class::getDeclaredFields (jboolean public_only)
311 for (int i = 0; i < field_count; ++i)
313 _Jv_Field *field = &fields[i];
314 if ((field->flags & java::lang::reflect::Modifier::PUBLIC))
321 JArray<java::lang::reflect::Field *> *result
322 = (JArray<java::lang::reflect::Field *> *)
323 JvNewObjectArray (size, &java::lang::reflect::Field::class$, NULL);
324 java::lang::reflect::Field** fptr = elements (result);
325 for (int i = 0; i < field_count; i++)
327 _Jv_Field *field = &fields[i];
329 && ! (field->flags & java::lang::reflect::Modifier::PUBLIC))
331 java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
332 rfield->offset = (char*) field - (char*) fields;
333 rfield->declaringClass = this;
340 java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
343 buffer->append((jchar) method_count);
346 jstring name = getName();
347 if (name->charAt(0) != '[')
348 buffer->append((jchar) 'L');
349 buffer->append(name);
350 if (name->charAt(0) != '[')
351 buffer->append((jchar) ';');
355 // This doesn't have to be native. It is an implementation detail
356 // only called from the C++ code, though, so maybe this is clearer.
358 java::lang::Class::getSignature (JArray<jclass> *param_types,
359 jboolean is_constructor)
361 java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
362 buf->append((jchar) '(');
363 // A NULL param_types means "no parameters".
364 if (param_types != NULL)
366 jclass *v = elements (param_types);
367 for (int i = 0; i < param_types->length; ++i)
368 v[i]->getSignature(buf);
370 buf->append((jchar) ')');
372 buf->append((jchar) 'V');
373 return buf->toString();
376 java::lang::reflect::Method *
377 java::lang::Class::_getDeclaredMethod (jstring name,
378 JArray<jclass> *param_types)
380 jstring partial_sig = getSignature (param_types, false);
381 jint p_len = partial_sig->length();
382 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
383 int i = isPrimitive () ? 0 : method_count;
386 if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
387 && _Jv_equaln (methods[i].signature, partial_sig, p_len)
388 && (methods[i].accflags
389 & java::lang::reflect::Modifier::INVISIBLE) == 0)
392 using namespace java::lang::reflect;
393 Method *rmethod = new Method ();
394 rmethod->offset = (char*) (&methods[i]) - (char*) methods;
395 rmethod->declaringClass = this;
402 JArray<java::lang::reflect::Method *> *
403 java::lang::Class::getDeclaredMethods (void)
405 memberAccessCheck(java::lang::reflect::Member::DECLARED);
408 int max = isPrimitive () ? 0 : method_count;
410 for (i = max; --i >= 0; )
412 _Jv_Method *method = &methods[i];
413 if (method->name == NULL
414 || _Jv_equalUtf8Consts (method->name, clinit_name)
415 || _Jv_equalUtf8Consts (method->name, init_name)
416 || _Jv_equalUtf8Consts (method->name, finit_name)
417 || (methods[i].accflags
418 & java::lang::reflect::Modifier::INVISIBLE) != 0)
422 JArray<java::lang::reflect::Method *> *result
423 = (JArray<java::lang::reflect::Method *> *)
424 JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);
425 java::lang::reflect::Method** mptr = elements (result);
426 for (i = 0; i < max; i++)
428 _Jv_Method *method = &methods[i];
429 if (method->name == NULL
430 || _Jv_equalUtf8Consts (method->name, clinit_name)
431 || _Jv_equalUtf8Consts (method->name, init_name)
432 || _Jv_equalUtf8Consts (method->name, finit_name)
433 || (methods[i].accflags
434 & java::lang::reflect::Modifier::INVISIBLE) != 0)
436 java::lang::reflect::Method* rmethod
437 = new java::lang::reflect::Method ();
438 rmethod->offset = (char*) method - (char*) methods;
439 rmethod->declaringClass = this;
446 java::lang::Class::getName (void)
448 return name->toString();
452 java::lang::Class::getClasses (void)
454 // FIXME: security checking.
456 // Until we have inner classes, it always makes sense to return an
458 JArray<jclass> *result
459 = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
465 java::lang::Class::getDeclaredClasses (void)
467 memberAccessCheck (java::lang::reflect::Member::DECLARED);
468 // Until we have inner classes, it always makes sense to return an
470 JArray<jclass> *result
471 = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
477 java::lang::Class::getDeclaringClass (void)
479 // Until we have inner classes, it makes sense to always return
485 java::lang::Class::getInterfaces (void)
487 jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
488 jobject *data = elements (r);
489 for (int i = 0; i < interface_count; ++i)
491 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
492 data[i] = interfaces[i];
493 if ((uaddr)data[i] < (uaddr)constants.size)
494 fprintf (stderr, "ERROR !!!\n");
496 return reinterpret_cast<JArray<jclass> *> (r);
499 java::lang::reflect::Method *
500 java::lang::Class::_getMethod (jstring name, JArray<jclass> *param_types)
502 jstring partial_sig = getSignature (param_types, false);
503 jint p_len = partial_sig->length();
504 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
506 for (Class *klass = this; klass; klass = klass->getSuperclass())
508 int i = klass->isPrimitive () ? 0 : klass->method_count;
511 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
512 && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len)
513 && (klass->methods[i].accflags
514 & java::lang::reflect::Modifier::INVISIBLE) == 0)
517 using namespace java::lang::reflect;
519 // Method must be public.
520 if (! Modifier::isPublic (klass->methods[i].accflags))
523 Method *rmethod = new Method ();
524 rmethod->offset = ((char *) (&klass->methods[i])
525 - (char *) klass->methods);
526 rmethod->declaringClass = klass;
532 // If we haven't found a match, and this class is an interface, then
533 // check all the superinterfaces.
536 for (int i = 0; i < interface_count; ++i)
538 using namespace java::lang::reflect;
539 Method *rmethod = interfaces[i]->_getMethod (name, param_types);
548 // This is a very slow implementation, since it re-scans all the
549 // methods we've already listed to make sure we haven't duplicated a
550 // method. It also over-estimates the required size, so we have to
551 // shrink the result array later.
553 java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
558 // First examine all local methods
559 for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
561 _Jv_Method *method = &methods[i];
562 if (method->name == NULL
563 || _Jv_equalUtf8Consts (method->name, clinit_name)
564 || _Jv_equalUtf8Consts (method->name, init_name)
565 || _Jv_equalUtf8Consts (method->name, finit_name)
567 & java::lang::reflect::Modifier::INVISIBLE) != 0)
569 // Only want public methods.
570 if (! java::lang::reflect::Modifier::isPublic (method->accflags))
573 // This is where we over-count the slots required if we aren't
574 // filling the result for real.
578 java::lang::reflect::Method **mp = elements (result);
579 // If we already have a method with this name and signature,
580 // then ignore this one. This can happen with virtual
582 for (int j = 0; j < offset; ++j)
584 _Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
585 if (_Jv_equalUtf8Consts (method->name, meth_2->name)
586 && _Jv_equalUtf8Consts (method->signature,
599 using namespace java::lang::reflect;
600 Method *rmethod = new Method ();
601 rmethod->offset = (char *) method - (char *) methods;
602 rmethod->declaringClass = this;
603 Method **mp = elements (result);
604 mp[offset + count] = rmethod;
610 // Now examine superclasses.
611 if (getSuperclass () != NULL)
613 jint s_count = getSuperclass()->_getMethods (result, offset);
618 // Finally, examine interfaces.
619 for (int i = 0; i < interface_count; ++i)
621 int f_count = interfaces[i]->_getMethods (result, offset);
629 JArray<java::lang::reflect::Method *> *
630 java::lang::Class::getMethods (void)
632 using namespace java::lang::reflect;
634 memberAccessCheck(Member::PUBLIC);
636 // This will overestimate the size we need.
637 jint count = _getMethods (NULL, 0);
639 JArray<Method *> *result
640 = ((JArray<Method *> *) JvNewObjectArray (count,
644 // When filling the array for real, we get the actual count. Then
645 // we resize the array.
646 jint real_count = _getMethods (result, 0);
648 if (real_count != count)
651 = ((JArray<Method *> *) JvNewObjectArray (real_count,
655 Method **destp = elements (r2);
656 Method **srcp = elements (result);
658 for (int i = 0; i < real_count; ++i)
668 java::lang::Class::isAssignableFrom (jclass klass)
670 // Arguments may not have been initialized, given ".class" syntax.
671 _Jv_InitClass (this);
672 _Jv_InitClass (klass);
673 return _Jv_IsAssignableFrom (this, klass);
677 java::lang::Class::isInstance (jobject obj)
681 _Jv_InitClass (this);
682 return _Jv_IsAssignableFrom (this, JV_CLASS (obj));
686 java::lang::Class::newInstance (void)
688 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
693 || java::lang::reflect::Modifier::isAbstract(accflags))
694 throw new java::lang::InstantiationException (getName ());
696 _Jv_InitClass (this);
698 _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
700 throw new java::lang::InstantiationException (getName());
702 jobject r = _Jv_AllocObject (this);
703 ((void (*) (jobject)) meth->ncode) (r);
708 java::lang::Class::finalize (void)
710 engine->unregister(this);
713 // This implements the initialization process for a class. From Spec
716 java::lang::Class::initializeClass (void)
718 // Short-circuit to avoid needless locking.
719 if (state == JV_STATE_DONE)
722 // Step 1. We introduce a new scope so we can synchronize more
725 JvSynchronize sync (this);
727 if (state < JV_STATE_LINKED)
728 java::lang::VMClassLoader::resolveClass (this);
731 java::lang::Thread *self = java::lang::Thread::currentThread();
732 self = (java::lang::Thread *) ((long) self | 1);
733 while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
737 if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS)
741 if (state == JV_STATE_ERROR)
742 throw new java::lang::NoClassDefFoundError (getName());
746 _Jv_Linker::wait_for_state (this, JV_STATE_LINKED);
747 state = JV_STATE_IN_PROGRESS;
751 if (! isInterface () && superclass)
755 _Jv_InitClass (superclass);
757 catch (java::lang::Throwable *except)
759 // Caught an exception.
760 JvSynchronize sync (this);
761 state = JV_STATE_ERROR;
767 // Steps 8, 9, 10, 11.
770 _Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name,
773 ((void (*) (void)) meth->ncode) ();
775 catch (java::lang::Throwable *except)
777 if (! java::lang::Error::class$.isInstance(except))
781 except = new ExceptionInInitializerError (except);
783 catch (java::lang::Throwable *t)
789 JvSynchronize sync (this);
790 state = JV_STATE_ERROR;
795 JvSynchronize sync (this);
796 state = JV_STATE_DONE;
800 // Only used by serialization
801 java::lang::reflect::Field *
802 java::lang::Class::getPrivateField (jstring name)
804 int hash = name->hashCode ();
806 java::lang::reflect::Field* rfield;
807 for (int i = 0; i < field_count; i++)
809 _Jv_Field *field = &fields[i];
810 if (! _Jv_equal (field->name, name, hash))
812 rfield = new java::lang::reflect::Field ();
813 rfield->offset = (char*) field - (char*) fields;
814 rfield->declaringClass = this;
818 jclass superclass = getSuperclass();
819 if (superclass == NULL)
821 rfield = superclass->getPrivateField(name);
822 for (int i = 0; i < interface_count && rfield == NULL; ++i)
823 rfield = interfaces[i]->getPrivateField (name);
827 // Only used by serialization
828 java::lang::reflect::Method *
829 java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
831 jstring partial_sig = getSignature (param_types, false);
832 jint p_len = partial_sig->length();
833 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
834 for (Class *klass = this; klass; klass = klass->getSuperclass())
836 int i = klass->isPrimitive () ? 0 : klass->method_count;
839 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
840 && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
843 using namespace java::lang::reflect;
845 Method *rmethod = new Method ();
846 rmethod->offset = ((char *) (&klass->methods[i])
847 - (char *) klass->methods);
848 rmethod->declaringClass = klass;
853 throw new java::lang::NoSuchMethodException (name);
856 // Private accessor method for Java code to retrieve the protection domain.
857 java::security::ProtectionDomain *
858 java::lang::Class::getProtectionDomain0 ()
860 return protectionDomain;
864 java::lang::Class::getSigners()
870 java::lang::Class::setSigners(JArray<jobject> *s)
878 // Some class-related convenience functions.
881 // Find a method declared in the class. If it is not declared locally
882 // (or if it is inherited), return NULL.
884 _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
885 _Jv_Utf8Const *signature)
887 for (int i = 0; i < klass->method_count; ++i)
889 if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
890 && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
891 return &klass->methods[i];
897 _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
898 _Jv_Utf8Const *signature,
899 jclass *declarer_result)
901 for (; klass; klass = klass->getSuperclass())
903 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
908 *declarer_result = klass;
916 // NOTE: MCACHE_SIZE should be a power of 2 minus one.
917 #define MCACHE_SIZE 1023
925 static _Jv_mcache method_cache[MCACHE_SIZE + 1];
928 _Jv_FindMethodInCache (jclass klass,
930 _Jv_Utf8Const *signature)
932 int index = name->hash16 () & MCACHE_SIZE;
933 _Jv_mcache *mc = method_cache + index;
934 _Jv_Method *m = mc->method;
936 if (mc->klass == klass
937 && m != NULL // thread safe check
938 && _Jv_equalUtf8Consts (m->name, name)
939 && _Jv_equalUtf8Consts (m->signature, signature))
940 return mc->method->ncode;
945 _Jv_AddMethodToCache (jclass klass,
948 _Jv_MonitorEnter (&java::lang::Class::class$);
950 int index = method->name->hash16 () & MCACHE_SIZE;
952 method_cache[index].method = method;
953 method_cache[index].klass = klass;
955 _Jv_MonitorExit (&java::lang::Class::class$);
959 _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
960 _Jv_Utf8Const *signature)
962 using namespace java::lang::reflect;
964 void *ncode = _Jv_FindMethodInCache (klass, name, signature);
968 for (; klass; klass = klass->getSuperclass())
970 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
974 if (Modifier::isStatic(meth->accflags))
975 throw new java::lang::IncompatibleClassChangeError
976 (_Jv_GetMethodString (klass, meth->name));
977 if (Modifier::isAbstract(meth->accflags))
978 throw new java::lang::AbstractMethodError
979 (_Jv_GetMethodString (klass, meth->name));
980 if (! Modifier::isPublic(meth->accflags))
981 throw new java::lang::IllegalAccessError
982 (_Jv_GetMethodString (klass, meth->name));
984 _Jv_AddMethodToCache (klass, meth);
988 throw new java::lang::IncompatibleClassChangeError;
991 // Fast interface method lookup by index.
993 _Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
995 _Jv_IDispatchTable *cldt = klass->idt;
996 int idx = iface->idt->iface.ioffsets[cldt->cls.iindex] + method_idx;
997 return cldt->cls.itable[idx];
1001 _Jv_IsAssignableFrom (jclass target, jclass source)
1003 if (source == target)
1006 // If target is array, so must source be.
1007 while (target->isArray ())
1009 if (! source->isArray())
1011 target = target->getComponentType();
1012 source = source->getComponentType();
1015 if (target->isInterface())
1017 // Abstract classes have no IDT, and IDTs provide no way to check
1018 // two interfaces for assignability.
1019 if (__builtin_expect
1020 (source->idt == NULL || source->isInterface(), false))
1021 return _Jv_InterfaceAssignableFrom (target, source);
1023 _Jv_IDispatchTable *cl_idt = source->idt;
1024 _Jv_IDispatchTable *if_idt = target->idt;
1026 if (__builtin_expect ((if_idt == NULL), false))
1027 return false; // No class implementing TARGET has been loaded.
1028 jshort cl_iindex = cl_idt->cls.iindex;
1029 if (cl_iindex < if_idt->iface.ioffsets[0])
1031 jshort offset = if_idt->iface.ioffsets[cl_iindex];
1032 if (offset != -1 && offset < cl_idt->cls.itable_length
1033 && cl_idt->cls.itable[offset] == target)
1039 // Primitive TYPE classes are only assignable to themselves.
1040 if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
1043 if (target == &java::lang::Object::class$)
1045 else if (source->ancestors == NULL || target->ancestors == NULL)
1047 // We need this case when either SOURCE or TARGET has not has
1048 // its constant-time tables prepared.
1050 // At this point we know that TARGET can't be Object, so it is
1051 // safe to use that as the termination point.
1052 while (source && source != &java::lang::Object::class$)
1054 if (source == target)
1056 source = source->getSuperclass();
1059 else if (source->depth >= target->depth
1060 && source->ancestors[source->depth - target->depth] == target)
1066 // Interface type checking, the slow way. Returns TRUE if IFACE is a
1067 // superinterface of SOURCE. This is used when SOURCE is also an interface,
1068 // or a class with no interface dispatch table.
1070 _Jv_InterfaceAssignableFrom (jclass iface, jclass source)
1072 for (int i = 0; i < source->interface_count; i++)
1074 jclass interface = source->interfaces[i];
1075 if (iface == interface
1076 || _Jv_InterfaceAssignableFrom (iface, interface))
1080 if (!source->isInterface()
1081 && source->superclass
1082 && _Jv_InterfaceAssignableFrom (iface, source->superclass))
1089 _Jv_IsInstanceOf(jobject obj, jclass cl)
1091 if (__builtin_expect (!obj, false))
1093 return (_Jv_IsAssignableFrom (cl, JV_CLASS (obj)));
1097 _Jv_CheckCast (jclass c, jobject obj)
1099 if (__builtin_expect
1100 (obj != NULL && ! _Jv_IsAssignableFrom(c, JV_CLASS (obj)), false))
1101 throw new java::lang::ClassCastException
1102 ((new java::lang::StringBuffer
1103 (obj->getClass()->getName()))->append
1104 (JvNewStringUTF(" cannot be cast to "))->append
1105 (c->getName())->toString());
1111 _Jv_CheckArrayStore (jobject arr, jobject obj)
1115 JvAssert (arr != NULL);
1116 jclass elt_class = (JV_CLASS (arr))->getComponentType();
1117 if (elt_class == &java::lang::Object::class$)
1119 jclass obj_class = JV_CLASS (obj);
1120 if (__builtin_expect
1121 (! _Jv_IsAssignableFrom (elt_class, obj_class), false))
1122 throw new java::lang::ArrayStoreException
1123 ((new java::lang::StringBuffer
1124 (JvNewStringUTF("Cannot store ")))->append
1125 (obj_class->getName())->append
1126 (JvNewStringUTF(" in array of type "))->append
1127 (elt_class->getName())->toString());
1132 _Jv_IsAssignableFromSlow (jclass target, jclass source)
1134 // First, strip arrays.
1135 while (target->isArray ())
1137 // If target is array, source must be as well.
1138 if (! source->isArray ())
1140 target = target->getComponentType ();
1141 source = source->getComponentType ();
1145 if (target == &java::lang::Object::class$)
1148 // Ensure that the classes have their supers installed.
1149 _Jv_Linker::wait_for_state (source, JV_STATE_LOADING);
1150 _Jv_Linker::wait_for_state (target, JV_STATE_LOADING);
1154 if (source == target)
1157 if (target->isPrimitive () || source->isPrimitive ())
1160 if (target->isInterface ())
1162 for (int i = 0; i < source->interface_count; ++i)
1164 // We use a recursive call because we also need to
1165 // check superinterfaces.
1166 if (_Jv_IsAssignableFromSlow (target, source->getInterface (i)))
1170 source = source->getSuperclass ();
1172 while (source != NULL);
1177 // Lookup an interface method by name. This is very similar to
1178 // purpose to _getMethod, but the interfaces are quite different. It
1179 // might be a good idea for _getMethod to call this function.
1181 // Return true of the method is found, with the class in FOUND_CLASS
1182 // and the index in INDEX.
1184 _Jv_getInterfaceMethod (jclass search_class, jclass &found_class, int &index,
1185 const _Jv_Utf8Const *utf_name,
1186 const _Jv_Utf8Const *utf_sig)
1188 for (jclass klass = search_class; klass; klass = klass->getSuperclass())
1190 // FIXME: Throw an exception?
1191 if (!klass->isInterface ())
1194 int i = klass->method_count;
1197 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
1198 && _Jv_equalUtf8Consts (klass->methods[i].signature, utf_sig))
1201 using namespace java::lang::reflect;
1203 // FIXME: Method must be public. Throw an exception?
1204 if (! Modifier::isPublic (klass->methods[i].accflags))
1207 found_class = klass;
1208 // Interface method indexes count from 1.
1215 // If we haven't found a match, and this class is an interface, then
1216 // check all the superinterfaces.
1217 if (search_class->isInterface())
1219 for (int i = 0; i < search_class->interface_count; ++i)
1221 using namespace java::lang::reflect;
1222 bool found = _Jv_getInterfaceMethod (search_class->interfaces[i],