From 0615b8a09fbfad99bcc1ed1a4579d8aaff2d8abf Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 2 Jun 2009 17:14:38 -0600 Subject: [PATCH] add classes and methods needed for GNU Classpath compatibility Most of these methods are stubs which throw UnsupportedOperationExceptions for now. --- classpath/java/lang/Appendable.java | 22 ++++ classpath/java/lang/Character.java | 36 ++++++ classpath/java/lang/Class.java | 62 +++++++++- classpath/java/lang/Enum.java | 8 ++ classpath/java/lang/Object.java | 13 +- classpath/java/lang/StackTraceElement.java | 4 +- classpath/java/lang/String.java | 116 ++++++++++++++++-- classpath/java/lang/StringBuilder.java | 18 ++- classpath/java/lang/Throwable.java | 8 +- classpath/java/lang/ref/Reference.java | 4 + classpath/java/lang/reflect/Constructor.java | 16 ++- classpath/java/lang/reflect/Field.java | 36 ++++++ .../java/lang/reflect/GenericDeclaration.java | 15 +++ classpath/java/lang/reflect/Member.java | 2 + classpath/java/lang/reflect/Method.java | 28 ++++- classpath/java/lang/reflect/Type.java | 13 ++ classpath/java/lang/reflect/TypeVariable.java | 19 +++ classpath/java/security/ProtectionDomain.java | 13 ++ 18 files changed, 416 insertions(+), 17 deletions(-) create mode 100644 classpath/java/lang/Appendable.java create mode 100644 classpath/java/lang/reflect/GenericDeclaration.java create mode 100644 classpath/java/lang/reflect/Type.java create mode 100644 classpath/java/lang/reflect/TypeVariable.java create mode 100644 classpath/java/security/ProtectionDomain.java diff --git a/classpath/java/lang/Appendable.java b/classpath/java/lang/Appendable.java new file mode 100644 index 0000000000..4c3b10bf22 --- /dev/null +++ b/classpath/java/lang/Appendable.java @@ -0,0 +1,22 @@ +/* Copyright (c) 2009, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.lang; + +import java.io.IOException; + +public interface Appendable { + public Appendable append(char c) throws IOException; + + public Appendable append(CharSequence sequence) throws IOException; + + public Appendable append(CharSequence sequence, int start, int end) + throws IOException; +} diff --git a/classpath/java/lang/Character.java b/classpath/java/lang/Character.java index 7d097366a8..61c3b3bedb 100644 --- a/classpath/java/lang/Character.java +++ b/classpath/java/lang/Character.java @@ -187,4 +187,40 @@ public final class Character implements Comparable { return isHighSurrogate(high) && isLowSurrogate(low); } + public static int codePointAt(CharSequence sequence, int offset) { + int length = sequence.length(); + if (offset < 0 || offset >= length) { + throw new IndexOutOfBoundsException(); + } + + char high = sequence.charAt(offset); + if (! isHighSurrogate(high) || offset >= length) { + return high; + } + char low = sequence.charAt(offset + 1); + if (! isLowSurrogate(low)) { + return high; + } + + return toCodePoint(high, low); + } + + public static int codePointCount(CharSequence sequence, int start, int end) { + int length = sequence.length(); + if (start < 0 || start > end || end >= length) { + throw new IndexOutOfBoundsException(); + } + + int count = 0; + for (int i = start; i < end; ++i) { + if (isHighSurrogate(sequence.charAt(i)) + && (i + 1) < end + && isLowSurrogate(sequence.charAt(i + 1))) + { + ++ i; + } + ++ count; + } + return count; + } } diff --git a/classpath/java/lang/Class.java b/classpath/java/lang/Class.java index 2d907f064b..7d54102164 100644 --- a/classpath/java/lang/Class.java +++ b/classpath/java/lang/Class.java @@ -14,12 +14,17 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.GenericDeclaration; +import java.lang.annotation.Annotation; import java.io.InputStream; import java.io.IOException; import java.net.URL; +import java.security.ProtectionDomain; -public final class Class { +public final class Class implements Type, GenericDeclaration { private static final int PrimitiveFlag = 1 << 4; private short flags; @@ -434,8 +439,61 @@ public final class Class { return null; } } - + public boolean desiredAssertionStatus() { return false; } + + public T cast(Object o) { + return (T) o; + } + + public Object[] getSigners() { + throw new UnsupportedOperationException(); + } + + public Annotation[] getDeclaredAnnotations() { + throw new UnsupportedOperationException(); + } + + public boolean isEnum() { + throw new UnsupportedOperationException(); + } + + public TypeVariable>[] getTypeParameters() { + throw new UnsupportedOperationException(); + } + + public String getSimpleName() { + throw new UnsupportedOperationException(); + } + + public Method getEnclosingMethod() { + throw new UnsupportedOperationException(); + } + + public Constructor getEnclosingConstructor() { + throw new UnsupportedOperationException(); + } + + public Class getEnclosingClass() { + throw new UnsupportedOperationException(); + } + + public Class[] getDeclaredClasses() { + throw new UnsupportedOperationException(); + } + + public A getAnnotation(Class c) { + throw new UnsupportedOperationException(); + } + + public ProtectionDomain getProtectionDomain() { + throw new UnsupportedOperationException(); + } + + // for GNU Classpath compatibility: + void setSigners(Object[] signers) { + throw new UnsupportedOperationException(); + } } diff --git a/classpath/java/lang/Enum.java b/classpath/java/lang/Enum.java index 5edcbf6cd5..db1c7fa496 100644 --- a/classpath/java/lang/Enum.java +++ b/classpath/java/lang/Enum.java @@ -53,4 +53,12 @@ public abstract class Enum> implements Comparable { public String toString() { return name; } + + public Class getDeclaringClass() { + Class c = getClass(); + while (c.getSuperclass() != Enum.class) { + c = c.getSuperclass(); + } + return c; + } } diff --git a/classpath/java/lang/Object.java b/classpath/java/lang/Object.java index b902b38060..a1fb1a0521 100644 --- a/classpath/java/lang/Object.java +++ b/classpath/java/lang/Object.java @@ -25,7 +25,7 @@ public class Object { return this == o; } - protected void finalize() { } + protected void finalize() throws Throwable { } public native final Class getClass(); @@ -41,5 +41,14 @@ public class Object { wait(0); } - public native final void wait(long timeout) throws InterruptedException; + public native final void wait(long milliseconds) throws InterruptedException; + + public final void wait(long milliseconds, int nanoseconds) + throws InterruptedException + { + if (nanoseconds != 0) { + ++ milliseconds; + } + wait(milliseconds); + } } diff --git a/classpath/java/lang/StackTraceElement.java b/classpath/java/lang/StackTraceElement.java index f20dc27803..61c742a941 100644 --- a/classpath/java/lang/StackTraceElement.java +++ b/classpath/java/lang/StackTraceElement.java @@ -18,8 +18,8 @@ public class StackTraceElement { private String file; private int line; - private StackTraceElement(String class_, String method, String file, - int line) + public StackTraceElement(String class_, String method, String file, + int line) { this.class_ = class_; this.method = method; diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index e7a6aabfff..23fdf52196 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -12,14 +12,30 @@ package java.lang; import java.io.UnsupportedEncodingException; import java.util.regex.Pattern; +import java.util.Comparator; +import java.util.Locale; import java.io.ByteArrayOutputStream; +import java.io.Serializable; + +public final class String + implements Comparable, CharSequence, Serializable +{ + public static Comparator CASE_INSENSITIVE_ORDER + = new Comparator() { + public int compare(String a, String b) { + return a.compareToIgnoreCase(b); + } + }; -public final class String implements Comparable, CharSequence { private final Object data; private final int offset; private final int length; private int hashCode; + public String() { + this(new char[0], 0, 0); + } + public String(char[] data, int offset, int length, boolean copy) { this((Object) data, offset, length, copy); } @@ -32,7 +48,9 @@ public final class String implements Comparable, CharSequence { this(data, 0, data.length); } - public String(byte bytes[], int offset, int length, String charsetName) throws UnsupportedEncodingException { + public String(byte bytes[], int offset, int length, String charsetName) + throws UnsupportedEncodingException + { this(bytes, offset, length); if (!charsetName.equals("UTF-8")) { throw new UnsupportedEncodingException(charsetName); @@ -57,12 +75,29 @@ public final class String implements Comparable, CharSequence { public String(byte[] data, String charset) throws UnsupportedEncodingException - { - this(data); - if (! charset.equals("UTF-8")) { - throw new UnsupportedEncodingException(charset); - } + { + this(data); + if (! charset.equals("UTF-8")) { + throw new UnsupportedEncodingException(charset); } + } + + public String(byte bytes[], int highByte, int offset, int length) { + if (offset < 0 || offset + length > bytes.length) { + throw new IndexOutOfBoundsException + (offset + " < 0 or " + offset + " + " + length + " > " + bytes.length); + } + + char[] c = new char[length]; + int mask = highByte << 8; + for (int i = 0; i < length; ++i) { + c[i] = (char) ((bytes[offset + i] & 0xFF) | mask); + } + + this.data = c; + this.offset = 0; + this.length = length; + } private String(Object data, int offset, int length, boolean copy) { int l; @@ -534,6 +569,10 @@ public final class String implements Comparable, CharSequence { return Pattern.matches(regex, this); } + public String replaceAll(String regex, String replacement) { + return Pattern.compile(regex).matcher(this).replaceAll(replacement); + } + public native String intern(); public static String valueOf(Object s) { @@ -572,6 +611,10 @@ public final class String implements Comparable, CharSequence { return Double.toString(v); } + public static String valueOf(char[] data, int offset, int length) { + return new String(data, offset, length); + } + public int lastIndexOf(int ch, int lastIndex) { for (int i = lastIndex ; i >= 0; --i) { if (charAt(i) == ch) { @@ -581,4 +624,63 @@ public final class String implements Comparable, CharSequence { return -1; } + + public boolean regionMatches(int thisOffset, String match, int matchOffset, + int length) + { + return regionMatches(false, thisOffset, match, matchOffset, length); + } + + public boolean regionMatches(boolean ignoreCase, int thisOffset, + String match, int matchOffset, int length) + { + String a = substring(thisOffset, length); + String b = match.substring(matchOffset, length); + if (ignoreCase) { + return a.equalsIgnoreCase(b); + } else { + return a.equals(b); + } + } + + public boolean isEmpty() { + return length == 0; + } + + public boolean contains(String match) { + return indexOf(match) != -1; + } + + public int codePointAt(int offset) { + return Character.codePointAt(this, offset); + } + + public int codePointCount(int start, int end) { + return Character.codePointCount(this, start, end); + } + + public String replace(CharSequence match, CharSequence replacement) { + throw new UnsupportedOperationException(); + } + + public String toUpperCase(Locale locale) { + throw new UnsupportedOperationException(); + } + + public String toLowerCase(Locale locale) { + throw new UnsupportedOperationException(); + } + + // for GNU Classpath compatibility: + static char[] zeroBasedStringValue(String s) { + if (s.offset == 0) { + if (s.data instanceof char[]) { + char[] data = (char[]) s.data; + if (data.length == s.length) { + return data; + } + } + } + return s.toCharArray(); + } } diff --git a/classpath/java/lang/StringBuilder.java b/classpath/java/lang/StringBuilder.java index 47b58134d1..77332204a5 100644 --- a/classpath/java/lang/StringBuilder.java +++ b/classpath/java/lang/StringBuilder.java @@ -10,7 +10,7 @@ package java.lang; -public class StringBuilder implements CharSequence { +public class StringBuilder implements CharSequence, Appendable { private static final int BufferSize = 32; private Cell chain; @@ -54,6 +54,14 @@ public class StringBuilder implements CharSequence { } } + public StringBuilder append(CharSequence sequence) { + return append(sequence.toString()); + } + + public Appendable append(CharSequence sequence, int start, int end) { + return append(sequence.subSequence(start, end)); + } + public StringBuilder append(char[] b, int offset, int length) { return append(new String(b, offset, length)); } @@ -150,6 +158,10 @@ public class StringBuilder implements CharSequence { return this; } + public StringBuilder insert(int i, CharSequence s) { + return insert(i, s.toString()); + } + public StringBuilder insert(int i, char c) { return insert(i, new String(new char[] { c }, 0, 1, false)); } @@ -332,4 +344,8 @@ public class StringBuilder implements CharSequence { deleteCharAt(index); insert(index, ch); } + + public void ensureCapacity(int capacity) { + // ignore + } } diff --git a/classpath/java/lang/Throwable.java b/classpath/java/lang/Throwable.java index 333a156166..09861fe9b4 100644 --- a/classpath/java/lang/Throwable.java +++ b/classpath/java/lang/Throwable.java @@ -13,8 +13,9 @@ package java.lang; import java.io.PrintStream; import java.io.PrintWriter; import java.io.IOException; +import java.io.Serializable; -public class Throwable { +public class Throwable implements Serializable { private String message; private Object trace; private Throwable cause; @@ -109,4 +110,9 @@ public class Throwable { cause.printStackTrace(sb, nl); } } + + public Throwable fillInStackTrace() { + trace = trace(0); + return this; + } } diff --git a/classpath/java/lang/ref/Reference.java b/classpath/java/lang/ref/Reference.java index cdfacb191a..ed322a8c42 100644 --- a/classpath/java/lang/ref/Reference.java +++ b/classpath/java/lang/ref/Reference.java @@ -22,6 +22,10 @@ public abstract class Reference { this.queue = queue; } + protected Reference(T target) { + this(target, null); + } + public T get() { return target; } diff --git a/classpath/java/lang/reflect/Constructor.java b/classpath/java/lang/reflect/Constructor.java index b4254e7c82..11df2f1fae 100644 --- a/classpath/java/lang/reflect/Constructor.java +++ b/classpath/java/lang/reflect/Constructor.java @@ -10,7 +10,9 @@ package java.lang.reflect; -public class Constructor extends AccessibleObject implements Member { +public class Constructor extends AccessibleObject + implements Member, GenericDeclaration +{ private Method method; public Constructor(Method method) { @@ -46,6 +48,18 @@ public class Constructor extends AccessibleObject implements Member { return method.getName(); } + public boolean isSynthetic() { + return method.isSynthetic(); + } + + public TypeVariable>[] getTypeParameters() { + throw new UnsupportedOperationException(); + } + + public Type[] getGenericParameterTypes() { + return method.getGenericParameterTypes(); + } + private static native T make(Class c); public T newInstance(Object ... arguments) diff --git a/classpath/java/lang/reflect/Field.java b/classpath/java/lang/reflect/Field.java index 978859ce05..19df851027 100644 --- a/classpath/java/lang/reflect/Field.java +++ b/classpath/java/lang/reflect/Field.java @@ -101,6 +101,38 @@ public class Field extends AccessibleObject { } } + public boolean getBoolean(Object instance) throws IllegalAccessException { + return ((Boolean) get(instance)).booleanValue(); + } + + public byte getByte(Object instance) throws IllegalAccessException { + return ((Byte) get(instance)).byteValue(); + } + + public short getShort(Object instance) throws IllegalAccessException { + return ((Short) get(instance)).shortValue(); + } + + public char getChar(Object instance) throws IllegalAccessException { + return ((Character) get(instance)).charValue(); + } + + public int getInt(Object instance) throws IllegalAccessException { + return ((Integer) get(instance)).intValue(); + } + + public float getFloat(Object instance) throws IllegalAccessException { + return ((Float) get(instance)).floatValue(); + } + + public long getLong(Object instance) throws IllegalAccessException { + return ((Long) get(instance)).longValue(); + } + + public double getDouble(Object instance) throws IllegalAccessException { + return ((Double) get(instance)).doubleValue(); + } + public void set(Object instance, Object value) throws IllegalAccessException { @@ -162,6 +194,10 @@ public class Field extends AccessibleObject { } } + public boolean isEnumConstant() { + throw new UnsupportedOperationException(); + } + private static native long getPrimitive (Object instance, int code, int offset); diff --git a/classpath/java/lang/reflect/GenericDeclaration.java b/classpath/java/lang/reflect/GenericDeclaration.java new file mode 100644 index 0000000000..61914661bb --- /dev/null +++ b/classpath/java/lang/reflect/GenericDeclaration.java @@ -0,0 +1,15 @@ +/* Copyright (c) 2009, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.lang.reflect; + +public interface GenericDeclaration { + public TypeVariable[] getTypeParameters(); +} diff --git a/classpath/java/lang/reflect/Member.java b/classpath/java/lang/reflect/Member.java index 4654d38875..6af945750f 100644 --- a/classpath/java/lang/reflect/Member.java +++ b/classpath/java/lang/reflect/Member.java @@ -19,4 +19,6 @@ public interface Member { public int getModifiers(); public String getName(); + + public boolean isSynthetic(); } diff --git a/classpath/java/lang/reflect/Method.java b/classpath/java/lang/reflect/Method.java index 68058aedce..f6ac1bd6cd 100644 --- a/classpath/java/lang/reflect/Method.java +++ b/classpath/java/lang/reflect/Method.java @@ -10,7 +10,9 @@ package java.lang.reflect; -public class Method extends AccessibleObject implements Member { +public class Method extends AccessibleObject + implements Member, GenericDeclaration +{ private byte vmFlags; private byte returnCode; private byte parameterCount; @@ -124,4 +126,28 @@ public class Method extends AccessibleObject implements Member { } throw new RuntimeException(); } + + public boolean isSynthetic() { + throw new UnsupportedOperationException(); + } + + public Object getDefaultValue() { + throw new UnsupportedOperationException(); + } + + public Type[] getGenericParameterTypes() { + throw new UnsupportedOperationException(); + } + + public Type getGenericReturnType() { + throw new UnsupportedOperationException(); + } + + public Class[] getExceptionTypes() { + throw new UnsupportedOperationException(); + } + + public TypeVariable>[] getTypeParameters() { + throw new UnsupportedOperationException(); + } } diff --git a/classpath/java/lang/reflect/Type.java b/classpath/java/lang/reflect/Type.java new file mode 100644 index 0000000000..b1a04ec765 --- /dev/null +++ b/classpath/java/lang/reflect/Type.java @@ -0,0 +1,13 @@ +/* Copyright (c) 2009, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.lang.reflect; + +public interface Type { } diff --git a/classpath/java/lang/reflect/TypeVariable.java b/classpath/java/lang/reflect/TypeVariable.java new file mode 100644 index 0000000000..65d531d6bf --- /dev/null +++ b/classpath/java/lang/reflect/TypeVariable.java @@ -0,0 +1,19 @@ +/* Copyright (c) 2009, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.lang.reflect; + +public interface TypeVariable + extends Type +{ + public Type[] getBounds(); + public T getGenericDeclaration(); + public String getName(); +} diff --git a/classpath/java/security/ProtectionDomain.java b/classpath/java/security/ProtectionDomain.java new file mode 100644 index 0000000000..191a8841ac --- /dev/null +++ b/classpath/java/security/ProtectionDomain.java @@ -0,0 +1,13 @@ +/* Copyright (c) 2009, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.security; + +public class ProtectionDomain { }