From abd9c2bc8d86ecf48d9b32c0be636aba4333cc25 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 28 Jul 2007 10:55:24 -0600 Subject: [PATCH] fix primitive class resolution to avoid mistaking normal classes with names like 'B' for primitive classes --- classpath/java/lang/Boolean.java | 2 +- classpath/java/lang/Byte.java | 2 +- classpath/java/lang/Character.java | 2 +- classpath/java/lang/Class.java | 23 +++++++++++++- classpath/java/lang/ClassLoader.java | 2 +- classpath/java/lang/Double.java | 2 +- classpath/java/lang/Float.java | 2 +- classpath/java/lang/Integer.java | 2 +- classpath/java/lang/Long.java | 2 +- classpath/java/lang/Short.java | 2 +- classpath/java/lang/Thread.java | 3 +- classpath/java/lang/Void.java | 2 +- classpath/java/lang/reflect/Method.java | 42 ++++++++++++++----------- src/builtin.cpp | 26 +++++++++++++++ src/machine.cpp | 2 +- src/types.def | 21 +++++++------ 16 files changed, 95 insertions(+), 42 deletions(-) diff --git a/classpath/java/lang/Boolean.java b/classpath/java/lang/Boolean.java index 42c5d513b6..6990400035 100644 --- a/classpath/java/lang/Boolean.java +++ b/classpath/java/lang/Boolean.java @@ -1,7 +1,7 @@ package java.lang; public final class Boolean { - public static final Class TYPE = Class.forName("Z"); + public static final Class TYPE = Class.forCanonicalName("Z"); private final boolean value; diff --git a/classpath/java/lang/Byte.java b/classpath/java/lang/Byte.java index 61a1f1d30a..e07c3b357a 100644 --- a/classpath/java/lang/Byte.java +++ b/classpath/java/lang/Byte.java @@ -1,7 +1,7 @@ package java.lang; public final class Byte extends Number { - public static final Class TYPE = Class.forName("B"); + public static final Class TYPE = Class.forCanonicalName("B"); private final byte value; diff --git a/classpath/java/lang/Character.java b/classpath/java/lang/Character.java index 7f8614887a..3eddc38dbc 100644 --- a/classpath/java/lang/Character.java +++ b/classpath/java/lang/Character.java @@ -1,7 +1,7 @@ package java.lang; public final class Character { - public static final Class TYPE = Class.forName("C"); + public static final Class TYPE = Class.forCanonicalName("C"); private final char value; diff --git a/classpath/java/lang/Class.java b/classpath/java/lang/Class.java index 88cc506ea7..e3b6a25359 100644 --- a/classpath/java/lang/Class.java +++ b/classpath/java/lang/Class.java @@ -26,7 +26,28 @@ public final class Class { return new String(name, 0, name.length - 1, false); } - public static native Class forName(String name); + public static native Class forName(String name) + throws ClassNotFoundException; + + private static native Class primitiveClass(char name); + + static Class forCanonicalName(String name) { + try { + if (name.startsWith("[")) { + return forName(name); + } else if (name.startsWith("L")) { + return forName(name.substring(1, name.length() - 1)); + } else { + if (name.length() == 0) { + return primitiveClass(name.charAt(0)); + } else { + throw new ClassNotFoundException(name); + } + } + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } public native boolean isAssignableFrom(Class c); diff --git a/classpath/java/lang/ClassLoader.java b/classpath/java/lang/ClassLoader.java index f66dd46814..08ef5916bd 100644 --- a/classpath/java/lang/ClassLoader.java +++ b/classpath/java/lang/ClassLoader.java @@ -9,7 +9,7 @@ public class ClassLoader { return instance; } - public Class loadClass(String name) { + public Class loadClass(String name) throws ClassNotFoundException { return Class.forName(name); } } diff --git a/classpath/java/lang/Double.java b/classpath/java/lang/Double.java index 0074ac7ea9..0d193aeb25 100644 --- a/classpath/java/lang/Double.java +++ b/classpath/java/lang/Double.java @@ -1,7 +1,7 @@ package java.lang; public final class Double { - public static final Class TYPE = Class.forName("D"); + public static final Class TYPE = Class.forCanonicalName("D"); private final double value; diff --git a/classpath/java/lang/Float.java b/classpath/java/lang/Float.java index 265363ae64..27e08564af 100644 --- a/classpath/java/lang/Float.java +++ b/classpath/java/lang/Float.java @@ -1,7 +1,7 @@ package java.lang; public final class Float extends Number { - public static final Class TYPE = Class.forName("F"); + public static final Class TYPE = Class.forCanonicalName("F"); private final float value; diff --git a/classpath/java/lang/Integer.java b/classpath/java/lang/Integer.java index 4363ff41ec..59917a6b29 100644 --- a/classpath/java/lang/Integer.java +++ b/classpath/java/lang/Integer.java @@ -1,7 +1,7 @@ package java.lang; public final class Integer extends Number { - public static final Class TYPE = Class.forName("I"); + public static final Class TYPE = Class.forCanonicalName("I"); private final int value; diff --git a/classpath/java/lang/Long.java b/classpath/java/lang/Long.java index 9b68062330..c348073f39 100644 --- a/classpath/java/lang/Long.java +++ b/classpath/java/lang/Long.java @@ -1,7 +1,7 @@ package java.lang; public final class Long extends Number { - public static final Class TYPE = Class.forName("J"); + public static final Class TYPE = Class.forCanonicalName("J"); private final long value; diff --git a/classpath/java/lang/Short.java b/classpath/java/lang/Short.java index b4cdd36b64..398c79545f 100644 --- a/classpath/java/lang/Short.java +++ b/classpath/java/lang/Short.java @@ -1,7 +1,7 @@ package java.lang; public final class Short extends Number { - public static final Class TYPE = Class.forName("S"); + public static final Class TYPE = Class.forCanonicalName("S"); private final short value; diff --git a/classpath/java/lang/Thread.java b/classpath/java/lang/Thread.java index a19cb51aba..6110b4c91d 100644 --- a/classpath/java/lang/Thread.java +++ b/classpath/java/lang/Thread.java @@ -4,10 +4,11 @@ import java.util.Map; import java.util.WeakHashMap; public class Thread implements Runnable { + private long peer; private final Runnable task; private Map locals; private Object sleepLock; - private long peer; + private boolean interrupted; public Thread(Runnable task) { this.task = task; diff --git a/classpath/java/lang/Void.java b/classpath/java/lang/Void.java index 777241d48b..51744b3c4d 100644 --- a/classpath/java/lang/Void.java +++ b/classpath/java/lang/Void.java @@ -1,7 +1,7 @@ package java.lang; public final class Void { - public static final Class TYPE = Class.forName("V"); + public static final Class TYPE = Class.forCanonicalName("V"); private Void() { } } diff --git a/classpath/java/lang/reflect/Method.java b/classpath/java/lang/reflect/Method.java index 84831c01b6..84777a0da1 100644 --- a/classpath/java/lang/reflect/Method.java +++ b/classpath/java/lang/reflect/Method.java @@ -51,31 +51,35 @@ public class Method extends AccessibleObject implements Member { String spec = new String(this.spec, 1, this.spec.length - 1, false); - for (int i = 0; i < spec.length(); ++i) { - char c = spec.charAt(i); - if (c == ')') { - break; - } else if (c == 'L') { - int start = i + 1; - i = next(';', spec, start); - String name = spec.substring(start, i); - types[index++] = Class.forName(name); - } else if (c == '[') { - int start = i; - while (spec.charAt(i) == '[') ++i; - - if (spec.charAt(i) == 'L') { - i = next(';', spec, i + 1); + try { + for (int i = 0; i < spec.length(); ++i) { + char c = spec.charAt(i); + if (c == ')') { + break; + } else if (c == 'L') { + int start = i + 1; + i = next(';', spec, start); String name = spec.substring(start, i); types[index++] = Class.forName(name); + } else if (c == '[') { + int start = i; + while (spec.charAt(i) == '[') ++i; + + if (spec.charAt(i) == 'L') { + i = next(';', spec, i + 1); + String name = spec.substring(start, i); + types[index++] = Class.forName(name); + } else { + String name = spec.substring(start, i + 1); + types[index++] = Class.forName(name); + } } else { - String name = spec.substring(start, i + 1); + String name = spec.substring(i, i + 1); types[index++] = Class.forName(name); } - } else { - String name = spec.substring(i, i + 1); - types[index++] = Class.forName(name); } + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); } return types; diff --git a/src/builtin.cpp b/src/builtin.cpp index 87a757c747..31f7b4e417 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -87,6 +87,32 @@ Class_forName(Thread* t, jclass, jstring name) } } +jclass +Class_primitiveClass(Thread* t, jclass, jchar name) +{ + switch (name) { + case 'B': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JbyteType)); + case 'C': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JcharType)); + case 'D': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JdoubleType)); + case 'F': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JfloatType)); + case 'I': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JintType)); + case 'L': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JlongType)); + case 'S': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JshortType)); + case 'V': + return pushReference(t, arrayBody(t, t->vm->types, Machine::JvoidType)); + default: + t->exception = makeIllegalArgumentException(t); + return 0; + } +} + jboolean Class_isAssignableFrom(Thread* t, jobject this_, jclass that) { diff --git a/src/machine.cpp b/src/machine.cpp index 573e692ea3..c4c061bf0d 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -1375,7 +1375,7 @@ Thread::Thread(Machine* m, object javaThread, Thread* parent): populateBuiltinMap(t, m->builtinMap); - t->javaThread = makeThread(t, 0, 0, 0, reinterpret_cast(t)); + t->javaThread = makeThread(t, reinterpret_cast(t), 0, 0, 0, 0); } else { threadPeer(this, javaThread) = reinterpret_cast(this); parent->child = this; diff --git a/src/types.def b/src/types.def index 27aed3796a..f5ff26d9f0 100644 --- a/src/types.def +++ b/src/types.def @@ -125,10 +125,11 @@ (type thread java/lang/Thread (extends jobject) + (int64_t peer) (object task) (object locals) (object sleepLock) - (int64_t peer)) + (uint8_t interrupted)) (type stackTraceElement java/lang/StackTraceElement (extends jobject) @@ -280,29 +281,29 @@ (extends jobject) (array uint64_t body)) -(type jbyte B +(type jbyte (extends jobject)) -(type jboolean Z +(type jboolean (extends jobject)) -(type jshort S +(type jshort (extends jobject)) -(type jchar C +(type jchar (extends jobject)) -(type jint I +(type jint (extends jobject)) -(type jlong L +(type jlong (extends jobject)) -(type jfloat F +(type jfloat (extends jobject)) -(type jdouble D +(type jdouble (extends jobject)) -(type jvoid V +(type jvoid (extends jobject))