// defineclass.cc - defining a class from .class format.
-/* Copyright (C) 1999 Cygnus Solutions
+/* Copyright (C) 1999, 2000, 2001 Free Software Foundation
This file is part of libgcj.
currently being ignored ("InnerClasses", "LineNumber", etc...).
*/
+#include <config.h>
+
#include <java-interp.h>
#ifdef INTERPRETER
+#include <stdlib.h>
#include <java-cpool.h>
-#include <cni.h>
+#include <gcj/cni.h>
#include <java/lang/Class.h>
#include <java/lang/Float.h>
#include <java/lang/ClassCircularityError.h>
#include <java/lang/ClassNotFoundException.h>
#include <java/lang/IncompatibleClassChangeError.h>
-
-#define ClassClass _CL_Q34java4lang5Class
-extern java::lang::Class ClassClass;
-#define StringClass _CL_Q34java4lang6String
-extern java::lang::Class StringClass;
-#define ClassObject _CL_Q34java4lang6Object
-extern java::lang::Class ClassObject;
+#include <java/lang/reflect/Modifier.h>
// we don't verify method names that match these.
static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
__attribute__ ((__noreturn__));
static void throw_class_format_error (jstring msg)
__attribute__ ((__noreturn__));
-static void throw_class_format_error (char *msg)
- __attribute__ ((__noreturn__));
static void throw_incompatible_class_change_error (jstring msg)
__attribute__ ((__noreturn__));
static void throw_class_circularity_error (jstring msg)
throw_class_format_error ("erroneous constant pool tag");
}
+ inline void verify_identifier (_Jv_Utf8Const* name)
+ {
+ if (! _Jv_VerifyIdentifier (name))
+ throw_class_format_error ("erroneous identifier");
+ }
+
+ inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
+ {
+ if (! _Jv_VerifyClassName (ptr, length))
+ throw_class_format_error ("erroneous class name");
+ }
+
+ inline void verify_classname (_Jv_Utf8Const *name)
+ {
+ if (! _Jv_VerifyClassName (name))
+ throw_class_format_error ("erroneous class name");
+ }
+
+ inline void verify_field_signature (_Jv_Utf8Const *sig)
+ {
+ if (! _Jv_VerifyFieldSignature (sig))
+ throw_class_format_error ("erroneous type descriptor");
+ }
+
+ inline void verify_method_signature (_Jv_Utf8Const *sig)
+ {
+ if (! _Jv_VerifyMethodSignature (sig))
+ throw_class_format_error ("erroneous type descriptor");
+ }
+
_Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length)
{
if (klass == 0 || length < 0 || offset+length > data->length)
void read_one_method_attribute (int method);
void read_one_code_attribute (int method);
void read_one_field_attribute (int field);
+ void throw_class_format_error (char *msg);
/** check an utf8 entry, without creating a Utf8Const object */
bool is_attribute_name (int index, char *name);
* could be implemented in prims.cc (_Jv_makeUtf8Const), since it
* computes the hash value anyway.
*/
-
- static const int PUBLIC = 0x001;
- static const int PRIVATE = 0x002;
- static const int PROTECTED = 0x004;
- static const int STATIC = 0x008;
- static const int FINAL = 0x010;
- static const int SYNCHRONIZED = 0x020;
- static const int VOLATILE = 0x040;
- static const int TRANSIENT = 0x080;
- static const int NATIVE = 0x100;
- static const int INTERFACE = 0x200;
- static const int ABSTRACT = 0x400;
- static const int ALL_FLAGS = 0x7FF;
-
};
/* This is used for the isJavaIdentifierStart & isJavaIdentifierPart
void _Jv_ClassReader::read_constpool ()
{
- tags = (unsigned char*) _Jv_AllocBytesChecked (pool_count);
- offsets = (unsigned int *) _Jv_AllocBytesChecked (sizeof (int)
+ tags = (unsigned char*) _Jv_AllocBytes (pool_count);
+ offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int)
* pool_count) ;
/** first, we scan the constant pool, collecting tags and offsets */
/** now, we actually define the class' constant pool */
// the pool is scanned explicitly by the collector
- jbyte *pool_tags = (jbyte*) _Jv_AllocBytesChecked (pool_count);
+ jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
_Jv_word *pool_data
- = (_Jv_word*) _Jv_AllocBytesChecked (pool_count * sizeof (_Jv_word));
+ = (_Jv_word*) _Jv_AllocBytes (pool_count * sizeof (_Jv_word));
def->constants.tags = pool_tags;
def->constants.data = pool_data;
// order to accomondate gcj's internal representation.
int len = get2u (this_data);
- char *buffer = (char*) alloca (len);
+ char *buffer = (char*) __builtin_alloca (len);
char *s = ((char*) this_data)+2;
/* FIXME: avoid using a buffer here */
prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
if (verify)
- _Jv_VerifyClassName (pool_data[utf_index].utf8);
+ verify_classname (pool_data[utf_index].utf8);
pool_data[index].utf8 = pool_data[utf_index].utf8;
pool_tags[index] = JV_CONSTANT_Class;
|| _Jv_equalUtf8Consts (name, init_name)))
/* ignore */;
else
- _Jv_VerifyIdentifier (pool_data[name_index].utf8);
+ verify_identifier (pool_data[name_index].utf8);
}
_Jv_storeIndexes (&pool_data[index], class_index, nat_index);
_Jv_ClassReader::handleClassBegin
(int access_flags, int this_class, int super_class)
{
+ using namespace java::lang::reflect;
+
unsigned char *pool_tags = (unsigned char*) def->constants.tags;
_Jv_word *pool_data = def->constants.data;
if (super_class == 0)
{
// interfaces have java.lang.Object as super.
- if (access_flags & INTERFACE)
+ if (access_flags & Modifier::INTERFACE)
{
- def->superclass = (jclass)&ClassObject;
+ def->superclass = (jclass)&java::lang::Class::class$;
}
// FIXME: Consider this carefully!
- else if (!_Jv_equalUtf8Consts (def->name, ClassObject.name))
+ else if (!_Jv_equalUtf8Consts (def->name,
+ java::lang::Class::class$.name))
{
throw_no_class_def_found_error ("loading java.lang.Object");
}
void
_Jv_ClassReader::checkExtends (jclass sub, jclass super)
{
+ using namespace java::lang::reflect;
+
// having an interface or a final class as a superclass is no good
- if ((super->accflags & (INTERFACE | FINAL)) != 0)
+ if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
{
throw_incompatible_class_change_error (sub->getName ());
}
// if the super class is not public, we need to check some more
- if ((super->accflags & PUBLIC) == 0)
+ if ((super->accflags & Modifier::PUBLIC) == 0)
{
// With package scope, the classes must have the same
// class loader.
void _Jv_ClassReader::handleInterfacesBegin (int count)
{
- def->interfaces = (jclass*) _Jv_AllocBytesChecked (count*sizeof (jclass));
+ def->interfaces = (jclass*) _Jv_AllocBytes (count*sizeof (jclass));
def->interface_count = count;
}
void
_Jv_ClassReader::checkImplements (jclass sub, jclass super)
{
+ using namespace java::lang::reflect;
+
// well, it *must* be an interface
- if ((super->accflags & INTERFACE) == 0)
+ if ((super->accflags & Modifier::INTERFACE) == 0)
{
throw_incompatible_class_change_error (sub->getName ());
}
// if it has package scope, it must also be defined by the
// same loader.
- if ((super->accflags & PUBLIC) == 0)
+ if ((super->accflags & Modifier::PUBLIC) == 0)
{
if ( sub->loader != super->loader
|| !_Jv_ClassNameSamePackage (sub->name, super->name))
void _Jv_ClassReader::handleFieldsBegin (int count)
{
def->fields = (_Jv_Field*)
- _Jv_AllocBytesChecked (count * sizeof (_Jv_Field));
+ _Jv_AllocBytes (count * sizeof (_Jv_Field));
def->field_count = count;
def->field_initializers = (_Jv_ushort*)
- _Jv_AllocBytesChecked (count * sizeof (_Jv_ushort));
+ _Jv_AllocBytes (count * sizeof (_Jv_ushort));
for (int i = 0; i < count; i++)
def->field_initializers[i] = (_Jv_ushort) 0;
}
int name,
int desc)
{
+ using namespace java::lang::reflect;
+
_Jv_word *pool_data = def->constants.data;
_Jv_Field *field = &def->fields[field_no];
#endif
if (verify)
- _Jv_VerifyIdentifier (field_name);
+ verify_identifier (field_name);
// ignore flags we don't know about.
- field->flags = flags & ALL_FLAGS;
+ field->flags = flags & Modifier::ALL_FLAGS;
if (verify)
{
- if (field->flags & (SYNCHRONIZED|NATIVE|INTERFACE|ABSTRACT))
+ if (field->flags & (Modifier::SYNCHRONIZED
+ | Modifier::NATIVE
+ | Modifier::INTERFACE
+ | Modifier::ABSTRACT))
throw_class_format_error ("erroneous field access flags");
- if (1 < ( ((field->flags & PUBLIC) ? 1 : 0)
- +((field->flags & PRIVATE) ? 1 : 0)
- +((field->flags & PROTECTED) ? 1 : 0)))
+ if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
+ +((field->flags & Modifier::PRIVATE) ? 1 : 0)
+ +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
throw_class_format_error ("erroneous field access flags");
}
void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
int value)
{
+ using namespace java::lang::reflect;
+
_Jv_Field *field = &def->fields[field_index];
- if ((field->flags & (STATIC|FINAL|PRIVATE)) == 0)
+ if ((field->flags & (Modifier::STATIC
+ | Modifier::FINAL
+ | Modifier::PRIVATE)) == 0)
{
// Ignore, as per vmspec #4.7.2
return;
void _Jv_ClassReader::handleFieldsEnd ()
{
+ using namespace java::lang::reflect;
+
// We need to reorganize the fields so that the static ones are first,
// to conform to GCJ class layout.
while (low < high)
{
// go forward on low, while it's a static
- while (low < high && (fields[low].flags & STATIC) != 0)
+ while (low < high && (fields[low].flags & Modifier::STATIC) != 0)
low++;
// go backwards on high, while it's a non-static
- while (low < high && (fields[high].flags & STATIC) == 0)
+ while (low < high && (fields[high].flags & Modifier::STATIC) == 0)
high--;
if (low==high)
low += 1;
}
- if ((fields[low].flags & STATIC) != 0)
+ if ((fields[low].flags & Modifier::STATIC) != 0)
low += 1;
def->static_field_count = low;
-void _Jv_ClassReader::handleMethodsBegin (int count)
+void
+_Jv_ClassReader::handleMethodsBegin (int count)
{
def->methods = (_Jv_Method*)
- _Jv_AllocBytesChecked (sizeof (_Jv_Method)*count);
+ _Jv_AllocBytes (sizeof (_Jv_Method)*count);
- def->interpreted_methods = (_Jv_InterpMethod**)
- _Jv_AllocBytesChecked (sizeof (_Jv_InterpMethod*) * count);
+ def->interpreted_methods
+ = (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *)
+ * count);
for (int i = 0; i < count; i++)
def->interpreted_methods[i] = 0;
void _Jv_ClassReader::handleMethod
(int mth_index, int accflags, int name, int desc)
{
+ using namespace java::lang::reflect;
+
_Jv_word *pool_data = def->constants.data;
_Jv_Method *method = &def->methods[mth_index];
method->signature = pool_data[desc].utf8;
// ignore unknown flags
- method->accflags = accflags & ALL_FLAGS;
+ method->accflags = accflags & Modifier::ALL_FLAGS;
// intialize...
method->ncode = 0;
|| _Jv_equalUtf8Consts (method->name, init_name))
/* ignore */;
else
- _Jv_VerifyIdentifier (method->name);
+ verify_identifier (method->name);
_Jv_VerifyMethodSignature (method->signature);
- if (method->accflags & (VOLATILE|TRANSIENT|INTERFACE))
+ if (method->accflags & (Modifier::VOLATILE
+ | Modifier::TRANSIENT
+ | Modifier::INTERFACE))
throw_class_format_error ("erroneous method access flags");
- if (1 < ( ((method->accflags & PUBLIC) ? 1 : 0)
- +((method->accflags & PRIVATE) ? 1 : 0)
- +((method->accflags & PROTECTED) ? 1 : 0)))
+ if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
+ +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
+ +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
throw_class_format_error ("erroneous method access flags");
}
}
{
int size = _Jv_InterpMethod::size (exc_table_length, code_length);
_Jv_InterpMethod *method =
- (_Jv_InterpMethod*) (_Jv_AllocBytesChecked (size));
+ (_Jv_InterpMethod*) (_Jv_AllocBytes (size));
method->max_stack = max_stack;
method->max_locals = max_locals;
(int method_index, int exc_index,
int start_pc, int end_pc, int handler_pc, int catch_type)
{
- _Jv_InterpMethod *method = def->interpreted_methods[method_index];
+ _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
+ (def->interpreted_methods[method_index]);
_Jv_InterpException *exc = method->exceptions ();
exc[exc_index].start_pc = start_pc;
void _Jv_ClassReader::handleMethodsEnd ()
{
+ using namespace java::lang::reflect;
+
for (int i = 0; i < def->method_count; i++)
{
_Jv_Method *method = &def->methods[i];
- if (method->accflags & (NATIVE|ABSTRACT))
+ if ((method->accflags & Modifier::NATIVE) != 0)
+ {
+ if (def->interpreted_methods[i] != 0)
+ throw_class_format_error ("code provided for native method");
+ else
+ {
+ _Jv_JNIMethod *m = (_Jv_JNIMethod *)
+ _Jv_AllocBytes (sizeof (_Jv_JNIMethod));
+ m->defining_class = def;
+ m->self = method;
+ m->function = NULL;
+ def->interpreted_methods[i] = m;
+ }
+ }
+ else if ((method->accflags & Modifier::ABSTRACT) != 0)
{
if (def->interpreted_methods[i] != 0)
- throw_class_format_error ("code provided "
- "for abstract or native method");
+ throw_class_format_error ("code provided for abstract method");
}
else
{
if (def->interpreted_methods[i] == 0)
- throw_class_format_error ("abstract or native method "
- "with no code");
+ throw_class_format_error ("method with no code");
}
}
}
+void _Jv_ClassReader::throw_class_format_error (char *msg)
+{
+ jstring str;
+ if (def->name != NULL)
+ {
+ jsize mlen = strlen (msg);
+ unsigned char* data = (unsigned char*) def->name->data;
+ int ulen = def->name->length;
+ unsigned char* limit = data + ulen;
+ jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen);
+ jsize len = nlen + mlen + 3;
+ str = JvAllocString(len);
+ jchar *chrs = JvGetStringChars(str);
+ while (data < limit)
+ *chrs++ = UTF8_GET(data, limit);
+ *chrs++ = ' ';
+ *chrs++ = '(';
+ for (;;)
+ {
+ char c = *msg++;
+ if (c == 0)
+ break;
+ *chrs++ = c & 0xFFFF;
+ }
+ *chrs++ = ')';
+ }
+ else
+ str = JvNewStringLatin1 (msg);
+ ::throw_class_format_error (str);
+}
\f
/** This section takes care of verifying integrity of identifiers,
signatures, field ddescriptors, and class names */
return 0;
} while (ch != ';');
- _Jv_VerifyClassName (start, (unsigned short) (end-start));
+ if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
+ return 0;
}
break;
/** verification and loading procedures **/
-void
+bool
_Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
{
unsigned char* ptr = (unsigned char*) sig->data;
ptr = _Jv_VerifyOne (ptr, limit, false);
- if (ptr != limit)
- throw_class_format_error ("erroneous type descriptor");
+ return ptr == limit;
}
-void
+bool
_Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
{
unsigned char* ptr = (unsigned char*) sig->data;
unsigned char* limit = ptr + sig->length;
- if (ptr == limit)
- throw_class_format_error ("erroneous type descriptor");
-
- if (UTF8_GET(ptr,limit) != '(')
- throw_class_format_error ("erroneous type descriptor");
+ if (ptr == limit || UTF8_GET(ptr,limit) != '(')
+ return false;
while (ptr && UTF8_PEEK (ptr, limit) != ')')
ptr = _Jv_VerifyOne (ptr, limit, false);
if (UTF8_GET (ptr, limit) != ')')
- throw_class_format_error ("erroneous type descriptor");
+ return false;
// get the return type
ptr = _Jv_VerifyOne (ptr, limit, true);
- if (ptr != limit)
- throw_class_format_error ("erroneous type descriptor");
-
- return;
-
+ return ptr == limit;
}
/* we try to avoid calling the Character methods all the time,
return character->isJavaIdentifierStart ((jchar) ch);
}
-void
+bool
_Jv_VerifyIdentifier (_Jv_Utf8Const* name)
{
unsigned char *ptr = (unsigned char*) name->data;
if ((ch = UTF8_GET (ptr, limit))==-1
|| ! is_identifier_start (ch))
- throw_class_format_error ("erroneous identifier");
+ return false;
while (ptr != limit)
{
if ((ch = UTF8_GET (ptr, limit))==-1
|| ! is_identifier_part (ch))
- throw_class_format_error ("erroneous identifier");
+ return false;
}
+ return true;
}
-
-void
+bool
_Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
{
unsigned char *limit = ptr+length;
if ('[' == UTF8_PEEK (ptr, limit))
{
if (! _Jv_VerifyOne (++ptr, limit, false))
- throw_class_format_error ("erroneous class name");
+ return false;
else
- return;
+ return true;
}
next_level:
- do {
+ for (;;) {
if ((ch = UTF8_GET (ptr, limit))==-1)
- throw_class_format_error ("erroneous class name");
+ return false;
if (! is_identifier_start (ch))
- throw_class_format_error ("erroneous class name");
- do {
+ return false;
+ for (;;) {
if (ptr == limit)
- return;
+ return true;
else if ((ch = UTF8_GET (ptr, limit))==-1)
- throw_class_format_error ("erroneous class name");
+ return false;
else if (ch == '.')
goto next_level;
else if (! is_identifier_part (ch))
- throw_class_format_error ("erroneous class name");
- } while (true);
- } while (true);
-
+ return false;
+ }
+ }
}
-void
+bool
_Jv_VerifyClassName (_Jv_Utf8Const *name)
{
- _Jv_VerifyClassName ((unsigned char*)&name->data[0],
- (_Jv_ushort) name->length);
+ return _Jv_VerifyClassName ((unsigned char*)&name->data[0],
+ (_Jv_ushort) name->length);
}
-
/** returns true, if name1 and name2 represents classes in the same
package. */
static void
throw_no_class_def_found_error (jstring msg)
{
- if (msg == 0)
- JvThrow (new java::lang::NoClassDefFoundError);
- else
- JvThrow (new java::lang::NoClassDefFoundError (msg));
+ throw (msg
+ ? new java::lang::NoClassDefFoundError (msg)
+ : new java::lang::NoClassDefFoundError);
}
static void
static void
throw_class_format_error (jstring msg)
{
- if (msg == 0)
- JvThrow (new java::lang::ClassFormatError);
- else
- JvThrow (new java::lang::ClassFormatError (msg));
-}
-
-static void
-throw_class_format_error (char *msg)
-{
- throw_class_format_error (JvNewStringLatin1 (msg));
+ throw (msg
+ ? new java::lang::ClassFormatError (msg)
+ : new java::lang::ClassFormatError);
}
static void
throw_internal_error (char *msg)
{
- JvThrow
- (new java::lang::InternalError (JvNewStringLatin1 (msg)));
+ throw new java::lang::InternalError (JvNewStringLatin1 (msg));
}
static jfloat int_bits_to_float (jint value)
static void throw_incompatible_class_change_error (jstring msg)
{
- JvThrow (new java::lang::IncompatibleClassChangeError (msg));
+ throw new java::lang::IncompatibleClassChangeError (msg);
}
static void throw_class_circularity_error (jstring msg)
{
- JvThrow (new java::lang::ClassCircularityError (msg));
+ throw new java::lang::ClassCircularityError (msg);
}
#endif /* INTERPRETER */