remove redundant casts when extract changed parameter type accordingly (IDEA-79743)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 1 Feb 2012 15:56:29 +0000 (19:56 +0400)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Thu, 2 Feb 2012 07:03:55 +0000 (11:03 +0400)
java/java-impl/src/com/intellij/codeInspection/redundantCast/RedundantCastInspection.java
java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java
java/java-tests/testData/refactoring/extractMethod/RedundantCast.java [new file with mode: 0644]
java/java-tests/testData/refactoring/extractMethod/RedundantCast_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java
java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java

index 7ed4a29690f622d90251a56e2b4dad18af2e96bb..6ec1a346bbe08841df4a6dc9c0d8dbdb38e76b2b 100644 (file)
@@ -28,7 +28,6 @@ import com.intellij.openapi.util.WriteExternalException;
 import com.intellij.psi.*;
 import com.intellij.psi.util.PsiUtil;
 import com.intellij.psi.util.RedundantCastUtil;
-import com.intellij.util.IncorrectOperationException;
 import com.intellij.util.containers.IntArrayList;
 import org.jdom.Element;
 import org.jetbrains.annotations.NonNls;
@@ -139,7 +138,7 @@ public class RedundantCastInspection extends GenericsInspectionToolBase {
       PsiElement castTypeElement = descriptor.getPsiElement();
       PsiTypeCastExpression cast = castTypeElement == null ? null : (PsiTypeCastExpression)castTypeElement.getParent();
       if (cast != null) {
-        removeCast(cast);
+        RedundantCastUtil.removeCast(cast);
       }
     }
 
@@ -163,29 +162,4 @@ public class RedundantCastInspection extends GenericsInspectionToolBase {
   public String getShortName() {
     return SHORT_NAME;
   }
-
-  private 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);
-    }
-  }
 }
index a9c46f8bb322e622f8568b57a63be9ee2d638da4..f1a0966c1dc6d0b8f840dafc7a4a681524579352 100644 (file)
@@ -758,6 +758,23 @@ public class ExtractMethodProcessor implements MatchProvider {
 
     adjustFinalParameters(newMethod);
 
+    for (int i = 0, length = myVariableDatum.length; i < length; i++) {
+      ParameterTablePanel.VariableData data = myVariableDatum[i];
+      final PsiVariable variable = data.variable;
+      final PsiParameter psiParameter = newMethod.getParameterList().getParameters()[i];
+      if (!TypeConversionUtil.isAssignable(variable.getType(), psiParameter.getType())) {
+        for (PsiReference reference : ReferencesSearch.search(psiParameter, new LocalSearchScope(body))){
+          final PsiElement element = reference.getElement();
+          if (element != null) {
+            final PsiElement parent = element.getParent();
+            if (parent instanceof PsiTypeCastExpression) {
+              RedundantCastUtil.removeCast((PsiTypeCastExpression)parent);
+            }
+          }
+        }
+      }
+    }
+
     myExtractedMethod = (PsiMethod)myTargetClass.addAfter(newMethod, myAnchor);
     if (isNeedToChangeCallContext() && myNeedChangeContext) {
       ChangeContextUtil.decodeContextInfo(myExtractedMethod, myTargetClass, RefactoringUtil.createThisExpression(myManager, null));
diff --git a/java/java-tests/testData/refactoring/extractMethod/RedundantCast.java b/java/java-tests/testData/refactoring/extractMethod/RedundantCast.java
new file mode 100644 (file)
index 0000000..b2f77df
--- /dev/null
@@ -0,0 +1,6 @@
+class Test {
+    void foo () {
+        int a = 1;
+        <selection>System.out.println("" + (Object)a);</selection>
+    }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/extractMethod/RedundantCast_after.java b/java/java-tests/testData/refactoring/extractMethod/RedundantCast_after.java
new file mode 100644 (file)
index 0000000..473819a
--- /dev/null
@@ -0,0 +1,10 @@
+class Test {
+    void foo () {
+        int a = 1;
+        newMethod(a);
+    }
+
+    private void newMethod(Object a) {
+        System.out.println("" + a);
+    }
+}
\ No newline at end of file
index 27478f6f787ce9f509c829d00af9ac0a9570bd17..cf4e75003a508f54cb4efef3c77f68ae2deaa486 100644 (file)
@@ -497,6 +497,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
     doTest();
   }
 
+  public void testRedundantCast() throws Exception {
+    doTest();
+  }
+
   private void doPrepareErrorTest(final String expectedMessage) throws Exception {
     String expectedError = null;
     try {
index 6ead55d1a05b958fda2eb6f74ecc2bfb3cd9bba4..71e45859578caa14efc71cd57b89549322dd5d7b 100644 (file)
@@ -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>();