Search in sources :

Example 11 with TypeToken

use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericMethodFromReturnValueWithSubclass.

@Test
public void testGenericMethodFromReturnValueWithSubclass() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassWithGenericMethodAndSubclass.class;
    Method targetMethod = targetClass.getMethod("wrap", new Class<?>[] { Object.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    GenericClass generatedType = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassWithGenericMethodAndSubclass.Foo<String>>() {
    }.getType());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(generatedType);
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getParameterTypes().get(0), String.class);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 12 with TypeToken

use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericMethodFromReturnValue.

@Test
public void testGenericMethodFromReturnValue() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericMethodWithBounds.class;
    Method targetMethod = targetClass.getMethod("is", new Class<?>[] { Comparable.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    GenericClass generatedType = new GenericClass(new TypeToken<java.util.List<Integer>>() {
    }.getType());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(generatedType);
    Assert.assertEquals(instantiatedMethod.getGeneratedClass(), generatedType);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 13 with TypeToken

use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericMethodAbstractType.

@Test
public void testGenericMethodAbstractType() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.ConcreteGenericClass.class;
    Method targetMethod = targetClass.getMethod("create", new Class<?>[] { int.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    Assert.assertEquals(genericMethod.getGeneratedClass().getRawClass(), com.examples.with.different.packagename.generic.ConcreteGenericClass.class);
    GenericClass iterableIntegerClass = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.AbstractGenericClass<java.lang.Integer>>() {
    }.getType());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(iterableIntegerClass);
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getRawClass(), com.examples.with.different.packagename.generic.ConcreteGenericClass.class);
    instantiatedMethod = genericMethod.copyWithOwnerFromReturnType(iterableIntegerClass);
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getRawClass(), com.examples.with.different.packagename.generic.ConcreteGenericClass.class);
    instantiatedMethod = genericMethod.getGenericInstantiation(iterableIntegerClass);
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getRawClass(), com.examples.with.different.packagename.generic.ConcreteGenericClass.class);
    instantiatedMethod = genericMethod.copyWithNewOwner(iterableIntegerClass);
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getRawClass(), com.examples.with.different.packagename.generic.ConcreteGenericClass.class);
}
Also used : GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 14 with TypeToken

use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.

the class TestGenericClass method testGenericSuperclassFromTypeVariableList.

@SuppressWarnings("rawtypes")
@Test
public void testGenericSuperclassFromTypeVariableList() {
    GenericClass listOfInteger = new GenericClass(new TypeToken<List<Integer>>() {
    }.getType());
    GenericClass linkedlistOfTypeVariable = new GenericClass(new TypeToken<LinkedList>() {
    }.getType());
    Assert.assertTrue(linkedlistOfTypeVariable.canBeInstantiatedTo(listOfInteger));
    Assert.assertFalse(listOfInteger.canBeInstantiatedTo(linkedlistOfTypeVariable));
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 15 with TypeToken

use of com.googlecode.gentyref.TypeToken in project evosuite by EvoSuite.

the class TestGenericClass method testSatisfiesTypeVariableInSubtype.

@Test
public void testSatisfiesTypeVariableInSubtype() {
    GenericClass iterableIntegerClass = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GuavaExample4<java.lang.Iterable<Integer>>>() {
    }.getType());
    ParameterizedType iterableInteger = (ParameterizedType) iterableIntegerClass.getParameterTypes().get(0);
    TypeVariable<?> typeVariable = ((Class<?>) iterableInteger.getRawType()).getTypeParameters()[0];
    TypeVariable<?> iterableTypeVariable = iterableIntegerClass.getTypeVariables().get(0);
    GenericClass listOfIntegerClass = new GenericClass(com.examples.with.different.packagename.generic.GuavaExample4.class);
    // Object bound
    Assert.assertTrue(iterableIntegerClass.satisfiesBoundaries(typeVariable));
    Assert.assertTrue(listOfIntegerClass.satisfiesBoundaries(typeVariable));
    // Iterable bound
    Assert.assertTrue(iterableIntegerClass.satisfiesBoundaries(iterableTypeVariable));
    Assert.assertTrue(listOfIntegerClass.satisfiesBoundaries(iterableTypeVariable));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Aggregations

TypeToken (com.googlecode.gentyref.TypeToken)20 GenericClass (org.evosuite.utils.generic.GenericClass)20 Test (org.junit.Test)20 Method (java.lang.reflect.Method)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 ParameterizedType (java.lang.reflect.ParameterizedType)3 AnnotatedType (java.lang.reflect.AnnotatedType)2 Type (java.lang.reflect.Type)2 WildcardType (java.lang.reflect.WildcardType)2 LinkedList (java.util.LinkedList)2 GuavaExample4 (com.examples.with.different.packagename.generic.GuavaExample4)1 Annotation (java.lang.annotation.Annotation)1 TypeVariable (java.lang.reflect.TypeVariable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Assertion (org.evosuite.assertion.Assertion)1 Inspector (org.evosuite.assertion.Inspector)1 InspectorAssertion (org.evosuite.assertion.InspectorAssertion)1 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)1