Search in sources :

Example 6 with TypeInfo

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

the class VectorTypeSerializerTest method testSerializationConstant.

@Test
void testSerializationConstant() {
    TestObject2 a = new TestObject2();
    a.v1 = new Vector3f(1.0f, 2.0f, 3.0f);
    a.v2 = new Vector4f(1.0f, 2.0f, 3.0f, 5.0f);
    a.v3 = new Vector2f(1.0f, 2.0f);
    byte[] data = gsonSerializer.serialize(a, new TypeInfo<TestObject2>() {
    }).get();
    TestObject2 o = gsonSerializer.deserialize(new TypeInfo<TestObject2>() {
    }, data).get();
    assertEquals(new Vector3f(1.0f, 2.0f, 3.0f), o.v1, .00001f);
    assertEquals(new Vector4f(1.0f, 2.0f, 3.0f, 5.0f), o.v2, .00001f);
    assertEquals(new Vector2f(1.0f, 2.0f), o.v3, .00001f);
}
Also used : Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test) ModuleEnvironmentTest(org.terasology.engine.ModuleEnvironmentTest)

Example 7 with TypeInfo

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

the class BlockAreaTypeHandlerTest method testGsonSerialization.

@Test
public void testGsonSerialization() throws IOException {
    TestObject a = new TestObject();
    a.b1 = new BlockArea(-1, -1, 0, 0);
    a.b2 = new BlockArea(0, 0, 1, 1);
    byte[] data = gsonSerializer.serialize(a, new TypeInfo<TestObject>() {
    }).get();
    TestObject o = gsonSerializer.deserialize(new TypeInfo<TestObject>() {
    }, data).get();
    assertEquals(new BlockArea(-1, -1, 0, 0), o.b1);
    assertEquals(new BlockArea(0, 0, 1, 1), o.b2);
}
Also used : BlockArea(org.terasology.engine.world.block.BlockArea) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test) ModuleEnvironmentTest(org.terasology.engine.ModuleEnvironmentTest)

Example 8 with TypeInfo

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

the class ReflectionUtilTest method testGetParameterForUnboundGenericInterface.

@Test
public void testGetParameterForUnboundGenericInterface() {
    Type parameter = ReflectionUtil.getTypeParameterForSuper(new TypeInfo<UnboundInterfaceImplementor<?>>() {
    }.getType(), CopyStrategy.class, 0);
    assertTrue(parameter instanceof WildcardType);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test)

Example 9 with TypeInfo

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

the class ReflectionUtilTest method testResolveGenericArray.

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

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

Example 10 with TypeInfo

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

the class ReflectionUtilTest method testResolveParameterizedType.

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

        private CopyStrategy<T> t;
    }
    TypeInfo<SomeClass<Float>> typeInfo = new TypeInfo<SomeClass<Float>>() {
    };
    Type resolvedFieldType = ReflectionUtil.resolveType(typeInfo.getType(), typeInfo.getRawType().getDeclaredFields()[0].getGenericType());
    assertEquals(new TypeInfo<CopyStrategy<Float>>() {
    }.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)

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