diff --git a/java/java-impl/src/com/intellij/refactoring/util/EnsureCodeBlockImpl.java b/java/java-impl/src/com/intellij/refactoring/util/EnsureCodeBlockImpl.java index ea7359ba389e..8f090a613ded 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/EnsureCodeBlockImpl.java +++ b/java/java-impl/src/com/intellij/refactoring/util/EnsureCodeBlockImpl.java @@ -169,6 +169,7 @@ class EnsureCodeBlockImpl { PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); if (body == null) { PsiWhileStatement newWhileStatement = (PsiWhileStatement)factory.createStatementFromText("while(true) {}", whileStatement); + Objects.requireNonNull(newWhileStatement.getCondition()).replace(oldCondition); whileStatement = (PsiWhileStatement)whileStatement.replace(newWhileStatement); blockBody = (PsiBlockStatement)Objects.requireNonNull(whileStatement.getBody()); oldCondition = Objects.requireNonNull(whileStatement.getCondition()); diff --git a/java/java-tests/testData/refactoring/introduceVariable/WhileConditionIncomplete.after.java b/java/java-tests/testData/refactoring/introduceVariable/WhileConditionIncomplete.after.java new file mode 100644 index 000000000000..0ebd2d3351be --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/WhileConditionIncomplete.after.java @@ -0,0 +1,8 @@ +class Test { + void test(boolean foo) { + while (true) { + boolean temp = foo; + if (!temp) break; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceVariable/WhileConditionIncomplete.java b/java/java-tests/testData/refactoring/introduceVariable/WhileConditionIncomplete.java new file mode 100644 index 000000000000..fe5b06af3c4e --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/WhileConditionIncomplete.java @@ -0,0 +1,5 @@ +class Test { + void test(boolean foo) { + while(foo) + } +} \ 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 a27ed183c09c..6226382c5857 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java @@ -210,6 +210,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase { doTest(new MockIntroduceVariableHandler("temp", true, false, false, "Node")); } + public void testWhileConditionIncomplete() { + doTest(new MockIntroduceVariableHandler("temp", true, false, false, "boolean")); + } + public void testField() { doTest(new MockIntroduceVariableHandler("temp", false, false, false, CommonClassNames.JAVA_LANG_STRING)); }