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);
}
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);
}
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);
}
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();
}
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;
}
Aggregations