use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testSrcMainJava.
// this test contains all the architectural rules that are valid for the whole src/main/java
// we put them in the same test in order to only build the full model once
@Test
public void testSrcMainJava() throws Exception {
Launcher spoon = new Launcher();
spoon.getEnvironment().setCommentEnabled(true);
spoon.addInputResource("src/main/java/");
// contract: all non-trivial public methods should be documented with proper API Javadoc
spoon.buildModel();
List<String> notDocumented = new ArrayList<>();
for (CtMethod method : spoon.getModel().getElements(new TypeFilter<>(CtMethod.class))) {
// now we see whether this should be documented
if (// public methods should be documented
method.hasModifier(ModifierKind.PUBLIC) && // all kinds of setters can be undocumented
!method.getSimpleName().startsWith("get") && !method.getSimpleName().startsWith("set") && !method.getSimpleName().startsWith("is") && !method.getSimpleName().startsWith("add") && !method.getSimpleName().startsWith("remove") && // only the top declarations should be documented (not the overriding methods which are lower in the hierarchy)
method.getTopDefinitions().size() == 0 && (// all interface methods and abstract class methods must be documented
method.hasModifier(ModifierKind.ABSTRACT) || // 4) you commit your changes and create the corresponding pull requests
method.filterChildren(new TypeFilter<>(CtCodeElement.class)).list().size() > // means that only large methods must be documented
35)) {
// is it really well documented?
if (method.getDocComment().length() <= 15) {
// the Javadoc must be at least at least 15 characters (still pretty short...)
notDocumented.add(method.getParent(CtType.class).getQualifiedName() + "#" + method.getSignature());
}
}
}
if (notDocumented.size() > 0) {
fail(notDocumented.size() + " public methods should be documented with proper API documentation: \n" + StringUtils.join(notDocumented, "\n"));
}
// contract: Spoon's code never uses TreeSet constructor, because they implicitly depend on Comparable (no static check, only dynamic checks)
List<CtConstructorCall> treeSetWithoutComparators = spoon.getFactory().Package().getRootPackage().filterChildren(new AbstractFilter<CtConstructorCall>() {
@Override
public boolean matches(CtConstructorCall element) {
return element.getType().getActualClass().equals(TreeSet.class) && element.getArguments().size() == 0;
}
}).list();
assertEquals(0, treeSetWithoutComparators.size());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class RefactoringTest method testThisInConstructorAfterATransformation.
@Test
public void testThisInConstructorAfterATransformation() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-i", "src/test/java/spoon/test/refactoring/testclasses", "-o", "target/spooned/refactoring", "-p", ThisTransformationProcessor.class.getName() });
launcher.run();
final CtClass<?> aClassX = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.refactoring.testclasses.AClassX");
final CtInvocation<?> thisInvocation = aClassX.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return element.getExecutable().isConstructor();
}
}).get(0);
assertEquals("this(\"\")", thisInvocation.toString());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class FilterTest method testReflectionBasedTypeFilter.
@Test
public void testReflectionBasedTypeFilter() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
// First collect all classes using tested TypeFilter
List<CtClass<?>> allClasses = launcher.getFactory().Package().getRootPackage().getElements(new TypeFilter<CtClass<?>>(CtClass.class));
assertTrue(allClasses.size() > 0);
allClasses.forEach(result -> {
assertTrue(result instanceof CtClass);
});
// then do it using Filter whose type is computed by reflection
List<CtClass<?>> allClasses2 = launcher.getFactory().Package().getRootPackage().getElements(new Filter<CtClass<?>>() {
@Override
public boolean matches(CtClass<?> element) {
return true;
}
});
assertArrayEquals(allClasses.toArray(), allClasses2.toArray());
// then do it using Filter implemented by lambda expression
List<CtClass<?>> allClasses3 = launcher.getFactory().Package().getRootPackage().getElements((CtClass<?> element) -> true);
assertArrayEquals(allClasses.toArray(), allClasses3.toArray());
// last try AbstractFilter constructor without class parameter
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtInvocation<?> invSize = aTacos.getElements(new AbstractFilter<CtInvocation<?>>() {
/*no class is needed here*/
@Override
public boolean matches(CtInvocation<?> element) {
if (element.getExecutable() == null) {
return false;
}
return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
assertNotNull(invSize);
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class RefactoringTest method testThisInConstructor.
@Test
public void testThisInConstructor() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-i", "src/test/java/spoon/test/refactoring/testclasses", "-o", "target/spooned/refactoring" });
launcher.run();
final CtClass<?> aClass = (CtClass<?>) launcher.getFactory().Type().get(AClass.class);
final CtInvocation<?> thisInvocation = aClass.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return element.getExecutable().isConstructor();
}
}).get(0);
assertEquals("this(\"\")", thisInvocation.toString());
}
use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationInCast.
@Test
public void testUsageOfTypeAnnotationInCast() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtReturn<?> returns = ctClass.getElements(new AbstractFilter<CtReturn<?>>(CtReturn.class) {
@Override
public boolean matches(CtReturn<?> element) {
return !element.getReturnedExpression().getTypeCasts().isEmpty();
}
}).get(0);
final CtExpression<?> returnedExpression = returns.getReturnedExpression();
final List<CtAnnotation<? extends Annotation>> typeAnnotations = returnedExpression.getTypeCasts().get(0).getAnnotations();
assertEquals("Cast with a type annotation must have it in its model", 1, typeAnnotations.size());
assertEquals("Type annotation in the cast must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Cast with an type annotation must be well printed", "((java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String) (s))", returnedExpression.toString());
}
Aggregations