Search in sources :

Example 1 with CtLineElementComparator

use of spoon.support.comparator.CtLineElementComparator 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 CtLineElementComparator

use of spoon.support.comparator.CtLineElementComparator 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 CtLineElementComparator

use of spoon.support.comparator.CtLineElementComparator in project spoon by INRIA.

the class ClassesTest method testIsAnonymousMethodInCtClass.

@Test
public void testIsAnonymousMethodInCtClass() throws Exception {
    CtClass<?> type = build("spoon.test.secondaryclasses.testclasses", "AnonymousClass");
    TreeSet<CtClass<?>> ts = new TreeSet<CtClass<?>>(new CtLineElementComparator());
    ts.addAll(type.getElements(new AbstractFilter<CtClass<?>>(CtClass.class) {

        @Override
        public boolean matches(CtClass<?> element) {
            return element.isAnonymous();
        }
    }));
    List<CtClass<?>> anonymousClass = new ArrayList<CtClass<?>>();
    anonymousClass.addAll(ts);
    assertFalse(type.isAnonymous());
    assertTrue(ts.first().isAnonymous());
    assertTrue(anonymousClass.get(1).isAnonymous());
    assertEquals(2, anonymousClass.size());
    assertEquals(2, ts.size());
    assertEquals("spoon.test.secondaryclasses.testclasses.AnonymousClass$1", anonymousClass.get(0).getQualifiedName());
    assertEquals("spoon.test.secondaryclasses.testclasses.AnonymousClass$2", anonymousClass.get(1).getQualifiedName());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) Test(org.junit.Test)

Example 4 with CtLineElementComparator

use of spoon.support.comparator.CtLineElementComparator 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)

Example 5 with CtLineElementComparator

use of spoon.support.comparator.CtLineElementComparator in project spoon by INRIA.

the class CtTypeImpl method addTypeMember.

/**
 * Adds a type member.
 * If it has a position, adds it at the right place according to the position (sourceStart).
 * If it is implicit, adds it at the beginning.
 * Otherwise, adds it at the end.
 */
@Override
public <C extends CtType<T>> C addTypeMember(CtTypeMember member) {
    if (member == null) {
        return (C) this;
    }
    Comparator c = new CtLineElementComparator();
    if (member.isImplicit()) {
        return addTypeMemberAt(0, member);
    }
    // by default, inserting at the end
    int insertionPosition = typeMembers.size();
    // we search for an insertion position only if this one has one position
    if (member.getPosition() != null && member.getPosition().getSourceStart() >= 0) {
        for (int i = typeMembers.size() - 1; i >= 0; i--) {
            CtTypeMember m = this.typeMembers.get(i);
            if (m.isImplicit() || (m.getPosition().getSourceStart() >= 0 && c.compare(member, m) > 0)) {
                break;
            }
            insertionPosition--;
        }
    }
    return addTypeMemberAt(insertionPosition, member);
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) Comparator(java.util.Comparator)

Aggregations

CtLineElementComparator (spoon.support.comparator.CtLineElementComparator)9 Test (org.junit.Test)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 Launcher (spoon.Launcher)3 SortedList (spoon.support.util.SortedList)3 CtInvocation (spoon.reflect.code.CtInvocation)2 CtRole (spoon.reflect.path.CtRole)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 FineModelChangeListener (spoon.experimental.modelobs.FineModelChangeListener)1 CtBlock (spoon.reflect.code.CtBlock)1 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtFieldAccess (spoon.reflect.code.CtFieldAccess)1