private boolean areEquivalent(ASTCatchStatement st1, ASTCatchStatement st2) { return hasSameSubTree(st1.getBlock(), st2.getBlock(), st1.getExceptionName(), st2.getExceptionName()); }
/** * Returns true if this node is a multi-catch statement, * that is, it catches several unrelated exception types * at the same time. Such a block can be declared like the * following for example: * * <p>{@code catch (IllegalStateException | IllegalArgumentException e) {}} * * @return True if this node is a multi-catch statement */ public boolean isMulticatchStatement() { return getCaughtExceptionTypeNodes().size() > 1; // the list is parsed multiple times... }
/** * Returns the Block node of this catch branch. */ public ASTBlock getBlock() { return getFirstChildOfType(ASTBlock.class); }
final public void CatchStatement() throws ParseException { ASTCatchStatement jjtn000 = new ASTCatchStatement(this, JJTCATCHSTATEMENT); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { jj_consume_token(CATCH); if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
@Override public Object visit(ASTCatchStatement catchStmt, Object data) { String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage(); List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class); for (ASTThrowStatement throwStatement : lstThrowStatements) { Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
/** * Returns exception name caught by this catch block. */ public String getExceptionName() { return getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(); }
ASTCatchStatement cStatement = getCatch(trySt); if (cStatement != null) { ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1); if (block.jjtGetNumChildren() != 0) { continue;
final public void CatchStatement() throws ParseException { ASTCatchStatement jjtn000 = new ASTCatchStatement(this, JJTCATCHSTATEMENT); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { jj_consume_token(CATCH); if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
@Override public Object visit(ASTCatchStatement catchStmt, Object data) { String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage(); List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class); for (ASTThrowStatement throwStatement : lstThrowStatements) { Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
@Override public Object visit(ASTCatchStatement node, Object data) { ASTType type = node.getFirstDescendantOfType(ASTType.class); ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (name.hasImageEqualTo("Throwable")) { addViolation(data, name); } return super.visit(node, data); } }
@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; }
private boolean areEquivalent(ASTCatchStatement st1, ASTCatchStatement st2) { return hasSameSubTree(st1.getBlock(), st2.getBlock(), st1.getExceptionName(), st2.getExceptionName()); }
private String getCaughtExceptionsAsString(ASTCatchStatement stmt) { StringBuilder sb = new StringBuilder(); final String delim = " | "; for (ASTType type : stmt.getCaughtExceptionTypeNodes()) { sb.append(type.getTypeImage()).append(delim); } // remove the last delimiter sb.replace(sb.length() - 3, sb.length(), ""); return sb.toString(); }
/** * Returns the list of type nodes denoting the exception types * caught by this catch block. The returned list has at least * one element. */ public List<ASTType> getCaughtExceptionTypeNodes() { // maybe cache the list return getFirstChildOfType(ASTFormalParameter.class).findChildrenOfType(ASTType.class); }
/** * Returns exception name caught by this catch block. */ public String getExceptionName() { return getFirstDescendantOfType(ASTVariableDeclaratorId.class).getImage(); }
ASTCatchStatement cStatement = getCatch(trySt); if (cStatement != null) { ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1); if (block.jjtGetNumChildren() != 0) { continue;
/** * Returns the list of exception types caught by this catch block. * Any of these can be null, if they couldn't be resolved. This can * happen if the auxclasspath is not correctly set. */ @SuppressWarnings("unchecked") public List<Class<? extends Exception>> getCaughtExceptionTypes() { List<Class<? extends Exception>> result = new ArrayList<>(); for (ASTType type : getCaughtExceptionTypeNodes()) { result.add((Class<? extends Exception>) type.getType()); } return result; }
/** * Returns the Block node of this catch branch. */ public ASTBlock getBlock() { return getFirstChildOfType(ASTBlock.class); }
@Override public Object visit(ASTCatchStatement node, Object data) { ASTType type = node.getFirstDescendantOfType(ASTType.class); ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (name.hasImageEqualTo("Throwable")) { addViolation(data, name); } return super.visit(node, data); } }
@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; }