From d8595269f4d4b50eed332e10f269eca48b8229df Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 15 Jun 2017 11:42:09 +0300 Subject: [PATCH] extract method: show conflict for same parameter names (IDEA-174452) --- .../extractMethod/ExtractMethodDialog.java | 15 +++++++++++++++ .../IntroduceFunctionalVariableHandler.java | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodDialog.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodDialog.java index 083df697eb10..ff020302affe 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodDialog.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodDialog.java @@ -54,6 +54,8 @@ import javax.swing.border.Border; import java.awt.*; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; +import java.util.HashSet; +import java.util.Set; /** @@ -607,11 +609,13 @@ public class ExtractMethodDialog extends DialogWrapper implements AbstractExtrac } protected void checkMethodConflicts(MultiMap conflicts) { + checkParametersConflicts(conflicts); PsiMethod prototype; try { PsiElementFactory factory = JavaPsiFacade.getInstance(myProject).getElementFactory(); prototype = factory.createMethod(myNameField.getEnteredName().trim(), myReturnType); if (myTypeParameterList != null) prototype.getTypeParameterList().replace(myTypeParameterList); + Set usedNames = new HashSet<>(); for (VariableData data : myInputVariables) { if (data.passAsParameter) { prototype.getParameterList().add(factory.createParameter(data.name, data.type)); @@ -626,6 +630,17 @@ public class ExtractMethodDialog extends DialogWrapper implements AbstractExtrac ConflictsUtil.checkMethodConflicts(myTargetClass, null, prototype, conflicts); } + protected void checkParametersConflicts(MultiMap conflicts) { + Set usedNames = new HashSet<>(); + for (VariableData data : myInputVariables) { + if (data.passAsParameter) { + if (!usedNames.add(data.name)) { + conflicts.putValue(null, "Conflicting parameter name: " + data.name); + } + } + } + } + @Override public PsiType getReturnType() { return mySelector != null ? mySelector.getSelectedType() : myReturnType; diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceFunctionalVariableHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceFunctionalVariableHandler.java index c907f87d3337..7bfc7722d493 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceFunctionalVariableHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceFunctionalVariableHandler.java @@ -43,6 +43,7 @@ import com.intellij.refactoring.util.RefactoringChangeUtil; import com.intellij.refactoring.util.RefactoringUtil; import com.intellij.refactoring.util.VariableData; import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.MultiMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -274,6 +275,11 @@ public class IntroduceFunctionalVariableHandler extends IntroduceVariableHandler return "(" + parametersList + ") -> " + returnTypeString; } + @Override + protected void checkMethodConflicts(MultiMap conflicts) { + checkParametersConflicts(conflicts); + } + @NotNull @Override public String getVisibility() {