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;
}
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;
}
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());
}
Aggregations