IDEA-216256 Provide fixes for receiver cannot be used in a static context

GitOrigin-RevId: f80081974933f296ec2b3ac94a0b85891afcb58c
This commit is contained in:
Andrey.Cherkasov
2021-02-15 15:07:49 +03:00
committed by intellij-monorepo-bot
parent 5e63bd63c9
commit 4ca740f0e6
4 changed files with 23 additions and 2 deletions

View File

@@ -1,9 +1,10 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.AnnotationTargetUtil;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.daemon.JavaErrorBundle;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.codeInsight.daemon.impl.quickfix.MoveAnnotationOnStaticMemberQualifyingTypeFix;
@@ -45,6 +46,8 @@ import static com.intellij.patterns.PsiJavaPatterns.psiElement;
public final class AnnotationsHighlightUtil {
private static final Logger LOG = Logger.getInstance(AnnotationsHighlightUtil.class);
private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
static HighlightInfo checkNameValuePair(@NotNull PsiNameValuePair pair,
@Nullable RefCountHolder refCountHolder) {
PsiAnnotation annotation = PsiTreeUtil.getParentOfType(pair, PsiAnnotation.class);
@@ -697,7 +700,16 @@ public final class AnnotationsHighlightUtil {
PsiMethod method = (PsiMethod)owner;
if (isStatic(method) || method.isConstructor() && isStatic(method.getContainingClass())) {
String text = JavaErrorBundle.message("receiver.static.context");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter.getIdentifier()).descriptionAndTooltip(text).create();
HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter.getIdentifier()).descriptionAndTooltip(text).create();
QuickFixAction.registerQuickFixAction(
info,
QUICK_FIX_FACTORY.createModifierListFix(method.getModifierList(), PsiModifier.STATIC, false, false)
);
QuickFixAction.registerQuickFixAction(
info,
QUICK_FIX_FACTORY.createDeleteFix(parameter, QuickFixBundle.message("remove.receiver.parameter")));
return info;
}
PsiElement leftNeighbour = PsiTreeUtil.skipWhitespacesAndCommentsBackward(parameter);

View File

@@ -133,6 +133,7 @@ make.class.an.interface.text=Make ''{0}'' an interface
make.interface.an.class.text=Make ''{0}'' a class
make.vararg.parameter.last.family=Make vararg parameter last
make.vararg.parameter.last.text=Move ''{0}'' to the end of the list
remove.receiver.parameter=Remove receiver parameter
fix.parameter.type.family=Fix Parameter Type
fix.parameter.type.text=Make ''{0}'' take parameter of type ''{1}'' here
fix.return.type.family=Fix return type

View File

@@ -0,0 +1,4 @@
// "Make 'foo' not static" "true"
class X {
void foo(X this) {}
}

View File

@@ -0,0 +1,4 @@
// "Make 'foo' not static" "true"
class X {
static void foo(X this<caret>) {}
}