Search in sources :

Example 11 with TypeInfo

use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.

the class ReflectionUtilTest method testResolveTypeVariable.

@Test
public void testResolveTypeVariable() {
    class SomeClass<T> {

        private T t;
    }
    TypeInfo<SomeClass<Float>> typeInfo = new TypeInfo<SomeClass<Float>>() {
    };
    Type resolvedFieldType = ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[0].getGenericType());
    assertEquals(Float.class, resolvedFieldType);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test)

Example 12 with TypeInfo

use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.

the class ReflectionUtilTest method testResolveNothing.

@Test
public void testResolveNothing() {
    class SomeClass {

        private CopyStrategy<Integer> t;

        private String o;
    }
    TypeInfo<SomeClass> typeInfo = new TypeInfo<SomeClass>() {
    };
    Type resolvedFieldType = ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[0].getGenericType());
    assertEquals(new TypeInfo<CopyStrategy<Integer>>() {
    }.getType(), resolvedFieldType);
    resolvedFieldType = ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[1].getGenericType());
    assertEquals(TypeInfo.of(String.class).getType(), resolvedFieldType);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) CopyStrategy(org.terasology.reflection.copy.CopyStrategy) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test)

Example 13 with TypeInfo

use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.

the class ReflectionUtilTest method testResolveRawParameterizedType.

@Test
public void testResolveRawParameterizedType() {
    class SomeClass<T> {

        private CopyStrategy<T> t;

        private T o;
    }
    TypeInfo<SomeClass> typeInfo = new TypeInfo<SomeClass>() {
    };
    Type resolvedFieldType = ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[0].getGenericType());
    assertEquals(new TypeInfo<CopyStrategy<Object>>() {
    }.getType(), resolvedFieldType);
    resolvedFieldType = ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[1].getGenericType());
    assertEquals(TypeInfo.of(Object.class).getType(), resolvedFieldType);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) CopyStrategy(org.terasology.reflection.copy.CopyStrategy) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test)

Example 14 with TypeInfo

use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.

the class AutoConfigTypeHandler method deserialize.

@Override
public Optional<AutoConfig> deserialize(PersistedData data) {
    try {
        AutoConfig config = typeInfo.getRawType().getConstructor().newInstance();
        Map<String, Field> settingFields = AutoConfig.getSettingFieldsIn(typeInfo.getRawType()).stream().collect(Collectors.toMap(Field::getName, f -> f));
        for (Map.Entry<String, PersistedData> entry : data.getAsValueMap().entrySet()) {
            Field settingField = settingFields.get(entry.getKey());
            if (settingField == null) {
                logger.warn("Cannot to find setting field with name [{}]", entry.getKey());
                continue;
            }
            try {
                Setting setting = (Setting) settingField.get(config);
                Optional<TypeHandler<Object>> typeHandler = typeHandlerLibrary.getTypeHandler(setting.getValueType());
                if (typeHandler.isPresent()) {
                    Optional<Object> value = typeHandler.get().deserialize(entry.getValue());
                    if (value.isPresent()) {
                        setting.set(value.get());
                    } else {
                        logger.error("Cannot deserialize value [{}] to type [{}]", entry.getValue(), setting.getValueType());
                    }
                } else {
                    logger.error("Cannot deserialize type [{}]", setting.getValueType());
                }
            } catch (IllegalAccessException e) {
            // ignore, AutoConfig.getSettingsFieldsIn return public fields.
            }
        }
        return Optional.of(config);
    } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
        logger.error("Cannot create type [" + typeInfo + "] for deserialization", e);
    }
    return Optional.empty();
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) TypeInfo(org.terasology.reflection.TypeInfo) Set(java.util.Set) HashMap(java.util.HashMap) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) TypeHandlerLibrary(org.terasology.persistence.typeHandling.TypeHandlerLibrary) Map(java.util.Map) Optional(java.util.Optional) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) PersistedData(org.terasology.persistence.typeHandling.PersistedData) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with TypeInfo

use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.

the class RecordedEventSerializer method deserializeRecordedEvents.

/**
 * Deserializes RecordedEvent's list.
 *
 * @param filePath path where the data should be saved.
 */
public List<RecordedEvent> deserializeRecordedEvents(String filePath) {
    List<RecordedEvent> events = new ArrayList<>();
    try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
        List<RecordedEvent> recordedEvents = serializer.deserialize(new TypeInfo<List<RecordedEvent>>() {
        }, fileInputStream).get();
        events.addAll(recordedEvents);
    } catch (SerializationException | IOException e) {
        logger.error("Error while serializing recorded events", e);
    }
    return events;
}
Also used : SerializationException(org.terasology.persistence.typeHandling.SerializationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TypeInfo(org.terasology.reflection.TypeInfo) FileInputStream(java.io.FileInputStream)

Aggregations

TypeInfo (org.terasology.reflection.TypeInfo)19 Test (org.junit.jupiter.api.Test)11 Type (java.lang.reflect.Type)10 ParameterizedType (java.lang.reflect.ParameterizedType)9 GenericArrayType (java.lang.reflect.GenericArrayType)8 WildcardType (java.lang.reflect.WildcardType)8 Optional (java.util.Optional)4 Set (java.util.Set)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 CopyStrategy (org.terasology.reflection.copy.CopyStrategy)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Constructor (java.lang.reflect.Constructor)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2