Search in sources :

Example 6 with CtLineElementComparator

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

the class CtModuleImpl method setModuleDirectives.

@Override
public <T extends CtModule> T setModuleDirectives(List<CtModuleDirective> moduleDirectives) {
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, CtRole.MODULE_DIRECTIVE, this.moduleDirectives, new ArrayList<>(this.moduleDirectives));
    if (moduleDirectives == null || moduleDirectives.isEmpty()) {
        this.moduleDirectives = CtElementImpl.emptyList();
        return (T) this;
    }
    if (this.moduleDirectives == CtElementImpl.<CtModuleDirective>emptyList()) {
        this.moduleDirectives = new SortedList<>(new CtLineElementComparator());
    }
    this.moduleDirectives.clear();
    for (CtModuleDirective moduleDirective : moduleDirectives) {
        this.addModuleDirective(moduleDirective);
    }
    return (T) this;
}
Also used : CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtModuleDirective(spoon.reflect.declaration.CtModuleDirective)

Example 7 with CtLineElementComparator

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

the class CtModuleImpl method addModuleDirective.

@Override
public <T extends CtModule> T addModuleDirective(CtModuleDirective moduleDirective) {
    if (moduleDirective == null) {
        return (T) this;
    }
    if (this.moduleDirectives == CtElementImpl.<CtModuleDirective>emptyList()) {
        this.moduleDirectives = new SortedList<>(new CtLineElementComparator());
    }
    if (!this.moduleDirectives.contains(moduleDirective)) {
        moduleDirective.setParent(this);
        CtRole role = this.computeRoleFromModuleDirectory(moduleDirective);
        getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.moduleDirectives, moduleDirective);
        this.moduleDirectives.add(moduleDirective);
    }
    return (T) this;
}
Also used : CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtRole(spoon.reflect.path.CtRole)

Example 8 with CtLineElementComparator

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

the class AstCheckerTest method testAvoidSetCollectionSavedOnAST.

@Test
public void testAvoidSetCollectionSavedOnAST() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/main/java");
    launcher.buildModel();
    final Factory factory = launcher.getFactory();
    final List<CtTypeReference<?>> collectionsRef = // 
    Arrays.asList(// 
    factory.Type().createReference(Collection.class), // 
    factory.Type().createReference(List.class), // 
    factory.Type().createReference(Set.class), factory.Type().createReference(Map.class));
    final List<CtInvocation<?>> invocations = Query.getElements(factory, new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            if (!(element.getParent() instanceof CtInvocation)) {
                return false;
            }
            final CtInvocation<?> parent = (CtInvocation<?>) element.getParent();
            if (parent.getTarget() == null || !parent.getTarget().equals(element)) {
                return false;
            }
            if (!element.getExecutable().getDeclaringType().getSimpleName().startsWith("Ct")) {
                return false;
            }
            boolean isDataStructure = false;
            for (int i = 0; i < collectionsRef.size(); i++) {
                CtTypeReference<?> ctTypeReference = collectionsRef.get(i);
                if (element.getType().isSubtypeOf(ctTypeReference)) {
                    isDataStructure = true;
                    break;
                }
            }
            if (!isDataStructure) {
                return false;
            }
            final String simpleName = parent.getExecutable().getSimpleName();
            return simpleName.startsWith("add") || simpleName.startsWith("remove") || simpleName.startsWith("put");
        }
    });
    if (invocations.size() > 0) {
        final String error = // 
        invocations.stream().sorted(// 
        new CtLineElementComparator()).map(// 
        i -> "see " + i.getPosition().getFile().getAbsoluteFile() + " at " + i.getPosition().getLine()).collect(Collectors.joining(",\n"));
        throw new AssertionError(error);
    }
}
Also used : Arrays(java.util.Arrays) CtIf(spoon.reflect.code.CtIf) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) CtThrow(spoon.reflect.code.CtThrow) FineModelChangeListener(spoon.experimental.modelobs.FineModelChangeListener) Map(java.util.Map) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) DerivedProperty(spoon.support.DerivedProperty) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtInvocation(spoon.reflect.code.CtInvocation) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) ModifierKind(spoon.reflect.declaration.ModifierKind) CtBlock(spoon.reflect.code.CtBlock) CtReturn(spoon.reflect.code.CtReturn) UnsettableProperty(spoon.support.UnsettableProperty) CtMethod(spoon.reflect.declaration.CtMethod) Set(java.util.Set) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Collection(java.util.Collection) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 9 with CtLineElementComparator

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

the class CtModuleImpl method addModuleDirectiveAt.

@Override
public <T extends CtModule> T addModuleDirectiveAt(int position, CtModuleDirective moduleDirective) {
    if (moduleDirective == null) {
        return (T) this;
    }
    if (this.moduleDirectives == CtElementImpl.<CtModuleDirective>emptyList()) {
        this.moduleDirectives = new SortedList<>(new CtLineElementComparator());
    }
    if (!this.moduleDirectives.contains(moduleDirective)) {
        moduleDirective.setParent(this);
        CtRole role = this.computeRoleFromModuleDirectory(moduleDirective);
        getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.moduleDirectives, position, moduleDirective);
        this.moduleDirectives.add(position, moduleDirective);
    }
    return (T) this;
}
Also used : CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtRole(spoon.reflect.path.CtRole)

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