[java] inferred type accessibility (IDEA-293838)

GitOrigin-RevId: 70bcc7dd766648d5b5d9389c1918293101563002
This commit is contained in:
Anna Kozlova
2022-05-12 13:57:17 +00:00
committed by intellij-monorepo-bot
parent 77d0f1c0e8
commit 4259113555
3 changed files with 44 additions and 1 deletions
@@ -414,6 +414,11 @@ public final class HighlightMethodUtil {
if (highlightInfo == null) {
highlightInfo = createIncompatibleTypeHighlightInfo(methodCall, resolveHelper, (MethodCandidateInfo)resolveResult, methodCall);
}
if (highlightInfo == null) {
highlightInfo = checkInferredReturnTypeAccessible((MethodCandidateInfo)resolveResult, methodCall);
}
}
else {
MethodCandidateInfo candidateInfo = resolveResult instanceof MethodCandidateInfo ? (MethodCandidateInfo)resolveResult : null;
@@ -1846,6 +1851,21 @@ public final class HighlightMethodUtil {
return null;
}
private static HighlightInfo checkInferredReturnTypeAccessible(@NotNull MethodCandidateInfo info, @NotNull PsiMethodCallExpression methodCall) {
PsiClass targetClass = PsiUtil.resolveClassInClassTypeOnly(info.getElement().getReturnType());
if (targetClass instanceof PsiTypeParameter) {
PsiClass inferred = PsiUtil.resolveClassInClassTypeOnly(info.getSubstitutor().substitute((PsiTypeParameter)targetClass));
if (inferred != null && !PsiUtil.isAccessible(inferred, methodCall, null)) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.descriptionAndTooltip(JavaErrorBundle.message("inaccessible.type",
PsiFormatUtil.formatClass(inferred, PsiFormatUtilBase.SHOW_FQ_NAME | PsiFormatUtilBase.SHOW_NAME)))
.range(methodCall.getArgumentList())
.create();
}
}
return null;
}
private static void registerFixesOnInvalidConstructorCall(@NotNull PsiConstructorCall constructorCall,
@Nullable PsiJavaCodeReferenceElement classReference,
@NotNull PsiExpressionList list,
@@ -0,0 +1,10 @@
package a.b;
import a.*;
public class Foo {
private <T> T f() { return null;}
void m(Outer o){
o.get(f<error descr="'a.Outer.Inner' is inaccessible here">()</error>);
o.get1();
o.f = f<error descr="'a.Outer.Inner' is inaccessible here">()</error>;
}
}
@@ -1,4 +1,4 @@
// 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.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInsight.daemon;
import com.intellij.JavaTestUtil;
@@ -254,6 +254,19 @@ public class LightAdvHighlightingFixtureTest extends LightJavaCodeInsightFixture
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
public void testProtectedInnerClass() {
myFixture.addClass("package a;\n" +
"public class Outer {\n" +
" public Object get(Inner key) {\n" +
" return null;\n" +
" }\n" +
" public Inner get1() {return null;} \n" +
" public Inner f; \n" +
" protected class Inner {}\n" +
"}");
doTest();
}
private void doTest() {
myFixture.configureByFile(getTestName(false) + ".java");
myFixture.checkHighlighting();