Search in sources :

Example 11 with AbstractFilter

use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.

the class AnnotationTest method testUsageOfTypeAnnotationInNewInstance.

@Test
public void testUsageOfTypeAnnotationInNewInstance() 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 CtConstructorCall<?> ctConstructorCall = ctClass.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {

        @Override
        public boolean matches(CtConstructorCall<?> element) {
            return "String".equals(element.getType().getSimpleName());
        }
    }).get(0);
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctConstructorCall.getType().getAnnotations();
    assertEquals("Type of the new class must use an annotation", 1, typeAnnotations.size());
    assertEquals("Type of the new class is typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("New class with an type annotation must be well printed", "new java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String()", ctConstructorCall.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtConstructorCall(spoon.reflect.code.CtConstructorCall) 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)

Example 12 with AbstractFilter

use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.

the class ConstructorCallTest method setUp.

@Before
public void setUp() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/" + Foo.class.getCanonicalName().replace(".", "/") + ".java");
    launcher.addInputResource("./src/test/java/" + Panini.class.getCanonicalName().replace(".", "/") + ".java");
    launcher.setSourceOutputDirectory("./target/spooned");
    launcher.run();
    final Factory factory = launcher.getFactory();
    final CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
    TreeSet ts = new TreeSet(new DeepRepresentationComparator());
    ts.addAll(foo.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {

        @Override
        public boolean matches(CtConstructorCall<?> element) {
            return true;
        }
    }));
    constructorCalls = new ArrayList(ts);
    final CtType<Panini> panini = factory.Type().get(Panini.class);
    constructorCallsPanini = panini.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class));
}
Also used : AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.constructorcallnewclass.testclasses.Foo) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) Panini(spoon.test.constructorcallnewclass.testclasses.Panini) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtConstructorCall(spoon.reflect.code.CtConstructorCall) TreeSet(java.util.TreeSet) Launcher(spoon.Launcher) Before(org.junit.Before)

Example 13 with AbstractFilter

use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.

the class TryCatchTest method testMultiTryCatchWithCustomExceptions.

@Test
public void testMultiTryCatchWithCustomExceptions() throws Exception {
    final Launcher launcher = new Launcher();
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/trycatch/testclasses/"));
    compiler.build();
    Factory factory = compiler.getFactory();
    final CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
    final CtCatch ctCatch = foo.getElements(new AbstractFilter<CtCatch>(CtCatch.class) {

        @Override
        public boolean matches(CtCatch element) {
            return true;
        }
    }).get(0);
    final String expected = "catch (spoon.test.trycatch.testclasses.internal.MyException | spoon.test.trycatch.testclasses.internal.MyException2 ignore) {" + System.lineSeparator() + "}";
    assertEquals(expected, ctCatch.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.trycatch.testclasses.Foo) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtCatch(spoon.reflect.code.CtCatch) File(java.io.File) Test(org.junit.Test)

Example 14 with AbstractFilter

use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.

the class LambdaTest method testLambdaExpressionInIfConditional.

@Test
public void testLambdaExpressionInIfConditional() throws Exception {
    final CtLambda<?> lambda = getLambdaInFooByNumber(7);
    assertTypedBy(Predicate.class, lambda.getType());
    assertParametersSizeIs(1, lambda.getParameters());
    final CtParameter<?> parameter = (CtParameter<?>) lambda.getParameters().get(0);
    assertParameterTypedBy(Foo.Person.class, parameter);
    assertParameterIsNamedBy("p", parameter);
    assertHasExpressionBody(lambda);
    final CtMethod<?> method = foo.getElements(new NamedElementFilter<>(CtMethod.class, "m8")).get(0);
    final CtIf condition = method.getElements(new AbstractFilter<CtIf>(CtIf.class) {

        @Override
        public boolean matches(CtIf element) {
            return true;
        }
    }).get(0);
    final String expected = "if (((java.util.function.Predicate<spoon.test.lambda.testclasses.Foo.Person>) (( p) -> (p.age) > 18)).test(new spoon.test.lambda.testclasses.Foo.Person(10))) {" + System.lineSeparator() + "    java.lang.System.err.println(\"Enjoy, you have more than 18.\");" + System.lineSeparator() + "}";
    assertEquals("Condition must be well printed", expected, condition.toString());
}
Also used : AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.lambda.testclasses.Foo) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtParameter(spoon.reflect.declaration.CtParameter) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 15 with AbstractFilter

use of spoon.reflect.visitor.filter.AbstractFilter in project spoon by INRIA.

the class ParentTest method testGetParentWithFilter.

@Test
public void testGetParentWithFilter() throws Exception {
    // addType should set Parent
    CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
    CtMethod<Object> m = clazz.getMethod("m");
    // get three = "" in one = two = three = "";
    CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
    CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
    assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
    CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element.getParent() instanceof CtStatementList && super.matches(element);
        }
    });
    // the filter has to return one = two = three = ""
    assertEquals(m.getBody().getStatement(3), ctStatement);
    m = clazz.getMethod("internalClass");
    CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element instanceof CtLocalVariable && super.matches(element);
        }
    }).get(0);
    // get the top class
    ctStatement1.getParent(CtType.class);
    CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {

        @Override
        public boolean matches(CtType element) {
            return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
        }
    });
    assertEquals(clazz, parent);
    assertNotEquals(ctStatement1.getParent(CtType.class), parent);
    // not present element
    CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
    assertEquals(null, ctWhile);
    CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return true;
        }
    });
    // getParent must not return the current element
    assertNotEquals(statement, statementParent);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExpression(spoon.reflect.code.CtExpression) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtWhile(spoon.reflect.code.CtWhile) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) 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