/* * @see ASTVisitor#visit(TryStatement) */ @Override public boolean visit(TryStatement node) { this.fBuffer.append("try "); // $NON-NLS-1$ if (node.getAST().apiLevel() >= JLS4) { if (!node.resources().isEmpty()) { this.fBuffer.append("("); // $NON-NLS-1$ for (Iterator<VariableDeclarationExpression> it = node.resources().iterator(); it.hasNext(); ) { VariableDeclarationExpression var = it.next(); var.accept(this); if (it.hasNext()) { this.fBuffer.append(","); // $NON-NLS-1$ } } this.fBuffer.append(") "); // $NON-NLS-1$ } } node.getBody().accept(this); this.fBuffer.append(" "); // $NON-NLS-1$ for (Iterator<CatchClause> it = node.catchClauses().iterator(); it.hasNext(); ) { CatchClause cc = it.next(); cc.accept(this); } if (node.getFinally() != null) { this.fBuffer.append("finally "); // $NON-NLS-1$ node.getFinally().accept(this); } return false; }
@Override public boolean visit(TryStatement node) { final List<Statement> tryStmts = asList(node.getBody()); if (tryStmts.isEmpty()) { final List<Statement> finallyStmts = asList(node.getFinally()); if (!finallyStmts.isEmpty()) { final ASTBuilder b = this.ctx.getASTBuilder(); this.ctx.getRefactorings().replace(node, b.copy(node.getFinally())); return DO_NOT_VISIT_SUBTREE; } else if (node.resources().isEmpty()) { this.ctx.getRefactorings().remove(node); return DO_NOT_VISIT_SUBTREE; } } // }else { // for (CatchClause catchClause : (List<CatchClause>) node.catchClauses()) { // final List<Statement> finallyStmts = asList(catchClause.getBody()); // if (finallyStmts.isEmpty()) { // // TODO cannot remove without checking what subsequent catch clauses are // catching // this.ctx.getRefactorings().remove(catchClause); // } // } // // final List<Statement> finallyStmts = asList(node.getFinally()); // if (finallyStmts.isEmpty()) { // this.ctx.getRefactorings().remove(node.getFinally()); // } // // TODO If all finally and catch clauses have been removed, // // then we can remove the whole try statement and replace it with a simple block // return DO_NOT_VISIT_SUBTREE; // TODO JNR is this correct? // } return VISIT_SUBTREE; }