Search in sources :

Example 31 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class TestCtBlock method testRemoveStatement.

@Test
public void testRemoveStatement() {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/ctBlock/testclasses/Toto.java");
    spoon.buildModel();
    List<CtMethod> methods = spoon.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "foo"));
    assertEquals(1, methods.size());
    CtMethod foo = methods.get(0);
    CtBlock block = foo.getBody();
    CtStatement lastStatement = block.getLastStatement();
    assertEquals("i++", lastStatement.toString());
    block.removeStatement(lastStatement);
    CtStatement newLastStatement = block.getLastStatement();
    assertTrue(newLastStatement != lastStatement);
    assertTrue(newLastStatement instanceof CtIf);
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 32 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CUFilterTest method testSingleExcludeWithFilter.

@Test
public void testSingleExcludeWithFilter() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/same-package");
    launcher.getModelBuilder().addCompilationUnitFilter(new CompilationUnitFilter() {

        @Override
        public boolean exclude(final String path) {
            return path.endsWith("B.java");
        }
    });
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    assertEquals(1, model.getAllTypes().size());
    // make sure `B` is not available in `model.getAllTypes`
    assertEquals("A", model.getAllTypes().iterator().next().getSimpleName());
    // make sure declaration of `B` is known in `model`
    final CtReturn ctReturn = model.getAllTypes().iterator().next().getMethod("createB").getBody().getStatement(0);
    final CtConstructorCall ctConstructorCall = (CtConstructorCall) ctReturn.getReturnedExpression();
    assertEquals("spoon.test.same.B", ctConstructorCall.getType().getQualifiedName());
}
Also used : CompilationUnitFilter(spoon.support.compiler.jdt.CompilationUnitFilter) CtReturn(spoon.reflect.code.CtReturn) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 33 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testFilterChildrenWithoutFilterQueryStep.

@Test
public void testFilterChildrenWithoutFilterQueryStep() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    // Contract: the filterChildren(null) without Filter returns same results like filter which returns true for each input.
    List<CtElement> list = launcher.getFactory().Package().getRootPackage().filterChildren(null).list();
    Iterator<CtElement> iter = list.iterator();
    launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        return true;
    }).forEach(real -> {
        // the elements produced by each query are same
        CtElement expected = iter.next();
        if (real != expected) {
            assertEquals(expected, real);
        }
    });
    assertTrue(list.size() > 0);
    assertTrue(iter.hasNext() == false);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 34 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testQueryWithOptionalNumberOfInputs.

@Test
public void testQueryWithOptionalNumberOfInputs() throws Exception {
    // contract: QueryFactory allows to create query with an optional number of inputs
    // the input can be provided as Array or Iterable
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    CtClass<?> cls2 = launcher.getFactory().Class().get(Tostada.class);
    CtClass<?> cls3 = launcher.getFactory().Class().get(Antojito.class);
    // here is the query
    CtQuery q1 = launcher.getFactory().Query().createQuery(cls, cls2).map((CtClass c) -> c.getSimpleName());
    assertArrayEquals(new String[] { "Tacos", "Tostada" }, q1.list().toArray());
    CtQuery q1b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2)).map((CtClass c) -> c.getSimpleName());
    assertArrayEquals(new String[] { "Tacos", "Tostada" }, q1b.list().toArray());
    CtQuery q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName());
    assertArrayEquals(new String[] { "Tacos", "Antojito" }, q2.list().toArray());
    CtQuery q2b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls3)).map((CtClass c) -> c.getSimpleName());
    assertArrayEquals(new String[] { "Tacos", "Antojito" }, q2b.list().toArray());
    CtQuery q3 = launcher.getFactory().Query().createQuery(cls, cls2, cls3).map((CtClass c) -> c.getSimpleName());
    assertArrayEquals(new String[] { "Tacos", "Tostada", "Antojito" }, q3.list().toArray());
    CtQuery q3b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2, cls3)).map((CtClass c) -> c.getSimpleName());
    assertArrayEquals(new String[] { "Tacos", "Tostada", "Antojito" }, q3b.list().toArray());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 35 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testEarlyTerminatingQuery.

@Test
public void testEarlyTerminatingQuery() throws Exception {
    // contract: a method first evaluates query until first element is found and then terminates the query
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        boolean wasTerminated = false;

        void failIfTerminated(String place) {
            assertTrue("The " + place + " is called after query was terminated.", wasTerminated == false);
        }
    }
    Context context = new Context();
    CtMethod firstMethod = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        context.failIfTerminated("Filter#match of filterChildren");
        return true;
    }).map((CtElement e) -> {
        context.failIfTerminated("Array returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return new CtElement[] { e, e };
    }).map((CtElement e) -> {
        context.failIfTerminated("List returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return Arrays.asList(new CtElement[] { e, e });
    }).map((CtElement e, CtConsumer<Object> out) -> {
        context.failIfTerminated("CtConsumableFunction#apply of map");
        if (e instanceof CtMethod) {
            // this call should pass and cause termination of the query
            out.accept(e);
            context.wasTerminated = true;
        // let it call out.accept(e); once more to check that next step is not called after query is terminated
        }
        out.accept(e);
    }).map(e -> {
        context.failIfTerminated("CtFunction#apply of map after CtConsumableFunction");
        return e;
    }).first(CtMethod.class);
    assertTrue(firstMethod != null);
    assertTrue(context.wasTerminated);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22