Fix migration of EntityInternalMap

This commit is contained in:
Magnus Ulf Jørgensen 2018-04-14 11:27:11 +02:00
parent a5747f102b
commit 8274b210ff
2 changed files with 16 additions and 2 deletions

View File

@ -2,11 +2,13 @@ package com.massivecraft.massivecore.store.migrator;
import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.util.ReflectionUtil; import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName; import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
import com.massivecraft.massivecore.xlib.gson.reflect.TypeToken;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -138,6 +140,15 @@ public class MigratorUtil
// GET // GET
public static Type getJsonRepresentation(Type actualType) public static Type getJsonRepresentation(Type actualType)
{ {
if (actualType instanceof ParameterizedType && ((ParameterizedType) actualType).getRawType().equals(EntityInternalMap.class))
{
ParameterizedType parameterizedType = (ParameterizedType) actualType;
Type valueType = parameterizedType.getActualTypeArguments()[0];
Type keyType = String.class;
return TypeToken.getParameterized(Map.class, keyType, valueType).getType();
}
if (!jsonRepresentation.containsKey(actualType)) if (!jsonRepresentation.containsKey(actualType))
{ {
return actualType; return actualType;
@ -237,13 +248,13 @@ public class MigratorUtil
public static boolean migrateFields(Type type, JsonObject object) public static boolean migrateFields(Type type, JsonObject object)
{ {
if (type == null) throw new NullPointerException("entityClass"); if (type == null) throw new NullPointerException("type");
if (object == null) throw new NullPointerException("object"); if (object == null) throw new NullPointerException("object");
Class<?> entityClass = getClassType(type); Class<?> entityClass = getClassType(type);
// We can't lookup the generic type of a map, so trying to convert the fields is useless // We can't lookup the generic type of a map, so trying to convert the fields is useless
// Furthermore maps are stored as jsopnobjects, and we can't treat them like other objects // Furthermore maps are stored as jsonobjects, and we can't treat them like other objects
// because their "fields" are dynamically made and can't be looked up with reflection // because their "fields" are dynamically made and can't be looked up with reflection
if (Map.class.isAssignableFrom(entityClass)) return false; if (Map.class.isAssignableFrom(entityClass)) return false;
@ -266,6 +277,7 @@ public class MigratorUtil
superClass = entityClass; superClass = entityClass;
} }
} }
if (superClass == null) throw new RuntimeException(type.getTypeName() + " : " + name);
Type elementType = ReflectionUtil.getField(superClass, name).getGenericType(); Type elementType = ReflectionUtil.getField(superClass, name).getGenericType();
migrated = migrate(elementType, element) | migrated; migrated = migrate(elementType, element) | migrated;
} }

View File

@ -293,6 +293,8 @@ public class ReflectionUtil
public static Field getField(Class<?> clazz, String name) public static Field getField(Class<?> clazz, String name)
{ {
if (clazz == null) throw new NullPointerException("clazz");
if (name == null) throw new NullPointerException("name");
try try
{ {
Field ret = clazz.getDeclaredField(name); Field ret = clazz.getDeclaredField(name);