[java highlighting] skip anonymous class context for its expression list (IDEA-272378)

GitOrigin-RevId: 5d8e034a4c550aed057ad860bdbaf1645118e5b3
This commit is contained in:
Anna Kozlova
2021-06-30 14:01:18 +00:00
committed by intellij-monorepo-bot
parent 9746172003
commit 4b7f896333
4 changed files with 57 additions and 24 deletions
@@ -1776,10 +1776,7 @@ public final class HighlightUtil {
aClass = (PsiClass)resolved;
}
else {
aClass = PsiTreeUtil.getParentOfType(expr, PsiClass.class);
if (aClass instanceof PsiAnonymousClass && PsiTreeUtil.isAncestor(((PsiAnonymousClass)aClass).getArgumentList(), expr, false)) {
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
}
aClass = getContainingClass(expr);
}
if (aClass == null) return null;
@@ -1804,7 +1801,7 @@ public final class HighlightUtil {
//If TypeName denotes an interface, I, then let T be the type declaration immediately enclosing the method reference expression.
//It is a compile-time error if I is not a direct superinterface of T,
//or if there exists some other direct superclass or direct superinterface of T, J, such that J is a subtype of I.
final PsiClass classT = PsiTreeUtil.getParentOfType(expr, PsiClass.class);
PsiClass classT = getContainingClass(expr);
if (classT != null) {
final PsiElement parent = expr.getParent();
final PsiElement resolved = parent instanceof PsiReferenceExpression ? ((PsiReferenceExpression)parent).resolve() : null;
@@ -1841,6 +1838,15 @@ public final class HighlightUtil {
return null;
}
@Nullable
private static PsiClass getContainingClass(@NotNull PsiExpression expr) {
PsiClass aClass = PsiTreeUtil.getParentOfType(expr, PsiClass.class);
while (aClass instanceof PsiAnonymousClass && PsiTreeUtil.isAncestor(((PsiAnonymousClass)aClass).getArgumentList(), expr, true)) {
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
}
return aClass;
}
static HighlightInfo checkUnqualifiedSuperInDefaultMethod(@NotNull LanguageLevel languageLevel,
@NotNull PsiReferenceExpression expr,
@Nullable PsiExpression qualifier) {
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
@@ -6,8 +6,8 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.ChildRoleBase;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
@@ -56,14 +56,20 @@ public class PsiSuperExpressionImpl extends ExpressionPsiElement implements PsiS
private PsiType getSuperType(PsiClass aClass, boolean checkImmediateSuperInterfaces) {
if (CommonClassNames.JAVA_LANG_OBJECT.equals(aClass.getQualifiedName())) return null;
final PsiClass containingClass = checkImmediateSuperInterfaces ? PsiTreeUtil.getContextOfType(this, PsiClass.class) : null;
PsiClass containingClass = checkImmediateSuperInterfaces ? PsiTreeUtil.getContextOfType(this, PsiClass.class) : null;
if (containingClass != null) {
final PsiClassType[] superTypes;
if (containingClass.isInterface()) {
superTypes = containingClass.getExtendsListTypes();
}
else if (containingClass instanceof PsiAnonymousClass) {
superTypes = new PsiClassType[]{((PsiAnonymousClass)containingClass).getBaseClassType()};
if (PsiTreeUtil.isAncestor(((PsiAnonymousClass)containingClass).getArgumentList(), this, true)) {
containingClass = PsiTreeUtil.getContextOfType(containingClass, PsiClass.class, true);
superTypes = containingClass != null ? containingClass.getSuperTypes() : PsiClassType.EMPTY_ARRAY;
}
else {
superTypes = new PsiClassType[]{((PsiAnonymousClass)containingClass).getBaseClassType()};
}
}
else {
superTypes = containingClass.getImplementsListTypes();
@@ -0,0 +1,32 @@
class Main implements I {
public Main(int i) { }
{
new Main(I.super.m()) {};
}
}
interface I {
default int m(){
return 42;
}
}
class Main1 extends I1 {
public Main1(int i) {
}
{
new Main1(<error descr="'I1' is not an enclosing class">I1.super</error>.m()) {};
}
}
abstract class I1 {
public int m(){
return 42;
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon.lambda;
import com.intellij.JavaTestUtil;
@@ -48,6 +34,9 @@ public class Interface8MethodsHighlightingTest extends LightJavaCodeInsightFixtu
public void testDefaultSupersInStaticContext() {
doTest(false, false);
}
public void testDefaultSupersInAnonymousContext() {
doTest(false, false);
}
public void testAnnotationTypeExtensionsNotSupported() {
doTest(false, false);
}