Search in sources :

Example 1 with SortedList

use of spoon.support.util.SortedList 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 2 with SortedList

use of spoon.support.util.SortedList 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 3 with SortedList

use of spoon.support.util.SortedList in project spoon by INRIA.

the class TargetedExpressionTest method testClassDeclaredInALambda.

@Test
public void testClassDeclaredInALambda() throws Exception {
    // contract: A class can be declared in a lambda expression where we use final fields.
    final CtType<Tapas> type = buildClass(Tapas.class);
    final List<CtFieldAccess> elements = new SortedList(new CtLineElementComparator());
    elements.addAll(type.getElements(new TypeFilter<>(CtFieldAccess.class)));
    assertEquals(3, elements.size());
    final CtTypeReference<Object> firstExpected = type.getFactory().Type().createReference("spoon.test.targeted.testclasses.Tapas$1$InnerSubscriber");
    CtThisAccess<Object> expectedThisAccess = type.getFactory().Code().createThisAccess(firstExpected);
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(firstExpected).target(expectedThisAccess).type(CtFieldWrite.class).result("this.index"), elements.get(0));
    final CtTypeReference<Object> secondExpectedInner = type.getFactory().Type().createReference("spoon.test.targeted.testclasses.Tapas$3InnerSubscriber");
    expectedThisAccess = type.getFactory().Code().createThisAccess(secondExpectedInner);
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(secondExpectedInner).target(expectedThisAccess).type(CtFieldWrite.class).result("this.index").isLocal(), elements.get(1));
    final CtTypeReference<Object> thirdExpectedInner = type.getFactory().Type().createReference("spoon.test.targeted.testclasses.Tapas$4InnerSubscriber");
    expectedThisAccess = type.getFactory().Code().createThisAccess(thirdExpectedInner);
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(thirdExpectedInner).target(expectedThisAccess).type(CtFieldWrite.class).result("this.index").isLocal(), elements.get(2));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldWrite(spoon.reflect.code.CtFieldWrite) Tapas(spoon.test.targeted.testclasses.Tapas) SortedList(spoon.support.util.SortedList) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 CtLineElementComparator (spoon.support.comparator.CtLineElementComparator)3 SortedList (spoon.support.util.SortedList)3 Launcher (spoon.Launcher)2 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtFieldAccess (spoon.reflect.code.CtFieldAccess)1 CtFieldWrite (spoon.reflect.code.CtFieldWrite)1 CtInvocation (spoon.reflect.code.CtInvocation)1 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)1 Tacos (spoon.test.generics.testclasses.Tacos)1 MainTest (spoon.test.main.MainTest)1 Tapas (spoon.test.targeted.testclasses.Tapas)1