use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FilterTest method testQueryInQuery.
@Test
public void testQueryInQuery() 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();
CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
// first query
CtQuery allChildPublicClasses = launcher.getFactory().Query().createQuery().filterChildren((CtClass clazz) -> clazz.hasModifier(ModifierKind.PUBLIC));
// second query,involving the first query
CtQuery q = launcher.getFactory().Package().getRootPackage().map((CtElement in) -> allChildPublicClasses.setInput(in).list());
// now the assertions
q.forEach((CtElement clazz) -> {
context.count++;
assertTrue(clazz instanceof CtClass);
assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
});
assertEquals(6, context.count);
// reset
context.count = 0;
// again second query, but now with CtConsumableFunction
CtQuery q2 = launcher.getFactory().Package().getRootPackage().map((CtElement in, CtConsumer<Object> out) -> allChildPublicClasses.setInput(in).forEach(out));
// now the assertions
q2.forEach((CtElement clazz) -> {
context.count++;
assertTrue(clazz instanceof CtClass);
assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
});
assertEquals(6, context.count);
// reset
context.count = 0;
// again second query, but with low-level circuitry thanks to cast
CtQuery q3 = launcher.getFactory().Package().getRootPackage().map((in, out) -> ((CtQueryImpl) allChildPublicClasses).evaluate(in, out));
// now the assertions
q3.forEach((CtElement clazz) -> {
context.count++;
assertTrue(clazz instanceof CtClass);
assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
});
assertEquals(6, context.count);
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FilterTest method testQueryStepScannWithConsumer.
@Test
public void testQueryStepScannWithConsumer() 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();
class Context {
int counter = 0;
}
Context context = new Context();
CtQuery l_qv = launcher.getFactory().getModel().filterChildren(new TypeFilter<>(CtClass.class));
assertEquals(0, context.counter);
l_qv.forEach(cls -> {
assertTrue(cls instanceof CtClass);
context.counter++;
});
assertTrue(context.counter > 0);
}
use of spoon.reflect.declaration.CtClass 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);
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FieldAccessTest method testFieldAccessAutoExplicit.
@Test
public void testFieldAccessAutoExplicit() throws Exception {
CtClass mouse = (CtClass) ModelUtils.buildClass(Mouse.class);
CtMethod method = mouse.filterChildren((CtMethod m) -> "meth1".equals(m.getSimpleName())).first();
CtFieldReference ageFR = method.filterChildren((CtFieldReference fr) -> "age".equals(fr.getSimpleName())).first();
// first is the field printed with implicit "this."
assertEquals("age", ageFR.getParent().toString());
// add local variable declaration which hides the field declaration
method.getBody().insertBegin((CtStatement) mouse.getFactory().createCodeSnippetStatement("int age = 1").compile());
// now the field access must use explicit "this."
assertEquals("this.age", ageFR.getParent().toString());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CtBodyHolderTest method testConstructor.
@Test
public void testConstructor() throws Exception {
Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
assertEquals(1, cwbClass.getConstructors().size());
CtConstructor<?> constructor = cwbClass.getConstructor();
checkCtBody(constructor, "constructor_body", 1);
}
Aggregations