Implementing class library emulation

This commit is contained in:
Alexey Andreev 2013-11-25 17:41:18 +04:00
parent 18e35b80e6
commit 1355e211e3
13 changed files with 339 additions and 2 deletions

View File

@ -0,0 +1,68 @@
package org.teavm.classlib.java.lang;
import org.teavm.classlib.java.lang.io.TSerializable;
import org.teavm.classlib.java.util.TArrays;
/**
*
* @author Alexey Andreev
*/
class AbstractStringBuilder extends TObject implements TSerializable {
private char[] buffer;
private int length;
public AbstractStringBuilder() {
this(16);
}
public AbstractStringBuilder(int capacity) {
buffer = new char[capacity];
}
protected AbstractStringBuilder append(TString string) {
ensureCapacity(length + string.length());
int j = length;
for (int i = 0; i < string.length(); ++i) {
buffer[j++] = string.charAt(i);
}
length = j;
return this;
}
protected AbstractStringBuilder append(int value) {
if (value < 0) {
append('-');
value = -value;
}
if (value < 10) {
append((char)('0' + value));
} else {
int pos = 10;
int sz = 1;
while (pos <= 1000000000 && pos <= value) {
pos *= 10;
++sz;
}
ensureCapacity(length + sz);
while (pos > 0) {
buffer[length++] = (char)('0' + value / pos);
value %= pos;
pos /= 10;
}
}
return this;
}
protected AbstractStringBuilder append(char c) {
ensureCapacity(length + 1);
buffer[length++] = c;
return this;
}
private void ensureCapacity(int capacity) {
if (buffer.length >= capacity) {
return;
}
buffer = TArrays.copyOf(buffer, capacity * 2 + 1);
}
}

View File

@ -6,4 +6,20 @@ package org.teavm.classlib.java.lang;
*/ */
public class TException extends TThrowable { public class TException extends TThrowable {
private static final long serialVersionUID = -2188339106250208952L; private static final long serialVersionUID = -2188339106250208952L;
public TException() {
super();
}
public TException(TString message, TThrowable cause) {
super(message, cause);
}
public TException(TString message) {
super(message);
}
public TException(TThrowable cause) {
super(cause);
}
} }

View File

@ -0,0 +1,25 @@
package org.teavm.classlib.java.lang;
/**
*
* @author Alexey Andreev
*/
public class TIllegalArgumentException extends TRuntimeException {
private static final long serialVersionUID = -1225768288500984373L;
public TIllegalArgumentException() {
super();
}
public TIllegalArgumentException(TString message, TThrowable cause) {
super(message, cause);
}
public TIllegalArgumentException(TString message) {
super(message);
}
public TIllegalArgumentException(TThrowable cause) {
super(cause);
}
}

View File

@ -0,0 +1,25 @@
package org.teavm.classlib.java.lang;
/**
*
* @author Alexey Andreev
*/
public class TIllegalStateException extends TException {
private static final long serialVersionUID = 218741044430713159L;
public TIllegalStateException() {
super();
}
public TIllegalStateException(TString message, TThrowable cause) {
super(message, cause);
}
public TIllegalStateException(TString message) {
super(message);
}
public TIllegalStateException(TThrowable cause) {
super(cause);
}
}

View File

@ -0,0 +1,17 @@
package org.teavm.classlib.java.lang;
/**
*
* @author Alexey Andreev
*/
public class TIndexOutOfBoundsException extends TRuntimeException {
private static final long serialVersionUID = -7329782331640782287L;
public TIndexOutOfBoundsException() {
super();
}
public TIndexOutOfBoundsException(TString message) {
super(message);
}
}

View File

@ -0,0 +1,28 @@
package org.teavm.classlib.java.lang;
import org.teavm.javascript.ni.Superclass;
/**
*
* @author Alexey Andreev
*/
@Superclass("java.lang.Exception")
public class TRuntimeException extends TException {
private static final long serialVersionUID = 3506083061304642891L;
public TRuntimeException() {
super();
}
public TRuntimeException(TString message, TThrowable cause) {
super(message, cause);
}
public TRuntimeException(TString message) {
super(message);
}
public TRuntimeException(TThrowable cause) {
super(cause);
}
}

View File

@ -1,12 +1,13 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import org.teavm.classlib.java.lang.io.TSerializable;
import org.teavm.javascript.ni.GeneratedBy; import org.teavm.javascript.ni.GeneratedBy;
/** /**
* *
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
public class TString extends TObject { public class TString extends TObject implements TSerializable {
private char[] characters; private char[] characters;
public TString() { public TString() {
@ -31,6 +32,21 @@ public class TString extends TObject {
} }
} }
public char charAt(int index) {
if (index < 0 || index >= characters.length) {
throw new StringIndexOutOfBoundsException(null);
}
return characters[index];
}
public int length() {
return characters.length;
}
public static TString valueOf(int index) {
return new TStringBuilder().append(index).toString0();
}
@GeneratedBy(StringNativeGenerator.class) @GeneratedBy(StringNativeGenerator.class)
public static native TString wrap(String str); public static native TString wrap(String str);
} }

View File

@ -0,0 +1,19 @@
package org.teavm.classlib.java.lang;
/**
*
* @author Alexey Andreev
*/
public class TStringBuilder extends AbstractStringBuilder {
@Override
public TStringBuilder append(TString string) {
super.append(string);
return this;
}
@Override
public TStringBuilder append(int value) {
super.append(value);
return this;
}
}

View File

@ -0,0 +1,21 @@
package org.teavm.classlib.java.lang;
/**
*
* @author Alexey Andreev
*/
public class TStringIndexOutOfBoundsException extends TIndexOutOfBoundsException {
private static final long serialVersionUID = 6706349858694463085L;
public TStringIndexOutOfBoundsException() {
super();
}
public TStringIndexOutOfBoundsException(TString message) {
super(message);
}
public TStringIndexOutOfBoundsException(int index) {
super(new TStringBuilder().append(TString.wrap("String index out of bounds: ")).append(index).toString0());
}
}

View File

@ -1,11 +1,72 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import org.teavm.javascript.ni.Remove;
import org.teavm.javascript.ni.Rename;
import org.teavm.javascript.ni.Superclass;
/** /**
* *
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
public class TThrowable extends Throwable { @Superclass("java.lang.Object")
public class TThrowable extends RuntimeException {
private static final long serialVersionUID = 2026791432677149320L; private static final long serialVersionUID = 2026791432677149320L;
private TString message; private TString message;
private TThrowable cause; private TThrowable cause;
public TThrowable() {
fillInStackTrace();
}
public TThrowable(TString message) {
fillInStackTrace();
this.message = message;
}
public TThrowable(TString message, TThrowable cause) {
fillInStackTrace();
this.message = message;
this.cause = cause;
}
public TThrowable(TThrowable cause) {
this.cause = cause;
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
@Rename("getMessage")
public TString getMessage0() {
return message;
}
@Rename("getLocalizedMessage")
public TString getLocalizedMessage0() {
return getMessage0();
}
@Override
public synchronized TThrowable getCause() {
return cause != this ? cause : null;
}
@Remove
public native TClass<?> getClass0();
@Remove
public native TString toString0();
public synchronized TThrowable initCause(TThrowable cause) {
if (this.cause != this && this.cause != null) {
throw new TIllegalStateException(TString.wrap("Cause already set"));
}
if (cause == this) {
throw new TIllegalArgumentException(TString.wrap("Circular causation relation"));
}
this.cause = cause;
return this;
}
} }

View File

@ -0,0 +1,8 @@
package org.teavm.classlib.java.lang.io;
/**
*
* @author Alexey Andreev
*/
public interface TSerializable {
}

View File

@ -0,0 +1,18 @@
package org.teavm.classlib.java.util;
import org.teavm.classlib.java.lang.TObject;
/**
*
* @author Alexey Andreev
*/
public class TArrays extends TObject {
public static char[] copyOf(char[] array, int length) {
char[] result = new char[length];
int sz = Math.min(length, array.length);
for (int i = 0; i < sz; ++i) {
result[i] = array[i];
}
return result;
}
}

View File

@ -0,0 +1,15 @@
package org.teavm.javascript.ni;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Alexey Andreev
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Remove {
}