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());
}
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));
}
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);
}
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);
}
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;
}
}
Aggregations