diff --git a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java
index 152172597d6e..bdc480656600 100644
--- a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java
+++ b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterProcessor.java
@@ -15,9 +15,13 @@
*/
package com.intellij.refactoring.introduceParameter;
+import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInsight.ChangeContextUtil;
import com.intellij.lang.findUsages.DescriptiveNameUtil;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
@@ -34,6 +38,7 @@ import com.intellij.refactoring.changeSignature.ChangeSignatureProcessor;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.refactoring.listeners.RefactoringEventData;
import com.intellij.refactoring.util.*;
+import com.intellij.refactoring.util.duplicates.MethodDuplicatesHandler;
import com.intellij.refactoring.util.occurrences.ExpressionOccurrenceManager;
import com.intellij.refactoring.util.occurrences.LocalVariableOccurrenceManager;
import com.intellij.refactoring.util.occurrences.OccurrenceManager;
@@ -51,6 +56,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Set;
/**
@@ -436,6 +442,35 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem
catch (IncorrectOperationException ex) {
LOG.error(ex);
}
+
+ if (isReplaceDuplicates()) {
+ ApplicationManager.getApplication().invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ processMethodsDuplicates();
+ }
+ }, ModalityState.NON_MODAL, myProject.getDisposed());
+ }
+ }
+
+ protected boolean isReplaceDuplicates() {
+ return true;
+ }
+
+ private void processMethodsDuplicates() {
+ final Runnable runnable = new Runnable() {
+ public void run() {
+ if (!myMethodToReplaceIn.isValid()) return;
+ MethodDuplicatesHandler.invokeOnScope(myProject, Collections.singleton(myMethodToReplaceIn),
+ new AnalysisScope(myMethodToReplaceIn.getContainingFile()), true);
+ }
+ };
+ ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
+ @Override
+ public void run() {
+ ApplicationManager.getApplication().runReadAction(runnable);
+ }
+ }, "Search method duplicates...", true, myProject);
}
private PsiMethod generateDelegate(final PsiMethod methodToReplaceIn) throws IncorrectOperationException {
diff --git a/java/java-impl/src/com/intellij/refactoring/util/duplicates/MethodDuplicatesHandler.java b/java/java-impl/src/com/intellij/refactoring/util/duplicates/MethodDuplicatesHandler.java
index 5db44958a78c..d908d35ec3cd 100644
--- a/java/java-impl/src/com/intellij/refactoring/util/duplicates/MethodDuplicatesHandler.java
+++ b/java/java-impl/src/com/intellij/refactoring/util/duplicates/MethodDuplicatesHandler.java
@@ -195,23 +195,24 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler {
}
}
});
- replaceDuplicate(project, duplicates, members);
- if (!silent) {
- final Runnable nothingFoundRunnable = new Runnable() {
- @Override
- public void run() {
- if (duplicates.isEmpty()) {
+ if (duplicates.isEmpty()) {
+ if (!silent) {
+ final Runnable nothingFoundRunnable = new Runnable() {
+ @Override
+ public void run() {
final String message = RefactoringBundle.message("idea.has.not.found.any.code.that.can.be.replaced.with.method.call",
ApplicationNamesInfo.getInstance().getProductName());
Messages.showInfoMessage(project, message, REFACTORING_NAME);
}
+ };
+ if (ApplicationManager.getApplication().isUnitTestMode()) {
+ nothingFoundRunnable.run();
+ } else {
+ ApplicationManager.getApplication().invokeLater(nothingFoundRunnable, ModalityState.NON_MODAL);
}
- };
- if (ApplicationManager.getApplication().isUnitTestMode()) {
- nothingFoundRunnable.run();
- } else {
- ApplicationManager.getApplication().invokeLater(nothingFoundRunnable, ModalityState.NON_MODAL);
}
+ } else {
+ replaceDuplicate(project, duplicates, members);
}
}
diff --git a/java/java-tests/testData/refactoring/introduceParameter/afterCodeDuplicates.java b/java/java-tests/testData/refactoring/introduceParameter/afterCodeDuplicates.java
new file mode 100644
index 000000000000..c402a1f6eb77
--- /dev/null
+++ b/java/java-tests/testData/refactoring/introduceParameter/afterCodeDuplicates.java
@@ -0,0 +1,9 @@
+class Test {
+ {
+ foo("abc");
+ }
+
+ void foo(final String anObject) {
+ System.out.println(anObject);
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/introduceParameter/beforeCodeDuplicates.java b/java/java-tests/testData/refactoring/introduceParameter/beforeCodeDuplicates.java
new file mode 100644
index 000000000000..dad0550b33b9
--- /dev/null
+++ b/java/java-tests/testData/refactoring/introduceParameter/beforeCodeDuplicates.java
@@ -0,0 +1,9 @@
+class Test {
+ {
+ System.out.println("abc");
+ }
+
+ void foo() {
+ System.out.println("");
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java
index 0423d0939242..560f7cb8ff1c 100644
--- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java
+++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceParameterTest.java
@@ -338,7 +338,13 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
public void testEnclosingWithParamDeletion() {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
- perform(true, 0, "anObject", false, true, true, false, 1);
+ perform(true, 0, "anObject", false, true, true, false, 1, false);
+ checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
+ }
+
+ public void testCodeDuplicates() {
+ configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
+ perform(true, 0, "anObject", false, true, true, false, 0, true);
checkResultByFile("/refactoring/introduceParameter/after" + getTestName(false) + ".java");
}
@@ -390,7 +396,7 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
boolean generateDelegate) {
return perform(
replaceAllOccurrences, replaceFieldsWithGetters, parameterName, searchForSuper, declareFinal,
- removeUnusedParameters, generateDelegate, 0
+ removeUnusedParameters, generateDelegate, 0, false
);
}
@@ -401,7 +407,8 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
boolean declareFinal,
boolean removeUnusedParameters,
boolean generateDelegate,
- int enclosingLevel) {
+ int enclosingLevel,
+ final boolean replaceDuplicates) {
final ElementToWorkOn[] elementToWorkOn = new ElementToWorkOn[1];
ElementToWorkOn.processElementToWorkOn(myEditor, myFile, "INtr param", HelpID.INTRODUCE_PARAMETER, getProject(),
new ElementToWorkOn.ElementsProcessor() {
@@ -452,7 +459,12 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
new IntroduceParameterProcessor(
getProject(), method, methodToSearchFor, initializer, expr, localVar, true, parameterName, replaceAllOccurrences,
replaceFieldsWithGetters, declareFinal, generateDelegate, null, parametersToRemove
- ).run();
+ ){
+ @Override
+ protected boolean isReplaceDuplicates() {
+ return replaceDuplicates;
+ }
+ }.run();
myEditor.getSelectionModel().removeSelection();
return true;