diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index c708ac693368..a4ccfba070e2 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -26,7 +26,6 @@ import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Pass; import com.intellij.openapi.util.TextRange; @@ -485,8 +484,13 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { return null; } } - else if (containingExpression != null && !Comparing.equal(containingExpression.getType(), tempExpr.getType())){ - return null; + else if (containingExpression != null) { + PsiType containingExpressionType = containingExpression.getType(); + PsiType tempExprType = tempExpr.getType(); + if (containingExpressionType != null && + (tempExprType == null || !TypeConversionUtil.isAssignable(containingExpressionType, tempExprType))) { + return null; + } } final PsiReferenceExpression refExpr = PsiTreeUtil.getParentOfType(toBeExpression.findElementAt(refIdx[0]), PsiReferenceExpression.class); diff --git a/java/java-tests/testData/refactoring/introduceVariable/SubExpressionFromPrimitiveWithConversion.after.java b/java/java-tests/testData/refactoring/introduceVariable/SubExpressionFromPrimitiveWithConversion.after.java new file mode 100644 index 000000000000..c835d6447610 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/SubExpressionFromPrimitiveWithConversion.after.java @@ -0,0 +1,9 @@ +import java.util.*; + +class MyTest { + + private void foo(List b) { + int i = 6 - b.size(); + if (i - 2L < 1) {} + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceVariable/SubExpressionFromPrimitiveWithConversion.java b/java/java-tests/testData/refactoring/introduceVariable/SubExpressionFromPrimitiveWithConversion.java new file mode 100644 index 000000000000..f7975412e8f5 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/SubExpressionFromPrimitiveWithConversion.java @@ -0,0 +1,8 @@ +import java.util.*; + +class MyTest { + + private void foo(List b) { + if (6 - b.size() - 2L < 1) {} + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java index 4d3a53f582ff..8d8753c6f7c4 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java @@ -368,6 +368,10 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase { doTest(new MockIntroduceVariableHandler("str", false, false, false, CommonClassNames.JAVA_LANG_STRING)); } + public void testSubExpressionFromPrimitiveWithConversion() { + doTest(new MockIntroduceVariableHandler("i", false, false, false, "int")); + } + public void testSubPrimitiveLiteral() { doTest(new MockIntroduceVariableHandler("str", false, false, false, CommonClassNames.JAVA_LANG_STRING)); }