don't offer fix to remove 'new' keyword if the member is not static or the class qualifier has type parameters (EA-411014 - AE: JavaParserUtil.parseFragment)

GitOrigin-RevId: c2585277e417432fbfd24be106667ff5cfd373f2
This commit is contained in:
Bas Leijdekkers
2021-11-17 19:58:05 +00:00
committed by intellij-monorepo-bot
parent 6260bbfd7f
commit 9cff7432ef
4 changed files with 47 additions and 11 deletions
@@ -3106,10 +3106,11 @@ public final class HighlightUtil {
}
/**
* Checks if the element of the {@link PsiNewExpression} type can be a reference to a static member of the class,
* which is the qualifier of the reference element of {@link PsiNewExpression}.
* Checks if the specified element is possibly a reference to a static member of a class,
* when the {@code new} keyword is removed.
* The element is split into two parts: the qualifier and the reference element.
* If the qualifier is a class and the reference element text matches either a field name or a method name of the class
* If they both exist and the qualifier references a class and the reference element text matches either
* the name of a static field or the name of a static method of the class
* then the method returns true
*
* @param element an element to examine
@@ -3125,18 +3126,25 @@ public final class HighlightUtil {
final PsiElement qualifier = reference.getQualifier();
final PsiElement memberName = reference.getReferenceNameElement();
if (!(qualifier instanceof PsiReference) || memberName == null) return false;
if (!(qualifier instanceof PsiJavaCodeReferenceElement) || memberName == null) return false;
final PsiReference psiReference = (PsiReference)qualifier;
final PsiJavaCodeReferenceElement psiReference = (PsiJavaCodeReferenceElement)qualifier;
if (psiReference.getTypeParameterCount() > 0) return false;
final PsiClass clazz = tryCast(psiReference.resolve(), PsiClass.class);
if (clazz == null) return false;
final PsiField field = clazz.findFieldByName(memberName.getText(), true);
if (field != null) return true;
final PsiMethod[] methods = clazz.findMethodsByName(memberName.getText(), true);
return methods.length != 0;
if (newExpression.getArgumentList() == null) {
final PsiField field = clazz.findFieldByName(memberName.getText(), true);
if (field != null && field.hasModifierProperty(PsiModifier.STATIC)) return true;
}
else {
final PsiMethod[] methods = clazz.findMethodsByName(memberName.getText(), true);
if (methods.length == 0) return false;
for (PsiMethod method : methods) {
if (method.hasModifierProperty(PsiModifier.STATIC)) return true;
}
}
return false;
}
@NotNull
@@ -0,0 +1,11 @@
// "Remove 'new'" "false"
class A<T> {
int x() {
return new A<Integer>.<caret>y();
}
int y() {
return 1;
}
}
@@ -0,0 +1,9 @@
// "Remove 'new'" "false"
class A {
int x() {
return new A.<caret>y();
}
public static int y = 1;
}
@@ -0,0 +1,8 @@
// "Remove 'new'" "false"
import java.util.*;
class A {
boolean x() {
return new ArrayList.add<caret>("");
}
}