Java: ignore duplicates in constructor after Introduce Parameter (IDEA-381116)

GitOrigin-RevId: 6d16a2a593d2eb28e4b1a949377bdb1126aa2487
This commit is contained in:
Bas Leijdekkers
2025-10-23 19:48:12 +00:00
committed by intellij-monorepo-bot
parent 2ec5f562ed
commit 02a33bd984
6 changed files with 46 additions and 6 deletions
@@ -501,7 +501,7 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem
}
private void processMethodsDuplicates() {
if (!myMethodToReplaceIn.isValid()) return;
if (!myMethodToReplaceIn.isValid() || !MethodDuplicatesHandler.isAvailableOn(myMethodToReplaceIn)) return;
MethodDuplicatesHandler.invokeOnScope(myProject, Collections.singleton(myMethodToReplaceIn),
new AnalysisScope(myMethodToReplaceIn.getContainingFile()), true);
}
@@ -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.refactoring.util.duplicates;
import com.intellij.analysis.AnalysisScope;
@@ -55,6 +55,10 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler, Contex
@Override
public boolean isAvailableForQuickList(@NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext dataContext) {
PsiMember member = findMember(editor, file);
return isAvailableOn(member);
}
public static boolean isAvailableOn(PsiMember member) {
return member != null && getCannotRefactorMessage(member) == null;
}
@@ -100,7 +104,8 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler, Contex
final Module module = ModuleUtilCore.findModuleForPsiElement(file);
final BaseAnalysisActionDialog dlg =
new BaseAnalysisActionDialog(JavaRefactoringBundle.message("replace.method.duplicates.scope.chooser.title", getRefactoringName()),
JavaRefactoringBundle.message("replace.method.duplicates.scope.chooser.message"), project, BaseAnalysisActionDialog.standardItems(project, scope, module, element),
JavaRefactoringBundle.message("replace.method.duplicates.scope.chooser.message"), project,
BaseAnalysisActionDialog.standardItems(project, scope, module, element),
AnalysisUIOptions.getInstance(project), false);
if (dlg.showAndGet()) {
AnalysisScope selectedScope = dlg.getScope(scope);
@@ -5,7 +5,7 @@ class A {
class B extends A {
public B(String a) {
this(a, "");
super(a);
}
public B(String a, final String anObject) {
super(a);
@@ -0,0 +1,14 @@
class Surreptitious {
Surreptitious(int i) {
System.out.println(i);
}
void x(int i) {
System.out.println(i);
}
}
class Clandestine {
void y(int i) {
System.out.println(i);
}
}
@@ -0,0 +1,14 @@
class Surreptitious {
Surreptitious() {
System.out.println(<caret>0);
}
void x(int i) {
System.out.println(i);
}
}
class Clandestine {
void y(int i) {
System.out.println(i);
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// 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;
@@ -386,6 +386,13 @@ public class IntroduceParameterTest extends LightJavaCodeInsightTestCase {
NonBlockingReadActionImpl.waitForAsyncTaskCompletion();
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
}
public void testIgnoreDuplicatesInConstructor() {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
perform(IntroduceVariableBase.JavaReplaceChoice.ALL, 0, "i", false, false, true, false, 0, true);
NonBlockingReadActionImpl.waitForAsyncTaskCompletion();
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
}
public void testTypeAnnotation() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, false, false);
@@ -521,7 +528,7 @@ public class IntroduceParameterTest extends LightJavaCodeInsightTestCase {
boolean removeLocalVariable,
boolean replaceAllOccurrences,
boolean declareFinal,
final boolean removeUnusedParameters) {
boolean removeUnusedParameters) {
final int offset = getEditor().getCaretModel().getOffset();
final PsiElement element = Objects.requireNonNull(getFile().findElementAt(offset)).getParent();
assertTrue(element instanceof PsiLocalVariable);