CLeanup MigratorUtil

This commit is contained in:
Magnus Ulf Jørgensen 2018-04-16 11:39:58 +02:00
parent 7c641be609
commit c9236895a4

View File

@ -2,6 +2,7 @@ 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.command.Parameter;
import com.massivecraft.massivecore.store.EntityInternalMap; 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;
@ -141,7 +142,7 @@ 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)) if (isParameterizedInternalMap(actualType))
{ {
ParameterizedType parameterizedType = (ParameterizedType) actualType; ParameterizedType parameterizedType = (ParameterizedType) actualType;
Type valueType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[0];
@ -157,6 +158,14 @@ public class MigratorUtil
return jsonRepresentation.get(actualType); return jsonRepresentation.get(actualType);
} }
private static boolean isParameterizedInternalMap(Type type)
{
if (!(type instanceof ParameterizedType)) return false;
ParameterizedType parameterizedType = (ParameterizedType) type;
return parameterizedType.getRawType().equals(EntityInternalMap.class);
}
// -------------------------------------------- // // -------------------------------------------- //
// MIGRATE // MIGRATE
@ -174,49 +183,71 @@ public class MigratorUtil
// JsonObject is if it is an object or a map // JsonObject is if it is an object or a map
if (jsonElement.isJsonObject()) if (jsonElement.isJsonObject())
{ {
JsonObject object = jsonElement.getAsJsonObject();
// For maps we loop over all the content and migrate the values // For maps we loop over all the content and migrate the values
if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType))) if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType)))
{ {
ParameterizedType parameterizedType = (ParameterizedType) jsonType; return migrateSimpleMap(getParameterizedType(jsonType), jsonElement.getAsJsonObject());
Type keyType = parameterizedType.getActualTypeArguments()[0]; }
// For objects we update the object itself and its fields
return migrateObject(realType, jsonType, object);
}
// Arrays are for arrays, collections and maps where the key is complex
if (jsonElement.isJsonArray())
{
if (jsonType == null) throw new RuntimeException("jsonType is null");
Class<?> clazz = getClassType(jsonType);
if (clazz == null) throw new RuntimeException("clazz is null");
JsonArray array = jsonElement.getAsJsonArray();
// So if it is a map with a complex key it is represented as an array
if (Map.class.isAssignableFrom(clazz))
{
return migrateComplexMap(getParameterizedType(jsonType), array);
}
// Entries are also serialised as list
if (Entry.class.isAssignableFrom(clazz))
{
return migrateEntry(getParameterizedType(jsonType), array);
}
return migrateList(clazz, jsonType, array);
}
throw new RuntimeException();
}
private static boolean migrateSimpleMap(ParameterizedType parameterizedType, JsonObject map)
{
Type valueType = parameterizedType.getActualTypeArguments()[1]; Type valueType = parameterizedType.getActualTypeArguments()[1];
JsonObject object = jsonElement.getAsJsonObject();
boolean migrated = false; boolean migrated = false;
for (Entry<String, JsonElement> entry : object.entrySet()) for (Entry<String, JsonElement> entry : map.entrySet())
{ {
migrated = migrate(valueType, entry.getValue()) | migrated; migrated = migrate(valueType, entry.getValue()) | migrated;
} }
return migrated; return migrated;
} }
// For objects we update the object itself and its fields private static boolean migrateObject(Type realType, Type jsonType, JsonObject object)
{
boolean migrated = false; boolean migrated = false;
JsonObject object = jsonElement.getAsJsonObject();
Type classType = jsonType != null ? jsonType : realType; Type classType = jsonType != null ? jsonType : realType;
migrated = migrateClass(classType, object) | migrated; migrated = migrateClass(classType, object) | migrated;
if (jsonType != null) migrated = migrateFields(jsonType, object) | migrated; if (jsonType != null) migrated = migrateFields(jsonType, object) | migrated;
return migrated; return migrated;
} }
// Arrays are for arrays, collections and maps where the key is complex
if (jsonElement.isJsonArray())
{
if (jsonType == null) throw new RuntimeException("jsonType is null"); private static boolean migrateComplexMap(ParameterizedType parameterizedType, JsonArray array)
Class<?> clazz = getClassType(jsonType);
if (clazz == null) throw new RuntimeException("clazz is null");
// So if it is a map with a complex key it is represented as an array
if (Map.class.isAssignableFrom(clazz))
{ {
ParameterizedType parameterizedType = (ParameterizedType) jsonType;
Type keyType = parameterizedType.getActualTypeArguments()[0]; Type keyType = parameterizedType.getActualTypeArguments()[0];
Type valueType = parameterizedType.getActualTypeArguments()[1]; Type valueType = parameterizedType.getActualTypeArguments()[1];
JsonArray array = jsonElement.getAsJsonArray();
boolean migrated = false; boolean migrated = false;
for (JsonElement element : array) for (JsonElement element : array)
{ {
@ -231,15 +262,11 @@ public class MigratorUtil
return migrated; return migrated;
} }
// Entries are also serialised as list private static boolean migrateEntry(ParameterizedType parameterizedType, JsonArray array)
if (Entry.class.isAssignableFrom(clazz))
{ {
ParameterizedType parameterizedType = (ParameterizedType) jsonType;
Type keyType = parameterizedType.getActualTypeArguments()[0]; Type keyType = parameterizedType.getActualTypeArguments()[0];
Type valueType = parameterizedType.getActualTypeArguments()[1]; Type valueType = parameterizedType.getActualTypeArguments()[1];
JsonArray array = jsonElement.getAsJsonArray();
JsonElement key = array.get(0); JsonElement key = array.get(0);
JsonElement value = array.get(1); JsonElement value = array.get(1);
@ -249,6 +276,9 @@ public class MigratorUtil
return migrated; return migrated;
} }
private static boolean migrateList(Class<?> clazz, Type jsonType, JsonArray array)
{
Type elementType = null; Type elementType = null;
if (clazz.isArray()) if (clazz.isArray())
@ -258,26 +288,23 @@ public class MigratorUtil
} }
else if (Collection.class.isAssignableFrom(clazz)) else if (Collection.class.isAssignableFrom(clazz))
{ {
ParameterizedType parameterizedType = (ParameterizedType) jsonType; ParameterizedType parameterizedType = getParameterizedType(jsonType);
elementType = parameterizedType.getActualTypeArguments()[0]; elementType = parameterizedType.getActualTypeArguments()[0];
if (elementType == null) throw new RuntimeException("elementType is null"); if (elementType == null) throw new RuntimeException("elementType is null");
} }
else else
{ {
throw new RuntimeException("no elementType specified"); throw new RuntimeException("no elementType found");
} }
boolean migrated = false; boolean migrated = false;
for (JsonElement element1 : jsonElement.getAsJsonArray()) for (JsonElement element1 : array)
{ {
migrated = migrate(elementType, element1) | migrated; migrated = migrate(elementType, element1) | migrated;
} }
return migrated; return migrated;
} }
throw new RuntimeException();
}
public static boolean migrateClass(Type type, JsonObject entity) public static boolean migrateClass(Type type, JsonObject entity)
{ {
if (type == null) throw new NullPointerException("entityClass"); if (type == null) throw new NullPointerException("entityClass");
@ -424,4 +451,36 @@ public class MigratorUtil
} }
} }
public static ParameterizedType getParameterizedType(Type jsonType)
{
if (jsonType instanceof ParameterizedType) return (ParameterizedType) jsonType;
if (jsonType instanceof Class<?>)
{
Class<?> clazz = (Class<?>) jsonType;
Type superClass = clazz.getGenericSuperclass();
// The exception throwing and catching is in order to catch the inheritance structure
// in case something is more complex than expected.
if (superClass == null)
{
System.out.println("Failed for: " + jsonType.getTypeName());
throw new RuntimeException();
}
try
{
ParameterizedType ret = getParameterizedType(superClass);
return ret;
}
catch (RuntimeException ex)
{
System.out.println("Failed for: " + jsonType.getTypeName());
throw ex;
}
}
throw new RuntimeException("Neither ParameterizedType nor Class: " + jsonType.getTypeName());
}
} }