1 // natClass.cc - Implementation of java.lang.Class native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 <gcj/method.h>
57 #include <gnu/gcj/runtime/MethodRef.h>
58 #include <gnu/gcj/RawData.h>
59 #include <java/lang/VerifyError.h>
61 #include <java-cpool.h>
62 #include <java-interp.h>
63 #include <java-assert.h>
64 #include <java-stack.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;
105 jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
107 loader = caller->getClassLoaderInternal();
109 return forName (className, true, loader);
112 java::lang::ClassLoader *
113 java::lang::Class::getClassLoader (void)
115 java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
118 jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
119 ClassLoader *caller_loader = NULL;
121 caller_loader = caller->getClassLoaderInternal();
123 // If the caller has a non-null class loader, and that loader
124 // is not this class' loader or an ancestor thereof, then do a
126 if (caller_loader != NULL && ! caller_loader->isAncestorOf(loader))
127 s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
130 // The spec requires us to return `null' for primitive classes. In
131 // other cases we have the option of returning `null' for classes
132 // loaded with the bootstrap loader. All gcj-compiled classes which
133 // are linked into the application used to return `null' here, but
134 // that confuses some poorly-written applications. It is a useful
135 // and apparently harmless compatibility hack to simply never return
139 return loader ? loader : ClassLoader::systemClassLoader;
142 java::lang::reflect::Constructor *
143 java::lang::Class::getConstructor (JArray<jclass> *param_types)
145 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
147 jstring partial_sig = getSignature (param_types, true);
148 jint hash = partial_sig->hashCode ();
150 int i = isPrimitive () ? 0 : method_count;
153 if (_Jv_equalUtf8Consts (methods[i].name, init_name)
154 && _Jv_equal (methods[i].signature, partial_sig, hash))
156 // Found it. For getConstructor, the constructor must be
158 using namespace java::lang::reflect;
159 if (! Modifier::isPublic(methods[i].accflags))
161 Constructor *cons = new Constructor ();
162 cons->offset = (char *) (&methods[i]) - (char *) methods;
163 cons->declaringClass = this;
167 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
170 JArray<java::lang::reflect::Constructor *> *
171 java::lang::Class::_getConstructors (jboolean declared)
173 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
175 int numConstructors = 0;
176 int max = isPrimitive () ? 0 : method_count;
178 for (i = max; --i >= 0; )
180 _Jv_Method *method = &methods[i];
181 if (method->name == NULL
182 || ! _Jv_equalUtf8Consts (method->name, init_name))
185 && ! java::lang::reflect::Modifier::isPublic(method->accflags))
189 JArray<java::lang::reflect::Constructor *> *result
190 = (JArray<java::lang::reflect::Constructor *> *)
191 JvNewObjectArray (numConstructors,
192 &java::lang::reflect::Constructor::class$,
194 java::lang::reflect::Constructor** cptr = elements (result);
195 for (i = 0; i < max; i++)
197 _Jv_Method *method = &methods[i];
198 if (method->name == NULL
199 || ! _Jv_equalUtf8Consts (method->name, init_name))
202 && ! java::lang::reflect::Modifier::isPublic(method->accflags))
204 java::lang::reflect::Constructor *cons
205 = new java::lang::reflect::Constructor ();
206 cons->offset = (char *) method - (char *) methods;
207 cons->declaringClass = this;
213 java::lang::reflect::Constructor *
214 java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
216 memberAccessCheck(java::lang::reflect::Member::DECLARED);
218 jstring partial_sig = getSignature (param_types, true);
219 jint hash = partial_sig->hashCode ();
221 int i = isPrimitive () ? 0 : method_count;
224 if (_Jv_equalUtf8Consts (methods[i].name, init_name)
225 && _Jv_equal (methods[i].signature, partial_sig, hash))
228 using namespace java::lang::reflect;
229 Constructor *cons = new Constructor ();
230 cons->offset = (char *) (&methods[i]) - (char *) methods;
231 cons->declaringClass = this;
235 throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
238 java::lang::reflect::Field *
239 java::lang::Class::getField (jstring name, jint hash)
241 java::lang::reflect::Field* rfield;
242 for (int i = 0; i < field_count; i++)
244 _Jv_Field *field = &fields[i];
245 if (! _Jv_equal (field->name, name, hash))
247 if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
249 rfield = new java::lang::reflect::Field ();
250 rfield->offset = (char*) field - (char*) fields;
251 rfield->declaringClass = this;
255 jclass superclass = getSuperclass();
256 if (superclass == NULL)
258 rfield = superclass->getField(name, hash);
259 for (int i = 0; i < interface_count && rfield == NULL; ++i)
260 rfield = interfaces[i]->getField (name, hash);
264 java::lang::reflect::Field *
265 java::lang::Class::getDeclaredField (jstring name)
267 memberAccessCheck(java::lang::reflect::Member::DECLARED);
268 int hash = name->hashCode();
269 for (int i = 0; i < field_count; i++)
271 _Jv_Field *field = &fields[i];
272 if (! _Jv_equal (field->name, name, hash))
274 java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
275 rfield->offset = (char*) field - (char*) fields;
276 rfield->declaringClass = this;
280 throw new java::lang::NoSuchFieldException (name);
283 JArray<java::lang::reflect::Field *> *
284 java::lang::Class::getDeclaredFields (jboolean public_only)
290 for (int i = 0; i < field_count; ++i)
292 _Jv_Field *field = &fields[i];
293 if ((field->flags & java::lang::reflect::Modifier::PUBLIC))
300 JArray<java::lang::reflect::Field *> *result
301 = (JArray<java::lang::reflect::Field *> *)
302 JvNewObjectArray (size, &java::lang::reflect::Field::class$, NULL);
303 java::lang::reflect::Field** fptr = elements (result);
304 for (int i = 0; i < field_count; i++)
306 _Jv_Field *field = &fields[i];
308 && ! (field->flags & java::lang::reflect::Modifier::PUBLIC))
310 java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
311 rfield->offset = (char*) field - (char*) fields;
312 rfield->declaringClass = this;
319 java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
322 buffer->append((jchar) method_count);
325 jstring name = getName();
326 if (name->charAt(0) != '[')
327 buffer->append((jchar) 'L');
328 buffer->append(name);
329 if (name->charAt(0) != '[')
330 buffer->append((jchar) ';');
334 // This doesn't have to be native. It is an implementation detail
335 // only called from the C++ code, though, so maybe this is clearer.
337 java::lang::Class::getSignature (JArray<jclass> *param_types,
338 jboolean is_constructor)
340 java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
341 buf->append((jchar) '(');
342 // A NULL param_types means "no parameters".
343 if (param_types != NULL)
345 jclass *v = elements (param_types);
346 for (int i = 0; i < param_types->length; ++i)
347 v[i]->getSignature(buf);
349 buf->append((jchar) ')');
351 buf->append((jchar) 'V');
352 return buf->toString();
355 java::lang::reflect::Method *
356 java::lang::Class::_getDeclaredMethod (jstring name,
357 JArray<jclass> *param_types)
359 jstring partial_sig = getSignature (param_types, false);
360 jint p_len = partial_sig->length();
361 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
362 int i = isPrimitive () ? 0 : method_count;
365 if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
366 && _Jv_equaln (methods[i].signature, partial_sig, p_len)
367 && (methods[i].accflags
368 & java::lang::reflect::Modifier::INVISIBLE) == 0)
371 using namespace java::lang::reflect;
372 Method *rmethod = new Method ();
373 rmethod->offset = (char*) (&methods[i]) - (char*) methods;
374 rmethod->declaringClass = this;
381 JArray<java::lang::reflect::Method *> *
382 java::lang::Class::getDeclaredMethods (void)
384 memberAccessCheck(java::lang::reflect::Member::DECLARED);
387 int max = isPrimitive () ? 0 : method_count;
389 for (i = max; --i >= 0; )
391 _Jv_Method *method = &methods[i];
392 if (method->name == NULL
393 || _Jv_equalUtf8Consts (method->name, clinit_name)
394 || _Jv_equalUtf8Consts (method->name, init_name)
395 || _Jv_equalUtf8Consts (method->name, finit_name)
396 || (methods[i].accflags
397 & java::lang::reflect::Modifier::INVISIBLE) != 0)
401 JArray<java::lang::reflect::Method *> *result
402 = (JArray<java::lang::reflect::Method *> *)
403 JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);
404 java::lang::reflect::Method** mptr = elements (result);
405 for (i = 0; i < max; i++)
407 _Jv_Method *method = &methods[i];
408 if (method->name == NULL
409 || _Jv_equalUtf8Consts (method->name, clinit_name)
410 || _Jv_equalUtf8Consts (method->name, init_name)
411 || _Jv_equalUtf8Consts (method->name, finit_name)
412 || (methods[i].accflags
413 & java::lang::reflect::Modifier::INVISIBLE) != 0)
415 java::lang::reflect::Method* rmethod
416 = new java::lang::reflect::Method ();
417 rmethod->offset = (char*) method - (char*) methods;
418 rmethod->declaringClass = this;
425 java::lang::Class::getName (void)
427 return name->toString();
431 java::lang::Class::getClasses (void)
433 // FIXME: security checking.
435 // Until we have inner classes, it always makes sense to return an
437 JArray<jclass> *result
438 = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
444 java::lang::Class::getDeclaredClasses (void)
446 memberAccessCheck (java::lang::reflect::Member::DECLARED);
447 // Until we have inner classes, it always makes sense to return an
449 JArray<jclass> *result
450 = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
456 java::lang::Class::getDeclaringClass (void)
458 // Until we have inner classes, it makes sense to always return
464 java::lang::Class::getInterfaces (void)
466 jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
467 jobject *data = elements (r);
468 for (int i = 0; i < interface_count; ++i)
470 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
471 data[i] = interfaces[i];
472 if ((uaddr)data[i] < (uaddr)constants.size)
473 fprintf (stderr, "ERROR !!!\n");
475 return reinterpret_cast<JArray<jclass> *> (r);
478 java::lang::reflect::Method *
479 java::lang::Class::_getMethod (jstring name, JArray<jclass> *param_types)
481 jstring partial_sig = getSignature (param_types, false);
482 jint p_len = partial_sig->length();
483 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
485 for (Class *klass = this; klass; klass = klass->getSuperclass())
487 int i = klass->isPrimitive () ? 0 : klass->method_count;
490 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
491 && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len)
492 && (klass->methods[i].accflags
493 & java::lang::reflect::Modifier::INVISIBLE) == 0)
496 using namespace java::lang::reflect;
498 // Method must be public.
499 if (! Modifier::isPublic (klass->methods[i].accflags))
502 Method *rmethod = new Method ();
503 rmethod->offset = ((char *) (&klass->methods[i])
504 - (char *) klass->methods);
505 rmethod->declaringClass = klass;
511 // If we haven't found a match, and this class is an interface, then
512 // check all the superinterfaces.
515 for (int i = 0; i < interface_count; ++i)
517 using namespace java::lang::reflect;
518 Method *rmethod = interfaces[i]->_getMethod (name, param_types);
527 // This is a very slow implementation, since it re-scans all the
528 // methods we've already listed to make sure we haven't duplicated a
529 // method. It also over-estimates the required size, so we have to
530 // shrink the result array later.
532 java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
537 // First examine all local methods
538 for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
540 _Jv_Method *method = &methods[i];
541 if (method->name == NULL
542 || _Jv_equalUtf8Consts (method->name, clinit_name)
543 || _Jv_equalUtf8Consts (method->name, init_name)
544 || _Jv_equalUtf8Consts (method->name, finit_name)
546 & java::lang::reflect::Modifier::INVISIBLE) != 0)
548 // Only want public methods.
549 if (! java::lang::reflect::Modifier::isPublic (method->accflags))
552 // This is where we over-count the slots required if we aren't
553 // filling the result for real.
557 java::lang::reflect::Method **mp = elements (result);
558 // If we already have a method with this name and signature,
559 // then ignore this one. This can happen with virtual
561 for (int j = 0; j < offset; ++j)
563 _Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
564 if (_Jv_equalUtf8Consts (method->name, meth_2->name)
565 && _Jv_equalUtf8Consts (method->signature,
578 using namespace java::lang::reflect;
579 Method *rmethod = new Method ();
580 rmethod->offset = (char *) method - (char *) methods;
581 rmethod->declaringClass = this;
582 Method **mp = elements (result);
583 mp[offset + count] = rmethod;
589 // Now examine superclasses.
590 if (getSuperclass () != NULL)
592 jint s_count = getSuperclass()->_getMethods (result, offset);
597 // Finally, examine interfaces.
598 for (int i = 0; i < interface_count; ++i)
600 int f_count = interfaces[i]->_getMethods (result, offset);
608 JArray<java::lang::reflect::Method *> *
609 java::lang::Class::getMethods (void)
611 using namespace java::lang::reflect;
613 memberAccessCheck(Member::PUBLIC);
615 // This will overestimate the size we need.
616 jint count = _getMethods (NULL, 0);
618 JArray<Method *> *result
619 = ((JArray<Method *> *) JvNewObjectArray (count,
623 // When filling the array for real, we get the actual count. Then
624 // we resize the array.
625 jint real_count = _getMethods (result, 0);
627 if (real_count != count)
630 = ((JArray<Method *> *) JvNewObjectArray (real_count,
634 Method **destp = elements (r2);
635 Method **srcp = elements (result);
637 for (int i = 0; i < real_count; ++i)
647 java::lang::Class::isAssignableFrom (jclass klass)
649 // Arguments may not have been initialized, given ".class" syntax.
650 _Jv_InitClass (this);
651 _Jv_InitClass (klass);
652 return _Jv_IsAssignableFrom (this, klass);
656 java::lang::Class::isInstance (jobject obj)
660 _Jv_InitClass (this);
661 return _Jv_IsAssignableFrom (this, JV_CLASS (obj));
665 java::lang::Class::newInstance (void)
667 memberAccessCheck(java::lang::reflect::Member::PUBLIC);
672 || java::lang::reflect::Modifier::isAbstract(accflags))
673 throw new java::lang::InstantiationException (getName ());
675 _Jv_InitClass (this);
677 _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
679 throw new java::lang::InstantiationException (getName());
681 jobject r = _Jv_AllocObject (this);
682 ((void (*) (jobject)) meth->ncode) (r);
687 java::lang::Class::finalize (void)
689 engine->unregister(this);
692 // This implements the initialization process for a class. From Spec
695 java::lang::Class::initializeClass (void)
697 // Short-circuit to avoid needless locking.
698 if (state == JV_STATE_DONE)
701 // Step 1. We introduce a new scope so we can synchronize more
704 JvSynchronize sync (this);
706 if (state < JV_STATE_LINKED)
707 java::lang::VMClassLoader::resolveClass (this);
710 java::lang::Thread *self = java::lang::Thread::currentThread();
711 self = (java::lang::Thread *) ((long) self | 1);
712 while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
716 if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS)
720 if (state == JV_STATE_ERROR)
721 throw new java::lang::NoClassDefFoundError (getName());
725 _Jv_Linker::wait_for_state (this, JV_STATE_LINKED);
726 state = JV_STATE_IN_PROGRESS;
730 if (! isInterface () && superclass)
734 _Jv_InitClass (superclass);
736 catch (java::lang::Throwable *except)
738 // Caught an exception.
739 JvSynchronize sync (this);
740 state = JV_STATE_ERROR;
746 // Steps 8, 9, 10, 11.
749 _Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name,
752 ((void (*) (void)) meth->ncode) ();
754 catch (java::lang::Throwable *except)
756 if (! java::lang::Error::class$.isInstance(except))
760 except = new ExceptionInInitializerError (except);
762 catch (java::lang::Throwable *t)
768 JvSynchronize sync (this);
769 state = JV_STATE_ERROR;
774 JvSynchronize sync (this);
775 state = JV_STATE_DONE;
779 // Only used by serialization
780 java::lang::reflect::Field *
781 java::lang::Class::getPrivateField (jstring name)
783 int hash = name->hashCode ();
785 java::lang::reflect::Field* rfield;
786 for (int i = 0; i < field_count; i++)
788 _Jv_Field *field = &fields[i];
789 if (! _Jv_equal (field->name, name, hash))
791 rfield = new java::lang::reflect::Field ();
792 rfield->offset = (char*) field - (char*) fields;
793 rfield->declaringClass = this;
797 jclass superclass = getSuperclass();
798 if (superclass == NULL)
800 rfield = superclass->getPrivateField(name);
801 for (int i = 0; i < interface_count && rfield == NULL; ++i)
802 rfield = interfaces[i]->getPrivateField (name);
806 // Only used by serialization
807 java::lang::reflect::Method *
808 java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
810 jstring partial_sig = getSignature (param_types, false);
811 jint p_len = partial_sig->length();
812 _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
813 for (Class *klass = this; klass; klass = klass->getSuperclass())
815 int i = klass->isPrimitive () ? 0 : klass->method_count;
818 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
819 && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
822 using namespace java::lang::reflect;
824 Method *rmethod = new Method ();
825 rmethod->offset = ((char *) (&klass->methods[i])
826 - (char *) klass->methods);
827 rmethod->declaringClass = klass;
832 throw new java::lang::NoSuchMethodException (name);
835 // Private accessor method for Java code to retrieve the protection domain.
836 java::security::ProtectionDomain *
837 java::lang::Class::getProtectionDomain0 ()
839 return protectionDomain;
843 java::lang::Class::getSigners()
849 java::lang::Class::setSigners(JArray<jobject> *s)
857 // Some class-related convenience functions.
860 // Find a method declared in the class. If it is not declared locally
861 // (or if it is inherited), return NULL.
863 _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
864 _Jv_Utf8Const *signature)
866 for (int i = 0; i < klass->method_count; ++i)
868 if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
869 && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
870 return &klass->methods[i];
876 _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
877 _Jv_Utf8Const *signature,
878 jclass *declarer_result)
880 for (; klass; klass = klass->getSuperclass())
882 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
887 *declarer_result = klass;
895 // NOTE: MCACHE_SIZE should be a power of 2 minus one.
896 #define MCACHE_SIZE 1023
904 static _Jv_mcache method_cache[MCACHE_SIZE + 1];
907 _Jv_FindMethodInCache (jclass klass,
909 _Jv_Utf8Const *signature)
911 int index = name->hash16 () & MCACHE_SIZE;
912 _Jv_mcache *mc = method_cache + index;
913 _Jv_Method *m = mc->method;
915 if (mc->klass == klass
916 && m != NULL // thread safe check
917 && _Jv_equalUtf8Consts (m->name, name)
918 && _Jv_equalUtf8Consts (m->signature, signature))
919 return mc->method->ncode;
924 _Jv_AddMethodToCache (jclass klass,
927 _Jv_MonitorEnter (&java::lang::Class::class$);
929 int index = method->name->hash16 () & MCACHE_SIZE;
931 method_cache[index].method = method;
932 method_cache[index].klass = klass;
934 _Jv_MonitorExit (&java::lang::Class::class$);
938 _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
939 _Jv_Utf8Const *signature)
941 using namespace java::lang::reflect;
943 void *ncode = _Jv_FindMethodInCache (klass, name, signature);
947 for (; klass; klass = klass->getSuperclass())
949 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
953 if (Modifier::isStatic(meth->accflags))
954 throw new java::lang::IncompatibleClassChangeError
955 (_Jv_GetMethodString (klass, meth));
956 if (Modifier::isAbstract(meth->accflags))
957 throw new java::lang::AbstractMethodError
958 (_Jv_GetMethodString (klass, meth));
959 if (! Modifier::isPublic(meth->accflags))
960 throw new java::lang::IllegalAccessError
961 (_Jv_GetMethodString (klass, meth));
963 _Jv_AddMethodToCache (klass, meth);
967 throw new java::lang::IncompatibleClassChangeError;
970 // Fast interface method lookup by index.
972 _Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
974 _Jv_IDispatchTable *cldt = klass->idt;
975 int idx = iface->idt->iface.ioffsets[cldt->cls.iindex] + method_idx;
976 return cldt->cls.itable[idx];
980 _Jv_IsAssignableFrom (jclass target, jclass source)
982 if (source == target)
985 // If target is array, so must source be.
986 while (target->isArray ())
988 if (! source->isArray())
990 target = target->getComponentType();
991 source = source->getComponentType();
994 if (target->isInterface())
996 // Abstract classes have no IDT, and IDTs provide no way to check
997 // two interfaces for assignability.
999 (source->idt == NULL || source->isInterface(), false))
1000 return _Jv_InterfaceAssignableFrom (target, source);
1002 _Jv_IDispatchTable *cl_idt = source->idt;
1003 _Jv_IDispatchTable *if_idt = target->idt;
1005 if (__builtin_expect ((if_idt == NULL), false))
1006 return false; // No class implementing TARGET has been loaded.
1007 jshort cl_iindex = cl_idt->cls.iindex;
1008 if (cl_iindex < if_idt->iface.ioffsets[0])
1010 jshort offset = if_idt->iface.ioffsets[cl_iindex];
1011 if (offset != -1 && offset < cl_idt->cls.itable_length
1012 && cl_idt->cls.itable[offset] == target)
1018 // Primitive TYPE classes are only assignable to themselves.
1019 if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
1022 if (target == &java::lang::Object::class$)
1024 else if (source->ancestors == NULL || target->ancestors == NULL)
1026 // We need this case when either SOURCE or TARGET has not has
1027 // its constant-time tables prepared.
1029 // At this point we know that TARGET can't be Object, so it is
1030 // safe to use that as the termination point.
1031 while (source && source != &java::lang::Object::class$)
1033 if (source == target)
1035 source = source->getSuperclass();
1038 else if (source->depth >= target->depth
1039 && source->ancestors[source->depth - target->depth] == target)
1045 // Interface type checking, the slow way. Returns TRUE if IFACE is a
1046 // superinterface of SOURCE. This is used when SOURCE is also an interface,
1047 // or a class with no interface dispatch table.
1049 _Jv_InterfaceAssignableFrom (jclass iface, jclass source)
1051 for (int i = 0; i < source->interface_count; i++)
1053 jclass interface = source->interfaces[i];
1054 if (iface == interface
1055 || _Jv_InterfaceAssignableFrom (iface, interface))
1059 if (!source->isInterface()
1060 && source->superclass
1061 && _Jv_InterfaceAssignableFrom (iface, source->superclass))
1068 _Jv_IsInstanceOf(jobject obj, jclass cl)
1070 if (__builtin_expect (!obj, false))
1072 return (_Jv_IsAssignableFrom (cl, JV_CLASS (obj)));
1076 _Jv_CheckCast (jclass c, jobject obj)
1078 if (__builtin_expect
1079 (obj != NULL && ! _Jv_IsAssignableFrom(c, JV_CLASS (obj)), false))
1080 throw new java::lang::ClassCastException
1081 ((new java::lang::StringBuffer
1082 (obj->getClass()->getName()))->append
1083 (JvNewStringUTF(" cannot be cast to "))->append
1084 (c->getName())->toString());
1090 _Jv_CheckArrayStore (jobject arr, jobject obj)
1094 JvAssert (arr != NULL);
1095 jclass elt_class = (JV_CLASS (arr))->getComponentType();
1096 if (elt_class == &java::lang::Object::class$)
1098 jclass obj_class = JV_CLASS (obj);
1099 if (__builtin_expect
1100 (! _Jv_IsAssignableFrom (elt_class, obj_class), false))
1101 throw new java::lang::ArrayStoreException
1102 ((new java::lang::StringBuffer
1103 (JvNewStringUTF("Cannot store ")))->append
1104 (obj_class->getName())->append
1105 (JvNewStringUTF(" in array of type "))->append
1106 (elt_class->getName())->toString());
1111 _Jv_IsAssignableFromSlow (jclass target, jclass source)
1113 // First, strip arrays.
1114 while (target->isArray ())
1116 // If target is array, source must be as well.
1117 if (! source->isArray ())
1119 target = target->getComponentType ();
1120 source = source->getComponentType ();
1124 if (target == &java::lang::Object::class$)
1127 // Ensure that the classes have their supers installed.
1128 _Jv_Linker::wait_for_state (source, JV_STATE_LOADING);
1129 _Jv_Linker::wait_for_state (target, JV_STATE_LOADING);
1133 if (source == target)
1136 if (target->isPrimitive () || source->isPrimitive ())
1139 if (target->isInterface ())
1141 for (int i = 0; i < source->interface_count; ++i)
1143 // We use a recursive call because we also need to
1144 // check superinterfaces.
1145 if (_Jv_IsAssignableFromSlow (target, source->getInterface (i)))
1149 source = source->getSuperclass ();
1151 while (source != NULL);
1156 // Lookup an interface method by name. This is very similar to
1157 // purpose to _getMethod, but the interfaces are quite different. It
1158 // might be a good idea for _getMethod to call this function.
1160 // Return true of the method is found, with the class in FOUND_CLASS
1161 // and the index in INDEX.
1163 _Jv_getInterfaceMethod (jclass search_class, jclass &found_class, int &index,
1164 const _Jv_Utf8Const *utf_name,
1165 const _Jv_Utf8Const *utf_sig)
1167 for (jclass klass = search_class; klass; klass = klass->getSuperclass())
1169 // FIXME: Throw an exception?
1170 if (!klass->isInterface ())
1173 int i = klass->method_count;
1176 if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
1177 && _Jv_equalUtf8Consts (klass->methods[i].signature, utf_sig))
1180 using namespace java::lang::reflect;
1182 // FIXME: Method must be public. Throw an exception?
1183 if (! Modifier::isPublic (klass->methods[i].accflags))
1186 found_class = klass;
1187 // Interface method indexes count from 1.
1194 // If we haven't found a match, and this class is an interface, then
1195 // check all the superinterfaces.
1196 if (search_class->isInterface())
1198 for (int i = 0; i < search_class->interface_count; ++i)
1200 using namespace java::lang::reflect;
1201 bool found = _Jv_getInterfaceMethod (search_class->interfaces[i],