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 82c99aa742f5..79e52a19c412 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 @@ -1731,8 +1731,8 @@ public class HighlightUtil { if (expr instanceof PsiThisExpression) { final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(expr, PsiMethod.class); - if (psiMethod == null || psiMethod.getContainingClass() != aClass && !isInsideDefaultMethod(psiMethod, aClass)) { - if (aClass.isInterface()) { + if (psiMethod == null || psiMethod.getContainingClass() != aClass) { + if (aClass.isInterface() && !isInsideInterfaceInstanceMethod(psiMethod, aClass)) { return thisNotFoundInInterfaceInfo(expr); } @@ -1762,15 +1762,16 @@ public class HighlightUtil { return null; } - private static boolean isInsideDefaultMethod(@NotNull PsiMethod method, @NotNull PsiClass aClass) { + private static boolean isInsideInterfaceInstanceMethod(@NotNull PsiMethod method, @NotNull PsiClass aClass) { while (method != null && method.getContainingClass() != aClass) { method = PsiTreeUtil.getParentOfType(method, PsiMethod.class, true); } - return method != null && method.hasModifierProperty(PsiModifier.DEFAULT); + return method != null && !method.hasModifierProperty(PsiModifier.STATIC); } private static HighlightInfo thisNotFoundInInterfaceInfo(@NotNull PsiExpression expr) { - return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip("Cannot find symbol variable this").create(); + return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expr).descriptionAndTooltip( + JavaErrorBundle.message("error.no.enclosing.this.in.interface")).create(); } private static boolean resolvesToImmediateSuperInterface(@NotNull PsiExpression expr, diff --git a/java/java-psi-impl/src/messages/JavaErrorBundle.properties b/java/java-psi-impl/src/messages/JavaErrorBundle.properties index 6f6f4a5424db..772667f1d5fe 100644 --- a/java/java-psi-impl/src/messages/JavaErrorBundle.properties +++ b/java/java-psi-impl/src/messages/JavaErrorBundle.properties @@ -458,4 +458,5 @@ non.static.method.cannot.be.referenced.from.a.static.context.method.reference.co abstract.method.0.cannot.be.accessed.directly.method.reference.context=Abstract method ''{0}'' cannot be accessed directly error.interface.member.clashes=@interface member clashes with ''{0}'' in {1} anonymous.class.implements.interface.cannot.have.type.arguments=Anonymous class implements interface; cannot have type arguments -formal.varargs.element.type.inaccessible.here=Formal varargs element type {0} is inaccessible here \ No newline at end of file +formal.varargs.element.type.inaccessible.here=Formal varargs element type {0} is inaccessible here +error.no.enclosing.this.in.interface=Cannot find symbol variable this \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting9/PrivateInInterfacesAnonymousThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting9/PrivateInInterfacesAnonymousThis.java new file mode 100644 index 000000000000..06c84652da68 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting9/PrivateInInterfacesAnonymousThis.java @@ -0,0 +1,10 @@ +interface Foo { + private void bar() { + new Runnable() { + @Override + public void run() { + System.out.println(Foo.this); + } + }; + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk9Test.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk9Test.java index 9e381fd6b628..64ff53392111 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk9Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingJdk9Test.java @@ -48,6 +48,7 @@ public class LightAdvHighlightingJdk9Test extends LightDaemonAnalyzerTestCase { public void testSafeVarargsApplicability() { doTest(true, false); } public void testPrivateInInterfaces() { doTest(false, false); } public void testPrivateInInterfacesOverriding() { doTest(false, false); } + public void testPrivateInInterfacesAnonymousThis() { doTest(false, false); } public void testUnderscore() { doTest(false, false); } public void testTryWithResources() { doTest(false, false); }