classpath progress

This commit is contained in:
Joel Dice 2007-07-26 20:39:53 -06:00
parent 7212ba1c30
commit c9f9b039e6
23 changed files with 411 additions and 10 deletions

View File

@ -29,6 +29,16 @@ public class File {
return path;
}
private static native String toCanonicalPath(String path);
public String getCanonicalPath() {
return toCanonicalPath(path);
}
public File getCanonicalFile() {
return new File(getCanonicalPath());
}
private static native String toAbsolutePath(String path);
public String getAbsolutePath() {

View File

@ -1,7 +1,7 @@
package java.io;
public class FileInputStream extends InputStream {
private final int fd;
private int fd;
public FileInputStream(FileDescriptor fd) {
this.fd = fd.value;
@ -42,5 +42,6 @@ public class FileInputStream extends InputStream {
public void close() throws IOException {
close(fd);
fd = -1;
}
}

View File

@ -0,0 +1,11 @@
package java.io;
public class FileNotFoundException extends IOException {
public FileNotFoundException(String message) {
super(message);
}
public FileNotFoundException() {
this(null);
}
}

View File

@ -1,7 +1,7 @@
package java.io;
public class FileOutputStream extends OutputStream {
private final int fd;
private int fd;
public FileOutputStream(FileDescriptor fd) {
this.fd = fd.value;
@ -42,5 +42,6 @@ public class FileOutputStream extends OutputStream {
public void close() throws IOException {
close(fd);
fd = -1;
}
}

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Boolean {
public static final Class TYPE = Class.forName("Z");
private final boolean value;
public Boolean(boolean value) {

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Byte extends Number {
public static final Class TYPE = Class.forName("B");
private final byte value;
public Byte(byte value) {

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Character {
public static final Class TYPE = Class.forName("C");
private final char value;
public Character(char value) {
@ -26,4 +28,24 @@ public final class Character {
return c;
}
}
public static boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
public static boolean isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
public static boolean isLowerCase(char c) {
return (c >= 'a' && c <= 'z');
}
public static boolean isUpperCase(char c) {
return (c >= 'A' && c <= 'Z');
}
public static boolean isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\n';
}
}

View File

@ -1,5 +1,6 @@
package java.lang;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
@ -29,13 +30,31 @@ public final class Class <T> {
public native boolean isAssignableFrom(Class c);
public Field getDeclaredField(String name) throws NoSuchFieldException {
private Field findField(String name) {
for (int i = 0; i < fieldTable.length; ++i) {
if (fieldTable[i].getName().equals(name)) {
return fieldTable[i];
}
}
return null;
}
public Field getDeclaredField(String name) throws NoSuchFieldException {
Field f = findField(name);
if (f == null) {
throw new NoSuchFieldException(name);
} else {
return f;
}
}
public Field getField(String name) throws NoSuchFieldException {
for (Class c = this; c != null; c = c.super_) {
Field f = c.findField(name);
if (f != null) {
return f;
}
}
throw new NoSuchFieldException(name);
}
@ -52,9 +71,7 @@ public final class Class <T> {
}
}
public Method getDeclaredMethod(String name, Class ... parameterTypes)
throws NoSuchMethodException
{
private Method findMethod(String name, Class[] parameterTypes) {
for (int i = 0; i < methodTable.length; ++i) {
if (methodTable[i].getName().equals(name)
&& match(parameterTypes, methodTable[i].getParameterTypes()))
@ -62,7 +79,123 @@ public final class Class <T> {
return methodTable[i];
}
}
return null;
}
public Method getDeclaredMethod(String name, Class ... parameterTypes)
throws NoSuchMethodException
{
Method f = findMethod(name, parameterTypes);
if (f == null) {
throw new NoSuchMethodException(name);
} else {
return f;
}
}
public Method getMethod(String name, Class ... parameterTypes)
throws NoSuchMethodException
{
for (Class c = this; c != null; c = c.super_) {
Method f = c.findMethod(name, parameterTypes);
if (f != null) {
return f;
}
}
throw new NoSuchMethodException(name);
}
public Constructor getConstructor(Class ... parameterTypes)
throws NoSuchMethodException
{
return new Constructor(getDeclaredMethod("<init>", parameterTypes));
}
public Constructor[] getConstructors() {
int count = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (methodTable[i].getName().equals("<init>")) {
++ count;
}
}
Constructor[] array = new Constructor[count];
int index = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (methodTable[i].getName().equals("<init>")) {
array[index++] = new Constructor(methodTable[i]);
}
}
return array;
}
public Constructor[] getDeclaredConstructors() {
return getConstructors();
}
public Field[] getDeclaredFields() {
Field[] array = new Field[fieldTable.length];
System.arraycopy(fieldTable, 0, array, 0, fieldTable.length);
return array;
}
public Method[] getDeclaredMethods() {
int count = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (! methodTable[i].getName().equals("<init>")) {
++ count;
}
}
Method[] array = new Method[count];
int index = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (! methodTable[i].getName().equals("<init>")) {
array[index++] = methodTable[i];
}
}
return array;
}
public Class[] getInterfaces() {
Class[] array = new Class[interfaceTable.length / 2];
for (int i = 0; i < array.length; ++i) {
array[i] = (Class) interfaceTable[i * 2];
}
return array;
}
public ClassLoader getClassLoader() {
return ClassLoader.getSystemClassLoader();
}
public int getModifiers() {
return flags;
}
public Class getSuperclass() {
return super_;
}
public boolean isArray() {
return arrayElementSize != 0;
}
public boolean isInstance(Object o) {
return isAssignableFrom(o.getClass());
}
public boolean isPrimitive() {
return equals(boolean.class)
|| equals(byte.class)
|| equals(char.class)
|| equals(short.class)
|| equals(int.class)
|| equals(long.class)
|| equals(float.class)
|| equals(double.class)
|| equals(void.class);
}
}

View File

@ -1,5 +1,11 @@
package java.lang;
public abstract class ClassLoader {
public class ClassLoader {
private static final ClassLoader instance = new ClassLoader();
private ClassLoader() { }
public static ClassLoader getSystemClassLoader() {
return instance;
}
}

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Double {
public static final Class TYPE = Class.forName("D");
private final double value;
public Double(double value) {

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Float extends Number {
public static final Class TYPE = Class.forName("F");
private final float value;
public Float(float value) {

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Integer extends Number {
public static final Class TYPE = Class.forName("I");
private final int value;
public Integer(int value) {

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Long extends Number {
public static final Class TYPE = Class.forName("J");
private final long value;
public Long(long value) {

View File

@ -0,0 +1,11 @@
package java.lang;
public class NegativeArraySizeException extends RuntimeException {
public NegativeArraySizeException(String message) {
super(message);
}
public NegativeArraySizeException() {
super();
}
}

View File

@ -14,4 +14,6 @@ public class Runtime {
public native void gc();
public native void exit(int code);
public native long freeMemory();
}

View File

@ -1,6 +1,8 @@
package java.lang;
public final class Short extends Number {
public static final Class TYPE = Class.forName("S");
private final short value;
public Short(short value) {

View File

@ -27,6 +27,10 @@ public class Throwable {
return cause;
}
public String getMessage() {
return message;
}
private static native Object trace(int skipCount);
private static native StackTraceElement[] resolveTrace(Object trace);

View File

@ -1,5 +1,7 @@
package java.lang;
public final class Void {
public static final Class TYPE = Class.forName("V");
private Void() { }
}

View File

@ -3,5 +3,35 @@ package java.lang.reflect;
public final class Array {
private Array() { }
public static native Object get(Object array, int index);
public static native int getLength(Object array);
private static native Object makeObjectArray(Class elementType, int length);
public static Object newInstance(Class elementType, int length) {
if (length < 0) {
throw new NegativeArraySizeException();
}
if (elementType.equals(boolean.class)) {
return new boolean[length];
} else if (elementType.equals(byte.class)) {
return new byte[length];
} else if (elementType.equals(char.class)) {
return new char[length];
} else if (elementType.equals(short.class)) {
return new short[length];
} else if (elementType.equals(int.class)) {
return new int[length];
} else if (elementType.equals(long.class)) {
return new long[length];
} else if (elementType.equals(float.class)) {
return new float[length];
} else if (elementType.equals(double.class)) {
return new double[length];
} else {
return makeObjectArray(elementType, length);
}
}
}

View File

@ -3,7 +3,9 @@ package java.lang.reflect;
public class Constructor<T> extends AccessibleObject implements Member {
private Method<T> method;
private Constructor() { }
public Constructor(Method<T> method) {
this.method = method;
}
public boolean equals(Object o) {
return o instanceof Constructor
@ -22,6 +24,10 @@ public class Constructor<T> extends AccessibleObject implements Member {
return method.getDeclaringClass();
}
public Class[] getParameterTypes() {
return method.getParameterTypes();
}
public int getModifiers() {
return method.getModifiers();
}
@ -29,4 +35,12 @@ public class Constructor<T> extends AccessibleObject implements Member {
public String getName() {
return method.getName();
}
private static native <T> T make(Class<T> c);
public T newInstance(Object ... arguments) {
T v = make(method.getDeclaringClass());
method.invoke(v, arguments);
return v;
}
}

View File

@ -16,4 +16,11 @@ public final class Modifier {
public static final int STRICT = 1 << 11;
private Modifier() { }
public static boolean isPublic (int v) { return (v & PUBLIC) != 0; }
public static boolean isPrivate (int v) { return (v & PRIVATE) != 0; }
public static boolean isProtected(int v) { return (v & PROTECTED) != 0; }
public static boolean isStatic (int v) { return (v & STATIC) != 0; }
public static boolean isFinal (int v) { return (v & FINAL) != 0; }
public static boolean isSuper (int v) { return (v & SUPER) != 0; }
}

View File

@ -92,12 +92,50 @@ public class LinkedList<T> implements List<T> {
return find(index).value;
}
public T getFirst() {
if (front != null) {
return front.value;
} else {
throw new NoSuchElementException();
}
}
public T getLast() {
if (rear != null) {
return rear.value;
} else {
throw new NoSuchElementException();
}
}
public T remove(int index) {
Cell<T> c = find(index);
remove(c);
return c.value;
}
public T removeFirst() {
if (front != null) {
T v = front.value;
front = front.next;
if (front != null) front.prev = null;
return v;
} else {
throw new NoSuchElementException();
}
}
public T removeLast() {
if (rear != null) {
T v = rear.value;
rear = rear.prev;
if (rear != null) rear.next = null;
return v;
} else {
throw new NoSuchElementException();
}
}
public boolean remove(T element) {
Cell<T> c = find(element);
if (c == null) {

View File

@ -162,6 +162,12 @@ Field_get(Thread* t, jobject this_, jobject instancep)
}
}
jobject
Constructor_make(Thread* t, jclass, jclass c)
{
return pushReference(t, make(t, c));
}
jobject
Method_invoke(Thread* t, jobject this_, jobject instancep,
jobjectArray argumentsp)
@ -201,6 +207,78 @@ Method_invoke(Thread* t, jobject this_, jobject instancep,
return 0;
}
jobject
Array_get(Thread* t, jobject array, int index)
{
if (LIKELY(array)) {
object a = *array;
unsigned elementSize = classArrayElementSize(t, objectClass(t, a));
if (LIKELY(elementSize)) {
intptr_t length = cast<uintptr_t>(a, BytesPerWord);
if (LIKELY(index >= 0 and index < length)) {
switch (byteArrayBody(t, className(t, objectClass(t, a)), 1)) {
case 'B':
return pushReference(t, makeByte(t, byteArrayBody(t, a, index)));
case 'C':
return pushReference(t, makeChar(t, charArrayBody(t, a, index)));
case 'D':
return pushReference(t, makeDouble(t, doubleArrayBody(t, a, index)));
case 'F':
return pushReference(t, makeFloat(t, floatArrayBody(t, a, index)));
case 'I':
return pushReference(t, makeInt(t, intArrayBody(t, a, index)));
case 'J':
return pushReference(t, makeLong(t, longArrayBody(t, a, index)));
case 'S':
return pushReference(t, makeShort(t, shortArrayBody(t, a, index)));
case 'Z':
return pushReference
(t, makeBoolean(t, booleanArrayBody(t, a, index)));
case 'L':
case '[':
return pushReference(t, objectArrayBody(t, a, index));
default: abort(t);
}
} else {
t->exception = makeArrayIndexOutOfBoundsException(t, 0);
}
} else {
t->exception = makeIllegalArgumentException(t);
}
} else {
t->exception = makeNullPointerException(t);
}
return 0;
}
jint
Array_getLength(Thread* t, jobject array)
{
if (LIKELY(array)) {
object a = *array;
unsigned elementSize = classArrayElementSize(t, objectClass(t, a));
if (LIKELY(elementSize)) {
return cast<uintptr_t>(a, BytesPerWord);
} else {
t->exception = makeIllegalArgumentException(t);
}
} else {
t->exception = makeNullPointerException(t);
}
return 0;
}
jobject
Array_makeObjectArray(Thread* t, jclass, jclass elementType, jint length)
{
return pushReference(t, makeObjectArray(t, *elementType, length, true));
}
void
System_arraycopy(Thread* t, jclass, jobject src, jint srcOffset, jobject dst,
jint dstOffset, jint length)
@ -289,6 +367,13 @@ Runtime_exit(Thread* t, jobject, jint code)
t->vm->system->exit(code);
}
jlong
Runtime_freeMemory(Thread*, jobject)
{
// todo
return 0;
}
jobject
Throwable_trace(Thread* t, jclass, jint skipCount)
{
@ -458,6 +543,16 @@ populateBuiltinMap(Thread* t, object map)
{ "Java_java_lang_Object_wait",
reinterpret_cast<void*>(::Object_wait) },
{ "Java_java_lang_reflect_Array_get",
reinterpret_cast<void*>(::Array_get) },
{ "Java_java_lang_reflect_Array_getLength",
reinterpret_cast<void*>(::Array_getLength) },
{ "Java_java_lang_reflect_Array_makeObjectArray",
reinterpret_cast<void*>(::Array_makeObjectArray) },
{ "Java_java_lang_reflect_Constructor_make",
reinterpret_cast<void*>(::Constructor_make) },
{ "Java_java_lang_reflect_Field_get",
reinterpret_cast<void*>(::Field_get) },