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;
}
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;
}
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);
}
}
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;
}
Aggregations