diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index a7121c37aa0b..16448b6df6a4 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -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 diff --git a/java/java-tests/testData/codeInsight/highlight/remove_new/beforeGeneric.java b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeGeneric.java new file mode 100644 index 000000000000..5c0823327fe9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeGeneric.java @@ -0,0 +1,11 @@ +// "Remove 'new'" "false" + +class A { + int x() { + return new A.y(); + } + + int y() { + return 1; + } +} diff --git a/java/java-tests/testData/codeInsight/highlight/remove_new/beforeMethodCallToField.java b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeMethodCallToField.java new file mode 100644 index 000000000000..4b3d0a0f0a95 --- /dev/null +++ b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeMethodCallToField.java @@ -0,0 +1,9 @@ +// "Remove 'new'" "false" + +class A { + int x() { + return new A.y(); + } + + public static int y = 1; +} diff --git a/java/java-tests/testData/codeInsight/highlight/remove_new/beforeNonStaticMethod.java b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeNonStaticMethod.java new file mode 100644 index 000000000000..58c4d9d43b4c --- /dev/null +++ b/java/java-tests/testData/codeInsight/highlight/remove_new/beforeNonStaticMethod.java @@ -0,0 +1,8 @@ +// "Remove 'new'" "false" +import java.util.*; + +class A { + boolean x() { + return new ArrayList.add(""); + } +}