mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: properly duplicate inner map when cloning TreeMap
This commit is contained in:
parent
2318caad7b
commit
6a398c9b8d
|
@ -16,6 +16,7 @@
|
|||
package org.teavm.classlib.java.util;
|
||||
|
||||
import org.teavm.classlib.java.io.TSerializable;
|
||||
import org.teavm.classlib.java.lang.TCloneNotSupportedException;
|
||||
import org.teavm.classlib.java.lang.TCloneable;
|
||||
|
||||
public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TCloneable, TSerializable, TNavigableMap<K, V> {
|
||||
|
@ -558,10 +559,19 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TCloneable, TS
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object clone() {
|
||||
TTreeMap<?, ?> copy = (TTreeMap<?, ?>) super.clone();
|
||||
copy.cachedEntrySet = null;
|
||||
return copy;
|
||||
try {
|
||||
TTreeMap<K, V> map = (TTreeMap<K, V>) super.clone();
|
||||
map.root = null;
|
||||
map.modCount = 0;
|
||||
map.cachedEntrySet = null;
|
||||
map.putAll(this);
|
||||
|
||||
return map;
|
||||
} catch (TCloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class EntrySet<K, V> extends TAbstractSet<Entry<K, V>> implements TSequencedSet<Entry<K, V>> {
|
||||
|
|
|
@ -215,6 +215,10 @@ public class TreeMapTest {
|
|||
Set<Object> key2 = map2.keySet();
|
||||
assertTrue("keySet() is identical", key2 != keys);
|
||||
assertEquals("keySet() was not cloned", "key2", key2.iterator().next());
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<Object, Object> map3 = (Map<Object, Object>) map.clone();
|
||||
map3.put("key2", "value2");
|
||||
assertFalse("Original map modified through clone", map.containsKey("key2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue
Block a user