IDEA-117332 Change signature refactoring should warn about @Contract conflict

This commit is contained in:
peter
2014-02-21 15:38:38 +01:00
parent cc56534dec
commit fdee76603c
3 changed files with 27 additions and 4 deletions
@@ -17,6 +17,7 @@ package com.intellij.refactoring.changeSignature;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInspection.dataFlow.ControlFlowAnalyzer;
import com.intellij.lang.StdLanguages;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
@@ -884,7 +885,7 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
private static class ConflictSearcher {
private final JavaChangeInfo myChangeInfo;
private ConflictSearcher(JavaChangeInfo changeInfo) {
private ConflictSearcher(@NotNull JavaChangeInfo changeInfo) {
this.myChangeInfo = changeInfo;
}
@@ -901,6 +902,8 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
LOG.error(e);
}
}
checkContract(conflictDescriptions, myChangeInfo.getMethod());
for (UsageInfo usageInfo : usagesSet) {
final PsiElement element = usageInfo.getElement();
@@ -914,6 +917,8 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
conflictDescriptions.putValue(baseMethod, "Implicit last parameter should not be deleted");
}
}
checkContract(conflictDescriptions, method);
} else if (element instanceof PsiMethodReferenceExpression) {
conflictDescriptions.putValue(element, "Changed method is used in method reference");
}
@@ -922,6 +927,12 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
return conflictDescriptions;
}
private static void checkContract(MultiMap<PsiElement, String> conflictDescriptions, PsiMethod method) {
if (ControlFlowAnalyzer.findContractAnnotation(method) != null) {
conflictDescriptions.putValue(method, "@Contract annotation will have to be changed manually");
}
}
private boolean needToChangeCalls() {
return myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged();
}
@@ -964,9 +975,6 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) {
String newMethodName = myChangeInfo.getNewName();
if (!(myChangeInfo instanceof JavaChangeInfo)) {
return;
}
try {
PsiMethod prototype;
final PsiMethod method = myChangeInfo.getMethod();
@@ -0,0 +1,6 @@
class A {
@org.jetbrains.annotations.Contract("null,_->fail")
public int method<caret>(Object i, Object j) {
return 0;
}
}
@@ -42,6 +42,15 @@ public class ChangeSignatureTest extends LightRefactoringTestCase {
doTest(null, new ParameterInfoImpl[]{new ParameterInfoImpl(1), new ParameterInfoImpl(0)}, false);
}
public void testWarnAboutContract() throws Exception {
try {
doTest(null, new ParameterInfoImpl[]{new ParameterInfoImpl(1), new ParameterInfoImpl(0)}, false);
fail("Conflict expected");
}
catch (BaseRefactoringProcessor.ConflictsInTestsException ignored) {
}
}
public void testGenericTypes() throws Exception {
doTest(null, null, "T", new GenParams() {
@Override