extract method: insert cast when duplicate with changed type processed (IDEA-98396)

This commit is contained in:
Anna Kozlova
2013-01-02 12:45:05 +01:00
parent 2e432c35fd
commit 7228521cd1
5 changed files with 33 additions and 1 deletions
@@ -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>;
}
}
@@ -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;