Search in sources :

Example 1 with CtQuery

use of spoon.reflect.visitor.chain.CtQuery 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 2 with CtQuery

use of spoon.reflect.visitor.chain.CtQuery in project spoon by INRIA.

the class FilterTest method testReuseOfBaseQuery.

@Test
public void testReuseOfBaseQuery() throws Exception {
    // contract: an empty  query can be used on several inputs
    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);
    // here is the query
    CtQuery q = launcher.getFactory().Query().createQuery().map((CtClass c) -> c.getSimpleName());
    // using it on a first input
    assertEquals("Tacos", q.setInput(cls).list().get(0));
    // using it on a second input
    assertEquals("Tostada", q.setInput(cls2).list().get(0));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 3 with CtQuery

use of spoon.reflect.visitor.chain.CtQuery in project spoon by INRIA.

the class FilterTest method testQueryBuilderWithFilterChain.

@Test
public void testQueryBuilderWithFilterChain() throws Exception {
    // contract: query methods can be lazy evaluated in a foreach
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        CtMethod<?> method;

        int count = 0;
    }
    Context context = new Context();
    // chaining queries
    CtQuery q = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<CtMethod<?>>(CtMethod.class)).map((CtMethod<?> method) -> {
        context.method = method;
        return method;
    }).map(new OverriddenMethodQuery());
    // actual evaluation
    q.forEach((CtMethod<?> method) -> {
        assertTrue(context.method.getReference().isOverriding(method.getReference()));
        assertTrue(context.method.isOverriding(method));
        context.count++;
    });
    // sanity check
    assertTrue(context.count > 0);
}
Also used : OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 4 with CtQuery

use of spoon.reflect.visitor.chain.CtQuery in project spoon by INRIA.

the class FilterTest method testFunctionQueryStep.

@Test
public void testFunctionQueryStep() 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();
    class Context {

        int count = 0;
    }
    Context context = new Context();
    CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
        return true;
    }).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
    ((CtQueryImpl) query).logging(true);
    query.forEach((CtInterface<?> c) -> {
        assertEquals("ITostada", c.getSimpleName());
        context.count++;
    });
    assertTrue(context.count > 0);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 5 with CtQuery

use of spoon.reflect.visitor.chain.CtQuery in project spoon by INRIA.

the class PotentialVariableDeclarationFunction method apply.

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
    isTypeOnTheWay = false;
    isInStaticScope = false;
    // Search previous siblings for element which may represents the declaration of this local variable
    CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
    if (variableName != null) {
        // variable name is defined so we have to search only for variables with that name
        siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
    }
    CtElement scopeElement = input;
    // Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
    while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
        CtElement parent = scopeElement.getParent();
        if (parent instanceof CtType<?>) {
            isTypeOnTheWay = true;
            // visit each CtField of `parent` CtType
            CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
            q.forEach((CtField<?> field) -> {
                if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
                    /*
						 * the variable reference is used in static scope,
						 * but the field is not static - ignore it
						 */
                    return;
                }
                // else send field as potential variable declaration
                if (sendToOutput(field, outputConsumer)) {
                    // and terminate the internal query q if outer query is already terminated
                    q.terminate();
                }
            });
            if (query.isTerminated()) {
                return;
            }
        } else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
            // visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
            siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
            if (query.isTerminated()) {
                return;
            }
            // visit parameters of CtCatch and CtExecutable (method, lambda)
            if (parent instanceof CtCatch) {
                CtCatch ctCatch = (CtCatch) parent;
                if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
                    return;
                }
            } else if (parent instanceof CtExecutable) {
                CtExecutable<?> exec = (CtExecutable<?>) parent;
                for (CtParameter<?> param : exec.getParameters()) {
                    if (sendToOutput(param, outputConsumer)) {
                        return;
                    }
                }
            }
        }
        if (parent instanceof CtModifiable) {
            isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
        }
        scopeElement = parent;
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) CtExecutable(spoon.reflect.declaration.CtExecutable) CtBodyHolder(spoon.reflect.code.CtBodyHolder) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) CtCatch(spoon.reflect.code.CtCatch) CtModifiable(spoon.reflect.declaration.CtModifiable)

Aggregations

CtQuery (spoon.reflect.visitor.chain.CtQuery)16 Test (org.junit.Test)10 Launcher (spoon.Launcher)10 CtType (spoon.reflect.declaration.CtType)7 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)5 CtMethod (spoon.reflect.declaration.CtMethod)5 HashSet (java.util.HashSet)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 ArrayList (java.util.ArrayList)2 SpoonException (spoon.SpoonException)2 CtField (spoon.reflect.declaration.CtField)2 CtQueryImpl (spoon.reflect.visitor.chain.CtQueryImpl)2 ArrayDeque (java.util.ArrayDeque)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1