diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java index 8f1135758..7d1effdcf 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ClassNativeGenerator.java @@ -66,10 +66,13 @@ public class ClassNativeGenerator implements Generator, Injector, DependencyPlug generateIsAssignableFrom(context); break; case "booleanClass": - generateBooleanClass(context); + context.getWriter().append("$rt_cls($rt_booleancls())"); break; case "intClass": - generateIntClass(context); + context.getWriter().append("$rt_cls($rt_intcls())"); + break; + case "voidClass": + context.getWriter().append("$rt_cls($rt_voidcls())"); break; case "wrap": context.writeExpr(context.getArgument(0)); @@ -99,14 +102,6 @@ public class ClassNativeGenerator implements Generator, Injector, DependencyPlug writer.append(".$data)"); } - private void generateBooleanClass(InjectorContext context) throws IOException { - context.getWriter().append("$rt_cls($rt_booleancls())"); - } - - private void generateIntClass(InjectorContext context) throws IOException { - context.getWriter().append("$rt_cls($rt_intcls())"); - } - @Override public void methodAchieved(DependencyChecker checker, MethodDependency graph) { switch (graph.getReference().getName()) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index 48d91f7b0..026873998 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -57,6 +57,9 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } protected TAbstractStringBuilder append(TString string) { + if (string == null) { + string = TString.wrap("null"); + } ensureCapacity(length + string.length()); int j = length; for (int i = 0; i < string.length(); ++i) { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index 7c9be16fc..99d786fb0 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -42,7 +42,7 @@ public class TClass extends TObject { public native boolean isAssignableFrom(TClass obj); public TString getName() { - return name; + return new TString(name); } public boolean isPrimitive() { @@ -77,6 +77,10 @@ public class TClass extends TObject { @PluggableDependency(ClassNativeGenerator.class) static native TClass intClass(); + @InjectedBy(ClassNativeGenerator.class) + @PluggableDependency(ClassNativeGenerator.class) + static native TClass voidClass(); + @InjectedBy(ClassNativeGenerator.class) @PluggableDependency(ClassNativeGenerator.class) public static native TClass wrap(Class cls); @@ -95,4 +99,14 @@ public class TClass extends TObject { @InjectedBy(ClassNativeGenerator.class) public native T[] getEnumConstantsImpl(); + + @SuppressWarnings("unchecked") + public T cast(TObject obj) { + if (obj != null && !isAssignableFrom(TClass.wrap(obj.getClass()))) { + throw new TClassCastException(TString.wrap(new TStringBuilder() + .append(TClass.wrap(obj.getClass()).getName()) + .append(TString.wrap(" is not subtype of ")).append(name).toString())); + } + return (T)obj; + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TIterable.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TIterable.java index f7a346b86..03f9abc12 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TIterable.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TIterable.java @@ -21,6 +21,6 @@ import org.teavm.classlib.java.util.TIterator; * * @author Alexey Andreev */ -public interface TIterable { +public interface TIterable { TIterator iterator(); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TNegativeArraySizeException.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TNegativeArraySizeException.java new file mode 100644 index 000000000..1480fcd92 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TNegativeArraySizeException.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang; + +/** + * + * @author Alexey Andreev + */ +public class TNegativeArraySizeException extends TRuntimeException { + private static final long serialVersionUID = 4838944281468611671L; + + public TNegativeArraySizeException() { + super(); + } + + public TNegativeArraySizeException(TString message) { + super(message); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVoid.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVoid.java new file mode 100644 index 000000000..72adf6726 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVoid.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang; + +/** + * + * @author Alexey Andreev + */ +public final class TVoid extends TObject { + public static final TClass TYPE = TClass.voidClass(); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java index ee15df29c..4a35810e8 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java @@ -32,10 +32,18 @@ import org.teavm.model.ValueType; * @author Alexey Andreev */ public class ArrayNativeGenerator implements Generator, DependencyPlugin { + private static final String[] primitives = { "Byte", "Short", "Char", "Int", "Long", "Float", "Double", + "Boolean" }; + @Override public void methodAchieved(DependencyChecker checker, MethodDependency method) { - if (method.getReference().getName().equals("getLength")) { - achieveGetLength(checker, method); + switch (method.getReference().getName()) { + case "getLength": + achieveGetLength(checker, method); + break; + case "newInstanceImpl": + method.getResult().propagate("[java.lang.Object"); + break; } } @@ -45,6 +53,9 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { case "getLength": generateGetLength(context, writer); break; + case "newInstanceImpl": + generateNewInstance(context, writer); + break; } } @@ -70,4 +81,19 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { } }); } + + private void generateNewInstance(GeneratorContext context, SourceWriter writer) throws IOException { + String type = context.getParameterName(1); + String length = context.getParameterName(2); + writer.append("var cls = " + type + ".$data;").softNewLine(); + writer.append("if (cls.primitive) {").softNewLine().indent(); + for (String primitive : primitives) { + writer.append("if (cls == $rt_" + primitive.toLowerCase() + "cls()) {").indent().softNewLine(); + writer.append("return $rt_create" + primitive + "Array(" + length + ");").softNewLine(); + writer.outdent().append("}").softNewLine(); + } + writer.outdent().append("} else {").indent().softNewLine(); + writer.append("return $rt_createArray(cls, " + length + ")").softNewLine(); + writer.outdent().append("}").softNewLine(); + } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/TArray.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/TArray.java index fca9234b0..2f3c283e8 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/TArray.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/TArray.java @@ -15,8 +15,7 @@ */ package org.teavm.classlib.java.lang.reflect; -import org.teavm.classlib.java.lang.TIllegalArgumentException; -import org.teavm.classlib.java.lang.TObject; +import org.teavm.classlib.java.lang.*; import org.teavm.dependency.PluggableDependency; import org.teavm.javascript.ni.GeneratedBy; @@ -28,4 +27,21 @@ public final class TArray extends TObject { @GeneratedBy(ArrayNativeGenerator.class) @PluggableDependency(ArrayNativeGenerator.class) public static native int getLength(TObject array) throws TIllegalArgumentException; + + public static TObject newInstance(TClass componentType, int length) throws TNegativeArraySizeException { + if (componentType == null) { + throw new TNullPointerException(); + } + if (componentType == TClass.wrap(void.class)) { + throw new TIllegalArgumentException(); + } + if (length < 0) { + throw new TNegativeArraySizeException(); + } + return newInstanceImpl(componentType, length); + } + + @GeneratedBy(ArrayNativeGenerator.class) + @PluggableDependency(ArrayNativeGenerator.class) + private static native TObject newInstanceImpl(TClass componentType, int length); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java new file mode 100644 index 000000000..9503de976 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java @@ -0,0 +1,146 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; +import org.teavm.classlib.java.lang.TUnsupportedOperationException; + +/** + * + * @author Alexey Andreev + */ +public abstract class TAbstractCollection extends TObject implements TCollection { + protected TAbstractCollection() { + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public boolean contains(Object o) { + for (TIterator iter = iterator(); iter.hasNext();) { + E e = iter.next(); + if (e == null ? o == null : e.equals(o)) { + return true; + } + } + return false; + } + + @Override + public Object[] toArray() { + Object[] arr = new Object[size()]; + int i = 0; + for (TIterator iter = iterator(); iter.hasNext();) { + arr[i++] = iter.next(); + } + return arr; + } + + @Override + public T[] toArray(T[] a) { + return null; + } + + @Override + public boolean add(E e) { + throw new TUnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + for (TIterator iter = iterator(); iter.hasNext();) { + E e = iter.next(); + if (e == null ? o == null : e.equals(o)) { + iter.remove(); + return true; + } + } + return false; + } + + @Override + public boolean containsAll(TCollection c) { + for (TIterator iter = c.iterator(); iter.hasNext();) { + if (!contains(iter.next())) { + return false; + } + } + return true; + } + + @Override + public boolean addAll(TCollection c) { + boolean changed = false; + for (TIterator iter = c.iterator(); iter.hasNext();) { + if (add(iter.next())) { + changed = true; + } + } + return changed; + } + + @Override + public boolean removeAll(TCollection c) { + boolean changed = false; + for (TIterator iter = iterator(); iter.hasNext();) { + E e = iter.next(); + if (c.contains(e)) { + iter.remove(); + changed = true; + } + } + return changed; + } + + @Override + public boolean retainAll(TCollection c) { + boolean changed = false; + for (TIterator iter = iterator(); iter.hasNext();) { + E e = iter.next(); + if (!c.contains(e)) { + iter.remove(); + changed = true; + } + } + return changed; + } + + @Override + public void clear() { + for (TIterator iter = iterator(); iter.hasNext();) { + iter.next(); + iter.remove(); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + TIterator iter = iterator(); + if (iter.hasNext()) { + sb.append(String.valueOf(iter.next())); + } + while (iter.hasNext()) { + sb.append(", ").append(String.valueOf(iter.next())); + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java new file mode 100644 index 000000000..4468ca4df --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TAbstractList.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +/** + * + * @author Alexey Andreev + */ +public class TAbstractList { + +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java new file mode 100644 index 000000000..29739c058 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TCollection.java @@ -0,0 +1,48 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TIterable; + +/** + * + * @author Alexey Andreev + */ +public interface TCollection extends TIterable { + int size(); + + boolean isEmpty(); + + boolean contains(Object o); + + Object[] toArray(); + + T[] toArray(T[] a); + + boolean add(E e); + + boolean remove(Object o); + + boolean containsAll(TCollection c); + + boolean addAll(TCollection c); + + boolean removeAll(TCollection c); + + boolean retainAll(TCollection c); + + void clear(); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TComparator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TComparator.java new file mode 100644 index 000000000..1e8435af6 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TComparator.java @@ -0,0 +1,28 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; + +/** + * + * @author Alexey Andreev + */ +public interface TComparator { + int compare(T o1, T o2); + + boolean equals(TObject obj); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TConcurrentModificationException.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TConcurrentModificationException.java new file mode 100644 index 000000000..4499dcd2c --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TConcurrentModificationException.java @@ -0,0 +1,44 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TRuntimeException; +import org.teavm.classlib.java.lang.TString; +import org.teavm.classlib.java.lang.TThrowable; + +/** + * + * @author Alexey Andreev + */ +public class TConcurrentModificationException extends TRuntimeException { + private static final long serialVersionUID = -2871226388713811335L; + + public TConcurrentModificationException() { + super(); + } + + public TConcurrentModificationException(TString message, TThrowable cause) { + super(message, cause); + } + + public TConcurrentModificationException(TString message) { + super(message); + } + + public TConcurrentModificationException(TThrowable cause) { + super(cause); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TDeque.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TDeque.java new file mode 100644 index 000000000..3672291ab --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TDeque.java @@ -0,0 +1,56 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +/** + * + * @author Alexey Andreev + */ +public interface TDeque extends TQueue { + void addFirst(E e); + + void addLast(E e); + + boolean offerFirst(E e); + + boolean offerLast(E e); + + E removeFirst(); + + E removeLast(); + + E pollFirst(); + + E pollLast(); + + E getFirst(); + + E getLast(); + + E peekFirst(); + + E peekLast(); + + boolean removeFirstOccurrence(Object o); + + boolean removeLastOccurrence(Object o); + + void push(E e); + + E pop(); + + TIterator descendingIterator(); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TEnumeration.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TEnumeration.java new file mode 100644 index 000000000..a3071f549 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TEnumeration.java @@ -0,0 +1,28 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; + +/** + * + * @author Alexey Andreev + */ +public interface TEnumeration { + boolean hasMoreElements(); + + E nextElement(); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TIterator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TIterator.java index 0cadc2e8b..124844c8d 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TIterator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TIterator.java @@ -15,13 +15,11 @@ */ package org.teavm.classlib.java.util; -import org.teavm.classlib.java.lang.TObject; - /** * * @author Alexey Andreev */ -public interface TIterator { +public interface TIterator { boolean hasNext(); E next(); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TList.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TList.java new file mode 100644 index 000000000..c297f20bb --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TList.java @@ -0,0 +1,44 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; + +/** + * + * @author Alexey Andreev + */ +public interface TList extends TCollection { + boolean addAll(int index, TCollection c); + + E get(int index); + + E set(int index, E element); + + void add(int index, E element); + + E remove(int index); + + int indexOf(Object o); + + int lastIndexOf(Object o); + + TListIterator listIterator(); + + TListIterator listIterator(int index); + + TList subList(int fromIndex, int toIndex); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TListIterator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TListIterator.java new file mode 100644 index 000000000..addc83a83 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TListIterator.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; + +/** + * + * @author Alexey Andreev + */ +public interface TListIterator extends TIterator { + boolean hasPrevious(); + + E previous(); + + int nextIndex(); + + int previousIndex(); + + void set(E e); + + void add(E e); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TMap.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TMap.java new file mode 100644 index 000000000..61f095350 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TMap.java @@ -0,0 +1,56 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; + +/** + * + * @author Alexey Andreev + */ +public interface TMap { + public static interface Entry { + K1 getKey(); + + V1 getValue(); + + V1 setValue(V1 value); + } + + int size(); + + boolean isEmpty(); + + boolean containsKey(TObject key); + + boolean containsValue(TObject value); + + V get(TObject key); + + V put(K key, V value); + + V remove(TObject key); + + void putAll(TMap m); + + void clear(); + + TSet keySet(); + + TCollection values(); + + TSet> entrySet(); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TQueue.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TQueue.java new file mode 100644 index 000000000..6260d1fea --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TQueue.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +/** + * + * @author Alexey Andreev + */ +public interface TQueue extends TCollection { + boolean offer(E e); + + E remove(); + + E poll(); + + E element(); + + E peek(); +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandomAccess.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandomAccess.java new file mode 100644 index 000000000..ad1165efb --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandomAccess.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +/** + * + * @author Alexey Andreev + */ +public interface TRandomAccess { +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TSet.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TSet.java new file mode 100644 index 000000000..de75dde26 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TSet.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +/** + * + * @author Alexey Andreev + */ +public interface TSet extends TCollection { +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TSortedSet.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TSortedSet.java new file mode 100644 index 000000000..333d5edd5 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TSortedSet.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +import org.teavm.classlib.java.lang.TObject; + +/** + * + * @author Alexey Andreev + */ +public interface TSortedSet extends TSet { + TComparator comparator(); + + TSortedSet subSet(E fromElement, E toElement); + + TSortedSet headSet(E toElement); + + TSortedSet tailSet(E fromElement); + + E first(); + + E last(); +} diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java index 852568c79..74572342c 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java @@ -77,4 +77,16 @@ public class ClassTest { public void nonArrayComponentTypeIsNull() { assertNull(Object.class.getComponentType()); } + + @Test + public void castingAppropriateObject() { + Object obj = 23; + assertEquals(Integer.valueOf(23), Integer.class.cast(obj)); + } + + @Test(expected = ClassCastException.class) + public void inappropriateObjectCastingFails() { + Object obj = 23; + Float.class.cast(obj); + } } diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/reflect/ArrayTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/reflect/ArrayTest.java new file mode 100644 index 000000000..7fb87cfe7 --- /dev/null +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/reflect/ArrayTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang.reflect; + +import static org.junit.Assert.*; +import java.lang.reflect.Array; +import org.junit.Test; + +/** + * + * @author Alexey Andreev + */ +public class ArrayTest { + @Test + public void createsNewInstance() { + Object instance = Array.newInstance(Object.class, 10); + assertEquals(Object[].class, instance.getClass()); + assertEquals(10, Array.getLength(instance)); + } + + @Test + public void createsNewPrimitiveInstance() { + Object instance = Array.newInstance(int.class, 15); + assertEquals(int[].class, instance.getClass()); + assertEquals(15, Array.getLength(instance)); + } +} diff --git a/teavm-html4j/pom.xml b/teavm-html4j/pom.xml index 064790534..6bd1e7bd0 100644 --- a/teavm-html4j/pom.xml +++ b/teavm-html4j/pom.xml @@ -84,9 +84,9 @@ true ${project.build.directory}/javascript-tck org.teavm.html4j.testing.KOTestAdapter - +