IDEA-238526 Wrong compilation error reported when anonymous class is defined inside private interface method

GitOrigin-RevId: cc0f0ec45152c1923836d6eee770b15685bf15e1
This commit is contained in:
Tagir Valeev
2020-04-23 06:06:05 +00:00
committed by intellij-monorepo-bot
parent 7016d3be67
commit 788e8bbed1
4 changed files with 19 additions and 6 deletions
@@ -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,
@@ -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
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
@@ -0,0 +1,10 @@
interface Foo {
private void bar() {
new Runnable() {
@Override
public void run() {
System.out.println(Foo.this);
}
};
}
}
@@ -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); }