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;
PsiElement castTypeElement = descriptor.getPsiElement();
PsiTypeCastExpression cast = castTypeElement == null ? null : (PsiTypeCastExpression)castTypeElement.getParent();
if (cast != null) {
- removeCast(cast);
+ RedundantCastUtil.removeCast(cast);
}
}
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);
- }
- }
}
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));
--- /dev/null
+class Test {
+ void foo () {
+ int a = 1;
+ <selection>System.out.println("" + (Object)a);</selection>
+ }
+}
\ No newline at end of file
--- /dev/null
+class Test {
+ void foo () {
+ int a = 1;
+ newMethod(a);
+ }
+
+ private void newMethod(Object a) {
+ System.out.println("" + a);
+ }
+}
\ No newline at end of file
doTest();
}
+ public void testRedundantCast() throws Exception {
+ doTest();
+ }
+
private void doPrepareErrorTest(final String expectedMessage) throws Exception {
String expectedError = null;
try {
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
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>();