Search in sources :

Example 1 with CopyStrategy

use of org.terasology.reflection.copy.CopyStrategy 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]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) CopyStrategy(org.terasology.reflection.copy.CopyStrategy) TypeInfo(org.terasology.reflection.TypeInfo) Test(org.junit.jupiter.api.Test)

Example 2 with CopyStrategy

use of org.terasology.reflection.copy.CopyStrategy 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)

Example 3 with CopyStrategy

use of org.terasology.reflection.copy.CopyStrategy in project Terasology by MovingBlocks.

the class ReflectionUtilTest method testGetTypeParameterForGenericSupertypeInGenericSubclass.

@Test
public void testGetTypeParameterForGenericSupertypeInGenericSubclass() {
    class SubInterface<T> implements CopyStrategy<T> {

        @Override
        public T copy(T value) {
            return null;
        }
    }
    class SubClass extends SubInterface<String> {
    }
    Type subInterfaceType = new TypeInfo<SubInterface<Integer>>() {
    }.getType();
    assertEquals(Integer.class, ReflectionUtil.getTypeParameterForSuper(subInterfaceType, CopyStrategy.class, 0));
    Type subClassType = new TypeInfo<SubClass>() {
    }.getType();
    assertEquals(String.class, ReflectionUtil.getTypeParameterForSuper(subClassType, CopyStrategy.class, 0));
}
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) Test(org.junit.jupiter.api.Test)

Example 4 with CopyStrategy

use of org.terasology.reflection.copy.CopyStrategy 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 5 with CopyStrategy

use of org.terasology.reflection.copy.CopyStrategy 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)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)5 WildcardType (java.lang.reflect.WildcardType)5 Test (org.junit.jupiter.api.Test)5 CopyStrategy (org.terasology.reflection.copy.CopyStrategy)5 GenericArrayType (java.lang.reflect.GenericArrayType)4 Type (java.lang.reflect.Type)4 TypeInfo (org.terasology.reflection.TypeInfo)4