diff --git a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java index 6358b248d248..3d5ea9998a7f 100644 --- a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java +++ b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java @@ -345,7 +345,7 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer } } } - if (psiVariable != null && psiVariable.isValid()) { + if (psiVariable != null && psiVariable.isValid() && !PsiTypes.nullType().equals(psiVariable.getType())) { DumbService.getInstance(project).runWithAlternativeResolveEnabled(() -> createCastInVariableDeclaration(project, psiVariable)); } } diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java new file mode 100644 index 000000000000..980ed8d67525 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable.java @@ -0,0 +1,11 @@ + +class Test { + void f(Object o) { + System.out.println(((SomeUnresolved)o).doSomething()); + } + + class SomeUnresolved{ + Test doSomething() {} + } +} + diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java new file mode 100644 index 000000000000..b41da4418898 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/unresolvedVariable_after.java @@ -0,0 +1,12 @@ + +class Test { + void f(Object o) { + var o = ((SomeUnresolved) o).doSomething(); + System.out.println(o); + } + + class SomeUnresolved{ + Test doSomething() {} + } +} + diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java index c1b354e0f6f6..fc44cbfe036e 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java @@ -20,6 +20,7 @@ import com.intellij.psi.codeStyle.JavaCodeStyleSettings; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.IntroduceVariableUtil; +import com.intellij.refactoring.JavaRefactoringSettings; import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer; import com.intellij.refactoring.introduceVariable.IntroduceVariableHandler; import com.intellij.refactoring.util.CommonRefactoringUtil; @@ -77,6 +78,19 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe doTest(introducer -> type("constants")); } + public void testUnresolvedVariable() { + JavaRefactoringSettings instance = JavaRefactoringSettings.getInstance(); + Boolean oldValue = instance.INTRODUCE_LOCAL_CREATE_VAR_TYPE; + instance.INTRODUCE_LOCAL_CREATE_VAR_TYPE = true; + try { + doTest(introducer -> type("o")); + } + finally { + instance.INTRODUCE_LOCAL_CREATE_VAR_TYPE = oldValue; + } + + } + public void testInsideInjectedString() { doTestInsideInjection(introducer -> type("expr")); }