use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.
the class AssetTypeHandlerFactoryTest method testCreate.
@Test
public void testCreate() {
TypeHandlerFactory factory = new AssetTypeHandlerFactory();
List<TypeInfo<? extends Asset>> typesToTest = Lists.newArrayList(TypeInfo.of(Texture.class), TypeInfo.of(UIElement.class), TypeInfo.of(StaticSound.class), TypeInfo.of(StreamingSound.class));
for (TypeInfo<? extends Asset> typeInfo : typesToTest) {
Optional<? extends TypeHandler<? extends Asset>> typeHandler = factory.create(typeInfo, null);
assertTrue(typeHandler.isPresent());
assertTrue(typeHandler.get() instanceof AssetTypeHandler);
}
}
use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.
the class ObjectLayoutBuilder method populateConstructorParameters.
private void populateConstructorParameters(Binding<T> binding, ColumnLayout parameterLayout, UIButton createInstanceButton, Binding<Constructor<T>> selectedConstructor) {
parameterLayout.removeAllWidgets();
Parameter[] parameters = selectedConstructor.get().getParameters();
List<TypeInfo<?>> parameterTypes = Arrays.stream(parameters).map(Parameter::getParameterizedType).map(parameterType -> ReflectionUtil.resolveType(type.getType(), parameterType)).map(TypeInfo::of).collect(Collectors.toList());
List<Binding<?>> argumentBindings = parameterTypes.stream().map(parameterType -> new DefaultBinding<>(Defaults.defaultValue(parameterType.getRawType()))).collect(Collectors.toList());
createInstanceButton.subscribe(widget -> {
Object[] arguments = argumentBindings.stream().map(Binding::get).toArray();
try {
binding.set(selectedConstructor.get().newInstance(arguments));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
if (argumentBindings.isEmpty()) {
// TODO: Translate
parameterLayout.addWidget(new UILabel("Constructor has no parameters"));
return;
}
ColumnLayout parametersExpandableLayout = WidgetUtil.createExpandableLayout(// TODO: Translate
"Constructor Parameters", this::createDefaultLayout, layout -> {
for (int i = 0; i < parameterTypes.size(); i++) {
TypeInfo<?> parameterType = parameterTypes.get(i);
Binding<?> argumentBinding = argumentBindings.get(i);
Parameter parameter = parameters[i];
Optional<UIWidget> optionalWidget = library.getBaseTypeWidget((Binding) argumentBinding, parameterType);
if (!optionalWidget.isPresent()) {
LOGGER.warn("Could not create widget for parameter of type {} of constructor {}", parameter, selectedConstructor.get());
continue;
}
UIWidget widget = optionalWidget.get();
String parameterLabelText = ReflectionUtil.typeToString(parameterType.getType(), true);
layout.addWidget(WidgetUtil.labelize(widget, parameterLabelText, LABEL_WIDGET_ID));
}
}, this::createDefaultLayout);
parameterLayout.addWidget(parametersExpandableLayout);
}
use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.
the class ReflectionUtilTest method testGetClassOfTypeWildcard.
@Test
public void testGetClassOfTypeWildcard() {
class C<T> {
}
ParameterizedType cType = (ParameterizedType) new TypeInfo<C<?>>() {
}.getType();
Type wildcardType = cType.getActualTypeArguments()[0];
assertEquals(Object.class, ReflectionUtil.getRawType(wildcardType));
}
use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.
the class ReflectionUtilTest method testResolveWildcardType.
@Test
public void testResolveWildcardType() {
class SomeClass<T, U> {
private CopyStrategy<? extends T> t;
private CopyStrategy<? super U> u;
}
TypeInfo<SomeClass<Float, Integer>> typeInfo = new TypeInfo<SomeClass<Float, Integer>>() {
};
ParameterizedType resolvedFieldType = (ParameterizedType) ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[0].getGenericType());
WildcardType resolvedWildcardType = (WildcardType) resolvedFieldType.getActualTypeArguments()[0];
assertEquals(Float.class, resolvedWildcardType.getUpperBounds()[0]);
resolvedFieldType = (ParameterizedType) ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[1].getGenericType());
resolvedWildcardType = (WildcardType) resolvedFieldType.getActualTypeArguments()[0];
assertEquals(Integer.class, resolvedWildcardType.getLowerBounds()[0]);
}
use of org.terasology.reflection.TypeInfo in project Terasology by MovingBlocks.
the class RuntimeDelegatingTypeHandler method serializeNonNull.
@SuppressWarnings("unchecked")
@Override
public PersistedData serializeNonNull(T value, PersistedDataSerializer serializer) {
// If primitive, don't go looking for the runtime type, serialize as is
if (typeInfo.getRawType().isPrimitive() || Number.class.isAssignableFrom(typeInfo.getRawType())) {
if (delegateHandler != null) {
return delegateHandler.serialize(value, serializer);
}
LOGGER.error("Primitive '{}' does not have a TypeHandler", typeInfo);
return serializer.serializeNull();
}
TypeHandler<T> chosenHandler = delegateHandler;
Type runtimeType = getRuntimeTypeIfMoreSpecific(value);
if (!typeInfo.getType().equals(runtimeType)) {
Optional<TypeHandler<?>> runtimeTypeHandler = typeHandlerLibrary.getTypeHandler(runtimeType);
chosenHandler = (TypeHandler<T>) runtimeTypeHandler.map(typeHandler -> {
if (delegateHandler == null) {
return typeHandler;
}
if (!(typeHandler instanceof ObjectFieldMapTypeHandler) && typeHandler.getClass().equals(delegateHandler.getClass())) {
// use delegateHandler which might have more info
return delegateHandler;
}
if (!isDefaultTypeHandler(typeHandler)) {
// Custom handler for runtime type
return typeHandler;
}
if (!isDefaultTypeHandler(delegateHandler)) {
// Custom handler for specified type
return delegateHandler;
}
return typeHandler;
}).orElse(delegateHandler);
}
if (chosenHandler == null) {
LOGGER.warn("Could not find appropriate TypeHandler for runtime type '{}', " + "serializing as base type '{}'", runtimeType, typeInfo);
return serializeViaDelegate(value, serializer);
}
if (chosenHandler == delegateHandler) {
return serializeViaDelegate(value, serializer);
}
Map<String, PersistedData> typeValuePersistedDataMap = Maps.newLinkedHashMap();
Class<? extends T> subType = (Class<? extends T>) ReflectionUtil.getRawType(runtimeType);
String subTypeIdentifier = sandbox.getSubTypeIdentifier(subType, typeInfo.getRawType());
typeValuePersistedDataMap.put(TYPE_FIELD, serializer.serialize(subTypeIdentifier));
PersistedData serialized = chosenHandler.serialize(value, serializer);
// If the serialized representation is a Map, flatten it to include the class variable
if (serialized.isValueMap()) {
for (Map.Entry<String, PersistedData> entry : serialized.getAsValueMap().entrySet()) {
typeValuePersistedDataMap.put(entry.getKey(), entry.getValue());
}
} else {
typeValuePersistedDataMap.put(VALUE_FIELD, serialized);
}
return serializer.serialize(typeValuePersistedDataMap);
}
Aggregations