Search in sources :

Example 11 with CtStatementList

use of spoon.reflect.code.CtStatementList 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

CtStatementList (spoon.reflect.code.CtStatementList)11 CtStatement (spoon.reflect.code.CtStatement)5 CtElement (spoon.reflect.declaration.CtElement)4 ArrayList (java.util.ArrayList)3 CtCatch (spoon.reflect.code.CtCatch)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtField (spoon.reflect.declaration.CtField)3 CtPackage (spoon.reflect.declaration.CtPackage)3 CtType (spoon.reflect.declaration.CtType)3 CtExpression (spoon.reflect.code.CtExpression)2 CtClass (spoon.reflect.declaration.CtClass)2 CtMethod (spoon.reflect.declaration.CtMethod)2 File (java.io.File)1 List (java.util.List)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1