diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java index 4eb14c03cf82..534b2393715f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java @@ -240,6 +240,7 @@ public final class ReplaceConstructorWithFactoryAction implements ModCommandActi if (!isSuitableClass(containingClass)) return null; PsiElement lBrace = containingClass.getLBrace(); if (lBrace == null || element.getTextRange().getStartOffset() >= lBrace.getTextRange().getStartOffset()) return null; + if (containingClass.getRBrace() == null) return null; // Incomplete class declaration: adding a method may cause errors if (containingClass.getConstructors().length > 0) return null; return containingClass; } diff --git a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/afterRedCode.java b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/afterRedCode.java deleted file mode 100644 index e86a7e509f72..000000000000 --- a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/afterRedCode.java +++ /dev/null @@ -1,9 +0,0 @@ -final class Sample { - private Sample() { - } - - static Sample createSample() { - return new Sample(); - } - - void a() { diff --git a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeImplicitClass.java b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeImplicitClass.java index fe51d05a51aa..3ba662758ee8 100644 --- a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeImplicitClass.java +++ b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeImplicitClass.java @@ -1,5 +1,8 @@ +enum E {A, B} -void main() { - Rar x = new Rar(); +record Rar() { } +void main() { + Rar rar = new Rar(); +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeIncompleteClass.java b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeIncompleteClass.java new file mode 100644 index 000000000000..fcb0ffd76098 --- /dev/null +++ b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeIncompleteClass.java @@ -0,0 +1,6 @@ +import java.io.FileInputStream; +import java.io.IOException; + +public class IncompleteClass { + public static void main(String[] args) throws IOException { + try (var s = new FileInputStream(args[0])) \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java index 098dffbdd0a1..9e593cbfc8a6 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java @@ -83,34 +83,21 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase public void testConstructorTypeParameters() { runTest("08", null); } public void testInnerClass2() { runTest("InnerClass2", "SimpleClass"); } + + public void testIncompleteClass() { + assertNotAvailable("IncompleteClass"); + } public void testInjection() { runTest("Injection", null); } public void testRecords() { - configureByFile("/refactoring/replaceConstructorWithFactory/before" + "RecordConstructor" + ".java"); - ReplaceConstructorWithFactoryAction action = new ReplaceConstructorWithFactoryAction(); - ActionContext context = ActionContext.from(getEditor(), getFile()); - Presentation presentation = action.getPresentation(context); - assertNull(presentation); + assertNotAvailable("RecordConstructor"); } public void testImplicitClass() { - configureFromFileText("A.java", """ - enum E {A, B} - - record Rar() { - } - - void main() { - Rar rar = new Rar(); - } - """); - ReplaceConstructorWithFactoryAction action = new ReplaceConstructorWithFactoryAction(); - ActionContext context = ActionContext.from(getEditor(), getFile()); - Presentation presentation = action.getPresentation(context); - assertNull(presentation); + assertNotAvailable("ImplicitClass"); } public void testImplicitClassNotChoose() { @@ -133,7 +120,15 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase } public void testRedCode() { - runTest("RedCode", null); + assertNotAvailable("RedCode"); + } + + private void assertNotAvailable(String name) { + configureByFile("/refactoring/replaceConstructorWithFactory/before" + name + ".java"); + ReplaceConstructorWithFactoryAction action = new ReplaceConstructorWithFactoryAction(); + ActionContext context = ActionContext.from(getEditor(), getFile()); + Presentation presentation = action.getPresentation(context); + assertNull(presentation); } private void runTest(final String testIndex, @NonNls String targetClassName) { @@ -144,7 +139,6 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase checkResultByFile("/refactoring/replaceConstructorWithFactory/after" + testIndex + ".java"); } - private void perform(String targetClassName) { if (targetClassName != null) { UiInterceptors.register(new ChooserInterceptor(null, Pattern.quote(targetClassName)));