Search in sources :

Example 26 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class GenericsTest method testMethodsWithGenericsWhoExtendsObject.

@Test
public void testMethodsWithGenericsWhoExtendsObject() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtMethod<?> m = aTacos.getMethodsByName("m4").get(0);
    final CtInvocation<?> invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(2, invocation1.getExecutable().getActualTypeArguments().size());
    assertEquals("spoon.test.generics.testclasses.Tacos.<V, C>makeTacos()", invocation1.toString());
    final CtInvocation<?> invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
    assertEquals("spoon.test.generics.testclasses.Tacos.makeTacos()", invocation2.toString());
}
Also used : Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 27 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter 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 28 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class ImportTest method testNewInnerClassDefinesInItsClassAndSuperClass.

@Test
public void testNewInnerClassDefinesInItsClassAndSuperClass() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/imports/testclasses/SuperClass.java", "./src/test/java/spoon/test/imports/testclasses/SubClass.java"));
    compiler.build();
    final CtClass<?> subClass = (CtClass<?>) factory.Type().get(SubClass.class);
    final CtConstructorCall<?> ctConstructorCall = subClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    assertEquals("new spoon.test.imports.testclasses.SubClass.Item(\"\")", ctConstructorCall.toString());
    // here the buggy behavior with type members was encoded
    // so we fix it
    final String expected = "public class SubClass extends spoon.test.imports.testclasses.SuperClass {" + System.lineSeparator() + "    public void aMethod() {" + System.lineSeparator() + "        new spoon.test.imports.testclasses.SubClass.Item(\"\");" + System.lineSeparator() + "    }" + System.lineSeparator() + System.lineSeparator() + "    public static class Item extends spoon.test.imports.testclasses.SuperClass.Item {" + System.lineSeparator() + "        public Item(java.lang.String s) {" + System.lineSeparator() + "            super(1, s);" + System.lineSeparator() + "        }" + System.lineSeparator() + "    }" + System.lineSeparator() + "}";
    assertEquals(expected, subClass.toString());
}
Also used : SubClass(spoon.test.imports.testclasses.SubClass) SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 29 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class ImportTest method testStaticImportForInvocationInNoClasspath.

@Test
public void testStaticImportForInvocationInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/resources/import-static", "--output-type", "nooutput", "--noclasspath" });
    final List<CtInvocation<?>> elements = new SortedList(new CtLineElementComparator());
    elements.addAll(Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return !element.getExecutable().isConstructor() && super.matches(element);
        }
    }));
    // Invocation for a static method with the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(true), elements.get(0));
    // Invocation for a static method without the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(true), elements.get(1));
    // Invocation for a static method with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(false), elements.get(2));
    // Invocation for a static method without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(false), elements.get(3));
    // Invocation for a static method in an inner class with the declaring class specified.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(4));
    // Invocation for a static method in an inner class without the declaring class specified.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(true), elements.get(5));
    // Invocation for a static method in an inner class with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(6));
    // Invocation for a static method in an innser class without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(7));
    // Invocation for a static method in an inner class with the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(true), elements.get(8));
    // Invocation for a static method in an inner class without the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(true), elements.get(9));
    // Invocation for a static method in an inner class with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(false), elements.get(10));
    // Invocation for a static method in an inner class without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(false), elements.get(11));
    // Invocation for a static method with the declaring class specified and an import *.
    assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(true), elements.get(12));
    // Invocation for a static method without the declaring class specified and an import *.
    assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(true), elements.get(13));
    // Invocation for a static method with the declaring class specified, a return type and an import *.
    assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(false), elements.get(14));
    // Invocation for a static method without the declaring class specified, a return type and an import *.
    assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(false), elements.get(15));
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) SortedList(spoon.support.util.SortedList) Launcher(spoon.Launcher) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 30 with TypeFilter

use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.

the class FieldAccessTest method testGetReference.

@Test
public void testGetReference() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setShouldCompile(true);
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/");
    launcher.getEnvironment().setAutoImports(true);
    launcher.run();
    final CtClass<B> aClass = launcher.getFactory().Class().get(B.class);
    // now static fields are used with the name of the parent class
    assertEquals("A.myField", aClass.getElements(new TypeFilter<>(CtFieldWrite.class)).get(0).toString());
    assertEquals("finalField", aClass.getElements(new TypeFilter<>(CtFieldWrite.class)).get(1).toString());
}
Also used : B(spoon.test.fieldaccesses.testclasses.B) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8