コード例 #1
0
 @Override
 public boolean visit(WhileStatement node) {
   final Object constantCondition = node.getExpression().resolveConstantExpressionValue();
   if (Boolean.FALSE.equals(constantCondition)) {
     this.ctx.getRefactorings().remove(node);
     return DO_NOT_VISIT_SUBTREE;
   }
   return VISIT_SUBTREE;
 }
コード例 #2
0
  @Override
  public boolean visit(IfStatement node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();

    final Statement thenStmt = node.getThenStatement();
    final Statement elseStmt = node.getElseStatement();
    final Expression condition = node.getExpression();
    if (elseStmt != null && asList(elseStmt).isEmpty()) {
      r.remove(elseStmt);
      return DO_NOT_VISIT_SUBTREE;
    } else if (thenStmt != null && asList(thenStmt).isEmpty()) {
      if (elseStmt != null) {
        r.replace(node, b.if0(b.negate(condition), b.move(elseStmt)));
      } else {
        final List<Expression> sideEffectExprs = new ArrayList<Expression>();
        collectSideEffects(condition, sideEffectExprs);
        if (!sideEffectExprs.isEmpty()) {
          for (Expression sideEffectExpr : sideEffectExprs) {
            r.insertBefore(b.toStmt(b.move(sideEffectExpr)), node);
          }
        }
        r.remove(node);
      }
      return DO_NOT_VISIT_SUBTREE;
    }

    final Object constantCondition = condition.resolveConstantExpressionValue();
    if (Boolean.TRUE.equals(constantCondition)) {
      r.replace(node, b.copy(thenStmt));
      if (lastStmtIsThrowOrReturn(thenStmt)) {
        r.remove(getNextSiblings(node));
      }
      return DO_NOT_VISIT_SUBTREE;
    } else if (Boolean.FALSE.equals(constantCondition)) {
      if (elseStmt != null) {
        r.replace(node, b.copy(elseStmt));
        if (lastStmtIsThrowOrReturn(elseStmt)) {
          r.remove(getNextSiblings(node));
        }
      } else {
        r.remove(node);
      }
      return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
  }