From acef03c3e4a3932f22ad7e7f77079b8884c2df9a Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 2 Mar 2026 17:35:12 +0100 Subject: [PATCH] IDEA-380311 [java-highlighting] Invalid code "cannot referenced from static context" not highlighted properly in Java Follow-up: proper qualified references handling GitOrigin-RevId: c7f470065ab5c4ad97c9fb6ba02d6fa603e62a84 --- .../highlighting/ExpressionChecker.java | 4 ++ .../ReferenceToPrivateClassOuterThis.java | 39 +++++++++++++++++++ .../daemon/LightAdvHighlightingJdk8Test.java | 1 + 3 files changed, 44 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting8/ReferenceToPrivateClassOuterThis.java diff --git a/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/ExpressionChecker.java b/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/ExpressionChecker.java index 97f8a8b6efd1..826dfee0798c 100644 --- a/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/ExpressionChecker.java +++ b/java/codeserver/highlighting/src/com/intellij/java/codeserver/highlighting/ExpressionChecker.java @@ -1700,6 +1700,10 @@ final class ExpressionChecker { newExpression.getClassOrAnonymousClassReference() == expression) { return; } + if (parent instanceof PsiNewExpression newExpression && newExpression.getQualifier() != null) { + // The qualifier will be checked instead + return; + } if (parent instanceof PsiThisExpression || parent instanceof PsiSuperExpression) return; } if (!(expression instanceof PsiThisExpression) && !(expression instanceof PsiSuperExpression) || diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting8/ReferenceToPrivateClassOuterThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting8/ReferenceToPrivateClassOuterThis.java new file mode 100644 index 000000000000..363a0b37561f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting8/ReferenceToPrivateClassOuterThis.java @@ -0,0 +1,39 @@ +class A { + A(C c) {} + + static class B extends A { + B() { + super(A.this.new C()); + } + + B(int x) { + super(A.this.getC()); + } + + B(double x) { + super(A.this.c); + } + } + + class B1 extends A { + B1() { + super(A.this.new C()); + } + + B1(int x) { + super(A.this.getC()); + } + + B1(double x) { + super(A.this.c); + } + } + + private C getC() { + return new C(); + } + + private C c = new C(); + + private class C {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk8Test.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk8Test.java index 516f516e17c9..a6146e2bb577 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk8Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk8Test.java @@ -50,4 +50,5 @@ public class LightAdvHighlightingJdk8Test extends LightDaemonAnalyzerTestCase { } public void testCyclicInheritanceOfTypeAnnotation() { doTest(true, true); } public void testReferenceToPrivateClass() { doTest(true, true); } + public void testReferenceToPrivateClassOuterThis() { doTest(true, true); } } \ No newline at end of file