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 bc9ef13ea32d..180e38e1e8b0 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/ReplaceConstructorWithFactoryAction.java @@ -119,6 +119,10 @@ public final class ReplaceConstructorWithFactoryAction implements ModCommandActi PsiUtil.setModifierProperty(wrConstructor, getMinimalAccessLevel(constructorOrClass, usages.otherUsages), true); for (PsiNewExpression newExpression : writableUsages) { + if (newExpression.isArrayCreation()) { + continue; + } + var factoryCall = (PsiMethodCallExpression)factory.createExpressionFromText(factoryName + "()", newExpression); CommentTracker ct = new CommentTracker(); diff --git a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/afterArrayCreation.java b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/afterArrayCreation.java new file mode 100644 index 000000000000..66d4f4e5cbe7 --- /dev/null +++ b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/afterArrayCreation.java @@ -0,0 +1,17 @@ +class List { + T t; + + private List() { + } + + static List createList() { + return new List(); + } +} + +class Test { + void foo (){ + List x = List.createList(); + List[] y = new List [10]; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeArrayCreation.java b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeArrayCreation.java new file mode 100644 index 000000000000..60203bbb81e4 --- /dev/null +++ b/java/java-tests/testData/refactoring/replaceConstructorWithFactory/beforeArrayCreation.java @@ -0,0 +1,10 @@ +class List { + T t; +} + +class Test { + void foo (){ + List x = new List(); + List[] y = new List [10]; + } +} \ 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 f7856cfdba45..3af84ea87b2d 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/ReplaceConstructorWithFactoryTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.refactoring; import com.intellij.JavaTestUtil; @@ -131,6 +131,10 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase assertNotAvailable("RedCodeFromIDEA376351"); } + public void testArrayCreation() { + runTest("ArrayCreation", null); + } + private void assertNotAvailable(String name) { configureByFile("/refactoring/replaceConstructorWithFactory/before" + name + ".java"); ReplaceConstructorWithFactoryAction action = new ReplaceConstructorWithFactoryAction();