// natClass.cc - Implementation of java.lang.Class native methods.
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
Free Software Foundation
This file is part of libgcj.
#include <limits.h>
#include <string.h>
#include <stddef.h>
+#include <stdio.h>
#pragma implementation "Class.h"
#include <java/lang/reflect/Member.h>
#include <java/lang/reflect/Method.h>
#include <java/lang/reflect/Field.h>
+#include <java/lang/reflect/Proxy.h>
#include <java/lang/reflect/Constructor.h>
#include <java/lang/AbstractMethodError.h>
#include <java/lang/ArrayStoreException.h>
#include <java/lang/NullPointerException.h>
#include <java/lang/RuntimePermission.h>
#include <java/lang/System.h>
+#include <java/lang/SecurityException.h>
#include <java/lang/SecurityManager.h>
#include <java/lang/StringBuffer.h>
#include <java/lang/VMClassLoader.h>
-#include <gnu/gcj/runtime/StackTrace.h>
#include <gcj/method.h>
-#include <gnu/gcj/runtime/MethodRef.h>
#include <gnu/gcj/RawData.h>
+#include <java/lang/VerifyError.h>
+#include <java/lang/InternalError.h>
+#include <java/lang/TypeNotPresentException.h>
+#include <java/lang/Byte.h>
+#include <java/lang/Short.h>
+#include <java/lang/Integer.h>
+#include <java/lang/Float.h>
+#include <java/lang/Double.h>
+#include <java/lang/Long.h>
+#include <java/lang/Character.h>
+#include <java/lang/Boolean.h>
+#include <java/lang/annotation/Annotation.h>
+#include <java/util/HashMap.h>
+#include <java/util/Map.h>
+#include <sun/reflect/annotation/AnnotationInvocationHandler.h>
+#include <java/lang/Enum.h>
#include <java-cpool.h>
#include <java-interp.h>
+#include <java-assert.h>
+#include <java-stack.h>
+#include <execution.h>
+
\f
using namespace gcj;
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 == NULL)
java::lang::Class::forName (jstring className)
{
java::lang::ClassLoader *loader = NULL;
- gnu::gcj::runtime::StackTrace *t
- = new gnu::gcj::runtime::StackTrace(4);
- java::lang::Class *klass = NULL;
- try
- {
- for (int i = 1; !klass; i++)
- {
- klass = t->classAt (i);
- }
- loader = klass->getClassLoaderInternal();
- }
- catch (::java::lang::ArrayIndexOutOfBoundsException *e)
- {
- }
+
+ jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
+ if (caller)
+ loader = caller->getClassLoaderInternal();
return forName (className, true, loader);
}
java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
if (s != NULL)
{
- gnu::gcj::runtime::StackTrace *t
- = new gnu::gcj::runtime::StackTrace(4);
- Class *caller = NULL;
- ClassLoader *caller_loader = NULL;
- try
- {
- for (int i = 1; !caller; i++)
- {
- caller = t->classAt (i);
- }
- caller_loader = caller->getClassLoaderInternal();
- }
- catch (::java::lang::ArrayIndexOutOfBoundsException *e)
- {
- }
+ jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
+ return getClassLoader (caller);
+ }
+
+ return loader;
+}
+
+java::lang::ClassLoader *
+java::lang::Class::getClassLoader (jclass caller)
+{
+ java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
+ if (s != NULL)
+ {
+ ClassLoader *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
s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
}
- // The spec requires us to return `null' for primitive classes. In
- // other cases we have the option of returning `null' for classes
- // loaded with the bootstrap loader. All gcj-compiled classes which
- // are linked into the application used to return `null' here, but
- // that confuses some poorly-written applications. It is a useful
- // and apparently harmless compatibility hack to simply never return
- // `null' instead.
- if (isPrimitive ())
- return NULL;
- return loader ? loader : ClassLoader::systemClassLoader;
+ return loader;
}
java::lang::reflect::Constructor *
}
JArray<java::lang::reflect::Constructor *> *
-java::lang::Class::_getConstructors (jboolean declared)
+java::lang::Class::getDeclaredConstructors (jboolean publicOnly)
{
- memberAccessCheck(java::lang::reflect::Member::PUBLIC);
-
int numConstructors = 0;
int max = isPrimitive () ? 0 : method_count;
int i;
if (method->name == NULL
|| ! _Jv_equalUtf8Consts (method->name, init_name))
continue;
- if (! declared
+ if (publicOnly
&& ! java::lang::reflect::Modifier::isPublic(method->accflags))
continue;
numConstructors++;
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
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);
-}
-
-JArray<jclass> *
-java::lang::Class::getClasses (void)
-{
- // FIXME: security checking.
-
- // Until we have inner classes, it always makes sense to return an
- // empty array.
- JArray<jclass> *result
- = (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
- NULL);
- return result;
-}
-
-JArray<jclass> *
-java::lang::Class::getDeclaredClasses (void)
-{
- memberAccessCheck (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, &java::lang::Class::class$,
- NULL);
- return result;
-}
-
-jclass
-java::lang::Class::getDeclaringClass (void)
-{
- // Until we have inner classes, it makes sense to always return
- // NULL.
- return NULL;
+ return name->toString();
}
JArray<jclass> *
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);
}
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)
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
{
if (! obj)
return false;
- _Jv_InitClass (this);
- return _Jv_IsAssignableFrom (this, JV_CLASS (obj));
+ return _Jv_IsAssignableFrom (JV_CLASS (obj), this);
}
jobject
void
java::lang::Class::finalize (void)
{
-#ifdef INTERPRETER
- JvAssert (_Jv_IsInterpretedClass (this));
- _Jv_UnregisterClass (this);
-#endif
+ engine->unregister(this);
+}
+
+void
+_Jv_ClosureList::releaseClosures (_Jv_ClosureList **closures)
+{
+ if (!closures)
+ return;
+
+ while (_Jv_ClosureList *current = *closures)
+ {
+ *closures = current->next;
+ ffi_closure_free (current->ptr);
+ }
+}
+
+void
+_Jv_ClosureList::registerClosure (jclass klass, void *ptr)
+{
+ _Jv_ClosureList **closures = klass->engine->get_closure_list (klass);
+ this->ptr = ptr;
+ this->next = *closures;
+ *closures = this;
}
// This implements the initialization process for a class. From Spec
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::VMClassLoader::resolveClass (this);
- _Jv_MonitorEnter (this);
- }
- else
-#endif
- {
- _Jv_PrepareCompiledClass (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 ();
+ // Step 1. We introduce a new scope so we can synchronize more
+ // easily.
+ {
+ JvSynchronize sync (this);
- // Steps 3 & 4.
- if (state == JV_STATE_DONE)
- {
- _Jv_MonitorExit (this);
- return;
- }
- if (state == JV_STATE_IN_PROGRESS)
- {
- _Jv_MonitorExit (this);
+ if (state < JV_STATE_LINKED)
+ {
+ try
+ {
+ _Jv_Linker::wait_for_state(this, JV_STATE_LINKED);
+ }
+ catch (java::lang::SecurityException *x)
+ {
+ throw x;
+ }
+ catch (java::lang::Throwable *x)
+ {
+ // Turn into a NoClassDefFoundError.
+ java::lang::NoClassDefFoundError *result
+ = new java::lang::NoClassDefFoundError(getName());
+ result->initCause(x);
+ throw result;
+ }
+ }
- /* Initialization in progress. The class is linked now,
- so ensure internal tables are built. */
- _Jv_PrepareConstantTimeTables (this);
- _Jv_MakeVTable(this);
- _Jv_LinkSymbolTable(this);
+ // 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);
+ // 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)
{
_Jv_InitClass (superclass);
}
+ catch (java::lang::SecurityException *x)
+ {
+ throw x;
+ }
catch (java::lang::Throwable *except)
{
// Caught an exception.
- _Jv_MonitorEnter (this);
+ JvSynchronize sync (this);
state = JV_STATE_ERROR;
notifyAll ();
- _Jv_MonitorExit (this);
throw except;
}
}
- _Jv_PrepareConstantTimeTables (this);
-
- if (vtable == NULL)
- _Jv_MakeVTable(this);
-
- if (otable || atable)
- _Jv_LinkSymbolTable(this);
-
- _Jv_linkExceptionClassTable (this);
-
// Steps 8, 9, 10, 11.
try
{
if (meth)
((void (*) (void)) meth->ncode) ();
}
+ catch (java::lang::SecurityException *x)
+ {
+ throw x;
+ }
catch (java::lang::Throwable *except)
{
if (! java::lang::Error::class$.isInstance(except))
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);
}
-\f
-
-//
-// Some class-related convenience functions.
-//
-
-// Find a method declared in the class. If it is not declared locally
-// (or if it is inherited), return NULL.
-_Jv_Method *
-_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
- _Jv_Utf8Const *signature)
+// Only used by serialization
+java::lang::reflect::Field *
+java::lang::Class::getPrivateField (jstring name)
{
- for (int i = 0; i < klass->method_count; ++i)
+ int hash = name->hashCode ();
+
+ java::lang::reflect::Field* rfield;
+ for (int i = 0; i < field_count; i++)
{
- if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
- && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
- return &klass->methods[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;
}
- return NULL;
+ 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_Method *
-_Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
- _Jv_Utf8Const *signature)
+// Only used by serialization
+java::lang::reflect::Method *
+java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
{
- for (; klass; klass = klass->getSuperclass())
+ 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())
{
- _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
+ 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;
- if (meth)
- return meth;
+ Method *rmethod = new Method ();
+ rmethod->offset = ((char *) (&klass->methods[i])
+ - (char *) klass->methods);
+ rmethod->declaringClass = klass;
+ return rmethod;
+ }
+ }
}
+ throw new java::lang::NoSuchMethodException (name);
+}
- return NULL;
+// Private accessor method for Java code to retrieve the protection domain.
+java::security::ProtectionDomain *
+java::lang::Class::getProtectionDomain0 ()
+{
+ return protectionDomain;
}
-// NOTE: MCACHE_SIZE should be a power of 2 minus one.
-#define MCACHE_SIZE 1023
+JArray<jobject> *
+java::lang::Class::getSigners()
+{
+ return hack_signers;
+}
-struct _Jv_mcache
+void
+java::lang::Class::setSigners(JArray<jobject> *s)
{
- jclass klass;
- _Jv_Method *method;
-};
+ hack_signers = s;
+}
-static _Jv_mcache method_cache[MCACHE_SIZE + 1];
+\f
-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;
- return NULL;
+static unsigned char
+read_u1 (unsigned char *&p)
+{
+ return *p++;
}
-static void
-_Jv_AddMethodToCache (jclass klass,
- _Jv_Method *method)
+static unsigned char
+read_u1 (unsigned char *&p, unsigned char *next)
{
- _Jv_MonitorEnter (&java::lang::Class::class$);
-
- int index = method->name->hash & MCACHE_SIZE;
-
- method_cache[index].method = method;
- method_cache[index].klass = klass;
+ if (next - p < 1)
+ throw new java::lang::InternalError();
+ return *p++;
+}
- _Jv_MonitorExit (&java::lang::Class::class$);
+static unsigned int
+read_u2 (unsigned char *&p)
+{
+ unsigned int b1 = *p++;
+ unsigned int b2 = *p++;
+ return (b1 << 8) | b2;
}
-void *
-_Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
- _Jv_Utf8Const *signature)
+static unsigned int
+read_u2 (unsigned char *&p, unsigned char *next)
{
- using namespace java::lang::reflect;
+ if (next - p < 2)
+ throw new java::lang::InternalError();
+ return read_u2 (p);
+}
- void *ncode = _Jv_FindMethodInCache (klass, name, signature);
- if (ncode != 0)
- return ncode;
+static int
+read_4 (unsigned char *&p)
+{
+ int b1 = *p++;
+ int b2 = *p++;
+ int b3 = *p++;
+ int b4 = *p++;
+ return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
+}
- for (; klass; klass = klass->getSuperclass())
+jstring
+java::lang::Class::getReflectionSignature (jint /*jv_attr_type*/ type,
+ jint obj_index)
+{
+ // We just re-parse the bytecode for this data each time. If
+ // necessary we can cache results, but I suspect this is not
+ // performance sensitive.
+ unsigned char *bytes = reflection_data;
+ if (bytes == NULL)
+ return NULL;
+ while (true)
{
- _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
- if (! meth)
- continue;
-
- 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));
+ int kind = read_u1 (bytes);
+ if (kind == JV_DONE_ATTR)
+ return NULL;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ if (kind != type)
+ {
+ bytes = next;
+ continue;
+ }
+ if (type != JV_CLASS_ATTR)
+ {
+ unsigned short index = read_u2 (bytes, next);
+ if (index != obj_index)
+ {
+ bytes = next;
+ continue;
+ }
+ }
+ int nt = read_u1 (bytes, next);
+ if (nt != JV_SIGNATURE_KIND)
+ {
+ bytes = next;
+ continue;
+ }
+ unsigned int cpool_idx = read_u2 (bytes, next);
+ if (cpool_idx >= (unsigned int) constants.size
+ || constants.tags[cpool_idx] != JV_CONSTANT_Utf8)
+ {
+ // We just ignore errors for now. It isn't clear what is
+ // best to do here, as an encoding error here means a bug
+ // either in the compiler or in defineclass.cc.
+ return NULL;
+ }
+ return _Jv_NewStringUtf8Const (constants.data[cpool_idx].utf8);
+ }
+}
- _Jv_AddMethodToCache (klass, meth);
+jstring
+java::lang::Class::getReflectionSignature (::java::lang::reflect::Constructor *c)
+{
+ _Jv_Method *meth = _Jv_FromReflectedConstructor (c);
+ unsigned short meth_index = meth - methods;
+ return getReflectionSignature (JV_METHOD_ATTR, meth_index);
+}
- return meth->ncode;
- }
- throw new java::lang::IncompatibleClassChangeError;
+jstring
+java::lang::Class::getReflectionSignature (::java::lang::reflect::Method *m)
+{
+ _Jv_Method *meth = _Jv_FromReflectedMethod (m);
+ unsigned short meth_index = meth - methods;
+ return getReflectionSignature (JV_METHOD_ATTR, meth_index);
}
-// Fast interface method lookup by index.
-void *
-_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
+jstring
+java::lang::Class::getReflectionSignature (::java::lang::reflect::Field *f)
{
- _Jv_IDispatchTable *cldt = klass->idt;
- int idx = iface->idt->iface.ioffsets[cldt->cls.iindex] + method_idx;
- return cldt->cls.itable[idx];
+ _Jv_Field *fld = _Jv_FromReflectedField (f);
+ unsigned short fld_index = fld - fields;
+ return getReflectionSignature (JV_FIELD_ATTR, fld_index);
}
-jboolean
-_Jv_IsAssignableFrom (jclass target, jclass source)
+jstring
+java::lang::Class::getClassSignature()
{
- if (source == target)
- return true;
+ return getReflectionSignature (JV_CLASS_ATTR, 0);
+}
- // If target is array, so must source be.
- while (target->isArray ())
- {
- if (! source->isArray())
- return false;
- target = target->getComponentType();
- source = source->getComponentType();
- }
-
- if (target->isInterface())
+jint
+java::lang::Class::getEnclosingMethodData()
+{
+ unsigned char *bytes = reflection_data;
+ if (bytes == NULL)
+ return 0;
+ while (true)
{
- // Abstract classes have no IDT, and IDTs provide no way to check
- // two interfaces for assignability.
- if (__builtin_expect
- (source->idt == NULL || source->isInterface(), false))
- return _Jv_InterfaceAssignableFrom (target, source);
-
- _Jv_IDispatchTable *cl_idt = source->idt;
- _Jv_IDispatchTable *if_idt = target->idt;
-
- if (__builtin_expect ((if_idt == 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 offset = if_idt->iface.ioffsets[cl_iindex];
- if (offset != -1 && offset < cl_idt->cls.itable_length
- && cl_idt->cls.itable[offset] == target)
- return true;
+ int kind = read_u1 (bytes);
+ if (kind == JV_DONE_ATTR)
+ return 0;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ if (kind != JV_CLASS_ATTR)
+ {
+ bytes = next;
+ continue;
}
- return false;
- }
-
- // 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;
- 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$)
+ int type = read_u1 (bytes, next);
+ if (type != JV_ENCLOSING_METHOD_KIND)
{
- if (source == target)
- return true;
- source = source->getSuperclass();
+ bytes = next;
+ continue;
}
+ int class_index = read_u2 (bytes, next);
+ int method_index = read_u2 (bytes, next);
+ _Jv_word result;
+ _Jv_storeIndexes (&result, class_index, method_index);
+ return result.i;
}
- 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)
+jclass
+java::lang::Class::getEnclosingClass()
{
- for (int i = 0; i < source->interface_count; i++)
- {
- jclass interface = source->interfaces[i];
- if (iface == interface
- || _Jv_InterfaceAssignableFrom (iface, interface))
- return true;
- }
-
- if (!source->isInterface()
- && source->superclass
- && _Jv_InterfaceAssignableFrom (iface, source->superclass))
- return true;
-
- return false;
+ _Jv_word indexes;
+ indexes.i = getEnclosingMethodData();
+ if (indexes.i == 0)
+ return NULL;
+ _Jv_ushort class_index, method_index;
+ _Jv_loadIndexes (&indexes, class_index, method_index);
+ return _Jv_Linker::resolve_pool_entry (this, class_index).clazz;
}
-jboolean
-_Jv_IsInstanceOf(jobject obj, jclass cl)
+::java::lang::reflect::Method *
+java::lang::Class::getEnclosingMethod()
{
- if (__builtin_expect (!obj, false))
- return false;
- return (_Jv_IsAssignableFrom (cl, JV_CLASS (obj)));
+ _Jv_word indexes;
+ indexes.i = getEnclosingMethodData();
+ if (indexes.i == 0)
+ return NULL;
+ _Jv_ushort class_index, method_index;
+ _Jv_loadIndexes (&indexes, class_index, method_index);
+ jclass found_class;
+ _Jv_Method *method = _Jv_Linker::resolve_method_entry (this, found_class,
+ class_index,
+ method_index,
+ false, false);
+ using namespace java::lang::reflect;
+ Method *rmethod = new Method ();
+ rmethod->offset = (char *) method - (char *) found_class->methods;
+ rmethod->declaringClass = found_class;
+ return rmethod;
}
-void *
-_Jv_CheckCast (jclass c, jobject obj)
+::java::lang::reflect::Constructor *
+java::lang::Class::getEnclosingConstructor()
{
- if (__builtin_expect
- (obj != NULL && ! _Jv_IsAssignableFrom(c, JV_CLASS (obj)), false))
- throw new java::lang::ClassCastException
- ((new java::lang::StringBuffer
- (obj->getClass()->getName()))->append
- (JvNewStringUTF(" cannot be cast to "))->append
- (c->getName())->toString());
-
- return obj;
+ _Jv_word indexes;
+ indexes.i = getEnclosingMethodData();
+ if (indexes.i == 0)
+ return NULL;
+ _Jv_ushort class_index, method_index;
+ _Jv_loadIndexes (&indexes, class_index, method_index);
+ jclass found_class;
+ _Jv_Method *method = _Jv_Linker::resolve_method_entry (this, found_class,
+ class_index,
+ method_index,
+ false, false);
+ using namespace java::lang::reflect;
+ Constructor *cons = new Constructor ();
+ cons->offset = (char *) method - (char *) found_class->methods;
+ cons->declaringClass = this;
+ return cons;
}
-void
-_Jv_CheckArrayStore (jobject arr, jobject obj)
+static void
+check_constant (_Jv_Constants *pool, jint cpool_index, jint type)
{
- if (obj)
+ if (cpool_index <= 0 || cpool_index >= pool->size)
+ throw new InternalError(JvNewStringLatin1("invalid constant pool index"));
+ if ((pool->tags[cpool_index] &
+ ~(JV_CONSTANT_ResolvedFlag|JV_CONSTANT_LazyFlag)) != type)
{
- 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
- ((new java::lang::StringBuffer
- (JvNewStringUTF("Cannot store ")))->append
- (obj_class->getName())->append
- (JvNewStringUTF(" in array of type "))->append
- (elt_class->getName())->toString());
+ ::java::lang::StringBuffer *sb = new ::java::lang::StringBuffer();
+ sb->append(JvNewStringLatin1("expected pool constant "));
+ sb->append(type);
+ sb->append(JvNewStringLatin1(" but got "));
+ sb->append(jint (pool->tags[cpool_index]));
+ throw new InternalError(sb->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;
+// Forward declaration
+static ::java::lang::annotation::Annotation *
+parseAnnotation(jclass klass, _Jv_Constants *pool,
+ unsigned char *&bytes, unsigned char *last);
- // 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 != &java::lang::Object::class$)
+static jobject
+parseAnnotationElement(jclass klass, _Jv_Constants *pool,
+ unsigned char *&bytes, unsigned char *last)
+{
+ int tag = read_u1 (bytes, last);
+ jobject result;
+ switch (tag)
{
- has_interfaces += klass0->interface_count;
- klass0 = klass0->superclass;
- klass->depth++;
+ case 'B':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Integer);
+ result = Byte::valueOf (pool->data[cindex].i);
+ }
+ break;
+ case 'C':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Integer);
+ result = Character::valueOf (pool->data[cindex].i);
+ }
+ break;
+ case 'S':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Integer);
+ result = Short::valueOf (pool->data[cindex].i);
+ }
+ break;
+ case 'Z':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Integer);
+ result = Boolean::valueOf (jboolean (pool->data[cindex].i));
+ }
+ break;
+ case 'I':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Integer);
+ result = Integer::valueOf (pool->data[cindex].i);
+ }
+ break;
+ case 'D':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Double);
+ _Jv_word2 word;
+ memcpy (&word, &pool->data[cindex], 2 * sizeof (_Jv_word));
+ result = Double::valueOf (word.d);
+ }
+ break;
+ case 'F':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Float);
+ result = Float::valueOf (pool->data[cindex].f);
+ }
+ break;
+ case 'J':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Long);
+ _Jv_word2 word;
+ memcpy (&word, &pool->data[cindex], 2 * sizeof (_Jv_word));
+ result = Long::valueOf (word.l);
+ }
+ break;
+ case 's':
+ {
+ int cindex = read_u2 (bytes, last);
+ // Despite what the JVM spec says, compilers generate a Utf8
+ // constant here, not a String.
+ check_constant (pool, cindex, JV_CONSTANT_Utf8);
+ result = pool->data[cindex].utf8->toString();
+ }
+ break;
+ case 'e':
+ {
+ int type_name_index = read_u2 (bytes, last);
+ check_constant (pool, type_name_index, JV_CONSTANT_Utf8);
+ int const_name_index = read_u2 (bytes, last);
+ check_constant (pool, const_name_index, JV_CONSTANT_Utf8);
+
+ _Jv_Utf8Const *u_name = pool->data[type_name_index].utf8;
+ _Jv_Utf8Const *e_name = pool->data[const_name_index].utf8;
+
+ // FIXME: throw correct exceptions at the correct times.
+ jclass e_class = _Jv_FindClassFromSignature(u_name->chars(),
+ klass->getClassLoaderInternal());
+ result = ::java::lang::Enum::valueOf(e_class, e_name->toString());
+ }
+ break;
+ case 'c':
+ {
+ int cindex = read_u2 (bytes, last);
+ check_constant (pool, cindex, JV_CONSTANT_Utf8);
+ _Jv_Utf8Const *u_name = pool->data[cindex].utf8;
+ jclass anno_class
+ = _Jv_FindClassFromSignatureNoException(u_name->chars(),
+ klass->getClassLoaderInternal());
+ // FIXME: not correct: we should lazily do this when trying to
+ // read the element. This means that
+ // AnnotationInvocationHandler needs to have a special case.
+ if (! anno_class)
+ // FIXME: original exception...
+ throw new TypeNotPresentException(u_name->toString(), NULL);
+ result = anno_class;
+ }
+ break;
+ case '@':
+ result = parseAnnotation (klass, pool, bytes, last);
+ break;
+ case '[':
+ {
+ int n_array_elts = read_u2 (bytes, last);
+ jobjectArray aresult = _Jv_NewObjectArray (n_array_elts,
+ &Object::class$, NULL);
+ jobject *elts = elements (aresult);
+ for (int i = 0; i < n_array_elts; ++i)
+ elts[i] = parseAnnotationElement(klass, pool, bytes, last);
+ result = aresult;
+ }
+ break;
+ default:
+ throw new java::lang::InternalError();
}
+ return result;
+}
- // 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)
+static ::java::lang::annotation::Annotation *
+parseAnnotation(jclass klass, _Jv_Constants *pool,
+ unsigned char *&bytes, unsigned char *last)
+{
+ int type_index = read_u2 (bytes, last);
+ check_constant (pool, type_index, JV_CONSTANT_Utf8);
+
+ _Jv_Utf8Const *u_name = pool->data[type_index].utf8;
+ jclass anno_class = _Jv_FindClassFromSignatureNoException(u_name->chars(),
+ klass->getClassLoaderInternal());
+ // FIXME: what to do if anno_class==NULL?
+
+ ::java::util::HashMap *hmap = new ::java::util::HashMap();
+ int npairs = read_u2 (bytes, last);
+ for (int i = 0; i < npairs; ++i)
{
- klass->idt = &null_idt;
- return;
+ int name_index = read_u2 (bytes, last);
+ check_constant (pool, name_index, JV_CONSTANT_Utf8);
+ jstring name = _Jv_NewStringUtf8Const (pool->data[name_index].utf8);
+ jobject value = parseAnnotationElement (klass, pool, bytes, last);
+ // FIXME: any checks needed for name?
+ hmap->put(name, value);
}
+ using namespace ::sun::reflect::annotation;
+ return AnnotationInvocationHandler::create (anno_class,
+ (::java::util::Map *) hmap);
+}
- 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 *));
+static jobjectArray
+parseAnnotations(jclass klass, _Jv_Constants *pool,
+ unsigned char *&bytes, unsigned char *last)
+{
+ int num = read_u2 (bytes, last);
+ jobjectArray result = _Jv_NewObjectArray (num,
+ &::java::lang::annotation::Annotation::class$,
+ NULL);
+ jobject *elts = elements (result);
+ for (int i = 0; i < num; ++i)
+ elts[i] = parseAnnotation(klass, pool, bytes, last);
+ return result;
+}
- int itable_size = _Jv_GetInterfaces (klass, &ifaces);
+static jobjectArray
+parseParameterAnnotations(jclass klass, _Jv_Constants *pool,
+ unsigned char *&bytes, unsigned char *last)
+{
+ jclass anno = &::java::lang::annotation::Annotation::class$;
+ jclass annoary = _Jv_GetArrayClass (anno, anno->getClassLoaderInternal());
+
+ // FIXME: something should check the number of params versus the
+ // method
+ int n_params = read_u1 (bytes, last);
+ jobjectArray result = _Jv_NewObjectArray (n_params, annoary, NULL);
+ jobject *elts = elements (result);
+ for (int i = 0; i < n_params; ++i)
+ elts[i] = parseAnnotations(klass, pool, bytes, last);
+ return result;
+}
- 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));
+jobject
+java::lang::Class::getMethodDefaultValue(::java::lang::reflect::Method *meth)
+{
+ // FIXME: could cache the value here...
- _Jv_GenerateITable (klass, &ifaces, itable_offsets);
+ unsigned char *bytes = reflection_data;
+ if (bytes == NULL)
+ return 0;
- jshort cls_iindex =
- _Jv_FindIIndex (ifaces.list, itable_offsets, ifaces.count);
+ unsigned short meth_index = _Jv_FromReflectedMethod (meth) - methods;
- for (int i=0; i < ifaces.count; i++)
+ while (true)
+ {
+ int type = read_u1 (bytes);
+ if (type == JV_DONE_ATTR)
+ return NULL;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ if (type != JV_METHOD_ATTR)
+ {
+ bytes = next;
+ continue;
+ }
+ int kind = read_u1 (bytes, next);
+ if (kind != JV_ANNOTATION_DEFAULT_KIND)
{
- ifaces.list[i]->idt->iface.ioffsets[cls_iindex] =
- itable_offsets[i];
+ bytes = next;
+ continue;
+ }
+ int index = read_u2 (bytes, next);
+ if (meth_index != index)
+ {
+ bytes = next;
+ continue;
}
- klass->idt->cls.iindex = cls_iindex;
-
- _Jv_Free (ifaces.list);
- _Jv_Free (itable_offsets);
- }
- else
- {
- klass->idt->cls.iindex = SHRT_MAX;
+ // FIXME: could cache here. If we do then we have to clone any
+ // array result.
+ return parseAnnotationElement(this, &this->constants, bytes, next);
}
}
-// Return index of item in list, or -1 if item is not present.
-inline jshort
-_Jv_IndexOf (void *item, void **list, jshort list_len)
+jobjectArray
+java::lang::Class::getDeclaredAnnotations(jint /* jv_attr_type */ member_type,
+ jint member_index,
+ jint /* jv_attr_kind */ kind_req)
{
- for (int i=0; i < list_len; i++)
- {
- if (list[i] == item)
- return i;
- }
- return -1;
-}
+ using namespace java::lang::annotation;
+ jobjectArray result;
-// 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)
-{
- jshort result = 0;
-
- for (int i=0; i < klass->interface_count; i++)
- {
- jclass iface = klass->interfaces[i];
+ unsigned char *bytes = reflection_data;
+ if (bytes == NULL)
+ return 0;
- /* Make sure interface is linked. */
- _Jv_WaitForState(iface, JV_STATE_LINKED);
+ if (loader == NULL)
+ loader = (ClassLoader *)VMClassLoader::bootLoader;
- 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 = (loader->getDeclaredAnnotations
+ (this, member_type, member_index, kind_req));
+ if (result)
+ return result;
- result += _Jv_GetInterfaces (klass->interfaces[i], ifaces);
- }
- }
-
- if (klass->isInterface())
- {
- result += klass->method_count + 1;
- }
- else
+ for (;;)
{
- if (klass->superclass)
- {
- result += _Jv_GetInterfaces (klass->superclass, ifaces);
+ int type = read_u1 (bytes);
+ if (type == JV_DONE_ATTR)
+ return NULL;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ if (type != member_type)
+ {
+ bytes = next;
+ continue;
}
+ int kind = read_u1 (bytes, next);
+ if (kind != kind_req)
+ {
+ bytes = next;
+ continue;
+ }
+ if (member_type != JV_CLASS_ATTR)
+ {
+ int index = read_u2 (bytes, next);
+ if (member_index != index)
+ {
+ bytes = next;
+ continue;
+ }
+ }
+
+ if (kind_req == JV_PARAMETER_ANNOTATIONS_KIND)
+ result = ((parseParameterAnnotations
+ (this, &this->constants, bytes, next)));
+ else
+ result = ((parseAnnotations (this, &this->constants, bytes, next)));
+ break;
}
- return result;
+
+ return (loader->putDeclaredAnnotations
+ (this, member_type, member_index, kind_req, 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)
+jobjectArray
+java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Method *meth,
+ jboolean is_param)
{
- 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);
-
- /* Create interface dispatch table for iface */
- if (iface->idt == NULL)
- {
- 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;
+ unsigned short meth_index = _Jv_FromReflectedMethod (meth) - methods;
+ return getDeclaredAnnotations(JV_METHOD_ATTR, meth_index,
+ (is_param
+ ? JV_PARAMETER_ANNOTATIONS_KIND
+ : JV_ANNOTATIONS_KIND));
+}
- iface->idt->iface.ioffsets = ioffsets;
- }
- }
+jobjectArray
+java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Constructor *cons,
+ jboolean is_param)
+{
+ unsigned short meth_index = _Jv_FromReflectedConstructor (cons) - methods;
+ return getDeclaredAnnotations(JV_METHOD_ATTR, meth_index,
+ (is_param
+ ? JV_PARAMETER_ANNOTATIONS_KIND
+ : JV_ANNOTATIONS_KIND));
}
-// Format method name for use in error messages.
-jstring
-_Jv_GetMethodString (jclass klass, _Jv_Utf8Const *name)
+jobjectArray
+java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Field *fld)
{
- jstring r = JvNewStringUTF (klass->name->data);
- r = r->concat (JvNewStringUTF ("."));
- r = r->concat (JvNewStringUTF (name->data));
- return r;
+ unsigned short field_index = _Jv_FromReflectedField (fld) - fields;
+ return getDeclaredAnnotations(JV_FIELD_ATTR, field_index,
+ JV_ANNOTATIONS_KIND);
}
-void
-_Jv_ThrowNoSuchMethodError ()
+JArray< ::java::lang::annotation::Annotation *> *
+java::lang::Class::getDeclaredAnnotationsInternal()
{
- throw new java::lang::NoSuchMethodError;
+ return (JArray< ::java::lang::annotation::Annotation *> *) getDeclaredAnnotations(JV_CLASS_ATTR, 0, JV_ANNOTATIONS_KIND);
}
-// 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)
+static jclass
+resolve_class_constant (jclass klass, _Jv_Constants *pool, int cpool_index)
{
- using namespace java::lang::reflect;
+ check_constant (pool, cpool_index, JV_CONSTANT_Class);
+ // FIXME: what is the correct thing to do with an exception here?
+ return _Jv_Linker::resolve_pool_entry (klass, cpool_index, false).clazz;
+}
- itable[pos++] = (void *) iface;
- _Jv_Method *meth;
-
- for (int j=0; j < iface->method_count; j++)
+jint
+java::lang::Class::findInnerClassAttribute()
+{
+ unsigned char *bytes = reflection_data;
+ if (bytes == NULL)
+ return -1;
+ while (true)
{
- 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;
- }
-
- if (meth && (meth->name->data[0] == '<'))
+ int type = read_u1 (bytes);
+ if (type == JV_DONE_ATTR)
+ break;
+ // After the type but before the length.
+ unsigned char *save = bytes;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ if (type != JV_CLASS_ATTR)
{
- // leave a placeholder in the itable for hidden init methods.
- itable[pos] = NULL;
- }
- 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;
+ bytes = next;
+ continue;
}
- 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;
+ int kind = read_u1 (bytes, next);
+ if (kind != JV_INNER_CLASSES_KIND)
+ {
+ bytes = next;
+ continue;
}
- pos++;
+ return save - reflection_data;
}
-
- return pos;
-}
-
-static _Jv_Mutex_t iindex_mutex;
-static 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)
+ return -1;
+}
+
+jint
+java::lang::Class::findDeclaredClasses(JArray<jclass> *result,
+ jboolean publicOnly,
+ jint offset)
{
- 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++)
+ unsigned char *bytes = reflection_data + offset;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ // Skip a byte.
+ read_u1 (bytes, next);
+ int n_classes = read_u2 (bytes, next);
+ int count = 0;
+ for (int i = 0; i < n_classes; ++i)
{
- int len = ifaces[j]->idt->iface.ioffsets[0];
- if (i >= len)
+ int inner_class_index = read_u2 (bytes, next);
+ int outer_class_index = read_u2 (bytes, next);
+ /*int inner_name_index = */ read_u2 (bytes, next);
+ int inner_flags = read_u2 (bytes, next);
+
+ if (inner_class_index == 0 || outer_class_index == 0)
+ continue;
+ if (resolve_class_constant (this, &constants, outer_class_index) == this)
{
- /* 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;
+ jclass inner = resolve_class_constant (this, &constants,
+ inner_class_index);
+ if (! publicOnly
+ || ((inner_flags
+ & java::lang::reflect::Modifier::PUBLIC) != 0))
+ {
+ if (result)
+ {
+ jclass *elts = elements (result);
+ elts[count] = inner;
+ }
+ ++count;
+ }
}
- ifaces[j]->idt->iface.ioffsets[i] = offsets[j];
}
- _Jv_MutexUnlock (&iindex_mutex);
-
- return i;
+ return count;
}
-// Only used by serialization
-java::lang::reflect::Field *
-java::lang::Class::getPrivateField (jstring name)
+JArray<jclass> *
+java::lang::Class::getDeclaredClasses (jboolean publicOnly)
{
- int hash = name->hashCode ();
-
- java::lang::reflect::Field* rfield;
- for (int i = 0; i < field_count; i++)
+ int offset = findInnerClassAttribute();
+ int count;
+ if (offset == -1)
{
- _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;
+ // No InnerClasses attribute, so no declared classes.
+ count = 0;
}
- 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;
+ else
+ count = findDeclaredClasses(NULL, publicOnly, offset);
+ JArray<jclass> *result
+ = (JArray<jclass> *) JvNewObjectArray (count, &java::lang::Class::class$,
+ NULL);
+ if (count > 0)
+ findDeclaredClasses(result, publicOnly, offset);
+ return result;
}
-// Only used by serialization
-java::lang::reflect::Method *
-java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
+jclass
+java::lang::Class::getDeclaringClass (void)
{
- 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 offset = findInnerClassAttribute();
+ if (offset == -1)
+ return NULL;
+
+ unsigned char *bytes = reflection_data + offset;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ // Skip a byte.
+ read_u1 (bytes, next);
+ int n_classes = read_u2 (bytes, next);
+ for (int i = 0; i < n_classes; ++i)
{
- 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;
+ int inner_class_index = read_u2 (bytes, next);
+ int outer_class_index = read_u2 (bytes, next);
+ /*int inner_name_index = */read_u2 (bytes, next);
+ /*int inner_flags = */read_u2 (bytes, next);
- Method *rmethod = new Method ();
- rmethod->offset = ((char *) (&klass->methods[i])
- - (char *) klass->methods);
- rmethod->declaringClass = klass;
- return rmethod;
- }
- }
+ if (inner_class_index == 0 || outer_class_index == 0)
+ continue;
+ if (resolve_class_constant (this, &constants, inner_class_index) == this)
+ return resolve_class_constant (this, &constants, outer_class_index);
}
- throw new java::lang::NoSuchMethodException (name);
+
+ return NULL;
}
-// Private accessor method for Java code to retrieve the protection domain.
-java::security::ProtectionDomain *
-java::lang::Class::getProtectionDomain0 ()
+jboolean
+java::lang::Class::isAnonymousClass()
{
- return protectionDomain;
+ int offset = findInnerClassAttribute();
+ if (offset == -1)
+ return false;
+
+ unsigned char *bytes = reflection_data + offset;
+ int len = read_4 (bytes);
+ unsigned char *next = bytes + len;
+ // Skip a byte.
+ read_u1 (bytes, next);
+ int n_classes = read_u2 (bytes, next);
+ for (int i = 0; i < n_classes; ++i)
+ {
+ int inner_class_index = read_u2 (bytes, next);
+ /*int outer_class_index = */read_u2 (bytes, next);
+ int inner_name_index = read_u2 (bytes, next);
+ /*int inner_flags = */read_u2 (bytes, next);
+
+ if (inner_class_index == 0)
+ continue;
+ if (resolve_class_constant (this, &constants, inner_class_index) == this)
+ return inner_name_index == 0;
+ }
+
+ return false;
}
-JArray<jobject> *
-java::lang::Class::getSigners()
+jboolean
+java::lang::Class::isLocalClass()
{
- return hack_signers;
+ _Jv_word indexes;
+ indexes.i = getEnclosingMethodData();
+ return indexes.i != 0;
}
-void
-java::lang::Class::setSigners(JArray<jobject> *s)
+jboolean
+java::lang::Class::isMemberClass()
{
- hack_signers = s;
+ // FIXME: is this correct?
+ return !isLocalClass() && getDeclaringClass() != NULL;
}
-// Functions for indirect dispatch (symbolic virtual binding) support.
-
-// There are two tables, atable and otable. atable is an array of
-// addresses, and otable is an array of offsets, and these are used
-// for static and virtual members respectively.
-
-// {a,o}table_syms is an array of _Jv_MethodSymbols. Each such symbol
-// is a tuple of {classname, member name, signature}.
-// _Jv_LinkSymbolTable() scans these two arrays and fills in the
-// corresponding atable and otable with the addresses of static
-// members and the offsets of virtual members.
-
-// The offset (in bytes) for each resolved method or field is placed
-// at the corresponding position in the virtual method offset table
-// (klass->otable).
+\f
-// The same otable and atable may be shared by many classes.
+//
+// Some class-related convenience functions.
+//
-void
-_Jv_LinkSymbolTable(jclass klass)
+// Find a method declared in the class. If it is not declared locally
+// (or if it is inherited), return NULL.
+_Jv_Method *
+_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
+ _Jv_Utf8Const *signature)
{
- //// FIXME: Need to lock the tables ////
-
- int index = 0;
- _Jv_MethodSymbol sym;
- if (klass->otable == NULL
- || klass->otable->state != 0)
- goto atable;
-
- klass->otable->state = 1;
-
- for (index = 0; sym = klass->otable_syms[index], sym.name != NULL; index++)
+ for (int i = 0; i < klass->method_count; ++i)
{
- // FIXME: Why are we passing NULL as the class loader?
- jclass target_class = _Jv_FindClass (sym.class_name, NULL);
- _Jv_Method *meth = NULL;
-
- const _Jv_Utf8Const *signature = sym.signature;
-
- {
- static char *bounce = (char *)_Jv_ThrowNoSuchMethodError;
- ptrdiff_t offset = (char *)(klass->vtable) - bounce;
- klass->otable->offsets[index] = offset;
- }
+ if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
+ && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
+ return &klass->methods[i];
+ }
+ return NULL;
+}
- if (target_class == NULL)
- continue;
+_Jv_Method *
+_Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
+ _Jv_Utf8Const *signature,
+ jclass *declarer_result)
+{
+ for (; klass; klass = klass->getSuperclass())
+ {
+ _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
- if (target_class->isInterface())
+ if (meth)
{
- // FIXME: This does not yet fully conform to binary compatibility
- // rules. It will break if a declaration is moved into a
- // superinterface.
- for (jclass cls = target_class; cls != 0; cls = cls->getSuperclass ())
- {
- for (int i=0; i < cls->method_count; i++)
- {
- meth = &cls->methods[i];
- if (_Jv_equalUtf8Consts (sym.name, meth->name)
- && _Jv_equalUtf8Consts (signature, meth->signature))
- {
- klass->otable->offsets[index] = i + 1;
- goto found;
- }
- }
-
- }
- found:
- continue;
+ if (declarer_result)
+ *declarer_result = klass;
+ return meth;
}
+ }
- // We're looking for a field or a method, and we can tell
- // which is needed by looking at the signature.
- if (signature->length >= 2
- && signature->data[0] == '(')
- {
- // If the target class does not have a vtable_method_count yet,
- // then we can't tell the offsets for its methods, so we must lay
- // it out now.
- if (target_class->vtable_method_count == -1)
- {
- JvSynchronize sync (target_class);
- _Jv_LayoutVTableMethods (target_class);
- }
-
- meth = _Jv_LookupDeclaredMethod(target_class, sym.name,
- sym.signature);
-
- if (meth != NULL)
- {
- klass->otable->offsets[index] =
- _Jv_VTable::idx_to_offset (meth->index);
- }
+ return NULL;
+}
- continue;
- }
+// The rules for finding proxy methods are different: first we search
+// the interfaces implemented by a proxy, then the methods declared in
+// class Proxy.
- // try fields
- {
- _Jv_Field *the_field = NULL;
+java::lang::reflect::Method *
+_Jv_LookupProxyMethod (jclass proxyClass, _Jv_Utf8Const *name,
+ _Jv_Utf8Const *signature)
+{
+ using namespace java::lang::reflect;
+ jclass declaringClass;
+ _Jv_Method * m;
- for (jclass cls = target_class; cls != 0; cls = cls->getSuperclass ())
- {
- for (int i = 0; i < cls->field_count; i++)
- {
- _Jv_Field *field = &cls->fields[i];
- if (! _Jv_equalUtf8Consts (field->name, sym.name))
- continue;
-
- // FIXME: What access checks should we perform here?
-// if (_Jv_CheckAccess (klass, cls, field->flags))
-// {
-
- if (!field->isResolved ())
- _Jv_ResolveField (field, cls->loader);
-
-// if (field_type != 0 && field->type != field_type)
-// throw new java::lang::LinkageError
-// (JvNewStringLatin1
-// ("field type mismatch with different loaders"));
-
- the_field = field;
- goto end_of_field_search;
- }
- }
- end_of_field_search:
- if (the_field != NULL)
- {
- if (the_field->flags & 0x0008 /* Modifier::STATIC */)
- {
- throw new java::lang::IncompatibleClassChangeError;
- }
- else
- {
- klass->otable->offsets[index] = the_field->u.boffset;
- }
- }
- else
- {
- throw new java::lang::NoSuchFieldError
- (_Jv_NewStringUtf8Const (sym.name));
- }
- }
+ for (int i = 0; i < proxyClass->interface_count; i++)
+ {
+ declaringClass = proxyClass->interfaces[i];
+ m = _Jv_GetMethodLocal (declaringClass, name, signature);
+ if (m)
+ break;
}
+ if (!m)
+ m = _Jv_LookupDeclaredMethod (&Proxy::class$,
+ name,
+ signature,
+ &declaringClass);
+
+ Method *rmethod = new Method ();
+ rmethod->offset = (char*) m - (char*) declaringClass->methods;
+ rmethod->declaringClass = declaringClass;
+ return rmethod;
+}
- atable:
- if (klass->atable == NULL
- || klass->atable->state != 0)
- return;
- klass->atable->state = 1;
- for (index = 0; sym = klass->atable_syms[index], sym.name != NULL; index++)
+java::lang::reflect::Method *
+_Jv_GetReflectedMethod (jclass klass, _Jv_Utf8Const *name,
+ _Jv_Utf8Const *signature)
+{
+ for (; klass; klass = klass->getSuperclass())
{
- // FIXME: Why are we passing NULL as the class loader?
- jclass target_class = _Jv_FindClass (sym.class_name, NULL);
- _Jv_Method *meth = NULL;
- const _Jv_Utf8Const *signature = sym.signature;
-
- // ??? Setting this pointer to null will at least get us a
- // NullPointerException
- klass->atable->addresses[index] = NULL;
-
- if (target_class == NULL)
- continue;
-
- // We're looking for a static field or a static method, and we
- // can tell which is needed by looking at the signature.
- if (signature->length >= 2
- && signature->data[0] == '(')
+ _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
+ if (meth)
{
- // If the target class does not have a vtable_method_count yet,
- // then we can't tell the offsets for its methods, so we must lay
- // it out now.
- if (target_class->vtable_method_count == -1)
- {
- JvSynchronize sync (target_class);
- _Jv_LayoutVTableMethods (target_class);
- }
-
- meth = _Jv_LookupDeclaredMethod(target_class, sym.name,
- sym.signature);
-
- if (meth != NULL)
- {
- if (meth->ncode) // Maybe abstract?
- klass->atable->addresses[index] = meth->ncode;
-#ifdef INTERPRETER
- else if (_Jv_IsInterpretedClass (target_class))
- _Jv_Defer_Resolution (target_class, meth,
- &klass->atable->addresses[index]);
-#endif
- }
- else
- klass->atable->addresses[index] = (void *)_Jv_ThrowNoSuchMethodError;
-
- continue;
+ using namespace java::lang::reflect;
+ Method *rmethod = new Method ();
+ rmethod->offset = (char*) meth - (char*) klass->methods;
+ rmethod->declaringClass = klass;
+ return rmethod;
}
-
- // try fields
- {
- _Jv_Field *the_field = NULL;
-
- for (jclass cls = target_class; cls != 0; cls = cls->getSuperclass ())
- {
- for (int i = 0; i < cls->field_count; i++)
- {
- _Jv_Field *field = &cls->fields[i];
- if (! _Jv_equalUtf8Consts (field->name, sym.name))
- continue;
-
- // FIXME: What access checks should we perform here?
-// if (_Jv_CheckAccess (klass, cls, field->flags))
-// {
-
- if (!field->isResolved ())
- _Jv_ResolveField (field, cls->loader);
-
-// if (field_type != 0 && field->type != field_type)
-// throw new java::lang::LinkageError
-// (JvNewStringLatin1
-// ("field type mismatch with different loaders"));
-
- the_field = field;
- goto end_of_static_field_search;
- }
- }
- end_of_static_field_search:
- if (the_field != NULL)
- {
- if (the_field->flags & 0x0008 /* Modifier::STATIC */)
- {
- klass->atable->addresses[index] = the_field->u.addr;
- }
- else
- {
- throw new java::lang::IncompatibleClassChangeError;
- }
- }
- else
- {
- throw new java::lang::NoSuchFieldError
- (_Jv_NewStringUtf8Const (sym.name));
- }
- }
}
+
+ return NULL;
}
+#ifdef HAVE_TLS
-// For each catch_record in the list of caught classes, fill in the
-// address field.
-void
-_Jv_linkExceptionClassTable (jclass self)
+// NOTE: MCACHE_SIZE should be a power of 2 minus one.
+#define MCACHE_SIZE 31
+
+struct _Jv_mcache
{
- struct _Jv_CatchClass *catch_record = self->catch_classes;
- if (!catch_record || catch_record->classname)
- return;
- catch_record++;
- while (catch_record->classname)
+ jclass klass;
+ _Jv_Method *method;
+};
+
+static __thread _Jv_mcache *method_cache;
+#endif // HAVE_TLS
+
+static void *
+_Jv_FindMethodInCache (jclass klass MAYBE_UNUSED,
+ _Jv_Utf8Const *name MAYBE_UNUSED,
+ _Jv_Utf8Const *signature MAYBE_UNUSED)
+{
+#ifdef HAVE_TLS
+ _Jv_mcache *cache = method_cache;
+ if (cache)
{
- jclass target_class = _Jv_FindClass (catch_record->classname,
- self->getClassLoaderInternal ());
- *catch_record->address = target_class;
- catch_record++;
+ 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;
}
- self->catch_classes->classname = (_Jv_Utf8Const *)-1;
+#endif // HAVE_TLS
+ return NULL;
}
-
-// This is put in empty vtable slots.
+
static void
-_Jv_abstractMethodError (void)
+_Jv_AddMethodToCache (jclass klass MAYBE_UNUSED,
+ _Jv_Method *method MAYBE_UNUSED)
{
- throw new java::lang::AbstractMethodError();
+#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
}
-// Prepare virtual method declarations in KLASS, and any superclasses as
-// required, by determining their vtable index, setting method->index, and
-// finally setting the class's vtable_method_count. Must be called with the
-// lock for KLASS held.
+// 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_LayoutVTableMethods (jclass klass)
+_Jv_FreeMethodCache ()
{
- if (klass->vtable != NULL || klass->isInterface()
- || klass->vtable_method_count != -1)
- return;
+#ifdef HAVE_TLS
+ if (method_cache != NULL)
+ {
+ _Jv_Free(method_cache);
+ method_cache = NULL;
+ }
+#endif // HAVE_TLS
+}
- jclass superclass = klass->superclass;
+void *
+_Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
+ _Jv_Utf8Const *signature)
+{
+ using namespace java::lang::reflect;
- typedef unsigned int uaddr __attribute__ ((mode (pointer)));
+ void *ncode = _Jv_FindMethodInCache (klass, name, signature);
+ if (ncode != 0)
+ return ncode;
- // If superclass looks like a constant pool entry,
- // resolve it now.
- if ((uaddr)superclass < (uaddr)klass->constants.size)
+ for (; klass; klass = klass->getSuperclass())
{
- if (klass->state < JV_STATE_LINKED)
- {
- _Jv_Utf8Const *name = klass->constants.data[(int)superclass].utf8;
- superclass = _Jv_FindClass (name, klass->loader);
- if (! superclass)
- {
- jstring str = _Jv_NewStringUTF (name->data);
- throw new java::lang::NoClassDefFoundError (str);
- }
- }
- else
- superclass = klass->constants.data[(int)superclass].clazz;
+ _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
+ if (! meth)
+ continue;
+
+ if (Modifier::isStatic(meth->accflags))
+ throw new java::lang::IncompatibleClassChangeError
+ (_Jv_GetMethodString (klass, meth));
+ if (Modifier::isAbstract(meth->accflags))
+ throw new java::lang::AbstractMethodError
+ (_Jv_GetMethodString (klass, meth));
+ if (! Modifier::isPublic(meth->accflags))
+ throw new java::lang::IllegalAccessError
+ (_Jv_GetMethodString (klass, meth));
+
+ _Jv_AddMethodToCache (klass, meth);
+
+ return meth->ncode;
}
+ throw new java::lang::IncompatibleClassChangeError;
+}
+
+// Fast interface method lookup by index.
+void *
+_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
+{
+ _Jv_IDispatchTable *cldt = klass->idt;
+ int idx = iface->ioffsets[cldt->iindex] + method_idx;
+ return cldt->itable[idx];
+}
- if (superclass != NULL && superclass->vtable_method_count == -1)
+jboolean
+_Jv_IsAssignableFrom (jclass source, jclass target)
+{
+ if (source == target)
+ return true;
+
+ // If target is array, so must source be.
+ while (target->isArray ())
{
- JvSynchronize sync (superclass);
- _Jv_LayoutVTableMethods (superclass);
+ if (! source->isArray())
+ return false;
+ target = target->getComponentType();
+ source = source->getComponentType();
}
- int index = (superclass == NULL ? 0 : superclass->vtable_method_count);
-
- for (int i = 0; i < klass->method_count; ++i)
+ if (target->isInterface())
{
- _Jv_Method *meth = &klass->methods[i];
- _Jv_Method *super_meth = NULL;
+ // Abstract classes have no IDT, and IDTs provide no way to check
+ // two interfaces for assignability.
+ if (__builtin_expect
+ (source->idt == NULL || source->isInterface(), false))
+ return _Jv_InterfaceAssignableFrom (source, target);
- if (! _Jv_isVirtualMethod (meth))
- continue;
+ _Jv_IDispatchTable *cl_idt = source->idt;
+
+ if (__builtin_expect ((target->ioffsets == NULL), false))
+ return false; // No class implementing TARGET has been loaded.
+ jshort cl_iindex = cl_idt->iindex;
+ if (cl_iindex < target->ioffsets[0])
+ {
+ jshort offset = target->ioffsets[cl_iindex];
+ if (offset != -1 && offset < cl_idt->itable_length
+ && cl_idt->itable[offset] == target)
+ return true;
+ }
+ return false;
+ }
- // FIXME: Must check that we don't override:
- // - Package-private method where superclass is in different package.
- // - Final or less-accessible declaration in superclass (check binary
- // spec, do we allocate new vtable entry or put throw node in vtable?)
- // - Static or private method in superclass.
+ // Primitive TYPE classes are only assignable to themselves.
+ if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
+ return false;
- if (superclass != NULL)
+ if (target == &java::lang::Object::class$)
+ return true;
+ 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$)
{
- super_meth = _Jv_LookupDeclaredMethod (superclass, meth->name,
- meth->signature);
+ if (source == target)
+ return true;
+ source = source->getSuperclass();
}
-
- if (super_meth)
- meth->index = super_meth->index;
- else
- meth->index = index++;
}
+ else if (source->depth >= target->depth
+ && source->ancestors[source->depth - target->depth] == target)
+ return true;
- klass->vtable_method_count = index;
+ return false;
}
-// Set entries in VTABLE for virtual methods declared in KLASS. If
-// KLASS has an immediate abstract parent, recursively do its methods
-// first. FLAGS is used to determine which slots we've actually set.
-void
-_Jv_SetVTableEntries (jclass klass, _Jv_VTable *vtable, jboolean *flags)
+// 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 source, jclass iface)
{
- using namespace java::lang::reflect;
+ for (int i = 0; i < source->interface_count; i++)
+ {
+ jclass interface = source->interfaces[i];
+ if (iface == interface
+ || _Jv_InterfaceAssignableFrom (interface, iface))
+ return true;
+ }
+
+ if (!source->isInterface()
+ && source->superclass
+ && _Jv_InterfaceAssignableFrom (source->superclass, iface))
+ return true;
+
+ return false;
+}
- jclass superclass = klass->getSuperclass();
+jboolean
+_Jv_IsInstanceOf(jobject obj, jclass cl)
+{
+ if (__builtin_expect (!obj, false))
+ return false;
+ return _Jv_IsAssignableFrom (JV_CLASS (obj), cl);
+}
- if (superclass != NULL && (superclass->getModifiers() & Modifier::ABSTRACT))
- _Jv_SetVTableEntries (superclass, vtable, flags);
+void *
+_Jv_CheckCast (jclass c, jobject obj)
+{
+ if (__builtin_expect
+ (obj != NULL && ! _Jv_IsAssignableFrom(JV_CLASS (obj), c), false))
+ throw new java::lang::ClassCastException
+ ((new java::lang::StringBuffer
+ (obj->getClass()->getName()))->append
+ (JvNewStringUTF(" cannot be cast to "))->append
+ (c->getName())->toString());
+
+ return obj;
+}
- for (int i = klass->method_count - 1; i >= 0; i--)
+void
+_Jv_CheckArrayStore (jobject arr, jobject obj)
+{
+ if (obj)
{
- _Jv_Method *meth = &klass->methods[i];
- if (meth->index == (_Jv_ushort) -1)
- continue;
- if ((meth->accflags & Modifier::ABSTRACT))
- {
- vtable->set_method(meth->index, (void *) &_Jv_abstractMethodError);
- flags[meth->index] = false;
- }
- else
- {
- vtable->set_method(meth->index, meth->ncode);
- flags[meth->index] = true;
- }
+ 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 (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());
}
}
-// Allocate and lay out the virtual method table for KLASS. This will also
-// cause vtables to be generated for any non-abstract superclasses, and
-// virtual method layout to occur for any abstract superclasses. Must be
-// called with monitor lock for KLASS held.
-void
-_Jv_MakeVTable (jclass klass)
+jboolean
+_Jv_IsAssignableFromSlow (jclass source, jclass target)
{
- using namespace java::lang::reflect;
+ // First, strip arrays.
+ while (target->isArray ())
+ {
+ // If target is array, source must be as well.
+ if (! source->isArray ())
+ return false;
+ target = target->getComponentType ();
+ source = source->getComponentType ();
+ }
- if (klass->vtable != NULL || klass->isInterface()
- || (klass->accflags & Modifier::ABSTRACT))
- return;
+ // Quick success.
+ if (target == &java::lang::Object::class$)
+ return true;
- // Class must be laid out before we can create a vtable.
- if (klass->vtable_method_count == -1)
- _Jv_LayoutVTableMethods (klass);
+ // 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);
- // Allocate the new vtable.
- _Jv_VTable *vtable = _Jv_VTable::new_vtable (klass->vtable_method_count);
- klass->vtable = vtable;
+ do
+ {
+ 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);
- jboolean flags[klass->vtable_method_count];
- for (int i = 0; i < klass->vtable_method_count; ++i)
- flags[i] = false;
+ return false;
+}
- // Copy the vtable of the closest non-abstract superclass.
- jclass superclass = klass->superclass;
- if (superclass != NULL)
+// 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)
+{
+ for (jclass klass = search_class; klass; klass = klass->getSuperclass())
{
- while ((superclass->accflags & Modifier::ABSTRACT) != 0)
- superclass = superclass->superclass;
-
- if (superclass->vtable == NULL)
+ // FIXME: Throw an exception?
+ if (!klass->isInterface ())
+ return false;
+
+ int max = klass->method_count;
+ int offset = 0;
+ for (int i = 0; i < max; ++i)
{
- JvSynchronize sync (superclass);
- _Jv_MakeVTable (superclass);
+ // Skip <clinit> here, as it will not be in the IDT.
+ if (klass->methods[i].name->first() == '<')
+ continue;
+
+ if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
+ && _Jv_equalUtf8Consts (klass->methods[i].signature, utf_sig))
+ {
+ // Found it.
+ using namespace java::lang::reflect;
+
+ // FIXME: Method must be public. Throw an exception?
+ if (! Modifier::isPublic (klass->methods[i].accflags))
+ break;
+
+ found_class = klass;
+ // Interface method indexes count from 1.
+ index = offset + 1;
+ return true;
+ }
+
+ ++offset;
}
+ }
- for (int i = 0; i < superclass->vtable_method_count; ++i)
+ // If we haven't found a match, and this class is an interface, then
+ // check all the superinterfaces.
+ if (search_class->isInterface())
+ {
+ for (int i = 0; i < search_class->interface_count; ++i)
{
- vtable->set_method (i, superclass->vtable->get_method (i));
- flags[i] = true;
+ using namespace java::lang::reflect;
+ bool found = _Jv_getInterfaceMethod (search_class->interfaces[i],
+ found_class, index,
+ utf_name, utf_sig);
+ if (found)
+ return true;
}
}
- // Set the class pointer and GC descriptor.
- vtable->clas = klass;
- vtable->gc_descr = _Jv_BuildGCDescr (klass);
+ return false;
+}
+
+#ifdef INTERPRETER
+_Jv_MethodBase *
+_Jv_FindInterpreterMethod (jclass klass, jmethodID desired_method)
+{
+ using namespace java::lang::reflect;
+
+ _Jv_InterpClass *iclass
+ = reinterpret_cast<_Jv_InterpClass *> (klass->aux_info);
+ _Jv_MethodBase **imethods = _Jv_GetFirstMethod (iclass);
+
+ for (int i = 0; i < JvNumMethods (klass); ++i)
+ {
+ _Jv_MethodBase *imeth = imethods[i];
+ if (imeth->get_method () == desired_method)
+ return imeth;
+ }
+
+ return NULL;
+}
+#endif
+
+// Return Utf8 name of a class. This function is here for code that
+// can't access klass->name directly.
+_Jv_Utf8Const*
+_Jv_GetClassNameUtf8 (jclass klass)
+{
+ return klass->name;
+}
+
+jclass
+_Jv_GetMethodDeclaringClass (jmethodID method)
+{
+ _Jv_StackTrace::UpdateNCodeMap ();
+ jobject obj = reinterpret_cast<jobject> (method->ncode);
+ return reinterpret_cast<jclass> (_Jv_StackTrace::ncodeMap->get (obj));
+}
- // For each virtual declared in klass and any immediate abstract
- // superclasses, set new vtable entry or override an old one.
- _Jv_SetVTableEntries (klass, vtable, flags);
+jbyte
+_Jv_GetClassState (jclass klass)
+{
+ return klass->state;
+}
- // It is an error to have an abstract method in a concrete class.
- if (! (klass->accflags & Modifier::ABSTRACT))
+jstring
+_Jv_GetInterpClassSourceFile (jclass klass)
+{
+ if (_Jv_IsInterpretedClass (klass))
{
- for (int i = 0; i < klass->vtable_method_count; ++i)
- if (! flags[i])
- {
- using namespace java::lang;
- while (klass != NULL)
- {
- for (int j = 0; j < klass->method_count; ++j)
- {
- if (klass->methods[i].index == i)
- {
- StringBuffer *buf = new StringBuffer ();
- buf->append (_Jv_NewStringUtf8Const (klass->methods[i].name));
- buf->append ((jchar) ' ');
- buf->append (_Jv_NewStringUtf8Const (klass->methods[i].signature));
- throw new AbstractMethodError (buf->toString ());
- }
- }
- klass = klass->getSuperclass ();
- }
- // Couldn't find the name, which is weird.
- // But we still must throw the error.
- throw new AbstractMethodError ();
- }
+ _Jv_InterpClass *iclass =
+ reinterpret_cast<_Jv_InterpClass *> (klass->aux_info);
+ return iclass->source_file_name;
}
+
+ return NULL;
}