fix for Set as value of Map, make sure the set is new
@@ -5,12 +5,10 @@ import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import org.apache.log4j.Logger;
+import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
import java.io.Serializable;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Modifier;
+import java.lang.reflect.*;
import java.util.*;
/**
@@ -101,8 +99,7 @@ public abstract class Watcher implements Serializable {
if (parameterType.getSimpleName().equalsIgnoreCase("boolean")) {
args[index] = false;
}
- }
- else {
+ } else {
args[index] = null;
}
@@ -119,8 +116,22 @@ public abstract class Watcher implements Serializable {
((Set) field.get(watcher)).clear();
((Set) field.get(watcher)).addAll((Set) field.get(this));
} else if (field.getType() == Map.class) {
- ((Map) field.get(watcher)).clear();
+ Map target = ((Map) field.get(watcher));
+ target.clear();
+ Map source = (Map) field.get(this);
+
+ ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
+ Type valueType = parameterizedType.getActualTypeArguments()[1];
+ if (valueType instanceof ParameterizedTypeImpl && ((ParameterizedTypeImpl) valueType).getRawType().getSimpleName().contains("Set")) {
+ source.entrySet().forEach(kv -> {
+ Object key = ((Map.Entry) kv).getKey();
+ Set value = (Set) ((Map.Entry) kv).getValue();
+ target.put(key, new HashSet<>(value));
+ });
+ }
+ else {
((Map) field.get(watcher)).putAll((Map) field.get(this));
+ }
} else if (field.getType() == List.class) {
((List) field.get(watcher)).clear();
((List) field.get(watcher)).addAll((List) field.get(this));
