Search in sources :

Example 1 with RuntimeDelegatingTypeHandler

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();
}
Also used : Field(java.lang.reflect.Field) ObjectFieldMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) ObjectFieldMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)

Example 2 with RuntimeDelegatingTypeHandler

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);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) GenericArrayType(java.lang.reflect.GenericArrayType) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) ArrayTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ArrayTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler) ArrayTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ArrayTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)

Example 3 with RuntimeDelegatingTypeHandler

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);
}
Also used : CollectionTypeHandler(org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler) Type(java.lang.reflect.Type) CollectionCopyConstructor(org.terasology.reflection.reflect.CollectionCopyConstructor) Collection(java.util.Collection) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) CollectionTypeHandler(org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)

Example 4 with RuntimeDelegatingTypeHandler

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));
    }
}
Also used : StringMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.StringMapTypeHandler) Type(java.lang.reflect.Type) GenericMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.GenericMapTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) GenericMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.GenericMapTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler) StringMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.StringMapTypeHandler) Map(java.util.Map) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)

Aggregations

TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)4 RuntimeDelegatingTypeHandler (org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)4 Type (java.lang.reflect.Type)3 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Collection (java.util.Collection)1 Map (java.util.Map)1 ArrayTypeHandler (org.terasology.persistence.typeHandling.coreTypes.ArrayTypeHandler)1 CollectionTypeHandler (org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler)1 GenericMapTypeHandler (org.terasology.persistence.typeHandling.coreTypes.GenericMapTypeHandler)1 ObjectFieldMapTypeHandler (org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler)1 StringMapTypeHandler (org.terasology.persistence.typeHandling.coreTypes.StringMapTypeHandler)1 CollectionCopyConstructor (org.terasology.reflection.reflect.CollectionCopyConstructor)1