extract method: show conflict for same parameter names (IDEA-174452)

This commit is contained in:
Anna Kozlova
2017-06-15 13:52:53 +03:00
parent 30b55a94e5
commit d8595269f4
2 changed files with 21 additions and 0 deletions
@@ -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<PsiElement, String> 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<String> 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<PsiElement, String> conflicts) {
Set<String> 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;
@@ -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<PsiElement, String> conflicts) {
checkParametersConflicts(conflicts);
}
@NotNull
@Override
public String getVisibility() {