diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index acde33d55..1752ccd70 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -56,6 +56,10 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ length = value.length(); } + public TAbstractStringBuilder append(Object obj) { + return append(String.valueOf(obj)); + } + protected TAbstractStringBuilder append(String string) { return insert(length, string); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java index ff3eb8c47..5dfe1173b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java @@ -32,6 +32,11 @@ public class TStringBuffer extends TAbstractStringBuilder implements TAppendable super(value); } + @Override + public TStringBuffer append(Object obj) { + return append(String.valueOf(obj)); + } + @Override public TStringBuffer append(String string) { super.append(string); diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java index db4f521ea..689fe6388 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java @@ -32,6 +32,11 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl super(value); } + @Override + public TStringBuilder append(Object obj) { + return append(String.valueOf(obj)); + } + @Override public TStringBuilder append(String string) { super.append(string); diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java b/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java index 401bb8801..6f5809d19 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractCollection.java @@ -142,15 +142,17 @@ public abstract class TAbstractCollection extends TObject implements TCollect @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append("["); + sb.append('['); TIterator iter = iterator(); if (iter.hasNext()) { - sb.append(String.valueOf(iter.next())); + E e = iter.next(); + sb.append(e == this ? "(this Collection)" : iter.next()); } while (iter.hasNext()) { - sb.append(", ").append(String.valueOf(iter.next())); + E e = iter.next(); + sb.append(", ").append(e == this ? "(this Collection)" : iter.next()); } - sb.append("]"); + sb.append(']'); return sb.toString(); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractMap.java b/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractMap.java index aeee82c26..6bb439399 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractMap.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TAbstractMap.java @@ -265,6 +265,28 @@ public abstract class TAbstractMap extends TObject implements TMap { return result; } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append('{'); + TIterator> iter = entrySet().iterator(); + if (iter.hasNext()) { + TMap.Entry e = iter.next(); + sb.append(e.getKey() == this ? "(this Map)" : e.getKey()); + sb.append('='); + sb.append(e.getValue() == this ? "(this Map)" : e.getValue()); + } + while (iter.hasNext()) { + sb.append(", "); + TMap.Entry e = iter.next(); + sb.append(e.getKey() == this ? "(this Map)" : e.getKey()); + sb.append('='); + sb.append(e.getValue() == this ? "(this Map)" : e.getValue()); + } + sb.append('}'); + return sb.toString(); + } + @Override protected Object clone() throws TCloneNotSupportedException { TAbstractMap copy = (TAbstractMap) super.clone(); diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java index 12146335a..3f83e821b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java @@ -195,4 +195,19 @@ public class TArrayList extends TAbstractList implements TCloneable, TSeri action.accept(array[i]); } } + + @Override + public String toString() { + if (size == 0) { + return "[]"; + } + int length = size - 1; + StringBuilder buffer = new StringBuilder(size * 16); + buffer.append('['); + for (int i = 0; i < length; i++) { + buffer.append(array[i] == this ? "(this Collection)" : array[i]).append(", "); + } + buffer.append(array[length] == this ? "(this Collection)" : array[length]); + return buffer.append(']').toString(); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java b/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java index d686601a7..6c87be8b6 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/ArrayListTest.java @@ -174,4 +174,14 @@ public class ArrayListTest { assertEquals("A1", list.get(0)); assertEquals("B1", list.get(1)); } + + @Test + public void testToString() { + List list = new ArrayList<>(); + list.add(list); + list.add("A"); + + assertEquals("[(this Collection), A]", list.toString()); + assertEquals("[]", new ArrayList().toString()); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/LinkedHashMapTest.java b/tests/src/test/java/org/teavm/classlib/java/util/LinkedHashMapTest.java index f178d3143..5d5d1927c 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/LinkedHashMapTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/LinkedHashMapTest.java @@ -550,4 +550,14 @@ public class LinkedHashMapTest { } assertTrue("Entries left to iterate on", !it2.hasNext()); } + + @Test + public void test_to_String() { + LinkedHashMap lhm = new LinkedHashMap(); + lhm.put("A", lhm); + lhm.put("B", "C"); + + assertEquals("{A=(this Map), B=C}", lhm.toString()); + assertEquals("{}", new LinkedHashMap().toString()); + } }