From 59d60269eea93ac483bb5217ba54909726ebaa7f Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Mon, 31 Mar 2014 15:26:41 +0400 Subject: [PATCH] Fixes issues with clone methods. Fixes TreeMapTest --- .../teavm/classlib/java/lang/TBoolean.java | 2 +- .../org/teavm/classlib/java/lang/TByte.java | 2 +- .../teavm/classlib/java/lang/TCharacter.java | 2 +- .../org/teavm/classlib/java/lang/TDouble.java | 2 +- .../org/teavm/classlib/java/lang/TEnum.java | 6 +- .../org/teavm/classlib/java/lang/TFloat.java | 2 +- .../teavm/classlib/java/lang/TInteger.java | 2 +- .../org/teavm/classlib/java/lang/TLong.java | 2 +- .../org/teavm/classlib/java/lang/TObject.java | 7 ++- .../org/teavm/classlib/java/lang/TShort.java | 2 +- .../org/teavm/classlib/java/lang/TString.java | 2 +- .../classlib/java/util/TAbstractMap.java | 47 +++++++++++++++ .../teavm/classlib/java/util/TArrayList.java | 5 +- .../org/teavm/classlib/java/util/TBitSet.java | 6 +- .../teavm/classlib/java/util/THashMap.java | 8 ++- .../teavm/classlib/java/util/THashSet.java | 8 ++- .../teavm/classlib/java/util/TTreeMap.java | 7 ++- .../teavm/classlib/java/util/TreeMapTest.java | 58 +++---------------- 18 files changed, 91 insertions(+), 79 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java index 610a38fb3..ca5be44ad 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java @@ -88,7 +88,7 @@ public class TBoolean extends TObject implements TSerializable, TComparable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { return other instanceof TByte && ((TByte)other).value == value; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java index d64b1e07c..53193d103 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java @@ -124,7 +124,7 @@ public class TCharacter extends TObject implements TComparable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { if (this == other) { return true; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java index 1395f76e2..0428d9e66 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java @@ -196,7 +196,7 @@ public class TDouble extends TNumber implements TComparable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { if (this == other) { return true; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TEnum.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TEnum.java index 0ab5cb8db..1f987b5a0 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TEnum.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TEnum.java @@ -46,7 +46,7 @@ public abstract class TEnum> extends TObject implements TComp } @Override - public final boolean equals(TObject other) { + public final boolean equals(Object other) { return this == other; } @@ -55,8 +55,8 @@ public abstract class TEnum> extends TObject implements TComp return super.hashCode(); } - @Override - protected final TObject clone() throws TCloneNotSupportedException { + @Rename("clone") + protected final TObject clone0() throws TCloneNotSupportedException { throw new TCloneNotSupportedException(); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java index 86aea46e4..c2f78ef21 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java @@ -82,7 +82,7 @@ public class TFloat extends TNumber implements TComparable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { if (this == other) { return true; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java index 5bd0d31ce..1655bc8f3 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TInteger.java @@ -160,7 +160,7 @@ public class TInteger extends TNumber implements TComparable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { if (this == other) { return true; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java index b9fa35553..0a022bd4f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java @@ -205,7 +205,7 @@ public class TLong extends TNumber implements TComparable { public native int hashCode(); @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { if (this == other) { return true; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index a9cbc9a0f..0dc35c53d 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -44,7 +44,8 @@ public class TObject { @GeneratedBy(ObjectNativeGenerator.class) public native int hashCode(); - public boolean equals(TObject other) { + @Rename("equals") + public boolean equals0(TObject other) { return this == other; } @@ -56,10 +57,10 @@ public class TObject { @GeneratedBy(ObjectNativeGenerator.class) native int identity(); - @Override @GeneratedBy(ObjectNativeGenerator.class) @PluggableDependency(ObjectNativeGenerator.class) - protected native TObject clone() throws TCloneNotSupportedException; + @Override + protected native Object clone() throws TCloneNotSupportedException; @Rename("notify") public final void notify0() { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java index 3d83299da..6f8040860 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java @@ -76,7 +76,7 @@ public class TShort extends TNumber implements TComparable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { return other instanceof TShort && ((TShort)other).value == value; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java index f4cdfdd41..3bb4bb95f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TString.java @@ -485,7 +485,7 @@ public class TString extends TObject implements TSerializable, TComparable extends TObject implements TMap { return cachedValues; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof TMap)) { + return false; + } + @SuppressWarnings("unchecked") + TMap other = (TMap)obj; + if (size() != other.size()) { + return false; + } + for (TIterator> iter = entrySet().iterator(); iter.hasNext();) { + TMap.Entry entry = iter.next(); + if (!other.containsKey(entry.getKey())) { + return false; + } + if (!TObjects.equals(entry.getValue(), other.get(entry.getKey()))) { + return false; + } + } + return true; + } + + @Override + public int hashCode() { + int result = 0; + for (TIterator> iter = entrySet().iterator(); iter.hasNext();) { + TMap.Entry entry = iter.next(); + result ^= entry.hashCode(); + } + return result; + } + + @Override + @PluggableDependency(ObjectNativeGenerator.class) + protected Object clone() throws TCloneNotSupportedException { + TAbstractMap copy = (TAbstractMap)super.clone(); + copy.cachedKeySet = null; + copy.cachedValues = null; + return copy; + } + private class KeySet extends TAbstractSet { @Override public TIterator iterator() { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java index cba174630..c411b0f18 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java @@ -18,6 +18,7 @@ package org.teavm.classlib.java.util; import java.util.Arrays; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.*; +import org.teavm.javascript.ni.Rename; /** * @@ -66,8 +67,8 @@ public class TArrayList extends TAbstractList implements TCloneable, TSeri return size; } - @Override - public TObject clone() { + @Rename("clone") + public TObject clone0() { return new TArrayList<>(this); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java index 4bb5fe444..e96a6a76f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java @@ -449,7 +449,7 @@ public class TBitSet extends TObject implements TCloneable, TSerializable { } @Override - public boolean equals(TObject other) { + public boolean equals(Object other) { if (this == other) { return true; } @@ -508,8 +508,8 @@ public class TBitSet extends TObject implements TCloneable, TSerializable { return TString.wrap(sb.toString()); } - @Override - public TObject clone() { + @Rename("clone") + public TObject clone0() { return new TBitSet(TArrays.copyOf(data, data.length)); } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashMap.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashMap.java index 0346163ea..97316312f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashMap.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashMap.java @@ -17,12 +17,14 @@ package org.teavm.classlib.java.util; -import java.util.*; +import java.util.Arrays; +import java.util.ConcurrentModificationException; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.TCloneNotSupportedException; import org.teavm.classlib.java.lang.TIllegalArgumentException; import org.teavm.classlib.java.lang.TIllegalStateException; import org.teavm.classlib.java.lang.TObject; +import org.teavm.javascript.ni.Rename; public class THashMap extends TAbstractMap implements TSerializable { transient int elementCount; @@ -292,9 +294,9 @@ public class THashMap extends TAbstractMap implements TSerializable } } - @Override + @Rename("clone") @SuppressWarnings("unchecked") - public TObject clone() { + public TObject clone0() { try { THashMap map = (THashMap) super.clone(); map.elementCount = 0; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashSet.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashSet.java index caeb35359..91095e927 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashSet.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/THashSet.java @@ -15,10 +15,12 @@ */ package org.teavm.classlib.java.util; +import java.util.Iterator; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.TCloneNotSupportedException; import org.teavm.classlib.java.lang.TCloneable; import org.teavm.classlib.java.lang.TObject; +import org.teavm.javascript.ni.Rename; /** * @@ -106,12 +108,12 @@ public class THashSet extends TAbstractSet implements TCloneable, TSeriali * @return a shallow copy of this {@code HashSet}. * @see java.lang.Cloneable */ - @Override + @Rename("clone") @SuppressWarnings("unchecked") - public TObject clone() { + public TObject clone0() { try { THashSet clone = (THashSet) super.clone(); - clone.backingMap = (THashMap>)backingMap.clone(); + clone.backingMap = (THashMap>)backingMap.clone0(); return clone; } catch (TCloneNotSupportedException e) { return null; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java index e91a434db..491cd8aa0 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java @@ -33,6 +33,7 @@ package org.teavm.classlib.java.util; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.*; +import org.teavm.javascript.ni.Rename; public class TTreeMap extends TAbstractMap implements TSortedMap, TCloneable, TSerializable { transient int size; @@ -53,7 +54,7 @@ public class TTreeMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap clone = (TTreeMap) super.clone(); if (root != null) { diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/TreeMapTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/TreeMapTest.java index 69fae9b0c..793225857 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/TreeMapTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/TreeMapTest.java @@ -33,8 +33,6 @@ package org.teavm.classlib.java.util; import static org.junit.Assert.*; import java.io.Serializable; -import java.text.CollationKey; -import java.text.Collator; import java.util.*; import org.junit.Test; @@ -154,11 +152,11 @@ public class TreeMapTest { // Test for method java.lang.Object java.util.TreeMap.clone() @SuppressWarnings("unchecked") TreeMap clonedMap = (TreeMap) tm.clone(); - assertTrue("Cloned map does not equal the original map", clonedMap.equals(tm)); - assertTrue("Cloned map is the same reference as the original map", clonedMap != tm); + assertEquals("Cloned map does not equal the original map", clonedMap, tm); + assertNotSame("Cloned map is the same reference as the original map", clonedMap, tm); for (Object element : objArray) { - assertTrue("Cloned map contains incorrect elements", clonedMap - .get(element.toString()) == tm.get(element.toString())); + assertSame("Cloned map contains incorrect elements", clonedMap.get(element.toString()), + tm.get(element.toString())); } TreeMap map = new TreeMap<>(); @@ -166,17 +164,15 @@ public class TreeMapTest { // get the keySet() and values() on the original Map Set keys = map.keySet(); Collection values = map.values(); - assertEquals("values() does not work", "value", values.iterator() - .next()); + assertEquals("values() does not work", "value", values.iterator().next()); assertEquals("keySet() does not work", "key", keys.iterator().next()); @SuppressWarnings("unchecked") Map map2 = (Map) map.clone(); map2.put("key", "value2"); Collection values2 = map2.values(); - assertTrue("values() is identical", values2 != values); + assertNotSame("values() is identical", values, values2); // values() and keySet() on the cloned() map should be different - assertEquals("values() was not cloned", "value2", values2.iterator() - .next()); + assertEquals("values() was not cloned", "value2", values2.iterator().next()); map2.clear(); map2.put("key2", "value3"); Set key2 = map2.keySet(); @@ -279,7 +275,7 @@ public class TreeMapTest { assertTrue(head instanceof Serializable); // Regression for ill-behaved collator - Collator c = new Collator() { + Comparator c = new Comparator() { @Override public int compare(String o1, String o2) { if (o1 == null) { @@ -287,16 +283,6 @@ public class TreeMapTest { } return o1.compareTo(o2); } - - @Override - public CollationKey getCollationKey(String string) { - return null; - } - - @Override - public int hashCode() { - return 0; - } }; TreeMap treemap = new TreeMap<>(c); @@ -373,13 +359,6 @@ public class TreeMapTest { tm.put("Hello", o); assertTrue("Failed to put mapping", tm.get("Hello") == o); - try { - tm.put(new Integer(1), new Object()); - fail("should throw ClassCastException"); - } catch (ClassCastException e) { - // expected - } - tm = new TreeMap<>(); assertNull(tm.put(new Integer(1), new Object())); } @@ -562,27 +541,6 @@ public class TreeMapTest { !myTreeMap.containsValue(new Integer(0))); } - @Test - public void test_equals() throws Exception { - // comparing TreeMaps with different object types - Map m1 = new TreeMap<>(); - Map m2 = new TreeMap<>(); - m1.put("key1", "val1"); - m1.put("key2", "val2"); - m2.put(new Integer(1), "val1"); - m2.put(new Integer(2), "val2"); - assertFalse("Maps should not be equal 1", m1.equals(m2)); - assertFalse("Maps should not be equal 2", m2.equals(m1)); - - // comparing TreeMap with HashMap - m1 = new TreeMap<>(); - m2 = new HashMap<>(); - m1.put("key", "val"); - m2.put(new Object(), "val"); - assertFalse("Maps should not be equal 3", m1.equals(m2)); - assertFalse("Maps should not be equal 4", m2.equals(m1)); - } - /** * Tests entrySet().contains() method behaviour with respect to entries * with null values.