Search in sources :

Example 1 with FieldAccessFilter

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

the class FilterTest method testFieldAccessFilter.

@Test
public void testFieldAccessFilter() throws Exception {
    // also specifies VariableAccessFilter since FieldAccessFilter is only a VariableAccessFilter with additional static typing
    CtClass<?> foo = factory.Package().get("spoon.test.filters").getType("Foo");
    assertEquals("Foo", foo.getSimpleName());
    List<CtNamedElement> elements = foo.getElements(new NamedElementFilter<>(CtNamedElement.class, "i"));
    assertEquals(1, elements.size());
    CtFieldReference<?> ref = (CtFieldReference<?>) (elements.get(0)).getReference();
    List<CtFieldAccess<?>> expressions = foo.getElements(new FieldAccessFilter(ref));
    assertEquals(2, expressions.size());
    final Factory build = build(FieldAccessFilterTacos.class);
    final CtType<FieldAccessFilterTacos> fieldAccessFilterTacos = build.Type().get(FieldAccessFilterTacos.class);
    try {
        List<CtField> fields = fieldAccessFilterTacos.getElements(new TypeFilter<CtField>(CtField.class));
        for (CtField ctField : fields) {
            fieldAccessFilterTacos.getElements(new FieldAccessFilter(ctField.getReference()));
        }
    } catch (NullPointerException e) {
        fail("FieldAccessFilter must not throw a NPE.");
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldReference(spoon.reflect.reference.CtFieldReference) Factory(spoon.reflect.factory.Factory) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) CtField(spoon.reflect.declaration.CtField) CtNamedElement(spoon.reflect.declaration.CtNamedElement) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 CtFieldAccess (spoon.reflect.code.CtFieldAccess)1 CtField (spoon.reflect.declaration.CtField)1 CtNamedElement (spoon.reflect.declaration.CtNamedElement)1 Factory (spoon.reflect.factory.Factory)1 CtFieldReference (spoon.reflect.reference.CtFieldReference)1 FieldAccessFilter (spoon.reflect.visitor.filter.FieldAccessFilter)1 FieldAccessFilterTacos (spoon.test.filters.testclasses.FieldAccessFilterTacos)1