Search in sources :

Example 1 with ArrayTypeHandler

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

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)1 Type (java.lang.reflect.Type)1 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)1 ArrayTypeHandler (org.terasology.persistence.typeHandling.coreTypes.ArrayTypeHandler)1 RuntimeDelegatingTypeHandler (org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)1