Search in sources :

Example 1 with CelebrationLunch

use of spoon.test.generics.testclasses.CelebrationLunch in project spoon by INRIA.

the class GenericsTest method testMethodTypingContext.

@Test
public void testMethodTypingContext() throws Exception {
    Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
    CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
    CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    MethodTypingContext methodSTH = new MethodTypingContext().setMethod(trWeddingLunch_eatMe);
    // contract: the method typing context provides its scope
    assertSame(trWeddingLunch_eatMe, methodSTH.getAdaptationScope());
    CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
    CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    CtInvocation<?> invokeReserve = factory.Class().get(CelebrationLunch.class).filterChildren(new TypeFilter<>(CtInvocation.class)).select((CtInvocation i) -> "reserve".equals(i.getExecutable().getSimpleName())).first();
    MethodTypingContext methodReserveTC = new MethodTypingContext().setInvocation(invokeReserve);
    // contract: the method typing context provides its scope
    assertSame(invokeReserve.getExecutable().getDeclaration(), methodReserveTC.getAdaptationScope());
    // check that MethodTypingContext made from invocation knows actual type arguments of method and all declaring types
    // 1) check method actual type argument
    CtMethod<?> methodReserve = (CtMethod<?>) invokeReserve.getExecutable().getDeclaration();
    CtTypeParameter methodReserve_S = methodReserve.getFormalCtTypeParameters().get(0);
    assertEquals("S", methodReserve_S.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(methodReserve_S).getQualifiedName());
    // 2) check actual type arguments of declaring type `Section`
    CtClass classSection = (CtClass) methodReserve.getDeclaringType();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch$Section", classSection.getQualifiedName());
    CtTypeParameter classSection_Y = classSection.getFormalCtTypeParameters().get(0);
    assertEquals("Y", classSection_Y.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classSection_Y).getQualifiedName());
    // 3) check actual type arguments of declaring type `WeddingLunch`
    CtClass classWeddingLunch = (CtClass) classSection.getDeclaringType();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch", classWeddingLunch.getQualifiedName());
    CtTypeParameter classWeddingLunch_X = classWeddingLunch.getFormalCtTypeParameters().get(0);
    assertEquals("X", classWeddingLunch_X.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classWeddingLunch_X).getQualifiedName());
    // 4) check actual type arguments of declaring type `CelebrationLunch`
    CtClass classCelebrationLunch = (CtClass) classWeddingLunch.getDeclaringType();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch", classCelebrationLunch.getQualifiedName());
    CtTypeParameter classCelebrationLunch_K = classCelebrationLunch.getFormalCtTypeParameters().get(0);
    CtTypeParameter classCelebrationLunch_L = classCelebrationLunch.getFormalCtTypeParameters().get(1);
    CtTypeParameter classCelebrationLunch_M = classCelebrationLunch.getFormalCtTypeParameters().get(2);
    assertEquals("K", classCelebrationLunch_K.getSimpleName());
    assertEquals("L", classCelebrationLunch_L.getSimpleName());
    assertEquals("M", classCelebrationLunch_M.getSimpleName());
    assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(classCelebrationLunch_K).getQualifiedName());
    assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classCelebrationLunch_L).getQualifiedName());
    assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classCelebrationLunch_M).getQualifiedName());
    // method->Section->WeddingLunch->CelebrationLunch
    GenericTypeAdapter celebrationLunchTC = methodReserveTC.getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter();
    assertEquals("java.lang.Integer", celebrationLunchTC.adaptType(classCelebrationLunch_K).getQualifiedName());
    assertEquals("java.lang.Long", celebrationLunchTC.adaptType(classCelebrationLunch_L).getQualifiedName());
    assertEquals("java.lang.Double", celebrationLunchTC.adaptType(classCelebrationLunch_M).getQualifiedName());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) GenericTypeAdapter(spoon.support.visitor.GenericTypeAdapter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) MethodTypingContext(spoon.support.visitor.MethodTypingContext) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CelebrationLunch(spoon.test.generics.testclasses.CelebrationLunch) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

File (java.io.File)1 Test (org.junit.Test)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtClass (spoon.reflect.declaration.CtClass)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)1 Factory (spoon.reflect.factory.Factory)1 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)1 GenericTypeAdapter (spoon.support.visitor.GenericTypeAdapter)1 MethodTypingContext (spoon.support.visitor.MethodTypingContext)1 CelebrationLunch (spoon.test.generics.testclasses.CelebrationLunch)1 LikeCtClass (spoon.test.generics.testclasses2.LikeCtClass)1 MainTest (spoon.test.main.MainTest)1 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)1