Search in sources :

Example 1 with ASTCatchStatement

use of net.sourceforge.pmd.lang.java.ast.ASTCatchStatement in project pmd by pmd.

the class ExceptionAsFlowControlRule method visit.

@Override
public Object visit(ASTThrowStatement node, Object data) {
    ASTTryStatement parent = node.getFirstParentOfType(ASTTryStatement.class);
    if (parent == null) {
        return data;
    }
    for (parent = parent.getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent.getFirstParentOfType(ASTTryStatement.class)) {
        List<ASTCatchStatement> list = parent.findDescendantsOfType(ASTCatchStatement.class);
        for (ASTCatchStatement catchStmt : list) {
            ASTFormalParameter fp = (ASTFormalParameter) catchStmt.jjtGetChild(0);
            ASTType type = fp.getFirstDescendantOfType(ASTType.class);
            ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
            if (node.getFirstClassOrInterfaceTypeImage() != null && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) {
                addViolation(data, name);
            }
        }
    }
    return data;
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 2 with ASTCatchStatement

use of net.sourceforge.pmd.lang.java.ast.ASTCatchStatement in project pmd by pmd.

the class JUnitUseExpectedRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    List<ASTTryStatement> catches = node.findDescendantsOfType(ASTTryStatement.class);
    List<Node> found = new ArrayList<>();
    if (catches.isEmpty()) {
        return found;
    }
    for (ASTTryStatement trySt : catches) {
        ASTCatchStatement cStatement = getCatch(trySt);
        if (cStatement != null) {
            ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
            if (block.jjtGetNumChildren() != 0) {
                continue;
            }
            List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
            if (blocks.isEmpty()) {
                continue;
            }
            ASTBlockStatement st = blocks.get(blocks.size() - 1);
            ASTName name = st.getFirstDescendantOfType(ASTName.class);
            if (name != null && st.equals(name.getNthParent(5)) && "fail".equals(name.getImage())) {
                found.add(name);
                continue;
            }
            ASTThrowStatement th = st.getFirstDescendantOfType(ASTThrowStatement.class);
            if (th != null && st.equals(th.getNthParent(2))) {
                found.add(th);
                continue;
            }
        }
    }
    return found;
}
Also used : ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ArrayList(java.util.ArrayList) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)

Example 3 with ASTCatchStatement

use of net.sourceforge.pmd.lang.java.ast.ASTCatchStatement in project pmd by pmd.

the class AcceptanceTest method testCatchBlocks.

@Test
public void testCatchBlocks() {
    parseCode(TEST_CATCH_BLOCKS);
    ASTCatchStatement c = acu.findDescendantsOfType(ASTCatchStatement.class).get(0);
    ASTBlock a = c.findDescendantsOfType(ASTBlock.class).get(0);
    Scope s = a.getScope();
    Map<NameDeclaration, List<NameOccurrence>> vars = s.getParent().getDeclarations();
    assertEquals(1, vars.size());
    NameDeclaration v = vars.keySet().iterator().next();
    assertEquals("e", v.getImage());
    assertEquals(1, (vars.get(v)).size());
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement) Test(org.junit.Test)

Aggregations

ASTCatchStatement (net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)3 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTThrowStatement (net.sourceforge.pmd.lang.java.ast.ASTThrowStatement)1 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)1 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)1 Scope (net.sourceforge.pmd.lang.symboltable.Scope)1 Test (org.junit.Test)1