[java-intentions] IDEA-370319 'Replace constructor with factory' fails on incomplete code

GitOrigin-RevId: 7f06233850dbbd58ba13a7ea67aca458ffd24658
This commit is contained in:
Tagir Valeev
2025-04-03 11:48:25 +00:00
committed by intellij-monorepo-bot
parent 131b3c9567
commit ab93bd6d65
5 changed files with 27 additions and 32 deletions
@@ -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;
}
@@ -1,9 +0,0 @@
final class Sample {
private Sample() {
}
static Sample createSample() {
return new Sample();
}
void a() {
@@ -1,5 +1,8 @@
enum E {A, B}
void main() {
Rar x = new R<caret>ar();
record Rar() {
}
void main() {
Rar rar = new R<caret>ar();
}
@@ -0,0 +1,6 @@
import java.io.FileInputStream;
import java.io.IOException;
public class <caret>IncompleteClass {
public static void main(String[] args) throws IOException {
try (var s = new FileInputStream(args[0]))
@@ -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 R<caret>ar();
}
""");
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)));