inline parameter: warn if method with target signature already exist (IDEA-162205)

This commit is contained in:
Anna.Kozlova
2016-10-14 16:21:09 +02:00
parent 1e3e879291
commit d209b373a6
5 changed files with 50 additions and 14 deletions
@@ -25,11 +25,15 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.changeSignature.ChangeSignatureProcessor;
import com.intellij.refactoring.changeSignature.ParameterInfoImpl;
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor;
import com.intellij.refactoring.ui.ConflictsDialog;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.refactoring.util.InlineUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -107,6 +111,19 @@ public class SameParameterValueInspection extends SameParameterValueInspectionBa
}
public static void inlineSameParameterValue(final PsiMethod method, final PsiParameter parameter, final PsiExpression defToInline) {
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
JavaSafeDeleteProcessor.collectMethodConflicts(conflicts, method, parameter);
if (!conflicts.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (!BaseRefactoringProcessor.ConflictsInTestsException.isTestIgnore()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
}
}
else if (!new ConflictsDialog(parameter.getProject(), conflicts).showAndGet()) {
return;
}
}
final Collection<PsiReference> refsToInline = ReferencesSearch.search(parameter).findAll();
ApplicationManager.getApplication().runWriteAction(() -> {
@@ -30,10 +30,8 @@ import com.intellij.refactoring.changeSignature.ChangeSignatureProcessorBase;
import com.intellij.refactoring.changeSignature.JavaChangeInfo;
import com.intellij.refactoring.changeSignature.JavaChangeInfoImpl;
import com.intellij.refactoring.changeSignature.ParameterInfoImpl;
import com.intellij.refactoring.util.CanonicalTypes;
import com.intellij.refactoring.util.InlineUtil;
import com.intellij.refactoring.util.RefactoringUIUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor;
import com.intellij.refactoring.util.*;
import com.intellij.usageView.UsageInfo;
import com.intellij.usageView.UsageViewDescriptor;
import com.intellij.usageView.UsageViewUtil;
@@ -204,6 +202,7 @@ public class InlineParameterExpressionProcessor extends BaseRefactoringProcessor
@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
JavaSafeDeleteProcessor.collectMethodConflicts(conflicts, myMethod, myParameter);
final UsageInfo[] usages = refUsages.get();
final InaccessibleExpressionsDetector detector = new InaccessibleExpressionsDetector(conflicts);
myInitializer.accept(detector);
@@ -296,15 +296,9 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
final PsiElement scope = ((PsiParameter)element).getDeclarationScope();
if (scope instanceof PsiMethod) {
final PsiMethod method = (PsiMethod)scope;
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null) {
final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter)element);
final PsiMethod methodCopy = (PsiMethod)method.copy();
methodCopy.getParameterList().getParameters()[parameterIndex].delete();
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
ConflictsUtil.checkMethodConflicts(containingClass, method, methodCopy, conflicts);
return (Collection<String>)conflicts.values();
}
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
collectMethodConflicts(conflicts, method, (PsiParameter)element);
return (Collection<String>)conflicts.values();
}
}
return null;
@@ -963,7 +957,17 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
return false;
}
public static void collectMethodConflicts(MultiMap<PsiElement, String> conflicts, PsiMethod method, PsiParameter parameter) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null) {
final int parameterIndex = method.getParameterList().getParameterIndex(parameter);
final PsiMethod methodCopy = (PsiMethod)method.copy();
methodCopy.getParameterList().getParameters()[parameterIndex].delete();
ConflictsUtil.checkMethodConflicts(containingClass, method, methodCopy, conflicts);
}
}
private static class SafeDeleteFunctionalExpressionUsageInfo extends SafeDeleteReferenceUsageInfo {
public SafeDeleteFunctionalExpressionUsageInfo(@NotNull PsiElement element, PsiElement referencedElement) {
super(element, referencedElement, false);