PsiClass (@NotNull PsiElement place) { if (place instanceof PsiReferenceExpression expression) { // check for package-private classes in the middle while (true) { if (expression.resolve() instanceof PsiField field) { PsiClass aClass = field.getContainingClass(); if (aClass != null && aClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && !JavaPsiFacade.getInstance(aClass.getProject()).arePackagesTheSame(aClass, place)) { return aClass; } } PsiExpression qualifier = expression.getQualifierExpression(); if (!(qualifier instanceof PsiReferenceExpression)) break; expression = (PsiReferenceExpression)qualifier; } } return null; }