classlib: properly duplicate inner map when cloning TreeMap

This commit is contained in:
Bernd Busse 2023-07-27 20:59:30 +02:00 committed by Alexey Andreev
parent 2318caad7b
commit 6a398c9b8d
2 changed files with 17 additions and 3 deletions

View File

@ -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>> {

View File

@ -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