mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
java 8: access static method through instance variable: don't show warning, error is enough; add quick fix for an error same as for inspection
This commit is contained in:
@@ -291,6 +291,9 @@ public abstract class QuickFixFactory {
|
||||
@NotNull
|
||||
public abstract IntentionAction createInsertMethodCallFix(@NotNull PsiMethodCallExpression call, PsiMethod method);
|
||||
|
||||
@NotNull
|
||||
public abstract LocalQuickFixAndIntentionActionOnPsiElement createAccessStaticViaInstanceFix(PsiReferenceExpression methodRef, JavaResolveResult result);
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createWrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression);
|
||||
}
|
||||
+8
-9
@@ -657,33 +657,32 @@ public class HighlightMethodUtil {
|
||||
return null;
|
||||
}
|
||||
String description;
|
||||
PsiElement elementToHighlight;
|
||||
PsiElement elementToHighlight = ObjectUtils.notNull(referenceToMethod.getReferenceNameElement(), referenceToMethod);
|
||||
if (element != null && !resolveResult.isAccessible()) {
|
||||
description = HighlightUtil.buildProblemWithAccessDescription(referenceToMethod, resolveResult);
|
||||
elementToHighlight = referenceToMethod.getReferenceNameElement();
|
||||
}
|
||||
else if (element != null && !resolveResult.isStaticsScopeCorrect()) {
|
||||
description = null;
|
||||
elementToHighlight = ObjectUtils.notNull(referenceToMethod.getReferenceNameElement(), referenceToMethod);
|
||||
|
||||
if (element instanceof PsiMethod && ((PsiMethod)element).hasModifierProperty(PsiModifier.STATIC)) {
|
||||
PsiClass containingClass = ((PsiMethod)element).getContainingClass();
|
||||
if (containingClass != null && containingClass.isInterface()) {
|
||||
HighlightInfo info = HighlightUtil.checkFeature(elementToHighlight, HighlightUtil.Feature.STATIC_INTERFACE_CALLS, languageLevel, file);
|
||||
if (info != null) return info;
|
||||
description = checkStaticInterfaceMethodCallQualifier(referenceToMethod, resolveResult.getCurrentFileResolveScope(), containingClass);
|
||||
if (description != null) {
|
||||
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(highlightInfoType).range(elementToHighlight).description(description)
|
||||
.escapedToolTip(XmlStringUtil.escapeString(description)).create();
|
||||
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createAccessStaticViaInstanceFix(referenceToMethod, resolveResult));
|
||||
return highlightInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (description == null) {
|
||||
description = HighlightUtil.buildProblemWithStaticDescription(element);
|
||||
}
|
||||
description = HighlightUtil.buildProblemWithStaticDescription(element);
|
||||
}
|
||||
else {
|
||||
String methodName = referenceToMethod.getReferenceName() + buildArgTypesList(list);
|
||||
description = JavaErrorMessages.message("cannot.resolve.method", methodName);
|
||||
if (candidates.length == 0) {
|
||||
elementToHighlight = referenceToMethod.getReferenceNameElement();
|
||||
highlightInfoType = HighlightInfoType.WRONG_REF;
|
||||
}
|
||||
else {
|
||||
|
||||
+7
@@ -669,6 +669,13 @@ public class EmptyQuickFixFactory extends QuickFixFactory {
|
||||
return QuickFixes.EMPTY_FIX;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createAccessStaticViaInstanceFix(PsiReferenceExpression methodRef,
|
||||
JavaResolveResult result) {
|
||||
return QuickFixes.EMPTY_FIX;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createWrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
|
||||
|
||||
+4
@@ -84,6 +84,10 @@ public class AccessStaticViaInstanceBase extends BaseJavaBatchLocalInspectionToo
|
||||
}
|
||||
if (!((PsiMember)resolved).hasModifierProperty(PsiModifier.STATIC)) return;
|
||||
|
||||
//don't report warnings on compilation errors
|
||||
PsiClass containingClass = ((PsiMember)resolved).getContainingClass();
|
||||
if (containingClass != null && containingClass.isInterface()) return;
|
||||
|
||||
String description = JavaErrorMessages.message("static.member.accessed.via.instance.reference",
|
||||
JavaHighlightUtil.formatType(qualifierExpression.getType()),
|
||||
HighlightMessageUtil.getSymbolName(resolved, result.getSubstitutor()));
|
||||
|
||||
+1
-1
@@ -96,7 +96,7 @@ public class AccessStaticViaInstanceFix extends LocalQuickFixAndIntentionActionO
|
||||
try {
|
||||
PsiElement newQualifier = qualifierExpression.replace(factory.createReferenceExpression(containingClass));
|
||||
PsiElement qualifiedWithClassName = myExpression.copy();
|
||||
if (myExpression.getTypeParameters().length == 0) {
|
||||
if (myExpression.getTypeParameters().length == 0 && !(containingClass.isInterface() && !containingClass.equals(PsiTreeUtil.getParentOfType(myExpression, PsiClass.class)))) {
|
||||
newQualifier.delete();
|
||||
if (myExpression.resolve() != myMember) {
|
||||
myExpression.replace(qualifiedWithClassName);
|
||||
|
||||
+7
@@ -851,6 +851,13 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
return new InsertMethodCallFix(call, method);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createAccessStaticViaInstanceFix(PsiReferenceExpression methodRef,
|
||||
JavaResolveResult result) {
|
||||
return new AccessStaticViaInstanceFix(methodRef, result, true);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createWrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Access static 'I.m()' via class 'I' reference" "true"
|
||||
interface I {
|
||||
static void m() {}
|
||||
}
|
||||
|
||||
class A implements I {
|
||||
{
|
||||
I.m();
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Access static 'I.m()' via class 'I' reference" "true"
|
||||
interface I {
|
||||
static void m() {}
|
||||
}
|
||||
|
||||
class A implements I {
|
||||
{
|
||||
this.<caret>m();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user