classpath progress

This commit is contained in:
Joel Dice
2007-07-27 17:56:19 -06:00
parent c9f9b039e6
commit 363801af1c
10 changed files with 211 additions and 24 deletions

View File

@ -45,7 +45,7 @@ public final class Character {
return (c >= 'A' && c <= 'Z');
}
public static boolean isWhiteSpace(char c) {
public static boolean isWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n';
}
}

View File

@ -3,6 +3,7 @@ package java.lang;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public final class Class <T> {
private short flags;
@ -85,21 +86,27 @@ public final class Class <T> {
public Method getDeclaredMethod(String name, Class ... parameterTypes)
throws NoSuchMethodException
{
Method f = findMethod(name, parameterTypes);
if (f == null) {
if (name.startsWith("<")) {
throw new NoSuchMethodException(name);
}
Method m = findMethod(name, parameterTypes);
if (m == null) {
throw new NoSuchMethodException(name);
} else {
return f;
return m;
}
}
public Method getMethod(String name, Class ... parameterTypes)
throws NoSuchMethodException
{
if (name.startsWith("<")) {
throw new NoSuchMethodException(name);
}
for (Class c = this; c != null; c = c.super_) {
Method f = c.findMethod(name, parameterTypes);
if (f != null) {
return f;
Method m = c.findMethod(name, parameterTypes);
if (m != null) {
return m;
}
}
throw new NoSuchMethodException(name);
@ -108,18 +115,29 @@ public final class Class <T> {
public Constructor getConstructor(Class ... parameterTypes)
throws NoSuchMethodException
{
return new Constructor(getDeclaredMethod("<init>", parameterTypes));
Method m = findMethod("<init>", parameterTypes);
if (m == null) {
throw new NoSuchMethodException();
} else {
return new Constructor(m);
}
}
public Constructor[] getConstructors() {
private int countConstructors(boolean publicOnly) {
int count = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (methodTable[i].getName().equals("<init>")) {
if (((! publicOnly)
|| ((methodTable[i].getModifiers() & Modifier.PUBLIC)) != 0)
&& methodTable[i].getName().equals("<init>"))
{
++ count;
}
}
return count;
}
Constructor[] array = new Constructor[count];
public Constructor[] getDeclaredConstructors() {
Constructor[] array = new Constructor[countConstructors(false)];
int index = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (methodTable[i].getName().equals("<init>")) {
@ -130,8 +148,18 @@ public final class Class <T> {
return array;
}
public Constructor[] getDeclaredConstructors() {
return getConstructors();
public Constructor[] getConstructors() {
Constructor[] array = new Constructor[countConstructors(true)];
int index = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (((methodTable[i].getModifiers() & Modifier.PUBLIC) != 0)
&& methodTable[i].getName().equals("<init>"))
{
array[index++] = new Constructor(methodTable[i]);
}
}
return array;
}
public Field[] getDeclaredFields() {
@ -140,18 +168,58 @@ public final class Class <T> {
return array;
}
public Method[] getDeclaredMethods() {
private int countPublicFields() {
int count = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (! methodTable[i].getName().equals("<init>")) {
for (int i = 0; i < fieldTable.length; ++i) {
if (((fieldTable[i].getModifiers() & Modifier.PUBLIC)) != 0) {
++ count;
}
}
return count;
}
Method[] array = new Method[count];
public Field[] getFields() {
Field[] array = new Field[countPublicFields()];
for (int i = 0; i < fieldTable.length; ++i) {
if (((fieldTable[i].getModifiers() & Modifier.PUBLIC)) != 0) {
array[i] = fieldTable[i];
}
}
return array;
}
private int countMethods(boolean publicOnly) {
int count = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (((! publicOnly)
|| ((methodTable[i].getModifiers() & Modifier.PUBLIC)) != 0)
&& (! methodTable[i].getName().startsWith("<")))
{
++ count;
}
}
return count;
}
public Method[] getDeclaredMethods() {
Method[] array = new Method[countMethods(false)];
int index = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (! methodTable[i].getName().equals("<init>")) {
if (! methodTable[i].getName().startsWith("<")) {
array[index++] = methodTable[i];
}
}
return array;
}
public Method[] getMethods() {
Method[] array = new Method[countMethods(true)];
int index = 0;
for (int i = 0; i < methodTable.length; ++i) {
if (((methodTable[i].getModifiers() & Modifier.PUBLIC) != 0)
&& (! methodTable[i].getName().startsWith("<")))
{
array[index++] = methodTable[i];
}
}

View File

@ -7,5 +7,9 @@ public class ClassLoader {
public static ClassLoader getSystemClassLoader() {
return instance;
}
}
public Class loadClass(String name) {
return Class.forName(name);
}
}

View File

@ -32,4 +32,8 @@ public final class Integer extends Number {
public double doubleValue() {
return (double) value;
}
public static int parseInt(String s, int radix) {
return (int) Long.parseLong(s, radix);
}
}

View File

@ -32,4 +32,28 @@ public final class Long extends Number {
public double doubleValue() {
return (double) value;
}
private static long pow(long a, long b) {
long c = 1;
for (int i = 0; i < b; ++i) c *= a;
return c;
}
public static long parseLong(String s, int radix) {
long number = 0;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (((c >= '0') && (c <= '9')) ||
((c >= 'a') && (c <= 'z'))) {
long digit = ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10));
number += digit * pow(radix, (s.length() - i - 1));
} else {
throw new NumberFormatException("Invalid character " + c + " code " +
(int) c);
}
}
return number;
}
}

View File

@ -144,7 +144,7 @@ public final class String implements Comparable<String> {
public boolean startsWith(String s) {
if (length >= s.length) {
return substring(0, s.length).compareTo(s) != 0;
return substring(0, s.length).compareTo(s) == 0;
} else {
return false;
}
@ -152,7 +152,7 @@ public final class String implements Comparable<String> {
public boolean endsWith(String s) {
if (length >= s.length) {
return substring(length - s.length).compareTo(s) != 0;
return substring(length - s.length).compareTo(s) == 0;
} else {
return false;
}

View File

@ -5,6 +5,8 @@ public final class Array {
public static native Object get(Object array, int index);
public static native void set(Object array, int index, Object value);
public static native int getLength(Object array);
private static native Object makeObjectArray(Class elementType, int length);

View File

@ -32,4 +32,6 @@ public class Field<T> extends AccessibleObject {
}
public native Object get(Object instance);
public native void set(Object instance, Object value);
}