[java-inspections] IDEA-294900 Remove repeating call fix on unresolved call error

GitOrigin-RevId: 220979068250f873c725310dc8cf38a36d1e7fdf
This commit is contained in:
Tagir Valeev
2022-05-27 13:26:35 +00:00
committed by intellij-monorepo-bot
parent b5599da5bc
commit 8fd79a665c
9 changed files with 127 additions and 1 deletions
@@ -615,4 +615,6 @@ intention.family.name.move.annotation.to.array=Move annotation to array
inspection.redundant.requires.statement.message.transitive.dependencies.on.can.be.used.directly=Transitive dependencies on ''{0}'' can be used directly.
inspection.redundant.requires.statement.message.module.unused=No usages of module packages are found.
inspection.redundant.requires.statement.message.java.base.implicitly.required='java.base' is implicitly required.
reassign.variable.display.name=Reassigned variable
reassign.variable.display.name=Reassigned variable
intention.family.name.remove.repeating.call=Remove repeating call
intention.name.remove.repeating.call=Remove repeating call ''{0}()''
@@ -815,6 +815,7 @@ public final class HighlightMethodUtil {
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info, fixRange);
PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
QuickFixAction.registerQuickFixAction(info, fixRange, RemoveRepeatingCallFix.createFix(methodCall));
registerChangeParameterClassFix(methodCall, list, info, fixRange);
if (candidates.length == 0 && info != null) {
UnresolvedReferenceQuickFixProvider.registerReferenceFixes(methodCall.getMethodExpression(), new QuickFixActionRegistrarImpl(info));
@@ -0,0 +1,60 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.PsiEquivalenceUtil;
import com.intellij.codeInsight.daemon.impl.actions.IntentionActionWithFixAllOption;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.java.analysis.JavaAnalysisBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethodCallExpression;
import com.siyeh.ig.psiutils.MethodCallUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class RemoveRepeatingCallFix extends LocalQuickFixAndIntentionActionOnPsiElement implements IntentionActionWithFixAllOption {
private final String myMethodName;
private RemoveRepeatingCallFix(PsiMethodCallExpression call, String methodName) {
super(call);
myMethodName = methodName;
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
PsiMethodCallExpression call = (PsiMethodCallExpression)startElement;
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (qualifier == null) {
return;
}
call.replace(qualifier);
}
@Override
public @NotNull String getText() {
return JavaAnalysisBundle.message("intention.name.remove.repeating.call", myMethodName);
}
@Override
public @NotNull String getFamilyName() {
return JavaAnalysisBundle.message("intention.family.name.remove.repeating.call");
}
public static @Nullable RemoveRepeatingCallFix createFix(@NotNull PsiMethodCallExpression call) {
String name = call.getMethodExpression().getReferenceName();
if (name == null) return null;
PsiMethodCallExpression qualifierCall = MethodCallUtils.getQualifierMethodCall(call);
if (qualifierCall == null) return null;
String qualifierName = qualifierCall.getMethodExpression().getReferenceName();
if (!name.equals(qualifierName)) return null;
if (!PsiEquivalenceUtil.areElementsEquivalent(call.getArgumentList(), qualifierCall.getArgumentList())) return null;
return new RemoveRepeatingCallFix(call, name);
}
}
@@ -0,0 +1,6 @@
// "Remove repeating call 'hashCode()'" "true"
class Test {
void test(Object obj) {
int x = obj.hashCode();
}
}
@@ -0,0 +1,8 @@
// "Remove repeating call 'equals()'" "true"
class Test {
void test(String s) {
if (s.equals("foo")) {
}
}
}
@@ -0,0 +1,6 @@
// "Remove repeating call 'hashCode()'" "true"
class Test {
void test(Object obj) {
int x = obj.hashCode().<caret>hashCode();
}
}
@@ -0,0 +1,8 @@
// "Remove repeating call 'equals()'" "false"
class Test {
void test(String s) {
if (s.equals("foo").<caret>equals("bar")) {
}
}
}
@@ -0,0 +1,8 @@
// "Remove repeating call 'equals()'" "true"
class Test {
void test(String s) {
if (s.equals("foo").<caret>equals("foo")) {
}
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
public class RemoveRepeatingCallFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/removeRepeatingCall";
}
}