remove redundant casts when extract changed parameter type accordingly (IDEA-79743)

This commit is contained in:
Anna Kozlova
2012-02-02 11:03:55 +04:00
parent 41c22fd6b9
commit 7d870ed603
6 changed files with 67 additions and 28 deletions
@@ -27,7 +27,10 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author max
@@ -65,6 +68,31 @@ public class RedundantCastUtil {
return arg;
}
public static void removeCast(PsiTypeCastExpression castExpression) {
if (castExpression == null) return;
PsiExpression operand = castExpression.getOperand();
if (operand instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parExpr = (PsiParenthesizedExpression)operand;
operand = parExpr.getExpression();
}
if (operand == null) return;
PsiElement toBeReplaced = castExpression;
PsiElement parent = castExpression.getParent();
while (parent instanceof PsiParenthesizedExpression) {
toBeReplaced = parent;
parent = parent.getParent();
}
try {
toBeReplaced.replace(operand);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
private static class MyCollectingVisitor extends MyIsRedundantVisitor {
private final Set<PsiTypeCastExpression> myFoundCasts = new HashSet<PsiTypeCastExpression>();