Search in sources :

Example 51 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class GenericsTest method testGenericsInQualifiedNameInConstructorCall.

@Test
public void testGenericsInQualifiedNameInConstructorCall() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtType<?> burritos = aTacos.getNestedType("Burritos");
    SortedList<CtConstructorCall> elements = new SortedList<CtConstructorCall>(new CtLineElementComparator());
    elements.addAll(burritos.getElements(new TypeFilter<>(CtConstructorCall.class)));
    assertEquals(3, elements.size());
    // Constructor call.
    assertEquals(0, elements.get(1).getExecutable().getType().getActualTypeArguments().size());
    assertNotNull(elements.get(1).getType().getDeclaringType());
    assertEquals("new Pozole()", elements.get(1).toString());
    assertEquals(2, elements.get(0).getExecutable().getType().getActualTypeArguments().size());
    assertNotNull(elements.get(0).getType().getDeclaringType());
    assertEquals("new Burritos<K, V>()", elements.get(0).toString());
    // New class.
    assertEquals(2, elements.get(2).getExecutable().getType().getActualTypeArguments().size());
    assertNotNull(elements.get(2).getType().getDeclaringType());
    assertEquals("new Burritos<K, V>() {}", elements.get(2).toString());
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) Tacos(spoon.test.generics.testclasses.Tacos) SortedList(spoon.support.util.SortedList) Launcher(spoon.Launcher) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 52 with Launcher

use of spoon.Launcher 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)

Example 53 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class GenericsTest method testIsSameSignatureWithMethodGenerics.

@Test
public void testIsSameSignatureWithMethodGenerics() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses2/SameSignature2.java");
    launcher.buildModel();
    CtClass ctClass = launcher.getFactory().Class().get(SameSignature2.class);
    CtMethod classMethod = (CtMethod) ctClass.getMethodsByName("visitCtConditional").get(0);
    CtType<?> iface = launcher.getFactory().Type().get("spoon.test.generics.testclasses2.ISameSignature");
    CtMethod ifaceMethod = (CtMethod) iface.getMethodsByName("visitCtConditional").get(0);
    ClassTypingContext ctcSub = new ClassTypingContext(ctClass.getReference());
    assertTrue(ctcSub.isOverriding(classMethod, ifaceMethod));
    assertTrue(ctcSub.isOverriding(ifaceMethod, classMethod));
    assertTrue(ctcSub.isSubSignature(classMethod, ifaceMethod));
    assertTrue(ctcSub.isSubSignature(ifaceMethod, classMethod));
    assertTrue(ctcSub.isSameSignature(classMethod, ifaceMethod));
    assertTrue(ctcSub.isSameSignature(ifaceMethod, classMethod));
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 54 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class GenericsTest method testIsSameSignatureWithGenerics.

@Test
public void testIsSameSignatureWithGenerics() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/SameSignature.java");
    launcher.buildModel();
    CtClass ctClass = launcher.getFactory().Class().get(SameSignature.class);
    List<CtMethod> methods = ctClass.getMethodsByName("forEach");
    assertEquals(1, methods.size());
    CtType<?> iterableItf = launcher.getFactory().Type().get(Iterable.class);
    List<CtMethod<?>> methodsItf = iterableItf.getMethodsByName("forEach");
    assertEquals(1, methodsItf.size());
    ClassTypingContext ctc = new ClassTypingContext(ctClass.getReference());
    assertTrue(ctc.isOverriding(methods.get(0), methodsItf.get(0)));
    assertTrue(ctc.isSubSignature(methods.get(0), methodsItf.get(0)));
    assertTrue(ctc.isSameSignature(methods.get(0), methodsItf.get(0)));
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 55 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testImportStaticAndFieldAccessWithImport.

@Test
public void testImportStaticAndFieldAccessWithImport() throws Exception {
    // contract: Qualified field access and an import static with import should import the type first, and not use static import
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--with-imports" });
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/");
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/Tacos.java");
    launcher.buildModel();
    final CtType<Object> aTacos = launcher.getFactory().Type().get(Tacos.class);
    final CtStatement assignment = aTacos.getMethod("m").getBody().getStatement(0);
    assertTrue(assignment instanceof CtLocalVariable);
    assertEquals("Constants.CONSTANT.foo", ((CtLocalVariable) assignment).getAssignment().toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22