OSDN Git Service

* java/lang/natClass.cc (_Jv_getInterfaceMethod): Skip <clinit>.
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / natClass.cc
index 25e92c7..d888350 100644 (file)
@@ -1,6 +1,7 @@
 // natClass.cc - Implementation of java.lang.Class native methods.
 
-/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006  
+   Free Software Foundation
 
    This file is part of libgcj.
 
@@ -12,6 +13,8 @@ details.  */
 
 #include <limits.h>
 #include <string.h>
+#include <stddef.h>
+#include <stdio.h>
 
 #pragma implementation "Class.h"
 
@@ -34,7 +37,10 @@ details.  */
 #include <java/lang/ExceptionInInitializerError.h>
 #include <java/lang/IllegalAccessException.h>
 #include <java/lang/IllegalAccessError.h>
+#include <java/lang/IllegalArgumentException.h>
 #include <java/lang/IncompatibleClassChangeError.h>
+#include <java/lang/NoSuchFieldError.h>
+#include <java/lang/ArrayIndexOutOfBoundsException.h>
 #include <java/lang/InstantiationException.h>
 #include <java/lang/NoClassDefFoundError.h>
 #include <java/lang/NoSuchFieldException.h>
@@ -42,79 +48,98 @@ details.  */
 #include <java/lang/NoSuchMethodException.h>
 #include <java/lang/Thread.h>
 #include <java/lang/NullPointerException.h>
+#include <java/lang/RuntimePermission.h>
 #include <java/lang/System.h>
 #include <java/lang/SecurityManager.h>
 #include <java/lang/StringBuffer.h>
+#include <java/lang/VMClassLoader.h>
 #include <gcj/method.h>
+#include <gnu/gcj/RawData.h>
+#include <java/lang/VerifyError.h>
 
 #include <java-cpool.h>
+#include <java-interp.h>
+#include <java-assert.h>
+#include <java-stack.h>
+#include <execution.h>
 
 \f
 
-// FIXME: remove these.
-#define CloneableClass java::lang::Cloneable::class$
-#define ObjectClass java::lang::Object::class$
-#define ErrorClass java::lang::Error::class$
-#define ClassClass java::lang::Class::class$
-#define MethodClass java::lang::reflect::Method::class$
-#define FieldClass java::lang::reflect::Field::class$
-#define ConstructorClass java::lang::reflect::Constructor::class$
-
-// Some constants we use to look up the class initializer.
-static _Jv_Utf8Const *void_signature = _Jv_makeUtf8Const ("()V", 3);
-static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
-static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
-static _Jv_Utf8Const *finit_name = _Jv_makeUtf8Const ("finit$", 6);
-// The legacy `$finit$' method name, which still needs to be
-// recognized as equivalent to the now prefered `finit$' name.
-static _Jv_Utf8Const *finit_leg_name = _Jv_makeUtf8Const ("$finit$", 7);
-
-\f
+using namespace gcj;
 
 jclass
-java::lang::Class::forName (jstring className, java::lang::ClassLoader *loader)
+java::lang::Class::forName (jstring className, jboolean initialize,
+                            java::lang::ClassLoader *loader)
 {
   if (! className)
     throw new java::lang::NullPointerException;
 
   jsize length = _Jv_GetStringUTFLength (className);
   char buffer[length];
-  _Jv_GetStringUTFRegion (className, 0, length, buffer);
+  _Jv_GetStringUTFRegion (className, 0, className->length(), buffer);
 
-  // FIXME: should check syntax of CLASSNAME and throw
-  // IllegalArgumentException on failure.
   _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
 
-  // FIXME: should use bootstrap class loader if loader is null.
+  if (! _Jv_VerifyClassName (name))
+    throw new java::lang::ClassNotFoundException (className);
+
   jclass klass = (buffer[0] == '[' 
-                 ? _Jv_FindClassFromSignature (name->data, loader)
+                 ? _Jv_FindClassFromSignature (name->chars(), loader)
                  : _Jv_FindClass (name, loader));
 
-  if (klass)
-    _Jv_InitClass (klass);
-  else
+  if (klass == NULL)
     throw new java::lang::ClassNotFoundException (className);
 
+  if (initialize)
+    _Jv_InitClass (klass);
+
   return klass;
 }
 
 jclass
 java::lang::Class::forName (jstring className)
 {
-  // FIXME: should use class loader from calling method.
-  return forName (className, NULL);
+  java::lang::ClassLoader *loader = NULL;
+
+  jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
+  if (caller)
+    loader = caller->getClassLoaderInternal();
+
+  return forName (className, true, loader);
+}
+
+java::lang::ClassLoader *
+java::lang::Class::getClassLoader (void)
+{
+  java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
+  if (s != NULL)
+    {
+      jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
+      ClassLoader *caller_loader = NULL;
+      if (caller)
+       caller_loader = caller->getClassLoaderInternal();
+
+      // If the caller has a non-null class loader, and that loader
+      // is not this class' loader or an ancestor thereof, then do a
+      // security check.
+      if (caller_loader != NULL && ! caller_loader->isAncestorOf(loader))
+       s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
+    }
+
+  return loader;
 }
 
 java::lang::reflect::Constructor *
 java::lang::Class::getConstructor (JArray<jclass> *param_types)
 {
+  memberAccessCheck(java::lang::reflect::Member::PUBLIC);
+
   jstring partial_sig = getSignature (param_types, true);
   jint hash = partial_sig->hashCode ();
 
   int i = isPrimitive () ? 0 : method_count;
   while (--i >= 0)
     {
-      // FIXME: access checks.
       if (_Jv_equalUtf8Consts (methods[i].name, init_name)
          && _Jv_equal (methods[i].signature, partial_sig, hash))
        {
@@ -129,14 +154,12 @@ java::lang::Class::getConstructor (JArray<jclass> *param_types)
          return cons;
        }
     }
-  throw new java::lang::NoSuchMethodException;
+  throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
 }
 
 JArray<java::lang::reflect::Constructor *> *
-java::lang::Class::_getConstructors (jboolean declared)
+java::lang::Class::getDeclaredConstructors (jboolean publicOnly)
 {
-  // FIXME: this method needs access checks.
-
   int numConstructors = 0;
   int max = isPrimitive () ? 0 : method_count;
   int i;
@@ -146,14 +169,16 @@ java::lang::Class::_getConstructors (jboolean declared)
       if (method->name == NULL
          || ! _Jv_equalUtf8Consts (method->name, init_name))
        continue;
-      if (! declared
+      if (publicOnly
          && ! java::lang::reflect::Modifier::isPublic(method->accflags))
        continue;
       numConstructors++;
     }
   JArray<java::lang::reflect::Constructor *> *result
     = (JArray<java::lang::reflect::Constructor *> *)
-    JvNewObjectArray (numConstructors, &ConstructorClass, NULL);
+    JvNewObjectArray (numConstructors,
+                     &java::lang::reflect::Constructor::class$,
+                     NULL);
   java::lang::reflect::Constructor** cptr = elements (result);
   for (i = 0;  i < max;  i++)
     {
@@ -161,7 +186,7 @@ java::lang::Class::_getConstructors (jboolean declared)
       if (method->name == NULL
          || ! _Jv_equalUtf8Consts (method->name, init_name))
        continue;
-      if (! declared
+      if (publicOnly
          && ! java::lang::reflect::Modifier::isPublic(method->accflags))
        continue;
       java::lang::reflect::Constructor *cons
@@ -176,13 +201,14 @@ java::lang::Class::_getConstructors (jboolean declared)
 java::lang::reflect::Constructor *
 java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
 {
+  memberAccessCheck(java::lang::reflect::Member::DECLARED);
+
   jstring partial_sig = getSignature (param_types, true);
   jint hash = partial_sig->hashCode ();
 
   int i = isPrimitive () ? 0 : method_count;
   while (--i >= 0)
     {
-      // FIXME: access checks.
       if (_Jv_equalUtf8Consts (methods[i].name, init_name)
          && _Jv_equal (methods[i].signature, partial_sig, hash))
        {
@@ -194,7 +220,7 @@ java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
          return cons;
        }
     }
-  throw new java::lang::NoSuchMethodException;
+  throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
 }
 
 java::lang::reflect::Field *
@@ -226,9 +252,7 @@ java::lang::Class::getField (jstring name, jint hash)
 java::lang::reflect::Field *
 java::lang::Class::getDeclaredField (jstring name)
 {
-  java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
-  if (s != NULL)
-    s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);
+  memberAccessCheck(java::lang::reflect::Member::DECLARED);
   int hash = name->hashCode();
   for (int i = 0;  i < field_count;  i++)
     {
@@ -245,18 +269,32 @@ java::lang::Class::getDeclaredField (jstring name)
 }
 
 JArray<java::lang::reflect::Field *> *
-java::lang::Class::getDeclaredFields (void)
+java::lang::Class::getDeclaredFields (jboolean public_only)
 {
-  java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
-  if (s != NULL)
-    s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);
+  int size;
+  if (public_only)
+    {
+      size = 0;
+      for (int i = 0; i < field_count; ++i)
+       {
+         _Jv_Field *field = &fields[i];
+         if ((field->flags & java::lang::reflect::Modifier::PUBLIC))
+           ++size;
+       }
+    }
+  else
+    size = field_count;
+
   JArray<java::lang::reflect::Field *> *result
     = (JArray<java::lang::reflect::Field *> *)
-    JvNewObjectArray (field_count, &FieldClass, NULL);
+    JvNewObjectArray (size, &java::lang::reflect::Field::class$, NULL);
   java::lang::reflect::Field** fptr = elements (result);
   for (int i = 0;  i < field_count;  i++)
     {
       _Jv_Field *field = &fields[i];
+      if (public_only
+         && ! (field->flags & java::lang::reflect::Modifier::PUBLIC))
+       continue;
       java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
       rfield->offset = (char*) field - (char*) fields;
       rfield->declaringClass = this;
@@ -289,10 +327,10 @@ java::lang::Class::getSignature (JArray<jclass> *param_types,
 {
   java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
   buf->append((jchar) '(');
-  jclass *v = elements (param_types);
   // A NULL param_types means "no parameters".
   if (param_types != NULL)
     {
+      jclass *v = elements (param_types);
       for (int i = 0; i < param_types->length; ++i)
        v[i]->getSignature(buf);
     }
@@ -303,8 +341,8 @@ java::lang::Class::getSignature (JArray<jclass> *param_types,
 }
 
 java::lang::reflect::Method *
-java::lang::Class::getDeclaredMethod (jstring name,
-                                     JArray<jclass> *param_types)
+java::lang::Class::_getDeclaredMethod (jstring name,
+                                      JArray<jclass> *param_types)
 {
   jstring partial_sig = getSignature (param_types, false);
   jint p_len = partial_sig->length();
@@ -312,9 +350,10 @@ java::lang::Class::getDeclaredMethod (jstring name,
   int i = isPrimitive () ? 0 : method_count;
   while (--i >= 0)
     {
-      // FIXME: access checks.
       if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
-         && _Jv_equaln (methods[i].signature, partial_sig, p_len))
+         && _Jv_equaln (methods[i].signature, partial_sig, p_len)
+         && (methods[i].accflags
+             & java::lang::reflect::Modifier::INVISIBLE) == 0)
        {
          // Found it.
          using namespace java::lang::reflect;
@@ -324,12 +363,14 @@ java::lang::Class::getDeclaredMethod (jstring name,
          return rmethod;
        }
     }
-  throw new java::lang::NoSuchMethodException;
+  return NULL;
 }
 
 JArray<java::lang::reflect::Method *> *
 java::lang::Class::getDeclaredMethods (void)
 {
+  memberAccessCheck(java::lang::reflect::Member::DECLARED);
+
   int numMethods = 0;
   int max = isPrimitive () ? 0 : method_count;
   int i;
@@ -340,14 +381,14 @@ java::lang::Class::getDeclaredMethods (void)
          || _Jv_equalUtf8Consts (method->name, clinit_name)
          || _Jv_equalUtf8Consts (method->name, init_name)
          || _Jv_equalUtf8Consts (method->name, finit_name)
-         // Backward compatibility hack: match the legacy `$finit$' name
-         || _Jv_equalUtf8Consts (method->name, finit_leg_name))
+         || (methods[i].accflags
+             & java::lang::reflect::Modifier::INVISIBLE) != 0)
        continue;
       numMethods++;
     }
   JArray<java::lang::reflect::Method *> *result
     = (JArray<java::lang::reflect::Method *> *)
-    JvNewObjectArray (numMethods, &MethodClass, NULL);
+    JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);
   java::lang::reflect::Method** mptr = elements (result);
   for (i = 0;  i < max;  i++)
     {
@@ -356,8 +397,8 @@ java::lang::Class::getDeclaredMethods (void)
          || _Jv_equalUtf8Consts (method->name, clinit_name)
          || _Jv_equalUtf8Consts (method->name, init_name)
          || _Jv_equalUtf8Consts (method->name, finit_name)
-         // Backward compatibility hack: match the legacy `$finit$' name
-         || _Jv_equalUtf8Consts (method->name, finit_leg_name))
+         || (methods[i].accflags
+             & java::lang::reflect::Modifier::INVISIBLE) != 0)
        continue;
       java::lang::reflect::Method* rmethod
        = new java::lang::reflect::Method ();
@@ -371,30 +412,17 @@ java::lang::Class::getDeclaredMethods (void)
 jstring
 java::lang::Class::getName (void)
 {
-  char buffer[name->length + 1];  
-  memcpy (buffer, name->data, name->length); 
-  buffer[name->length] = '\0';
-  return _Jv_NewStringUTF (buffer);
+  return name->toString();
 }
 
 JArray<jclass> *
-java::lang::Class::getClasses (void)
+java::lang::Class::getDeclaredClasses (jboolean /*publicOnly*/)
 {
   // Until we have inner classes, it always makes sense to return an
   // empty array.
   JArray<jclass> *result
-    = (JArray<jclass> *) JvNewObjectArray (0, &ClassClass, NULL);
-  return result;
-}
-
-JArray<jclass> *
-java::lang::Class::getDeclaredClasses (void)
-{
-  checkMemberAccess (java::lang::reflect::Member::DECLARED);
-  // Until we have inner classes, it always makes sense to return an
-  // empty array.
-  JArray<jclass> *result
-    = (JArray<jclass> *) JvNewObjectArray (0, &ClassClass, NULL);
+    = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
+                                          NULL);
   return result;
 }
 
@@ -406,84 +434,37 @@ java::lang::Class::getDeclaringClass (void)
   return NULL;
 }
 
-jint
-java::lang::Class::_getFields (JArray<java::lang::reflect::Field *> *result,
-                              jint offset)
-{
-  int count = 0;
-  for (int i = 0;  i < field_count;  i++)
-    {
-      _Jv_Field *field = &fields[i];
-      if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
-       continue;
-      ++count;
-
-      if (result != NULL)
-       {
-         java::lang::reflect::Field *rfield
-           = new java::lang::reflect::Field ();
-         rfield->offset = (char *) field - (char *) fields;
-         rfield->declaringClass = this;
-         rfield->name = _Jv_NewStringUtf8Const (field->name);
-         (elements (result))[offset++] = rfield;
-       }
-    }
-  jclass superclass = getSuperclass();
-  if (superclass != NULL)
-    {
-      int s_count = superclass->_getFields (result, offset);
-      count += s_count;
-      offset += s_count;
-    }
-  for (int i = 0; i < interface_count; ++i)
-    {
-      int f_count = interfaces[i]->_getFields (result, offset);
-      count += f_count;
-      offset += f_count;
-    }
-  return count;
-}
-
-JArray<java::lang::reflect::Field *> *
-java::lang::Class::getFields (void)
-{
-  using namespace java::lang::reflect;
-
-  int count = _getFields (NULL, 0);
-
-  JArray<java::lang::reflect::Field *> *result
-    = ((JArray<java::lang::reflect::Field *> *)
-       JvNewObjectArray (count, &FieldClass, NULL));
-
-  _getFields (result, 0);
-
-  return result;
-}
-
 JArray<jclass> *
 java::lang::Class::getInterfaces (void)
 {
   jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
   jobject *data = elements (r);
   for (int i = 0; i < interface_count; ++i)
-    data[i] = interfaces[i];
+    {
+      typedef unsigned int uaddr __attribute__ ((mode (pointer)));
+      data[i] = interfaces[i];
+      if ((uaddr)data[i] < (uaddr)constants.size)
+       fprintf (stderr, "ERROR !!!\n");
+    }
   return reinterpret_cast<JArray<jclass> *> (r);
 }
 
 java::lang::reflect::Method *
-java::lang::Class::getMethod (jstring name, JArray<jclass> *param_types)
+java::lang::Class::_getMethod (jstring name, JArray<jclass> *param_types)
 {
   jstring partial_sig = getSignature (param_types, false);
   jint p_len = partial_sig->length();
   _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
-  for (Class *klass = this; klass; klass = klass->getSuperclass())
+
+   for (Class *klass = this; klass; klass = klass->getSuperclass())
     {
       int i = klass->isPrimitive () ? 0 : klass->method_count;
       while (--i >= 0)
        {
-         // FIXME: access checks.
          if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
-             && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
+             && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len)
+             && (klass->methods[i].accflags
+                 & java::lang::reflect::Modifier::INVISIBLE) == 0)
            {
              // Found it.
              using namespace java::lang::reflect;
@@ -500,7 +481,21 @@ java::lang::Class::getMethod (jstring name, JArray<jclass> *param_types)
            }
        }
     }
-  throw new java::lang::NoSuchMethodException;
+
+  // If we haven't found a match, and this class is an interface, then
+  // check all the superinterfaces.
+  if (isInterface())
+    {
+      for (int i = 0; i < interface_count; ++i)
+       {
+         using namespace java::lang::reflect;
+         Method *rmethod = interfaces[i]->_getMethod (name, param_types);
+         if (rmethod != NULL)
+           return rmethod;
+       }
+    }
+
+  return NULL;
 }
 
 // This is a very slow implementation, since it re-scans all the
@@ -521,8 +516,8 @@ java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
          || _Jv_equalUtf8Consts (method->name, clinit_name)
          || _Jv_equalUtf8Consts (method->name, init_name)
          || _Jv_equalUtf8Consts (method->name, finit_name)
-         // Backward compatibility hack: match the legacy `$finit$' name
-         || _Jv_equalUtf8Consts (method->name, finit_leg_name))
+         || (method->accflags
+             & java::lang::reflect::Modifier::INVISIBLE) != 0)
        continue;
       // Only want public methods.
       if (! java::lang::reflect::Modifier::isPublic (method->accflags))
@@ -589,13 +584,15 @@ java::lang::Class::getMethods (void)
 {
   using namespace java::lang::reflect;
 
-  // FIXME: security checks.
+  memberAccessCheck(Member::PUBLIC);
 
   // This will overestimate the size we need.
   jint count = _getMethods (NULL, 0);
 
   JArray<Method *> *result
-    = ((JArray<Method *> *) JvNewObjectArray (count, &MethodClass, NULL));
+    = ((JArray<Method *> *) JvNewObjectArray (count,
+                                             &Method::class$,
+                                             NULL));
 
   // When filling the array for real, we get the actual count.  Then
   // we resize the array.
@@ -604,7 +601,8 @@ java::lang::Class::getMethods (void)
   if (real_count != count)
     {
       JArray<Method *> *r2
-       = ((JArray<Method *> *) JvNewObjectArray (real_count, &MethodClass,
+       = ((JArray<Method *> *) JvNewObjectArray (real_count,
+                                                 &Method::class$,
                                                  NULL));
       
       Method **destp = elements (r2);
@@ -623,43 +621,38 @@ jboolean
 java::lang::Class::isAssignableFrom (jclass klass)
 {
   // Arguments may not have been initialized, given ".class" syntax.
-  _Jv_InitClass (this);
-  _Jv_InitClass (klass);
-  return _Jv_IsAssignableFrom (this, klass);
+  // This ensures we can at least look at their superclasses.
+  _Jv_Linker::wait_for_state (this, JV_STATE_LOADING);
+  _Jv_Linker::wait_for_state (klass, JV_STATE_LOADING);
+  return _Jv_IsAssignableFrom (klass, this);
 }
 
 jboolean
 java::lang::Class::isInstance (jobject obj)
 {
-  if (__builtin_expect (! obj || isPrimitive (), false))
+  if (! obj)
     return false;
-  _Jv_InitClass (this);
-  return _Jv_IsAssignableFrom (this, JV_CLASS (obj));
+  return _Jv_IsAssignableFrom (JV_CLASS (obj), this);
 }
 
 jobject
 java::lang::Class::newInstance (void)
 {
-  // FIXME: do accessibility checks here.  There currently doesn't
-  // seem to be any way to do these.
-  // FIXME: we special-case one check here just to pass a Plum Hall
-  // test.  Once access checking is implemented, remove this.
-  if (this == &ClassClass)
-    throw new java::lang::IllegalAccessException;
+  memberAccessCheck(java::lang::reflect::Member::PUBLIC);
 
   if (isPrimitive ()
       || isInterface ()
       || isArray ()
       || java::lang::reflect::Modifier::isAbstract(accflags))
-    throw new java::lang::InstantiationException;
+    throw new java::lang::InstantiationException (getName ());
 
   _Jv_InitClass (this);
 
   _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
   if (! meth)
-    throw new java::lang::NoSuchMethodException;
+    throw new java::lang::InstantiationException (getName());
 
-  jobject r = JvAllocObject (this);
+  jobject r = _Jv_AllocObject (this);
   ((void (*) (jobject)) meth->ncode) (r);
   return r;
 }
@@ -667,10 +660,7 @@ java::lang::Class::newInstance (void)
 void
 java::lang::Class::finalize (void)
 {
-#ifdef INTERPRETER
-  JvAssert (_Jv_IsInterpretedClass (this));
-  _Jv_UnregisterClass (this);
-#endif
+  engine->unregister(this);
 }
 
 // This implements the initialization process for a class.  From Spec
@@ -678,60 +668,51 @@ java::lang::Class::finalize (void)
 void
 java::lang::Class::initializeClass (void)
 {
-  // short-circuit to avoid needless locking.
-  if (state == JV_STATE_DONE)
+  // Short-circuit to avoid needless locking (expression includes
+  // JV_STATE_PHANTOM and JV_STATE_DONE).
+  if (state >= JV_STATE_PHANTOM)
     return;
 
-  // Step 1.
-  _Jv_MonitorEnter (this);
-
-  if (state < JV_STATE_LINKED)
-    {    
-#ifdef INTERPRETER
-      if (_Jv_IsInterpretedClass (this))
-       {
-         // this can throw exceptions, so exit the monitor as a precaution.
-         _Jv_MonitorExit (this);
-         java::lang::ClassLoader::resolveClass0 (this);
-         _Jv_MonitorEnter (this);
-       }
-      else
-#endif
-        {
-         _Jv_PrepareCompiledClass (this);
-       }
-    }
-  
-  if (state <= JV_STATE_LINKED)
-    _Jv_PrepareConstantTimeTables (this);
-
-  // Step 2.
-  java::lang::Thread *self = java::lang::Thread::currentThread();
-  // FIXME: `self' can be null at startup.  Hence this nasty trick.
-  self = (java::lang::Thread *) ((long) self | 1);
-  while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
-    wait ();
-
-  // Steps 3 &  4.
-  if (state == JV_STATE_DONE
-      || state == JV_STATE_IN_PROGRESS
-      || thread == self)
-    {
-      _Jv_MonitorExit (this);
+  // Step 1.  We introduce a new scope so we can synchronize more
+  // easily.
+  {
+    JvSynchronize sync (this);
+
+    if (state < JV_STATE_LINKED)
+      {
+       try
+         {
+           _Jv_Linker::wait_for_state(this, JV_STATE_LINKED);
+         }
+       catch (java::lang::Throwable *x)
+         {
+           // Turn into a NoClassDefFoundError.
+           java::lang::NoClassDefFoundError *result
+             = new java::lang::NoClassDefFoundError(getName());
+           result->initCause(x);
+           throw result;
+         }
+      }
+
+    // Step 2.
+    java::lang::Thread *self = java::lang::Thread::currentThread();
+    self = (java::lang::Thread *) ((long) self | 1);
+    while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
+      wait ();
+
+    // Steps 3 &  4.
+    if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS)
       return;
-    }
 
-  // Step 5.
-  if (state == JV_STATE_ERROR)
-    {
-      _Jv_MonitorExit (this);
-      throw new java::lang::NoClassDefFoundError;
-    }
+    // Step 5.
+    if (state == JV_STATE_ERROR)
+      throw new java::lang::NoClassDefFoundError (getName());
 
-  // Step 6.
-  thread = self;
-  state = JV_STATE_IN_PROGRESS;
-  _Jv_MonitorExit (this);
+    // Step 6.
+    thread = self;
+    _Jv_Linker::wait_for_state (this, JV_STATE_LINKED);
+    state = JV_STATE_IN_PROGRESS;
+  }
 
   // Step 7.
   if (! isInterface () && superclass)
@@ -743,10 +724,9 @@ java::lang::Class::initializeClass (void)
       catch (java::lang::Throwable *except)
        {
          // Caught an exception.
-         _Jv_MonitorEnter (this);
+         JvSynchronize sync (this);
          state = JV_STATE_ERROR;
          notifyAll ();
-         _Jv_MonitorExit (this);
          throw except;
        }
     }
@@ -761,7 +741,7 @@ java::lang::Class::initializeClass (void)
     }
   catch (java::lang::Throwable *except)
     {
-      if (! ErrorClass.isInstance(except))
+      if (! java::lang::Error::class$.isInstance(except))
        {
          try
            {
@@ -772,17 +752,91 @@ java::lang::Class::initializeClass (void)
              except = t;
            }
        }
-      _Jv_MonitorEnter (this);
+
+      JvSynchronize sync (this);
       state = JV_STATE_ERROR;
       notifyAll ();
-      _Jv_MonitorExit (this);
       throw except;
     }
 
-  _Jv_MonitorEnter (this);
+  JvSynchronize sync (this);
   state = JV_STATE_DONE;
   notifyAll ();
-  _Jv_MonitorExit (this);
+}
+
+// Only used by serialization
+java::lang::reflect::Field *
+java::lang::Class::getPrivateField (jstring name)
+{
+  int hash = name->hashCode ();
+
+  java::lang::reflect::Field* rfield;
+  for (int i = 0;  i < field_count;  i++)
+    {
+      _Jv_Field *field = &fields[i];
+      if (! _Jv_equal (field->name, name, hash))
+       continue;
+      rfield = new java::lang::reflect::Field ();
+      rfield->offset = (char*) field - (char*) fields;
+      rfield->declaringClass = this;
+      rfield->name = name;
+      return rfield;
+    }
+  jclass superclass = getSuperclass();
+  if (superclass == NULL)
+    return NULL;
+  rfield = superclass->getPrivateField(name);
+  for (int i = 0; i < interface_count && rfield == NULL; ++i)
+    rfield = interfaces[i]->getPrivateField (name);
+  return rfield;
+}
+
+// Only used by serialization
+java::lang::reflect::Method *
+java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
+{
+  jstring partial_sig = getSignature (param_types, false);
+  jint p_len = partial_sig->length();
+  _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
+  for (Class *klass = this; klass; klass = klass->getSuperclass())
+    {
+      int i = klass->isPrimitive () ? 0 : klass->method_count;
+      while (--i >= 0)
+       {
+         if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
+             && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
+           {
+             // Found it.
+             using namespace java::lang::reflect;
+
+             Method *rmethod = new Method ();
+             rmethod->offset = ((char *) (&klass->methods[i])
+                                - (char *) klass->methods);
+             rmethod->declaringClass = klass;
+             return rmethod;
+           }
+       }
+    }
+  throw new java::lang::NoSuchMethodException (name);
+}
+
+// Private accessor method for Java code to retrieve the protection domain.
+java::security::ProtectionDomain *
+java::lang::Class::getProtectionDomain0 ()
+{
+  return protectionDomain;
+}
+
+JArray<jobject> *
+java::lang::Class::getSigners()
+{
+  return hack_signers;
+}
+
+void
+java::lang::Class::setSigners(JArray<jobject> *s)
+{
+  hack_signers = s;
 }
 
 \f
@@ -808,21 +862,28 @@ _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
 
 _Jv_Method *
 _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
-                          _Jv_Utf8Const *signature)
+                          _Jv_Utf8Const *signature,
+                         jclass *declarer_result)
 {
   for (; klass; klass = klass->getSuperclass())
     {
       _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
 
       if (meth)
-        return meth;
+       {
+         if (declarer_result)
+           *declarer_result = klass;
+         return meth;
+       }
     }
 
   return NULL;
 }
 
+#ifdef HAVE_TLS
+
 // NOTE: MCACHE_SIZE should be a power of 2 minus one.
-#define MCACHE_SIZE 1023
+#define MCACHE_SIZE 31
 
 struct _Jv_mcache
 {
@@ -830,37 +891,60 @@ struct _Jv_mcache
   _Jv_Method *method;
 };
 
-static _Jv_mcache method_cache[MCACHE_SIZE + 1];
+static __thread _Jv_mcache *method_cache;
+#endif // HAVE_TLS
 
 static void *
 _Jv_FindMethodInCache (jclass klass,
                        _Jv_Utf8Const *name,
                        _Jv_Utf8Const *signature)
 {
-  int index = name->hash & MCACHE_SIZE;
-  _Jv_mcache *mc = method_cache + index;
-  _Jv_Method *m = mc->method;
-
-  if (mc->klass == klass
-      && m != NULL             // thread safe check
-      && _Jv_equalUtf8Consts (m->name, name)
-      && _Jv_equalUtf8Consts (m->signature, signature))
-    return mc->method->ncode;
+#ifdef HAVE_TLS
+  _Jv_mcache *cache = method_cache;
+  if (cache)
+    {
+      int index = name->hash16 () & MCACHE_SIZE;
+      _Jv_mcache *mc = &cache[index];
+      _Jv_Method *m = mc->method;
+
+      if (mc->klass == klass
+         && _Jv_equalUtf8Consts (m->name, name)
+         && _Jv_equalUtf8Consts (m->signature, signature))
+       return mc->method->ncode;
+    }
+#endif // HAVE_TLS
   return NULL;
 }
 
 static void
-_Jv_AddMethodToCache (jclass klass,
-                       _Jv_Method *method)
+_Jv_AddMethodToCache (jclass klass, _Jv_Method *method)
 {
-  _Jv_MonitorEnter (&ClassClass); 
-
-  int index = method->name->hash & MCACHE_SIZE;
-
-  method_cache[index].method = method;
-  method_cache[index].klass = klass;
+#ifdef HAVE_TLS
+  if (method_cache == NULL)
+    method_cache = (_Jv_mcache *) _Jv_MallocUnchecked((MCACHE_SIZE + 1)
+                                                     * sizeof (_Jv_mcache));
+  // If the allocation failed, just keep going.
+  if (method_cache != NULL)
+    {
+      int index = method->name->hash16 () & MCACHE_SIZE;
+      method_cache[index].method = method;
+      method_cache[index].klass = klass;
+    }
+#endif // HAVE_TLS
+}
 
-  _Jv_MonitorExit (&ClassClass);
+// Free this thread's method cache.  We explicitly manage this memory
+// as the GC does not yet know how to scan TLS on all platforms.
+void
+_Jv_FreeMethodCache ()
+{
+#ifdef HAVE_TLS
+  if (method_cache != NULL)
+    {
+      _Jv_Free(method_cache);
+      method_cache = NULL;
+    }
+#endif // HAVE_TLS
 }
 
 void *
@@ -881,13 +965,13 @@ _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
 
       if (Modifier::isStatic(meth->accflags))
        throw new java::lang::IncompatibleClassChangeError
-         (_Jv_GetMethodString (klass, meth->name));
+         (_Jv_GetMethodString (klass, meth));
       if (Modifier::isAbstract(meth->accflags))
        throw new java::lang::AbstractMethodError
-         (_Jv_GetMethodString (klass, meth->name));
+         (_Jv_GetMethodString (klass, meth));
       if (! Modifier::isPublic(meth->accflags))
        throw new java::lang::IllegalAccessError
-         (_Jv_GetMethodString (klass, meth->name));
+         (_Jv_GetMethodString (klass, meth));
 
       _Jv_AddMethodToCache (klass, meth);
 
@@ -901,23 +985,23 @@ void *
 _Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
 {
   _Jv_IDispatchTable *cldt = klass->idt;
-  int idx = iface->idt->iface.ioffsets[cldt->cls.iindex] + method_idx;
-  return cldt->cls.itable[idx];
+  int idx = iface->ioffsets[cldt->iindex] + method_idx;
+  return cldt->itable[idx];
 }
 
 jboolean
-_Jv_IsAssignableFrom (jclass target, jclass source)
+_Jv_IsAssignableFrom (jclass source, jclass target)
 {
   if (source == target)
     return true;
-     
+
   // If target is array, so must source be.  
-  if (target->isArray ())
+  while (target->isArray ())
     {
       if (! source->isArray())
        return false;
-      return _Jv_IsAssignableFrom(target->getComponentType(), 
-                                  source->getComponentType());
+      target = target->getComponentType();
+      source = source->getComponentType();
     }
 
   if (target->isInterface())
@@ -926,49 +1010,67 @@ _Jv_IsAssignableFrom (jclass target, jclass source)
       // two interfaces for assignability.
       if (__builtin_expect 
           (source->idt == NULL || source->isInterface(), false))
-        return _Jv_InterfaceAssignableFrom (target, source);
-       
+        return _Jv_InterfaceAssignableFrom (source, target);
+
       _Jv_IDispatchTable *cl_idt = source->idt;
-      _Jv_IDispatchTable *if_idt = target->idt;
 
-      if (__builtin_expect ((if_idt == NULL), false))
+      if (__builtin_expect ((target->ioffsets == NULL), false))
        return false; // No class implementing TARGET has been loaded.    
-      jshort cl_iindex = cl_idt->cls.iindex;
-      if (cl_iindex <= if_idt->iface.ioffsets[0])
+      jshort cl_iindex = cl_idt->iindex;
+      if (cl_iindex < target->ioffsets[0])
         {
-         jshort offset = if_idt->iface.ioffsets[cl_iindex];
-         if (offset < cl_idt->cls.itable_length
-             && cl_idt->cls.itable[offset] == target)
+         jshort offset = target->ioffsets[cl_iindex];
+         if (offset != -1 && offset < cl_idt->itable_length
+             && cl_idt->itable[offset] == target)
            return true;
        }
       return false;
     }
-     
-  if ((target == &ObjectClass && !source->isPrimitive())
-      || (source->ancestors != NULL 
-         && source->ancestors[source->depth - target->depth] == target))
+
+  // Primitive TYPE classes are only assignable to themselves.
+  if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
+    return false;
+
+  if (target == &java::lang::Object::class$)
     return true;
-      
- return false;
+  else if (source->ancestors == NULL || target->ancestors == NULL)
+    {
+      // We need this case when either SOURCE or TARGET has not has
+      // its constant-time tables prepared.
+
+      // At this point we know that TARGET can't be Object, so it is
+      // safe to use that as the termination point.
+      while (source && source != &java::lang::Object::class$)
+       {
+         if (source == target)
+           return true;
+         source = source->getSuperclass();
+       }
+    }
+  else if (source->depth >= target->depth
+          && source->ancestors[source->depth - target->depth] == target)
+    return true;
+
+  return false;
 }
 
 // Interface type checking, the slow way. Returns TRUE if IFACE is a 
 // superinterface of SOURCE. This is used when SOURCE is also an interface,
 // or a class with no interface dispatch table.
 jboolean
-_Jv_InterfaceAssignableFrom (jclass iface, jclass source)
+_Jv_InterfaceAssignableFrom (jclass source, jclass iface)
 {
   for (int i = 0; i < source->interface_count; i++)
     {
       jclass interface = source->interfaces[i];
       if (iface == interface
-          || _Jv_InterfaceAssignableFrom (iface, interface))
+          || _Jv_InterfaceAssignableFrom (interface, iface))
         return true;      
     }
     
   if (!source->isInterface()
       && source->superclass 
-      && _Jv_InterfaceAssignableFrom (iface, source->superclass))
+      && _Jv_InterfaceAssignableFrom (source->superclass, iface))
     return true;
         
   return false;
@@ -979,14 +1081,14 @@ _Jv_IsInstanceOf(jobject obj, jclass cl)
 {
   if (__builtin_expect (!obj, false))
     return false;
-  return (_Jv_IsAssignableFrom (cl, JV_CLASS (obj)));
+  return _Jv_IsAssignableFrom (JV_CLASS (obj), cl);
 }
 
 void *
 _Jv_CheckCast (jclass c, jobject obj)
 {
   if (__builtin_expect 
-       (obj != NULL && ! _Jv_IsAssignableFrom(c, JV_CLASS (obj)), false))
+      (obj != NULL && ! _Jv_IsAssignableFrom(JV_CLASS (obj), c), false))
     throw new java::lang::ClassCastException
       ((new java::lang::StringBuffer
        (obj->getClass()->getName()))->append
@@ -1003,415 +1105,151 @@ _Jv_CheckArrayStore (jobject arr, jobject obj)
     {
       JvAssert (arr != NULL);
       jclass elt_class = (JV_CLASS (arr))->getComponentType();
+      if (elt_class == &java::lang::Object::class$)
+       return;
       jclass obj_class = JV_CLASS (obj);
       if (__builtin_expect 
-          (! _Jv_IsAssignableFrom (elt_class, obj_class), false))
-       throw new java::lang::ArrayStoreException;
+          (! _Jv_IsAssignableFrom (obj_class, elt_class), false))
+       throw new java::lang::ArrayStoreException
+               ((new java::lang::StringBuffer
+                (JvNewStringUTF("Cannot store ")))->append
+                (obj_class->getName())->append
+                (JvNewStringUTF(" in array of type "))->append
+                (elt_class->getName())->toString());
     }
 }
 
-#define INITIAL_IOFFSETS_LEN 4
-#define INITIAL_IFACES_LEN 4
-
-static _Jv_IDispatchTable null_idt = { {SHRT_MAX, 0, NULL} };
-
-// Generate tables for constant-time assignment testing and interface
-// method lookup. This implements the technique described by Per Bothner
-// <per@bothner.com> on the java-discuss mailing list on 1999-09-02:
-// http://gcc.gnu.org/ml/java/1999-q3/msg00377.html
-void 
-_Jv_PrepareConstantTimeTables (jclass klass)
-{  
-  if (klass->isPrimitive () || klass->isInterface ())
-    return;
-  
-  // Short-circuit in case we've been called already.
-  if ((klass->idt != NULL) || klass->depth != 0)
-    return;
-
-  // Calculate the class depth and ancestor table. The depth of a class 
-  // is how many "extends" it is removed from Object. Thus the depth of 
-  // java.lang.Object is 0, but the depth of java.io.FilterOutputStream 
-  // is 2. Depth is defined for all regular and array classes, but not 
-  // interfaces or primitive types.
-   
-  jclass klass0 = klass;
-  jboolean has_interfaces = 0;
-  while (klass0 != &ObjectClass)
-    {
-      has_interfaces += klass0->interface_count;
-      klass0 = klass0->superclass;
-      klass->depth++;
-    }
-
-  // We do class member testing in constant time by using a small table 
-  // of all the ancestor classes within each class. The first element is 
-  // a pointer to the current class, and the rest are pointers to the 
-  // classes ancestors, ordered from the current class down by decreasing 
-  // depth. We do not include java.lang.Object in the table of ancestors, 
-  // since it is redundant.
-       
-  klass->ancestors = (jclass *) _Jv_Malloc (klass->depth * sizeof (jclass));
-  klass0 = klass;
-  for (int index = 0; index < klass->depth; index++)
-    {
-      klass->ancestors[index] = klass0;
-      klass0 = klass0->superclass;
-    }
-    
-  if (java::lang::reflect::Modifier::isAbstract (klass->accflags))
-    return;
-  
-  // Optimization: If class implements no interfaces, use a common
-  // predefined interface table.
-  if (!has_interfaces)
+jboolean
+_Jv_IsAssignableFromSlow (jclass source, jclass target)
+{
+  // First, strip arrays.
+  while (target->isArray ())
     {
-      klass->idt = &null_idt;
-      return;
+      // If target is array, source must be as well.
+      if (! source->isArray ())
+       return false;
+      target = target->getComponentType ();
+      source = source->getComponentType ();
     }
 
-  klass->idt = 
-    (_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
-    
-  _Jv_ifaces ifaces;
-
-  ifaces.count = 0;
-  ifaces.len = INITIAL_IFACES_LEN;
-  ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
-
-  int itable_size = _Jv_GetInterfaces (klass, &ifaces);
-
-  if (ifaces.count > 0)
-    {
-      klass->idt->cls.itable = 
-       (void **) _Jv_Malloc (itable_size * sizeof (void *));
-      klass->idt->cls.itable_length = itable_size;
-          
-      jshort *itable_offsets = 
-       (jshort *) _Jv_Malloc (ifaces.count * sizeof (jshort));
-
-      _Jv_GenerateITable (klass, &ifaces, itable_offsets);
-
-      jshort cls_iindex = 
-       _Jv_FindIIndex (ifaces.list, itable_offsets, ifaces.count);
-
-      for (int i=0; i < ifaces.count; i++)
-       {
-         ifaces.list[i]->idt->iface.ioffsets[cls_iindex] =
-           itable_offsets[i];
-       }
+  // Quick success.
+  if (target == &java::lang::Object::class$)
+    return true;
 
-      klass->idt->cls.iindex = cls_iindex;         
+  // Ensure that the classes have their supers installed.
+  _Jv_Linker::wait_for_state (source, JV_STATE_LOADING);
+  _Jv_Linker::wait_for_state (target, JV_STATE_LOADING);
 
-      _Jv_Free (ifaces.list);
-      _Jv_Free (itable_offsets);
-    }
-  else 
+  do
     {
-      klass->idt->cls.iindex = SHRT_MAX;
+      if (source == target)
+       return true;
+
+      if (target->isPrimitive () || source->isPrimitive ())
+       return false;
+
+      if (target->isInterface ())
+       {
+         for (int i = 0; i < source->interface_count; ++i)
+           {
+             // We use a recursive call because we also need to
+             // check superinterfaces.
+             if (_Jv_IsAssignableFromSlow (source->getInterface (i), target))
+               return true;
+           }
+       }
+      source = source->getSuperclass ();
     }
-}
+  while (source != NULL);
 
-// Return index of item in list, or -1 if item is not present.
-inline jshort
-_Jv_IndexOf (void *item, void **list, jshort list_len)
-{
-  for (int i=0; i < list_len; i++)
-    {
-      if (list[i] == item)
-        return i;
-    }
-  return -1;
+  return false;
 }
 
-// Find all unique interfaces directly or indirectly implemented by klass.
-// Returns the size of the interface dispatch table (itable) for klass, which 
-// is the number of unique interfaces plus the total number of methods that 
-// those interfaces declare. May extend ifaces if required.
-jshort
-_Jv_GetInterfaces (jclass klass, _Jv_ifaces *ifaces)
+// Lookup an interface method by name.  This is very similar to
+// purpose to _getMethod, but the interfaces are quite different.  It
+// might be a good idea for _getMethod to call this function.
+//
+// Return true of the method is found, with the class in FOUND_CLASS
+// and the index in INDEX.
+bool
+_Jv_getInterfaceMethod (jclass search_class, jclass &found_class, int &index,
+                       const _Jv_Utf8Const *utf_name,  
+                       const _Jv_Utf8Const *utf_sig)
 {
-  jshort result = 0;
-  
-  for (int i=0; i < klass->interface_count; i++)
-    {
-      jclass iface = klass->interfaces[i];
-      if (_Jv_IndexOf (iface, (void **) ifaces->list, ifaces->count) == -1)
-        {
-         if (ifaces->count + 1 >= ifaces->len)
-           {
-             /* Resize ifaces list */
-             ifaces->len = ifaces->len * 2;
-             ifaces->list = (jclass *) _Jv_Realloc (ifaces->list, 
-                            ifaces->len * sizeof(jclass));
-           }
-         ifaces->list[ifaces->count] = iface;
-         ifaces->count++;
-
-         result += _Jv_GetInterfaces (klass->interfaces[i], ifaces);
-       }
-    }
-    
-  if (klass->isInterface())
-    {
-      result += klass->method_count + 1;
-    }
-  else
+   for (jclass klass = search_class; klass; klass = klass->getSuperclass())
     {
-      if (klass->superclass)
-        {
-         result += _Jv_GetInterfaces (klass->superclass, ifaces);
-       }
-    }
-  return result;
-}
-
-// Fill out itable in klass, resolving method declarations in each ifaces.
-// itable_offsets is filled out with the position of each iface in itable,
-// such that itable[itable_offsets[n]] == ifaces.list[n].
-void
-_Jv_GenerateITable (jclass klass, _Jv_ifaces *ifaces, jshort *itable_offsets)
-{
-  void **itable = klass->idt->cls.itable;
-  jshort itable_pos = 0;
-
-  for (int i=0; i < ifaces->count; i++)
-    { 
-      jclass iface = ifaces->list[i];
-      itable_offsets[i] = itable_pos;
-      itable_pos = _Jv_AppendPartialITable (klass, iface, itable,
-                   itable_pos);
+      // FIXME: Throw an exception?
+      if (!klass->isInterface ())
+       return false;
       
-      /* Create interface dispatch table for iface */
-      if (iface->idt == NULL)
+      int max = klass->method_count;
+      int offset = 0;
+      for (int i = 0; i < max; ++i)
        {
-         iface->idt = 
-           (_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
-
-         // The first element of ioffsets is its length (itself included).
-         jshort *ioffsets = 
-           (jshort *) _Jv_Malloc (INITIAL_IOFFSETS_LEN * sizeof (jshort));
-         ioffsets[0] = INITIAL_IOFFSETS_LEN;
-         for (int i=1; i < INITIAL_IOFFSETS_LEN; i++)
-           ioffsets[i] = -1;
-
-         iface->idt->iface.ioffsets = ioffsets;            
-       }
-    }
-}
-
-// Format method name for use in error messages.
-jstring
-_Jv_GetMethodString (jclass klass, _Jv_Utf8Const *name)
-{
-  jstring r = JvNewStringUTF (klass->name->data);
-  r = r->concat (JvNewStringUTF ("."));
-  r = r->concat (JvNewStringUTF (name->data));
-  return r;
-}
+         // Skip <clinit> here, as it will not be in the IDT.
+         if (klass->methods[i].name->first() == '<')
+           continue;
 
-void 
-_Jv_ThrowNoSuchMethodError ()
-{
-  throw new java::lang::NoSuchMethodError;
-}
+         if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
+             && _Jv_equalUtf8Consts (klass->methods[i].signature, utf_sig))
+           {
+             // Found it.
+             using namespace java::lang::reflect;
 
-// Each superinterface of a class (i.e. each interface that the class
-// directly or indirectly implements) has a corresponding "Partial
-// Interface Dispatch Table" whose size is (number of methods + 1) words.
-// The first word is a pointer to the interface (i.e. the java.lang.Class
-// instance for that interface).  The remaining words are pointers to the
-// actual methods that implement the methods declared in the interface,
-// in order of declaration.
-//
-// Append partial interface dispatch table for "iface" to "itable", at
-// position itable_pos.
-// Returns the offset at which the next partial ITable should be appended.
-jshort
-_Jv_AppendPartialITable (jclass klass, jclass iface, void **itable, 
-                         jshort pos)
-{
-  using namespace java::lang::reflect;
+             // FIXME: Method must be public.  Throw an exception?
+             if (! Modifier::isPublic (klass->methods[i].accflags))
+               break;
 
-  itable[pos++] = (void *) iface;
-  _Jv_Method *meth;
-  
-  for (int j=0; j < iface->method_count; j++)
-    {
-      meth = NULL;
-      for (jclass cl = klass; cl; cl = cl->getSuperclass())
-        {
-         meth = _Jv_GetMethodLocal (cl, iface->methods[j].name,
-                                    iface->methods[j].signature);
-                
-         if (meth)
-           break;
-       }
+             found_class = klass;
+             // Interface method indexes count from 1.
+             index = offset + 1;
+             return true;
+           }
 
-      if (meth && (meth->name->data[0] == '<'))
-       {
-         // leave a placeholder in the itable for hidden init methods.
-          itable[pos] = NULL;  
+         ++offset;
        }
-      else if (meth)
-        {
-         if (Modifier::isStatic(meth->accflags))
-           throw new java::lang::IncompatibleClassChangeError
-             (_Jv_GetMethodString (klass, meth->name));
-         if (Modifier::isAbstract(meth->accflags))
-           throw new java::lang::AbstractMethodError
-             (_Jv_GetMethodString (klass, meth->name));
-         if (! Modifier::isPublic(meth->accflags))
-           throw new java::lang::IllegalAccessError
-             (_Jv_GetMethodString (klass, meth->name));
-
-         itable[pos] = meth->ncode;
-       }
-      else
-        {
-         // The method doesn't exist in klass. Binary compatibility rules
-         // permit this, so we delay the error until runtime using a pointer
-         // to a method which throws an exception.
-         itable[pos] = (void *) _Jv_ThrowNoSuchMethodError;
-       }
-      pos++;
     }
-    
-  return pos;
-}
 
-static _Jv_Mutex_t iindex_mutex;
-bool iindex_mutex_initialized = false;
-
-// We need to find the correct offset in the Class Interface Dispatch 
-// Table for a given interface. Once we have that, invoking an interface 
-// method just requires combining the Method's index in the interface 
-// (known at compile time) to get the correct method.  Doing a type test 
-// (cast or instanceof) is the same problem: Once we have a possible Partial 
-// Interface Dispatch Table, we just compare the first element to see if it 
-// matches the desired interface. So how can we find the correct offset?  
-// Our solution is to keep a vector of candiate offsets in each interface 
-// (idt->iface.ioffsets), and in each class we have an index 
-// (idt->cls.iindex) used to select the correct offset from ioffsets.
-//
-// Calculate and return iindex for a new class. 
-// ifaces is a vector of num interfaces that the class implements.
-// offsets[j] is the offset in the interface dispatch table for the
-// interface corresponding to ifaces[j].
-// May extend the interface ioffsets if required.
-jshort
-_Jv_FindIIndex (jclass *ifaces, jshort *offsets, jshort num)
-{
-  int i;
-  int j;
-  
-  // Acquire a global lock to prevent itable corruption in case of multiple 
-  // classes that implement an intersecting set of interfaces being linked
-  // simultaneously. We can assume that the mutex will be initialized
-  // single-threaded.
-  if (! iindex_mutex_initialized)
-    {
-      _Jv_MutexInit (&iindex_mutex);
-      iindex_mutex_initialized = true;
-    }
-  
-  _Jv_MutexLock (&iindex_mutex);
-  
-  for (i=1;; i++)  /* each potential position in ioffsets */
-    {
-      for (j=0;; j++)  /* each iface */
-        {
-         if (j >= num)
-           goto found;
-         if (i > ifaces[j]->idt->iface.ioffsets[0])
-           continue;
-         int ioffset = ifaces[j]->idt->iface.ioffsets[i];
-         /* We can potentially share this position with another class. */
-         if (ioffset >= 0 && ioffset != offsets[j])
-           break; /* Nope. Try next i. */        
-       }
-    }
-  found:
-  for (j = 0; j < num; j++)
+  // If we haven't found a match, and this class is an interface, then
+  // check all the superinterfaces.
+  if (search_class->isInterface())
     {
-      int len = ifaces[j]->idt->iface.ioffsets[0];
-      if (i >= len) 
+      for (int i = 0; i < search_class->interface_count; ++i)
        {
-         /* Resize ioffsets. */
-         int newlen = 2 * len;
-         if (i >= newlen)
-           newlen = i + 3;
-         jshort *old_ioffsets = ifaces[j]->idt->iface.ioffsets;
-         jshort *new_ioffsets = (jshort *) _Jv_Realloc (old_ioffsets, 
-                                         newlen * sizeof(jshort));       
-         new_ioffsets[0] = newlen;
-
-         while (len < newlen)
-           new_ioffsets[len++] = -1;
-         
-         ifaces[j]->idt->iface.ioffsets = new_ioffsets;
+         using namespace java::lang::reflect;
+         bool found = _Jv_getInterfaceMethod (search_class->interfaces[i], 
+                                              found_class, index,
+                                              utf_name, utf_sig);
+         if (found)
+           return true;
        }
-      ifaces[j]->idt->iface.ioffsets[i] = offsets[j];
     }
 
-  _Jv_MutexUnlock (&iindex_mutex);
-
-  return i;
+  return false;
 }
 
-// Only used by serialization
-java::lang::reflect::Field *
-java::lang::Class::getPrivateField (jstring name)
+#ifdef INTERPRETER
+_Jv_InterpMethod*
+_Jv_FindInterpreterMethod (jclass klass, jmethodID desired_method)
 {
-  int hash = name->hashCode ();
+  using namespace java::lang::reflect;
 
-  java::lang::reflect::Field* rfield;
-  for (int i = 0;  i < field_count;  i++)
-    {
-      _Jv_Field *field = &fields[i];
-      if (! _Jv_equal (field->name, name, hash))
-       continue;
-      rfield = new java::lang::reflect::Field ();
-      rfield->offset = (char*) field - (char*) fields;
-      rfield->declaringClass = this;
-      rfield->name = name;
-      return rfield;
-    }
-  jclass superclass = getSuperclass();
-  if (superclass == NULL)
-    return NULL;
-  rfield = superclass->getPrivateField(name);
-  for (int i = 0; i < interface_count && rfield == NULL; ++i)
-    rfield = interfaces[i]->getPrivateField (name);
-  return rfield;
-}
+  _Jv_InterpClass* iclass
+    = reinterpret_cast<_Jv_InterpClass*> (klass->aux_info);
+  _Jv_MethodBase** imethods = _Jv_GetFirstMethod (iclass);
 
-// Only used by serialization
-java::lang::reflect::Method *
-java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
-{
-  jstring partial_sig = getSignature (param_types, false);
-  jint p_len = partial_sig->length();
-  _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
-  for (Class *klass = this; klass; klass = klass->getSuperclass())
+  for (int i = 0; i < JvNumMethods (klass); ++i)
     {
-      int i = klass->isPrimitive () ? 0 : klass->method_count;
-      while (--i >= 0)
+      _Jv_MethodBase* imeth = imethods[i];
+      _Jv_ushort accflags = klass->methods[i].accflags;
+      if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
        {
-         // FIXME: access checks.
-         if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
-             && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
-           {
-             // Found it.
-             using namespace java::lang::reflect;
-
-             Method *rmethod = new Method ();
-             rmethod->offset = ((char *) (&klass->methods[i])
-                                - (char *) klass->methods);
-             rmethod->declaringClass = klass;
-             return rmethod;
-           }
+         _Jv_InterpMethod* im = reinterpret_cast<_Jv_InterpMethod*> (imeth);
+         if (im->get_method () == desired_method)
+           return im;
        }
     }
-  throw new java::lang::NoSuchMethodException;
+
+  return NULL;
 }
+#endif