use of org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler in project Terasology by MovingBlocks.
the class ObjectFieldMapTypeHandlerFactory method create.
@Override
public <T> Optional<TypeHandler<T>> create(TypeInfo<T> typeInfo, TypeHandlerContext context) {
Class<? super T> typeClass = typeInfo.getRawType();
if (!Modifier.isAbstract(typeClass.getModifiers()) && !typeClass.isLocalClass() && !(typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) {
Map<Field, TypeHandler<?>> fieldTypeHandlerMap = Maps.newLinkedHashMap();
getResolvedFields(typeInfo).forEach((field, fieldType) -> {
Optional<TypeHandler<?>> declaredFieldTypeHandler = context.getTypeHandlerLibrary().getTypeHandler(fieldType);
TypeInfo<?> fieldTypeInfo = TypeInfo.of(fieldType);
fieldTypeHandlerMap.put(field, new RuntimeDelegatingTypeHandler(declaredFieldTypeHandler.orElse(null), fieldTypeInfo, context));
});
ObjectFieldMapTypeHandler<T> mappedHandler = new ObjectFieldMapTypeHandler<>(constructorLibrary.get(typeInfo), fieldTypeHandlerMap);
return Optional.of(mappedHandler);
}
return Optional.empty();
}
use of org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler in project Terasology by MovingBlocks.
the class ArrayTypeHandlerFactory method create.
@Override
public <T> Optional<TypeHandler<T>> create(TypeInfo<T> typeInfo, TypeHandlerContext context) {
Type type = typeInfo.getType();
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
return Optional.empty();
}
Type elementType = type instanceof GenericArrayType ? ((GenericArrayType) type).getGenericComponentType() : ((Class<?>) type).getComponentType();
TypeInfo<?> elementTypeInfo = TypeInfo.of(elementType);
Optional<TypeHandler<?>> declaredElementTypeHandler = context.getTypeHandlerLibrary().getTypeHandler(elementType);
@SuppressWarnings("unchecked") TypeHandler<?> elementTypeHandler = new RuntimeDelegatingTypeHandler(declaredElementTypeHandler.orElse(null), elementTypeInfo, context);
@SuppressWarnings("unchecked") TypeHandler<T> typeHandler = new ArrayTypeHandler(elementTypeHandler, elementTypeInfo);
return Optional.of(typeHandler);
}
use of org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler in project Terasology by MovingBlocks.
the class CollectionTypeHandlerFactory method create.
@Override
public <T> Optional<TypeHandler<T>> create(TypeInfo<T> typeInfo, TypeHandlerContext context) {
Class<? super T> rawType = typeInfo.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return Optional.empty();
}
Type elementType = ReflectionUtil.getTypeParameterForSuper(typeInfo.getType(), Collection.class, 0);
if (elementType == null) {
LOGGER.error("Collection is not parameterized and cannot be serialized");
return Optional.empty();
}
TypeInfo<?> elementTypeInfo = TypeInfo.of(elementType);
Optional<TypeHandler<?>> declaredElementTypeHandler = context.getTypeHandlerLibrary().getTypeHandler(elementType);
@SuppressWarnings("unchecked") TypeHandler<?> elementTypeHandler = new RuntimeDelegatingTypeHandler(declaredElementTypeHandler.orElse(null), elementTypeInfo, context);
CollectionCopyConstructor constructor = ConstructorLibrary.getCollectionCopyConstructor((TypeInfo) typeInfo);
@SuppressWarnings("unchecked") TypeHandler<T> typeHandler = new CollectionTypeHandler(elementTypeHandler, constructor);
return Optional.of(typeHandler);
}
use of org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler in project Terasology by MovingBlocks.
the class MapTypeHandlerFactory method create.
@SuppressWarnings("unchecked")
@Override
public <T> Optional<TypeHandler<T>> create(TypeInfo<T> typeInfo, TypeHandlerContext context) {
if (!Map.class.isAssignableFrom(typeInfo.getRawType())) {
return Optional.empty();
}
Type keyType = ReflectionUtil.getTypeParameterForSuper(typeInfo.getType(), Map.class, 0);
Type valueType = ReflectionUtil.getTypeParameterForSuper(typeInfo.getType(), Map.class, 1);
if (valueType == null) {
LOGGER.error("Map is not parameterized and cannot be serialized");
return Optional.empty();
}
Optional<TypeHandler<?>> declaredValueTypeHandler = context.getTypeHandlerLibrary().getTypeHandler(valueType);
TypeInfo<?> valueTypeInfo = TypeInfo.of(valueType);
@SuppressWarnings("unchecked") TypeHandler<?> valueTypeHandler = new RuntimeDelegatingTypeHandler(declaredValueTypeHandler.orElse(null), valueTypeInfo, context);
if (String.class.equals(keyType)) {
return Optional.of((TypeHandler<T>) new StringMapTypeHandler<>(valueTypeHandler));
} else {
Optional<TypeHandler<?>> declaredKeyTypeHandler = context.getTypeHandlerLibrary().getTypeHandler(keyType);
TypeInfo<?> keyTypeInfo = TypeInfo.of(keyType);
@SuppressWarnings("unchecked") TypeHandler<?> keyTypeHandler = new RuntimeDelegatingTypeHandler(declaredKeyTypeHandler.orElse(null), keyTypeInfo, context);
return Optional.of((TypeHandler<T>) new GenericMapTypeHandler<>(keyTypeHandler, valueTypeHandler));
}
}
Aggregations