Search in sources :

Example 1 with CompositeFilter

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

use of spoon.reflect.visitor.filter.CompositeFilter in project spoon by INRIA.

the class FilterTest method classCastExceptionIsNotThrown.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void classCastExceptionIsNotThrown() throws Exception {
    Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
    NamedElementFilter<CtVariable> nameFilterA = new NamedElementFilter<>(CtVariable.class, "j");
    NamedElementFilter<CtVariable> nameFilterB = new NamedElementFilter<>(CtVariable.class, "k");
    CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.INTERSECTION, nameFilterA, nameFilterB);
    List filteredWithCompositeFilter = Query.getElements(factory, compositeFilter);
    assertTrue(filteredWithCompositeFilter.isEmpty());
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtVariable(spoon.reflect.declaration.CtVariable) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Factory(spoon.reflect.factory.Factory) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with CompositeFilter

use of spoon.reflect.visitor.filter.CompositeFilter in project spoon by INRIA.

the class FilterTest method intersectionOfTwoFilters.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void intersectionOfTwoFilters() throws Exception {
    Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
    TypeFilter<CtMethod> statementFilter = new TypeFilter<CtMethod>(CtMethod.class);
    TypeFilter<CtMethodImpl> statementImplFilter = new TypeFilter<CtMethodImpl>(CtMethodImpl.class);
    CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.INTERSECTION, statementFilter, statementImplFilter);
    List<CtMethod> methodsWithInterfaceSuperclass = Query.getElements(factory, statementFilter);
    List<CtMethodImpl> methodWithConcreteClass = Query.getElements(factory, statementImplFilter);
    assertEquals(methodsWithInterfaceSuperclass.size(), methodWithConcreteClass.size());
    assertEquals(methodsWithInterfaceSuperclass, methodWithConcreteClass);
    List intersection = Query.getElements(factory, compositeFilter);
    assertEquals(methodsWithInterfaceSuperclass.size(), intersection.size());
    assertEquals(methodsWithInterfaceSuperclass, intersection);
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) List(java.util.List) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.junit.Test)3 Factory (spoon.reflect.factory.Factory)3 CompositeFilter (spoon.reflect.visitor.filter.CompositeFilter)3 CtMethod (spoon.reflect.declaration.CtMethod)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 CtNewClass (spoon.reflect.code.CtNewClass)1 CtElement (spoon.reflect.declaration.CtElement)1 CtVariable (spoon.reflect.declaration.CtVariable)1 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)1 CtMethodImpl (spoon.support.reflect.declaration.CtMethodImpl)1