Search in sources :

Example 1 with CollectionTypeHandler

use of org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler 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)

Aggregations

Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)1 CollectionTypeHandler (org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler)1 RuntimeDelegatingTypeHandler (org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)1 CollectionCopyConstructor (org.terasology.reflection.reflect.CollectionCopyConstructor)1