Search in sources :

Example 46 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testCtTypeReference_getSuperclass.

@Test
public void testCtTypeReference_getSuperclass() throws Exception {
    Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
    CtClass<?> ctClassCelebrationLunch = factory.Class().get(CelebrationLunch.class);
    CtTypeReference<?> trWeddingLunch_Mole = ctClassCelebrationLunch.filterChildren(new NamedElementFilter<>(CtNamedElement.class, "disgust")).map((CtTypedElement te) -> {
        return te.getType();
    }).first();
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>.WeddingLunch<spoon.test.generics.testclasses.Mole>", trWeddingLunch_Mole.toString());
    CtType<?> tWeddingLunch_X = trWeddingLunch_Mole.getDeclaration();
    CtTypeReference<?> trCelebrationLunch_Tacos_Paella_X = tWeddingLunch_X.getSuperclass();
    // current correct behavior of CtType#getSuperclass()
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch<" + "spoon.test.generics.testclasses.Tacos, " + "spoon.test.generics.testclasses.Paella, " + "X" + ">", trCelebrationLunch_Tacos_Paella_X.toString());
    // current - wrong behavior of CtTypeReference#getSuperclass()
    assertEquals("spoon.test.generics.testclasses.CelebrationLunch<" + "spoon.test.generics.testclasses.Tacos, " + "spoon.test.generics.testclasses.Paella, " + "X" + ">", trWeddingLunch_Mole.getSuperclass().toString());
// future suggested behavior of CtTypeReference#getSuperclass() - the 3rd argument is Mole.
// assertEquals("spoon.test.generics.testclasses.CelebrationLunch<"
// + "spoon.test.generics.testclasses.Tacos, "
// + "spoon.test.generics.testclasses.Paella, "
// + "spoon.test.generics.testclasses.Mole"
// + ">",trWeddingLunch_Mole.getSuperclass().toString());
// future suggested behavior of CtTypeReference#getSuperclass()
// assertEquals("spoon.test.generics.testclasses.Lunch<"
// + "spoon.test.generics.testclasses.Mole, "
// + "spoon.test.generics.testclasses.Tacos"
// + ">",trWeddingLunch_Mole.getSuperclass().getSuperclass().toString());
}
Also used : ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTypedElement(spoon.reflect.declaration.CtTypedElement) File(java.io.File) CtNamedElement(spoon.reflect.declaration.CtNamedElement) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 47 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testGenericWithExtendsInDeclaration.

@Test
public void testGenericWithExtendsInDeclaration() throws Exception {
    final Factory build = build(Panini.class);
    final CtType<Panini> panini = build.Type().get(Panini.class);
    final CtMethod<?> apply = panini.getMethodsByName("apply").get(0);
    assertEquals(1, apply.getType().getActualTypeArguments().size());
    assertEquals("? super java.lang.Object", apply.getType().getActualTypeArguments().get(0).toString());
    assertEquals(1, apply.getParameters().get(0).getType().getActualTypeArguments().size());
    assertEquals("? extends java.lang.Long", apply.getParameters().get(0).getType().getActualTypeArguments().get(0).toString());
}
Also used : ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Panini(spoon.test.generics.testclasses.Panini) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 48 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testMethodTypingContextAdaptMethod.

@Test
public void testMethodTypingContextAdaptMethod() throws Exception {
    // core contracts of MethodTypingContext#adaptMethod
    Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
    CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
    // represents <C> void eatMe(A paramA, B paramB, C paramC){}
    CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
    CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
    ClassTypingContext ctcWeddingLunch = new ClassTypingContext(ctClassWeddingLunch);
    // we all analyze new methods
    final MethodTypingContext methodSTH = new MethodTypingContext().setClassTypingContext(ctcWeddingLunch);
    // contract: method can be adapted only using MethodTypingContext
    methodSTH.setMethod(trLunch_eatMe);
    CtMethod<?> adaptedLunchEatMe = (CtMethod<?>) methodSTH.getAdaptationScope();
    // contract: adapting of method declared in different scope, returns new method
    assertTrue(adaptedLunchEatMe != trLunch_eatMe);
    // check that new method is adapted correctly
    // is declared in correct class
    assertSame(ctClassWeddingLunch, adaptedLunchEatMe.getDeclaringType());
    // is not member of the same class (WeddingLunch)
    for (CtTypeMember typeMember : ctClassWeddingLunch.getTypeMembers()) {
        assertFalse(adaptedLunchEatMe == typeMember);
    }
    // the name is the same
    assertEquals("eatMe", adaptedLunchEatMe.getSimpleName());
    // it has the same number of of formal type parameters
    assertEquals(1, adaptedLunchEatMe.getFormalCtTypeParameters().size());
    assertEquals("C", adaptedLunchEatMe.getFormalCtTypeParameters().get(0).getQualifiedName());
    // parameters are correct
    assertEquals(3, adaptedLunchEatMe.getParameters().size());
    // "A paramA" becomes "X paramA" becomes Lunch%A corresponds to X in WeddingLunch
    assertEquals("X", adaptedLunchEatMe.getParameters().get(0).getType().getQualifiedName());
    // B paramB becomes Tacos becomes Lunch%B corresponds to Tacos in WeddingLunch (class WeddingLunch<X> extends CelebrationLunch<Tacos, Paella, X>)
    assertEquals(Tacos.class.getName(), adaptedLunchEatMe.getParameters().get(1).getType().getQualifiedName());
    // "C paramC" stays "C paramC"
    assertEquals("C", adaptedLunchEatMe.getParameters().get(2).getType().getQualifiedName());
    // contract: adapting of adapted method returns input method
    methodSTH.setMethod(adaptedLunchEatMe);
    assertSame(adaptedLunchEatMe, methodSTH.getAdaptationScope());
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) CtTypeMember(spoon.reflect.declaration.CtTypeMember) MethodTypingContext(spoon.support.visitor.MethodTypingContext) Tacos(spoon.test.generics.testclasses.Tacos) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 49 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testTypeParameterReference.

@Test
public void testTypeParameterReference() throws Exception {
    Factory factory = build(ClassThatBindsAGenericType.class, ClassThatDefinesANewTypeArgument.class);
    CtClass<?> classThatBindsAGenericType = factory.Class().get(ClassThatBindsAGenericType.class);
    CtClass<?> classThatDefinesANewTypeArgument = factory.Class().get(ClassThatDefinesANewTypeArgument.class);
    CtTypeReference<?> tr1 = classThatBindsAGenericType.getSuperclass();
    CtTypeReference<?> trExtends = tr1.getActualTypeArguments().get(0);
    CtTypeParameter tr2 = classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0);
    CtTypeReference<?> tr3 = classThatDefinesANewTypeArgument.getMethodsByName("foo").get(0).getParameters().get(0).getReference().getType();
    // an bound type is not an TypeParameterRefernce
    assertTrue(!(trExtends instanceof CtTypeParameterReference));
    // a used type parameter T is a CtTypeParameterReference
    assertTrue(tr3 instanceof CtTypeParameterReference);
    assertEquals("File", trExtends.getSimpleName());
    assertEquals(java.io.File.class, trExtends.getActualClass());
    assertEquals("T", tr2.getSimpleName());
    assertEquals("T", tr3.getSimpleName());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 50 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class GenericsTest method testWildCardonShadowClass.

@Test
public void testWildCardonShadowClass() throws Exception {
    // contract: generics should be treated the same way in shadow classes
    // test that apply argument type contains a wildcard
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    launcher.addInputResource("src/test/java/spoon/test/generics/testclasses/FakeTpl.java");
    launcher.buildModel();
    CtInterface<?> fakeTplItf = factory.Interface().get("spoon.test.generics.testclasses.FakeTpl");
    checkFakeTpl(fakeTplItf);
    // same test with a shadow class
    launcher = new Launcher();
    factory = launcher.getFactory();
    CtInterface<?> fakeTplItf2 = factory.Interface().get(FakeTpl.class);
    checkFakeTpl(fakeTplItf2);
}
Also used : Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19