mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
remove redundant casts when extract changed parameter type accordingly (IDEA-79743)
This commit is contained in:
+1
-27
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user