mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
extract method: insert cast when duplicate with changed type processed (IDEA-98396)
This commit is contained in:
@@ -938,10 +938,20 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
datas.add(variableData);
|
||||
}
|
||||
}
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myProject);
|
||||
for (ParameterTablePanel.VariableData data : datas) {
|
||||
final List<PsiElement> parameterValue = match.getParameterValues(data.variable);
|
||||
if (parameterValue != null) {
|
||||
for (PsiElement val : parameterValue) {
|
||||
if (val instanceof PsiExpression) {
|
||||
final PsiType exprType = ((PsiExpression)val).getType();
|
||||
if (exprType != null && !TypeConversionUtil.isAssignable(data.type, exprType)) {
|
||||
final PsiTypeCastExpression cast = (PsiTypeCastExpression)elementFactory.createExpressionFromText("(A)a", val);
|
||||
cast.getCastType().replace(elementFactory.createTypeElement(data.type));
|
||||
cast.getOperand().replace(val.copy());
|
||||
val = cast;
|
||||
}
|
||||
}
|
||||
methodCallExpression.getArgumentList().add(val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public abstract class ParameterTablePanel extends JPanel {
|
||||
|
||||
public VariableData(PsiVariable var, PsiType type) {
|
||||
variable = var;
|
||||
this.type = type;
|
||||
this.type = SmartTypePointerManager.getInstance(var.getProject()).createSmartTypePointer(type).getType();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
|
||||
void foo(Object x) {
|
||||
if (x instanceof String) x = ((String)x).substring(1);
|
||||
if (x instanceof String) x = <selection>((String)x).substring(1)</selection>;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
class Test {
|
||||
|
||||
void foo(Object x) {
|
||||
if (x instanceof String) x = newMethod((String) x);
|
||||
if (x instanceof String) x = newMethod((String) x);
|
||||
}
|
||||
|
||||
private String newMethod(String x) {
|
||||
return ((String)x).substring(1);
|
||||
}
|
||||
}
|
||||
@@ -571,6 +571,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
public void testCastWhenDuplicateReplacement() throws Exception {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
private void doTestDisabledParam() throws PrepareFailedException {
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.ELSE_ON_NEW_LINE = true;
|
||||
|
||||
Reference in New Issue
Block a user