Search in sources :

Example 21 with TypeFilter

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 22 with TypeFilter

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));
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtNewClass(spoon.reflect.code.CtNewClass) List(java.util.List) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 23 with TypeFilter

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());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 24 with TypeFilter

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);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 25 with TypeFilter

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);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

TypeFilter (spoon.reflect.visitor.filter.TypeFilter)132 Test (org.junit.Test)119 Launcher (spoon.Launcher)54 Factory (spoon.reflect.factory.Factory)52 CtMethod (spoon.reflect.declaration.CtMethod)43 CtInvocation (spoon.reflect.code.CtInvocation)27 CtClass (spoon.reflect.declaration.CtClass)24 CtLiteral (spoon.reflect.code.CtLiteral)18 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)18 AbstractTest (fr.inria.AbstractTest)17 List (java.util.List)17 CtTypeReference (spoon.reflect.reference.CtTypeReference)17 ArrayList (java.util.ArrayList)14 CtBlock (spoon.reflect.code.CtBlock)10 CtConstructorCall (spoon.reflect.code.CtConstructorCall)10 CtIf (spoon.reflect.code.CtIf)10 CtStatement (spoon.reflect.code.CtStatement)10 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)10 File (java.io.File)9 CtElement (spoon.reflect.declaration.CtElement)8