From 888c7be55e65f3a86e4939bb53fe4f1335af3cf0 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Fri, 25 Jul 2025 00:57:23 +0200 Subject: [PATCH] [java-intentions] IDEA-376351 Fix false positive of 'Replace constructor with factory method' GitOrigin-RevId: dc8c6eb072906fe89bbed5d64e7e1b108c77f3b5 --- .../ReplaceConstructorWithFactoryAction.java | 1 + .../beforeRedCodeFromIDEA376351.java | 32 +++++++++++++++++++ .../ReplaceConstructorWithFactoryTest.java | 10 ++++-- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeRedCodeFromIDEA376351.java 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 534b2393715f..3ba219f849ad 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java @@ -231,6 +231,7 @@ public final class ReplaceConstructorWithFactoryAction implements ModCommandActi if (element == null) return null; PsiMethod method = MethodUtils.getJavaMethodFromHeader(element); if (method != null) { + if (method.getBody() == null) return null; if (!method.isConstructor()) return null; var containingClass = method.getContainingClass(); if (!isSuitableClass(containingClass)) return null; diff --git a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeRedCodeFromIDEA376351.java b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeRedCodeFromIDEA376351.java new file mode 100644 index 000000000000..5007b1359c91 --- /dev/null +++ b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeRedCodeFromIDEA376351.java @@ -0,0 +1,32 @@ +package com.intellij.openapi.graph.services; + +public final class GraphExportService { + + private volatile boo(lean myPrintMode = false; + private volatile boolean myExportMode = false; + + private static final GraphExportService INSTANCE = new GraphExportService(); + + public static GraphExportService getInstance() { + return INSTANCE; + } + + private GraphExportService() { + } + + public boolean isPrintMode() { + return myPrintMode; + } + + public void setPrintMode(boolean printMode) { + myPrintMode = printMode; + } + + public boolean isExportMode() { + return myExportMode; + } + + public void setExportMode(boolean exportMode) { + myExportMode = exportMode; + } +} 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 9e593cbfc8a6..7d3703ff38e6 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java @@ -83,9 +83,9 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase public void testConstructorTypeParameters() { runTest("08", null); } public void testInnerClass2() { runTest("InnerClass2", "SimpleClass"); } - - public void testIncompleteClass() { - assertNotAvailable("IncompleteClass"); + + public void testIncompleteClass() { + assertNotAvailable("IncompleteClass"); } public void testInjection() { @@ -123,6 +123,10 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase assertNotAvailable("RedCode"); } + public void testRedCodeFromIDEA376351() { + assertNotAvailable("RedCodeFromIDEA376351"); + } + private void assertNotAvailable(String name) { configureByFile("/refactoring/replaceConstructorWithFactory/before" + name + ".java"); ReplaceConstructorWithFactoryAction action = new ReplaceConstructorWithFactoryAction();