#include <config.h>
#include <limits.h>
#include <string.h>
#pragma implementation "Class.h"
#include <gcj/cni.h>
#include <jvm.h>
#include <java-threads.h>
#include <java/lang/Class.h>
#include <java/lang/ClassLoader.h>
#include <java/lang/String.h>
#include <java/lang/reflect/Modifier.h>
#include <java/lang/reflect/Member.h>
#include <java/lang/reflect/Method.h>
#include <java/lang/reflect/Field.h>
#include <java/lang/reflect/Constructor.h>
#include <java/lang/AbstractMethodError.h>
#include <java/lang/ArrayStoreException.h>
#include <java/lang/ClassCastException.h>
#include <java/lang/ClassNotFoundException.h>
#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/ArrayIndexOutOfBoundsException.h>
#include <java/lang/InstantiationException.h>
#include <java/lang/NoClassDefFoundError.h>
#include <java/lang/NoSuchFieldException.h>
#include <java/lang/NoSuchMethodError.h>
#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 <gnu/gcj/runtime/StackTrace.h>
#include <gcj/method.h>
#include <gnu/gcj/runtime/MethodRef.h>
#include <gnu/gcj/RawData.h>
#include <java-cpool.h>
using namespace gcj;
jclass
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_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
if (! _Jv_VerifyClassName (name))
throw new java::lang::ClassNotFoundException (className);
jclass klass = (buffer[0] == '['
? _Jv_FindClassFromSignature (name->data, loader)
: _Jv_FindClass (name, loader));
if (klass == NULL)
throw new java::lang::ClassNotFoundException (className);
if (initialize)
_Jv_InitClass (klass);
return klass;
}
jclass
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->getClassLoader();
}
catch (::java::lang::ArrayIndexOutOfBoundsException *e)
{
}
return forName (className, true, loader);
}
java::lang::ClassLoader *
java::lang::Class::getClassLoader (void)
{
#if 0
java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
if (s != NULL)
s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
#endif
if (isPrimitive ())
return NULL;
return loader ? loader : ClassLoader::getSystemClassLoader ();
}
java::lang::reflect::Constructor *
java::lang::Class::getConstructor (JArray<jclass> *param_types)
{
jstring partial_sig = getSignature (param_types, true);
jint hash = partial_sig->hashCode ();
int i = isPrimitive () ? 0 : method_count;
while (--i >= 0)
{
if (_Jv_equalUtf8Consts (methods[i].name, init_name)
&& _Jv_equal (methods[i].signature, partial_sig, hash))
{
using namespace java::lang::reflect;
if (! Modifier::isPublic(methods[i].accflags))
break;
Constructor *cons = new Constructor ();
cons->offset = (char *) (&methods[i]) - (char *) methods;
cons->declaringClass = this;
return cons;
}
}
throw new java::lang::NoSuchMethodException;
}
JArray<java::lang::reflect::Constructor *> *
java::lang::Class::_getConstructors (jboolean declared)
{
int numConstructors = 0;
int max = isPrimitive () ? 0 : method_count;
int i;
for (i = max; --i >= 0; )
{
_Jv_Method *method = &methods[i];
if (method->name == NULL
|| ! _Jv_equalUtf8Consts (method->name, init_name))
continue;
if (! declared
&& ! java::lang::reflect::Modifier::isPublic(method->accflags))
continue;
numConstructors++;
}
JArray<java::lang::reflect::Constructor *> *result
= (JArray<java::lang::reflect::Constructor *> *)
JvNewObjectArray (numConstructors,
&java::lang::reflect::Constructor::class$,
NULL);
java::lang::reflect::Constructor** cptr = elements (result);
for (i = 0; i < max; i++)
{
_Jv_Method *method = &methods[i];
if (method->name == NULL
|| ! _Jv_equalUtf8Consts (method->name, init_name))
continue;
if (! declared
&& ! java::lang::reflect::Modifier::isPublic(method->accflags))
continue;
java::lang::reflect::Constructor *cons
= new java::lang::reflect::Constructor ();
cons->offset = (char *) method - (char *) methods;
cons->declaringClass = this;
*cptr++ = cons;
}
return result;
}
java::lang::reflect::Constructor *
java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
{
jstring partial_sig = getSignature (param_types, true);
jint hash = partial_sig->hashCode ();
int i = isPrimitive () ? 0 : method_count;
while (--i >= 0)
{
if (_Jv_equalUtf8Consts (methods[i].name, init_name)
&& _Jv_equal (methods[i].signature, partial_sig, hash))
{
using namespace java::lang::reflect;
Constructor *cons = new Constructor ();
cons->offset = (char *) (&methods[i]) - (char *) methods;
cons->declaringClass = this;
return cons;
}
}
throw new java::lang::NoSuchMethodException;
}
java::lang::reflect::Field *
java::lang::Class::getField (jstring name, jint hash)
{
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;
if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
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->getField(name, hash);
for (int i = 0; i < interface_count && rfield == NULL; ++i)
rfield = interfaces[i]->getField (name, hash);
return rfield;
}
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);
int hash = name->hashCode();
for (int i = 0; i < field_count; i++)
{
_Jv_Field *field = &fields[i];
if (! _Jv_equal (field->name, name, hash))
continue;
java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
rfield->offset = (char*) field - (char*) fields;
rfield->declaringClass = this;
rfield->name = name;
return rfield;
}
throw new java::lang::NoSuchFieldException (name);
}
JArray<java::lang::reflect::Field *> *
java::lang::Class::getDeclaredFields (void)
{
java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
if (s != NULL)
s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);
JArray<java::lang::reflect::Field *> *result
= (JArray<java::lang::reflect::Field *> *)
JvNewObjectArray (field_count, &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];
java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
rfield->offset = (char*) field - (char*) fields;
rfield->declaringClass = this;
*fptr++ = rfield;
}
return result;
}
void
java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
{
if (isPrimitive())
buffer->append((jchar) method_count);
else
{
jstring name = getName();
if (name->charAt(0) != '[')
buffer->append((jchar) 'L');
buffer->append(name);
if (name->charAt(0) != '[')
buffer->append((jchar) ';');
}
}
jstring
java::lang::Class::getSignature (JArray<jclass> *param_types,
jboolean is_constructor)
{
java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
buf->append((jchar) '(');
if (param_types != NULL)
{
jclass *v = elements (param_types);
for (int i = 0; i < param_types->length; ++i)
v[i]->getSignature(buf);
}
buf->append((jchar) ')');
if (is_constructor)
buf->append((jchar) 'V');
return buf->toString();
}
java::lang::reflect::Method *
java::lang::Class::_getDeclaredMethod (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);
int i = isPrimitive () ? 0 : method_count;
while (--i >= 0)
{
if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
&& _Jv_equaln (methods[i].signature, partial_sig, p_len)
&& (methods[i].accflags
& java::lang::reflect::Modifier::INVISIBLE) == 0)
{
using namespace java::lang::reflect;
Method *rmethod = new Method ();
rmethod->offset = (char*) (&methods[i]) - (char*) methods;
rmethod->declaringClass = this;
return rmethod;
}
}
return NULL;
}
JArray<java::lang::reflect::Method *> *
java::lang::Class::getDeclaredMethods (void)
{
int numMethods = 0;
int max = isPrimitive () ? 0 : method_count;
int i;
for (i = max; --i >= 0; )
{
_Jv_Method *method = &methods[i];
if (method->name == NULL
|| _Jv_equalUtf8Consts (method->name, clinit_name)
|| _Jv_equalUtf8Consts (method->name, init_name)
|| _Jv_equalUtf8Consts (method->name, finit_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, &java::lang::reflect::Method::class$, NULL);
java::lang::reflect::Method** mptr = elements (result);
for (i = 0; i < max; i++)
{
_Jv_Method *method = &methods[i];
if (method->name == NULL
|| _Jv_equalUtf8Consts (method->name, clinit_name)
|| _Jv_equalUtf8Consts (method->name, init_name)
|| _Jv_equalUtf8Consts (method->name, finit_name)
|| (methods[i].accflags
& java::lang::reflect::Modifier::INVISIBLE) != 0)
continue;
java::lang::reflect::Method* rmethod
= new java::lang::reflect::Method ();
rmethod->offset = (char*) method - (char*) methods;
rmethod->declaringClass = this;
*mptr++ = rmethod;
}
return result;
}
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)
{
JArray<jclass> *result
= (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
NULL);
return result;
}
JArray<jclass> *
java::lang::Class::getDeclaredClasses (void)
{
checkMemberAccess (java::lang::reflect::Member::DECLARED);
JArray<jclass> *result
= (JArray<jclass> *) JvNewObjectArray (0, &java::lang::Class::class$,
NULL);
return result;
}
jclass
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, &java::lang::reflect::Field::class$, 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];
return reinterpret_cast<JArray<jclass> *> (r);
}
java::lang::reflect::Method *
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())
{
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)
&& (klass->methods[i].accflags
& java::lang::reflect::Modifier::INVISIBLE) == 0)
{
using namespace java::lang::reflect;
if (! Modifier::isPublic (klass->methods[i].accflags))
break;
Method *rmethod = new Method ();
rmethod->offset = ((char *) (&klass->methods[i])
- (char *) klass->methods);
rmethod->declaringClass = klass;
return rmethod;
}
}
}
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;
}
jint
java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
jint offset)
{
jint count = 0;
for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
{
_Jv_Method *method = &methods[i];
if (method->name == NULL
|| _Jv_equalUtf8Consts (method->name, clinit_name)
|| _Jv_equalUtf8Consts (method->name, init_name)
|| _Jv_equalUtf8Consts (method->name, finit_name)
|| (method->accflags
& java::lang::reflect::Modifier::INVISIBLE) != 0)
continue;
if (! java::lang::reflect::Modifier::isPublic (method->accflags))
continue;
if (result != NULL)
{
jboolean add = true;
java::lang::reflect::Method **mp = elements (result);
for (int j = 0; j < offset; ++j)
{
_Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
if (_Jv_equalUtf8Consts (method->name, meth_2->name)
&& _Jv_equalUtf8Consts (method->signature,
meth_2->signature))
{
add = false;
break;
}
}
if (! add)
continue;
}
if (result != NULL)
{
using namespace java::lang::reflect;
Method *rmethod = new Method ();
rmethod->offset = (char *) method - (char *) methods;
rmethod->declaringClass = this;
Method **mp = elements (result);
mp[offset + count] = rmethod;
}
++count;
}
offset += count;
if (getSuperclass () != NULL)
{
jint s_count = getSuperclass()->_getMethods (result, offset);
offset += s_count;
count += s_count;
}
for (int i = 0; i < interface_count; ++i)
{
int f_count = interfaces[i]->_getMethods (result, offset);
count += f_count;
offset += f_count;
}
return count;
}
JArray<java::lang::reflect::Method *> *
java::lang::Class::getMethods (void)
{
using namespace java::lang::reflect;
jint count = _getMethods (NULL, 0);
JArray<Method *> *result
= ((JArray<Method *> *) JvNewObjectArray (count,
&Method::class$,
NULL));
jint real_count = _getMethods (result, 0);
if (real_count != count)
{
JArray<Method *> *r2
= ((JArray<Method *> *) JvNewObjectArray (real_count,
&Method::class$,
NULL));
Method **destp = elements (r2);
Method **srcp = elements (result);
for (int i = 0; i < real_count; ++i)
*destp++ = *srcp++;
result = r2;
}
return result;
}
jboolean
java::lang::Class::isAssignableFrom (jclass klass)
{
_Jv_InitClass (this);
_Jv_InitClass (klass);
return _Jv_IsAssignableFrom (this, klass);
}
jboolean
java::lang::Class::isInstance (jobject obj)
{
if (! obj)
return false;
_Jv_InitClass (this);
return _Jv_IsAssignableFrom (this, JV_CLASS (obj));
}
jobject
java::lang::Class::newInstance (void)
{
if (this == &java::lang::Class::class$)
throw new java::lang::IllegalAccessException;
if (isPrimitive ()
|| isInterface ()
|| isArray ()
|| java::lang::reflect::Modifier::isAbstract(accflags))
throw new java::lang::InstantiationException;
_Jv_InitClass (this);
_Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
if (! meth)
throw new java::lang::NoSuchMethodException;
jobject r = JvAllocObject (this);
((void (*) (jobject)) meth->ncode) (r);
return r;
}
void
java::lang::Class::finalize (void)
{
#ifdef INTERPRETER
JvAssert (_Jv_IsInterpretedClass (this));
_Jv_UnregisterClass (this);
#endif
}
void
java::lang::Class::initializeClass (void)
{
if (state == JV_STATE_DONE)
return;
_Jv_MonitorEnter (this);
if (state < JV_STATE_LINKED)
{
#ifdef INTERPRETER
if (_Jv_IsInterpretedClass (this))
{
_Jv_MonitorExit (this);
java::lang::ClassLoader::resolveClass0 (this);
_Jv_MonitorEnter (this);
}
else
#endif
{
_Jv_PrepareCompiledClass (this);
}
}
if (state <= JV_STATE_LINKED)
_Jv_PrepareConstantTimeTables (this);
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 ();
if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS)
{
_Jv_MonitorExit (this);
return;
}
if (state == JV_STATE_ERROR)
{
_Jv_MonitorExit (this);
throw new java::lang::NoClassDefFoundError (getName());
}
thread = self;
state = JV_STATE_IN_PROGRESS;
_Jv_MonitorExit (this);
if (! isInterface () && superclass)
{
try
{
_Jv_InitClass (superclass);
}
catch (java::lang::Throwable *except)
{
_Jv_MonitorEnter (this);
state = JV_STATE_ERROR;
notifyAll ();
_Jv_MonitorExit (this);
throw except;
}
}
try
{
_Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name,
void_signature);
if (meth)
((void (*) (void)) meth->ncode) ();
}
catch (java::lang::Throwable *except)
{
if (! java::lang::Error::class$.isInstance(except))
{
try
{
except = new ExceptionInInitializerError (except);
}
catch (java::lang::Throwable *t)
{
except = t;
}
}
_Jv_MonitorEnter (this);
state = JV_STATE_ERROR;
notifyAll ();
_Jv_MonitorExit (this);
throw except;
}
_Jv_MonitorEnter (this);
state = JV_STATE_DONE;
notifyAll ();
_Jv_MonitorExit (this);
}
_Jv_Method *
_Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
_Jv_Utf8Const *signature)
{
for (int i = 0; i < klass->method_count; ++i)
{
if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
&& _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
return &klass->methods[i];
}
return NULL;
}
_Jv_Method *
_Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
_Jv_Utf8Const *signature)
{
for (; klass; klass = klass->getSuperclass())
{
_Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
if (meth)
return meth;
}
return NULL;
}
#define MCACHE_SIZE 1023
struct _Jv_mcache
{
jclass klass;
_Jv_Method *method;
};
static _Jv_mcache method_cache[MCACHE_SIZE + 1];
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 && _Jv_equalUtf8Consts (m->name, name)
&& _Jv_equalUtf8Consts (m->signature, signature))
return mc->method->ncode;
return NULL;
}
static void
_Jv_AddMethodToCache (jclass klass,
_Jv_Method *method)
{
_Jv_MonitorEnter (&java::lang::Class::class$);
int index = method->name->hash & MCACHE_SIZE;
method_cache[index].method = method;
method_cache[index].klass = klass;
_Jv_MonitorExit (&java::lang::Class::class$);
}
void *
_Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
_Jv_Utf8Const *signature)
{
using namespace java::lang::reflect;
void *ncode = _Jv_FindMethodInCache (klass, name, signature);
if (ncode != 0)
return ncode;
for (; klass; klass = klass->getSuperclass())
{
_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));
_Jv_AddMethodToCache (klass, meth);
return meth->ncode;
}
throw new java::lang::IncompatibleClassChangeError;
}
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];
}
jboolean
_Jv_IsAssignableFrom (jclass target, jclass source)
{
if (source == target)
return true;
if (target->isArray ())
{
if (! source->isArray())
return false;
return _Jv_IsAssignableFrom(target->getComponentType(),
source->getComponentType());
}
if (target->isInterface())
{
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; 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;
}
return false;
}
if (__builtin_expect (target->isPrimitive(), false))
return false;
if (target == &java::lang::Object::class$)
{
if (source->isPrimitive())
return false;
return true;
}
else if (source->ancestors != NULL
&& target->ancestors != NULL
&& source->depth >= target->depth
&& source->ancestors[source->depth - target->depth] == target)
return true;
return false;
}
jboolean
_Jv_InterfaceAssignableFrom (jclass iface, jclass source)
{
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;
}
jboolean
_Jv_IsInstanceOf(jobject obj, jclass cl)
{
if (__builtin_expect (!obj, false))
return false;
return (_Jv_IsAssignableFrom (cl, JV_CLASS (obj)));
}
void *
_Jv_CheckCast (jclass c, jobject obj)
{
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;
}
void
_Jv_CheckArrayStore (jobject arr, jobject obj)
{
if (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
((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} };
void
_Jv_PrepareConstantTimeTables (jclass klass)
{
if (klass->isPrimitive () || klass->isInterface ())
return;
if ((klass->idt != NULL) || klass->depth != 0)
return;
jclass klass0 = klass;
jboolean has_interfaces = 0;
while (klass0 != &java::lang::Object::class$)
{
has_interfaces += klass0->interface_count;
klass0 = klass0->superclass;
klass->depth++;
}
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;
if (!has_interfaces)
{
klass->idt = &null_idt;
return;
}
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];
}
klass->idt->cls.iindex = cls_iindex;
_Jv_Free (ifaces.list);
_Jv_Free (itable_offsets);
}
else
{
klass->idt->cls.iindex = SHRT_MAX;
}
}
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;
}
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];
if (_Jv_IndexOf (iface, (void **) ifaces->list, ifaces->count) == -1)
{
if (ifaces->count + 1 >= ifaces->len)
{
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
{
if (klass->superclass)
{
result += _Jv_GetInterfaces (klass->superclass, ifaces);
}
}
return result;
}
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);
if (iface->idt == NULL)
{
iface->idt =
(_Jv_IDispatchTable *) _Jv_Malloc (sizeof (_Jv_IDispatchTable));
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;
}
}
}
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;
}
void
_Jv_ThrowNoSuchMethodError ()
{
throw new java::lang::NoSuchMethodError;
}
jshort
_Jv_AppendPartialITable (jclass klass, jclass iface, void **itable,
jshort pos)
{
using namespace java::lang::reflect;
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;
}
if (meth && (meth->name->data[0] == '<'))
{
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;
}
else
{
itable[pos] = (void *) _Jv_ThrowNoSuchMethodError;
}
pos++;
}
return pos;
}
static _Jv_Mutex_t iindex_mutex;
bool iindex_mutex_initialized = false;
jshort
_Jv_FindIIndex (jclass *ifaces, jshort *offsets, jshort num)
{
int i;
int j;
if (! iindex_mutex_initialized)
{
_Jv_MutexInit (&iindex_mutex);
iindex_mutex_initialized = true;
}
_Jv_MutexLock (&iindex_mutex);
for (i=1;; i++)
{
for (j=0;; j++)
{
if (j >= num)
goto found;
if (i >= ifaces[j]->idt->iface.ioffsets[0])
continue;
int ioffset = ifaces[j]->idt->iface.ioffsets[i];
if (ioffset >= 0 && ioffset != offsets[j])
break;
}
}
found:
for (j = 0; j < num; j++)
{
int len = ifaces[j]->idt->iface.ioffsets[0];
if (i >= len)
{
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;
}
ifaces[j]->idt->iface.ioffsets[i] = offsets[j];
}
_Jv_MutexUnlock (&iindex_mutex);
return i;
}
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;
}
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))
{
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;
}
java::security::ProtectionDomain *
java::lang::Class::getProtectionDomain0 ()
{
return protectionDomain;
}
void
_Jv_LinkOffsetTable(jclass klass)
{
if (klass->otable == NULL
|| klass->otable->state != 0)
return;
klass->otable->state = 1;
int index = 0;
_Jv_MethodSymbol sym = klass->otable_syms[0];
while (sym.name != NULL)
{
jclass target_class = _Jv_FindClass (sym.class_name, NULL);
_Jv_Method *meth = NULL;
if (target_class != NULL)
if (target_class->isInterface())
{
for (int i=0; i < target_class->method_count; i++)
{
meth = &target_class->methods[i];
if (_Jv_equalUtf8Consts (sym.name, meth->name)
&& _Jv_equalUtf8Consts (sym.signature, meth->signature))
{
klass->otable->offsets[index] = i + 1;
break;
}
}
}
else
{
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);
}
}
if (meth == NULL)
klass->otable->offsets[index] = -1;
sym = klass->otable_syms[++index];
}
}
static jboolean
isVirtualMethod (_Jv_Method *meth)
{
using namespace java::lang::reflect;
return (((meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) == 0)
&& meth->name->data[0] != '<');
}
static void
_Jv_abstractMethodError (void)
{
throw new java::lang::AbstractMethodError();
}
void
_Jv_LayoutVTableMethods (jclass klass)
{
if (klass->vtable != NULL || klass->isInterface()
|| klass->vtable_method_count != -1)
return;
jclass superclass = klass->superclass;
if (superclass != NULL && superclass->vtable_method_count == -1)
{
JvSynchronize sync (superclass);
_Jv_LayoutVTableMethods (superclass);
}
int index = (superclass == NULL ? 0 : superclass->vtable_method_count);
for (int i = 0; i < klass->method_count; ++i)
{
_Jv_Method *meth = &klass->methods[i];
_Jv_Method *super_meth = NULL;
if (! isVirtualMethod (meth))
continue;
if (superclass != NULL)
{
super_meth = _Jv_LookupDeclaredMethod (superclass, meth->name,
meth->signature);
}
if (super_meth)
meth->index = super_meth->index;
else if (! (meth->accflags & java::lang::reflect::Modifier::FINAL)
&& ! (klass->accflags & java::lang::reflect::Modifier::FINAL))
meth->index = index++;
}
klass->vtable_method_count = index;
}
void
_Jv_SetVTableEntries (jclass klass, _Jv_VTable *vtable, jboolean *flags)
{
using namespace java::lang::reflect;
jclass superclass = klass->getSuperclass();
if (superclass != NULL && (superclass->getModifiers() & Modifier::ABSTRACT))
_Jv_SetVTableEntries (superclass, vtable, flags);
for (int i = klass->method_count - 1; i >= 0; i--)
{
_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;
}
}
}
void
_Jv_MakeVTable (jclass klass)
{
using namespace java::lang::reflect;
if (klass->vtable != NULL || klass->isInterface()
|| (klass->accflags & Modifier::ABSTRACT))
return;
if (klass->vtable_method_count == -1)
_Jv_LayoutVTableMethods (klass);
_Jv_VTable *vtable = _Jv_VTable::new_vtable (klass->vtable_method_count);
klass->vtable = vtable;
jboolean flags[klass->vtable_method_count];
for (int i = 0; i < klass->vtable_method_count; ++i)
flags[i] = false;
jclass superclass = klass->superclass;
if (superclass != NULL)
{
while ((superclass->accflags & Modifier::ABSTRACT) != 0)
superclass = superclass->superclass;
if (superclass->vtable == NULL)
{
JvSynchronize sync (superclass);
_Jv_MakeVTable (superclass);
}
for (int i = 0; i < superclass->vtable_method_count; ++i)
{
vtable->set_method (i, superclass->vtable->get_method (i));
flags[i] = true;
}
}
vtable->clas = klass;
vtable->gc_descr = _Jv_BuildGCDescr (klass);
_Jv_SetVTableEntries (klass, vtable, flags);
if (! (klass->accflags & Modifier::ABSTRACT))
{
for (int i = 0; i < klass->vtable_method_count; ++i)
if (! flags[i])
throw new java::lang::AbstractMethodError ();
}
}