[java] inferred type accessibility (IDEA-293838)

ensure only inferred types (from method type parameters) are affected

GitOrigin-RevId: 8eea25ebd9956b4b405c08a4396c6a4b79a3c952
This commit is contained in:
Anna Kozlova
2022-05-13 10:00:16 +00:00
committed by intellij-monorepo-bot
parent 5840073672
commit 4b5d4875a3
3 changed files with 22 additions and 2 deletions
@@ -1852,8 +1852,9 @@ public final class HighlightMethodUtil {
}
private static HighlightInfo checkInferredReturnTypeAccessible(@NotNull MethodCandidateInfo info, @NotNull PsiMethodCallExpression methodCall) {
PsiClass targetClass = PsiUtil.resolveClassInClassTypeOnly(info.getElement().getReturnType());
if (targetClass instanceof PsiTypeParameter) {
PsiMethod method = info.getElement();
PsiClass targetClass = PsiUtil.resolveClassInClassTypeOnly(method.getReturnType());
if (targetClass instanceof PsiTypeParameter && ((PsiTypeParameter)targetClass).getOwner() == method) {
PsiClass inferred = PsiUtil.resolveClassInClassTypeOnly(info.getSubstitutor().substitute((PsiTypeParameter)targetClass));
if (inferred != null && !PsiUtil.isAccessible(inferred, methodCall, null)) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
@@ -0,0 +1,7 @@
package a.b;
import a.*;
class Foo {
void m(Outer o){
Object obj = o.getData();
}
}
@@ -266,6 +266,18 @@ public class LightAdvHighlightingFixtureTest extends LightJavaCodeInsightFixture
"}");
doTest();
}
public void testProtectedInnerClass1() {
myFixture.addClass("package a;\n" +
"public class A<T> {\n" +
" public T getData() {return null;}\n" +
"}");
myFixture.addClass("package a;\n" +
"public class Outer extends A<Outer.Inner> {\n" +
" protected class Inner {}\n" +
"}");
doTest();
}
private void doTest() {
myFixture.configureByFile(getTestName(false) + ".java");