java change signature: don't warn if delete parameters in hierarchy (IDEA-254255)

GitOrigin-RevId: fd8b56048348b3220d360711acdb3585bb10747a
This commit is contained in:
Anna Kozlova
2020-11-02 14:56:46 +01:00
committed by intellij-monorepo-bot
parent 03d47f3c36
commit bf1ec8868c
4 changed files with 49 additions and 3 deletions

View File

@@ -48,6 +48,7 @@ import com.intellij.util.VisibilityUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.psiutils.MethodCallUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1213,15 +1214,38 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
final PsiCodeBlock body = method.getBody();
if (body != null) {
final LocalSearchScope searchScope = new LocalSearchScope(body);
final PsiMethodCallExpression superCall = getSuperCall(method, body);
for (int i = 0; i < toRemove.length; i++) {
if (toRemove[i] && ReferencesSearch.search(parameters[i], searchScope).findFirst() != null) {
String paramName = StringUtil.capitalize(RefactoringUIUtil.getDescription(parameters[i], true));
conflictDescriptions.putValue(parameters[i], JavaRefactoringBundle.message("parameter.used.in.method.body.warning", paramName));
if (toRemove[i]) {
for (PsiReference ref : ReferencesSearch.search(parameters[i], searchScope)) {
if (superCall == null || !passUnchangedParameterToSuperCall(superCall, i, ref)) {
String paramName = StringUtil.capitalize(RefactoringUIUtil.getDescription(parameters[i], true));
conflictDescriptions.putValue(parameters[i], JavaRefactoringBundle.message("parameter.used.in.method.body.warning", paramName));
break;
}
}
}
}
}
}
private static boolean passUnchangedParameterToSuperCall(PsiMethodCallExpression superCall, int i, PsiReference ref) {
return ArrayUtil.find(superCall.getArgumentList().getExpressions(), PsiUtil.skipParenthesizedExprUp(ref.getElement())) == i;
}
private static PsiMethodCallExpression getSuperCall(PsiMethod method, PsiCodeBlock body) {
PsiStatement[] statements = body.getStatements();
PsiStatement firstStmt = statements.length > 0 ? statements[0] : null;
if (firstStmt instanceof PsiExpressionStatement) {
PsiExpression call = ((PsiExpressionStatement)firstStmt).getExpression();
if (call instanceof PsiMethodCallExpression &&
MethodCallUtils.isSuperMethodCall((PsiMethodCallExpression)call, method)) {
return (PsiMethodCallExpression)call;
}
}
return null;
}
private void checkContract(MultiMap<PsiElement, @Nls String> conflictDescriptions, PsiMethod method, boolean override) {
try {
ContractConverter.convertContract(method, myChangeInfo);

View File

@@ -0,0 +1,9 @@
class A {
void f<caret>oo(int i) {}
}
class B extends A {
void foo(int i) {
super.foo(i);
}
}

View File

@@ -0,0 +1,9 @@
class A {
void foo() {}
}
class B extends A {
void foo() {
super.foo();
}
}

View File

@@ -80,6 +80,10 @@ public class ChangeSignatureTest extends ChangeSignatureBaseTest {
catch (BaseRefactoringProcessor.ConflictsInTestsException ignored) { }
}
public void testNoConflictForSuperCallDelegation() {
doTest(null, new ParameterInfoImpl[0], false);
}
public void testGenericTypes() {
doTest(null, null, "T", method -> new ParameterInfoImpl[]{
ParameterInfoImpl.createNew().withName("x").withType(myFactory.createTypeFromText("T", method.getParameterList())).withDefaultValue("null"),