Search in sources :

Example 6 with AbstractFilter

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());
}
Also used : CtCodeElement(spoon.reflect.code.CtCodeElement) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtConstructorCall(spoon.reflect.code.CtConstructorCall) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 7 with AbstractFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 8 with AbstractFilter

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);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with AbstractFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) AClass(spoon.test.refactoring.testclasses.AClass) Test(org.junit.Test)

Example 10 with AbstractFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtReturn(spoon.reflect.code.CtReturn) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Aggregations

AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)15 Test (org.junit.Test)13 CtClass (spoon.reflect.declaration.CtClass)13 Launcher (spoon.Launcher)11 Factory (spoon.reflect.factory.Factory)8 Annotation (java.lang.annotation.Annotation)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)4 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)4 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)4 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)4 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)4 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)4 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)4 ArrayList (java.util.ArrayList)3 TreeSet (java.util.TreeSet)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 Before (org.junit.Before)2