Search in sources :

Example 6 with CtWhile

use of spoon.reflect.code.CtWhile in project spoon by INRIA.

the class ParentTest method testGetParentWithFilter.

@Test
public void testGetParentWithFilter() throws Exception {
    // addType should set Parent
    CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
    CtMethod<Object> m = clazz.getMethod("m");
    // get three = "" in one = two = three = "";
    CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
    CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
    assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
    CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element.getParent() instanceof CtStatementList && super.matches(element);
        }
    });
    // the filter has to return one = two = three = ""
    assertEquals(m.getBody().getStatement(3), ctStatement);
    m = clazz.getMethod("internalClass");
    CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element instanceof CtLocalVariable && super.matches(element);
        }
    }).get(0);
    // get the top class
    ctStatement1.getParent(CtType.class);
    CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {

        @Override
        public boolean matches(CtType element) {
            return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
        }
    });
    assertEquals(clazz, parent);
    assertNotEquals(ctStatement1.getParent(CtType.class), parent);
    // not present element
    CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
    assertEquals(null, ctWhile);
    CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return true;
        }
    });
    // getParent must not return the current element
    assertNotEquals(statement, statementParent);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExpression(spoon.reflect.code.CtExpression) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtWhile(spoon.reflect.code.CtWhile) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) Test(org.junit.Test)

Aggregations

CtWhile (spoon.reflect.code.CtWhile)6 Test (org.junit.Test)4 CtStatement (spoon.reflect.code.CtStatement)3 Launcher (spoon.Launcher)2 CtBlock (spoon.reflect.code.CtBlock)2 CtIf (spoon.reflect.code.CtIf)2 CtClass (spoon.reflect.declaration.CtClass)2 Factory (spoon.reflect.factory.Factory)2 CtAssignment (spoon.reflect.code.CtAssignment)1 CtBreak (spoon.reflect.code.CtBreak)1 CtCase (spoon.reflect.code.CtCase)1 CtContinue (spoon.reflect.code.CtContinue)1 CtDo (spoon.reflect.code.CtDo)1 CtExpression (spoon.reflect.code.CtExpression)1 CtFor (spoon.reflect.code.CtFor)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CtSwitch (spoon.reflect.code.CtSwitch)1 CtElement (spoon.reflect.declaration.CtElement)1