Search in sources :

Example 1 with ObjectFieldMapTypeHandler

use of org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler in project Terasology by MovingBlocks.

the class GsonTypeSerializationLibraryAdapterFactory method create.

@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Type rawType = type.getType();
    TypeHandler<T> typeHandler = (TypeHandler<T>) typeHandlerLibrary.getTypeHandler(rawType).orElse(null);
    if (typeHandler == null || typeHandler instanceof ObjectFieldMapTypeHandler) {
        return null;
    }
    return new GsonTypeHandlerAdapter<>(typeHandler, gson, type);
}
Also used : Type(java.lang.reflect.Type) ObjectFieldMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) ObjectFieldMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler)

Example 2 with ObjectFieldMapTypeHandler

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

Aggregations

TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)2 ObjectFieldMapTypeHandler (org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler)2 Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1 RuntimeDelegatingTypeHandler (org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)1