Example #1
0
    /** {@inheritDoc} */
    @Override
    public void rewriteAST(CompilationUnitRewrite cuRewrite) throws CoreException {
      ASTRewrite rewrite = cuRewrite.getASTRewrite();

      TextEditGroup group =
          createTextEditGroup(
              FixMessages.INSTANCE.UnusedCodeFix_RemoveCast_description(), cuRewrite);

      while (fUnnecessaryCasts.size() > 0) {
        CastExpression castExpression = fUnnecessaryCasts.iterator().next();
        fUnnecessaryCasts.remove(castExpression);
        CastExpression down = castExpression;
        while (fUnnecessaryCasts.contains(down.getExpression())) {
          down = (CastExpression) down.getExpression();
          fUnnecessaryCasts.remove(down);
        }

        Expression expression = down.getExpression();
        ASTNode move = rewrite.createMoveTarget(expression);

        CastExpression top = castExpression;
        while (fUnnecessaryCasts.contains(top.getParent())) {
          top = (CastExpression) top.getParent();
          fUnnecessaryCasts.remove(top);
        }

        ASTNode toReplace = top;
        if (top.getParent() instanceof ParenthesizedExpression
            && !NecessaryParenthesesChecker.needsParentheses(
                expression, top.getParent(), top.getLocationInParent())) {
          toReplace = top.getParent();
        }
        rewrite.replace(toReplace, move, group);
      }
    }
Example #2
0
    /** {@inheritDoc} */
    @Override
    public void rewriteAST(CompilationUnitRewrite cuRewrite) throws CoreException {

      TextEditGroup group =
          createTextEditGroup(
              FixMessages.INSTANCE.UnusedCodeFix_RemoveCast_description(), cuRewrite);

      ASTRewrite rewrite = cuRewrite.getASTRewrite();

      CastExpression cast = fCast;
      Expression expression = cast.getExpression();
      ASTNode placeholder = rewrite.createCopyTarget(expression);

      if (NecessaryParenthesesChecker.needsParentheses(
          expression, cast.getParent(), cast.getLocationInParent())) {
        rewrite.replace(fCast, placeholder, group);
      } else {
        rewrite.replace(
            fCast.getParent() instanceof ParenthesizedExpression
                ? fCast.getParent()
                : fSelectedNode,
            placeholder,
            group);
      }
    }