use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class CtClassTest method testCloneAnonymousClassInvocation.
@Test
public void testCloneAnonymousClassInvocation() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when not using autoimport
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(false);
launcher.buildModel();
CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();
CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();
// The test stops failing if we set the parent below
// newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));
assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());
assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);
assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class FilterTest method unionOfTwoFilters.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void unionOfTwoFilters() throws Exception {
Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
TypeFilter<CtNewClass> newClassFilter = new TypeFilter<CtNewClass>(CtNewClass.class);
TypeFilter<CtMethod> methodFilter = new TypeFilter<CtMethod>(CtMethod.class);
CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.UNION, methodFilter, newClassFilter);
List filteredWithCompositeFilter = Query.getElements(factory, compositeFilter);
List<CtMethod> methods = Query.getElements(factory, methodFilter);
List<CtNewClass> newClasses = Query.getElements(factory, newClassFilter);
List<CtElement> union = new ArrayList<CtElement>();
union.addAll(methods);
union.addAll(newClasses);
assertEquals(methods.size() + newClasses.size(), union.size());
assertEquals(union.size(), filteredWithCompositeFilter.size());
assertTrue(filteredWithCompositeFilter.containsAll(union));
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class FilterTest method testInvocationFilterWithExecutableInLibrary.
@Test
public void testInvocationFilterWithExecutableInLibrary() throws Exception {
// contract: When we have an invocation of an executable declared in a library,
// we can filter it and get the executable of the invocation.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtInvocation<?> invSize = aTacos.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
if (element.getExecutable() == null) {
return false;
}
return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
final List<CtInvocation<?>> invocations = aTacos.getElements(new InvocationFilter(invSize.getExecutable()));
assertEquals(1, invocations.size());
final CtInvocation<?> expectedInv = invocations.get(0);
assertNotNull(expectedInv);
final CtExecutableReference<?> expectedExecutable = expectedInv.getExecutable();
assertNotNull(expectedExecutable);
assertEquals("size", expectedExecutable.getSimpleName());
assertNull(expectedExecutable.getDeclaration());
CtExecutable<?> exec = expectedExecutable.getExecutableDeclaration();
assertEquals("size", exec.getSimpleName());
assertEquals("ArrayList", ((CtClass) exec.getParent()).getSimpleName());
final CtExecutable<?> declaration = expectedExecutable.getExecutableDeclaration();
assertNotNull(declaration);
assertEquals("size", declaration.getSimpleName());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class FilterTest method testFilterQueryStep.
@Test
public void testFilterQueryStep() 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 filter(Filter) can be used to detect if input of query step should pass to next query step.
List<CtElement> realList = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
return true;
}).select(new TypeFilter<>(CtClass.class)).list();
List<CtElement> expectedList = launcher.getFactory().Package().getRootPackage().filterChildren(new TypeFilter<>(CtClass.class)).list();
assertArrayEquals(expectedList.toArray(), realList.toArray());
assertTrue(expectedList.size() > 0);
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class GenericsTest method testInvocationGenerics.
@Test
public void testInvocationGenerics() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtConstructor<?> defaultConstructor = aTacos.getConstructor();
final CtInvocation<?> explicitConstructorCall = (CtInvocation<?>) defaultConstructor.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(1, explicitConstructorCall.getExecutable().getActualTypeArguments().size());
assertEquals("<java.lang.String>this(1)", explicitConstructorCall.toString());
final CtMethod<?> m = aTacos.getMethodsByName("m2").get(0);
final CtInvocation invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(1, invocation1.getExecutable().getActualTypeArguments().size());
assertEquals("this.<java.lang.String>makeTacos(null)", invocation1.toString());
final CtInvocation invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
assertEquals("this.makeTacos(null)", invocation2.toString());
canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
Aggregations