mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
[java-highlight] IDEA-253796 Trying to call static method on a new-expression should offer a quickfix to remove new
Fix notes from the review, it includes: - moving the check if the new expression can be a call to static field/method to HighlightUtil - checking the base class when looking for a static member GitOrigin-RevId: 0a914f8cc76a13ae4249f9f6b752dcbccae8c940
This commit is contained in:
committed by
intellij-monorepo-bot
parent
a163f6d311
commit
35bdacb51f
+13
-6
@@ -3165,6 +3165,14 @@ public final class HighlightUtil {
|
||||
PsiElement resolved = result.getElement();
|
||||
|
||||
PsiElement refParent = ref.getParent();
|
||||
|
||||
if (isCallToStaticMember(refParent)) {
|
||||
final String text = JavaErrorBundle.message("cannot.resolve.symbol", refName.getText());
|
||||
final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(text).create();
|
||||
QuickFixAction.registerQuickFixAction(info, new RemoveNewKeywordFix(refParent));
|
||||
return info;
|
||||
}
|
||||
|
||||
PsiElement granny;
|
||||
if (refParent instanceof PsiReferenceExpression && (granny = refParent.getParent()) instanceof PsiMethodCallExpression) {
|
||||
PsiReferenceExpression referenceToMethod = ((PsiMethodCallExpression)granny).getMethodExpression();
|
||||
@@ -3280,7 +3288,7 @@ public final class HighlightUtil {
|
||||
* @return true if the new expression can actually be a call to a class member (field or method), false otherwise.
|
||||
*/
|
||||
@Contract(value = "null -> false", pure = true)
|
||||
static boolean isCallToStaticMember(@Nullable PsiElement element) {
|
||||
private static boolean isCallToStaticMember(@Nullable PsiElement element) {
|
||||
if (!(element instanceof PsiNewExpression)) return false;
|
||||
|
||||
final PsiNewExpression newExpression = (PsiNewExpression)element;
|
||||
@@ -3292,14 +3300,13 @@ public final class HighlightUtil {
|
||||
if (!(qualifier instanceof PsiReference) || memberName == null) return false;
|
||||
|
||||
final PsiReference psiReference = (PsiReference)qualifier;
|
||||
final PsiElement maybeClass = psiReference.resolve();
|
||||
if (!(maybeClass instanceof PsiClass)) return false;
|
||||
final PsiClass clazz = tryCast(psiReference.resolve(), PsiClass.class);
|
||||
if (clazz == null) return false;
|
||||
|
||||
final PsiClass clazz = (PsiClass)maybeClass;
|
||||
final PsiField field = clazz.findFieldByName(memberName.getText(), false);
|
||||
final PsiField field = clazz.findFieldByName(memberName.getText(), true);
|
||||
|
||||
if (field != null) return true;
|
||||
final PsiMethod[] methods = clazz.findMethodsByName(memberName.getText(), false);
|
||||
final PsiMethod[] methods = clazz.findMethodsByName(memberName.getText(), true);
|
||||
|
||||
return methods.length != 0;
|
||||
}
|
||||
|
||||
-4
@@ -1296,10 +1296,6 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
|
||||
String text = JavaErrorBundle.message("cannot.resolve.symbol", ((PsiNamedElement)resolved).getName());
|
||||
final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(text).create();
|
||||
|
||||
if (HighlightUtil.isCallToStaticMember(parent)) {
|
||||
QuickFixAction.registerQuickFixAction(info, new RemoveNewKeywordFix(parent));
|
||||
}
|
||||
|
||||
myHolder.add(info);
|
||||
}
|
||||
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Remove 'new'" "true"
|
||||
|
||||
class A {
|
||||
public static A field = new A();
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
{
|
||||
/*
|
||||
* hello
|
||||
* world
|
||||
*/
|
||||
A a = B.field;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Remove 'new'" "true"
|
||||
|
||||
class A {
|
||||
public static void process() {}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
{
|
||||
/*
|
||||
* hello
|
||||
* world
|
||||
*/
|
||||
B.process();
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Remove 'new'" "true"
|
||||
|
||||
class A {
|
||||
public static A field = new A();
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
{
|
||||
A a = new /*
|
||||
* hello
|
||||
* world
|
||||
*/ B.<caret>field;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Remove 'new'" "true"
|
||||
|
||||
class A {
|
||||
public static void process() {}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
{
|
||||
new /*
|
||||
* hello
|
||||
* world
|
||||
*/ B.<caret>process();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user