Search in sources :

Example 1 with CatchVariableScopeFunction

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

the class VariableReferencesTest method testVariableScopeFunction.

@Test
public void testVariableScopeFunction() throws Exception {
    // visits all the CtVariable elements whose name is "field" and search for all elements in their scopes
    // Comparing with the result found by basic functions
    List list = modelClass.filterChildren((CtVariable<?> var) -> {
        if (var.getSimpleName().equals("field")) {
            if (var instanceof CtField) {
                // field scope is not supported
                return false;
            }
            CtElement[] real = var.map(new VariableScopeFunction()).list().toArray(new CtElement[0]);
            if (var instanceof CtLocalVariable) {
                assertArrayEquals(var.map(new LocalVariableScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtField) {
            // assertArrayEquals(var.map(new FieldScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtParameter) {
                assertArrayEquals(var.map(new ParameterScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtCatchVariable) {
                assertArrayEquals(var.map(new CatchVariableScopeFunction()).list().toArray(new CtElement[0]), real);
            } else {
                fail("Unexpected variable of type " + var.getClass().getName());
            }
            return true;
        }
        return false;
    }).list();
    assertTrue(list.size() > 0);
}
Also used : CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableScopeFunction(spoon.reflect.visitor.filter.VariableScopeFunction) ParameterScopeFunction(spoon.reflect.visitor.filter.ParameterScopeFunction) CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) CtField(spoon.reflect.declaration.CtField) CtElement(spoon.reflect.declaration.CtElement) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Aggregations

List (java.util.List)1 Test (org.junit.Test)1 CtCatchVariable (spoon.reflect.code.CtCatchVariable)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtElement (spoon.reflect.declaration.CtElement)1 CtField (spoon.reflect.declaration.CtField)1 CtParameter (spoon.reflect.declaration.CtParameter)1 CatchVariableScopeFunction (spoon.reflect.visitor.filter.CatchVariableScopeFunction)1 LocalVariableScopeFunction (spoon.reflect.visitor.filter.LocalVariableScopeFunction)1 ParameterScopeFunction (spoon.reflect.visitor.filter.ParameterScopeFunction)1 VariableScopeFunction (spoon.reflect.visitor.filter.VariableScopeFunction)1 VariableReferencesModelTest (spoon.test.query_function.testclasses.VariableReferencesModelTest)1