static HighlightInfo.Builder checkReference(@NotNull PsiJavaCodeReferenceElement ref,
                                              @NotNull JavaResolveResult result,
                                              @NotNull PsiFile containingFile,
                                              @NotNull LanguageLevel languageLevel) {
    PsiElement refName = ref.getReferenceNameElement();
    if (!(refName instanceof PsiIdentifier) && !(refName instanceof PsiKeyword)) return null;
    PsiElement resolved = result.getElement();

    PsiElement refParent = ref.getParent();

    if (refParent instanceof PsiReferenceExpression && refParent.getParent() instanceof PsiMethodCallExpression granny) {
      PsiReferenceExpression referenceToMethod = granny.getMethodExpression();
      PsiExpression qualifierExpression = referenceToMethod.getQualifierExpression();
      if (qualifierExpression == ref && resolved != null && !(resolved instanceof PsiClass) && !(resolved instanceof PsiVariable)) {
        String message = JavaErrorBundle.message("qualifier.must.be.expression");
        return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(qualifierExpression).descriptionAndTooltip(message);
      }
    }
    else if (refParent instanceof PsiMethodCallExpression) {
      return null;  // methods checked elsewhere
    }

    if (resolved == null) {
      // do not highlight unknown packages (javac does not care), Javadoc, and module references (checked elsewhere)
      PsiJavaCodeReferenceElement parent = getOuterReferenceParent(ref);
      PsiElement outerParent = parent.getParent();
      if (outerParent instanceof PsiPackageStatement ||
          result.isPackagePrefixPackageReference() ||
          PsiUtil.isInsideJavadocComment(ref) ||
          parent.resolve() instanceof PsiMember ||
          outerParent instanceof PsiPackageAccessibilityStatement) {
        return null;
      }

      JavaResolveResult[] results = ref.multiResolve(true);
      String description;
      if (results.length > 1) {
        if (ref instanceof PsiMethodReferenceExpression methodRef &&
            IncompleteModelUtil.isIncompleteModel(ref) &&
            IncompleteModelUtil.isUnresolvedClassType(methodRef.getFunctionalInterfaceType())) {
          return null;
        }
        String t1 = format(Objects.requireNonNull(results[0].getElement()));
        String t2 = format(Objects.requireNonNull(results[1].getElement()));
        description = JavaErrorBundle.message("ambiguous.reference", refName.getText(), t1, t2);
      }
      else {
        description = JavaErrorBundle.message("cannot.resolve.symbol", refName.getText());
        boolean definitelyIncorrect = false;
        if (ref instanceof PsiReferenceExpression expression) {
          PsiExpression qualifierExpression = expression.getQualifierExpression();
          if (qualifierExpression != null &&
              qualifierExpression.getType() instanceof PsiPrimitiveType primitiveType &&
              !primitiveType.equals(PsiTypes.nullType()) && !primitiveType.equals(PsiTypes.voidType())) {
            description = JavaErrorBundle.message("cannot.access.member.on.type", qualifierExpression.getText(),
                                                  primitiveType.getPresentableText(false));
            definitelyIncorrect = true;
          }
        } else if (!JavaImplicitClassIndex.getInstance().getElements(ref.getQualifiedName(), ref.getProject(), ref.getResolveScope()).isEmpty()) {
          description = JavaErrorBundle.message("implicit.class.can.not.be.referenced", ref.getText());
          definitelyIncorrect = true;
        }
        if (!definitelyIncorrect && IncompleteModelUtil.isIncompleteModel(containingFile) && IncompleteModelUtil.canBePendingReference(ref)) {
          return getPendingReferenceHighlightInfo(refName);
        }
      }

      HighlightInfo.Builder info =
        HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description);
      if (outerParent instanceof PsiNewExpression newExpression && isCallToStaticMember(newExpression)) {
        var action = new RemoveNewKeywordFix(newExpression);
        info.registerFix(action, null, null, null, null);
      }
      UnresolvedReferenceQuickFixUpdater.getInstance(containingFile.getProject()).registerQuickFixesLater(ref, info);

      return info;
    }

    boolean skipValidityChecks =
      PsiUtil.isInsideJavadocComment(ref) ||
      PsiTreeUtil.getParentOfType(ref, PsiPackageStatement.class, true) != null ||
      resolved instanceof PsiPackage && ref.getParent() instanceof PsiJavaCodeReferenceElement;

    final ErrorWithFixes moduleProblem = checkModuleAccess(resolved, ref, result);
    if (!skipValidityChecks && !(result.isValidResult() && moduleProblem == null)) {
      if (moduleProblem != null) {
        HighlightInfo.Builder info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(findPackagePrefix(ref))
          .descriptionAndTooltip(moduleProblem.message);
        moduleProblem.fixes.forEach(fix -> info.registerFix(fix, List.of(), null, null, null));
        return info;
      }

      if (!result.isAccessible()) {
        @Nls String description = accessProblemDescription(ref, resolved, result);
        HighlightInfo.Builder info =
          HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description);
        if (result.isStaticsScopeCorrect() && resolved instanceof PsiJvmMember) {
          HighlightFixUtil.registerAccessQuickFixAction(info, refName.getTextRange(), (PsiJvmMember)resolved, ref, result.getCurrentFileResolveScope(), null);
          if (ref instanceof PsiReferenceExpression) {
            IntentionAction action = getFixFactory().createRenameWrongRefFix((PsiReferenceExpression)ref);
            info.registerFix(action, null, null, null, null);
          }
        }
        UnresolvedReferenceQuickFixUpdater.getInstance(containingFile.getProject()).registerQuickFixesLater(ref, info);
        return info;
      }

      if (!result.isStaticsScopeCorrect()) {
        String description = staticContextProblemDescription(resolved);
        HighlightInfo.Builder info =
          HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description);
        HighlightFixUtil.registerStaticProblemQuickFixAction(info, resolved, ref);
        if (ref instanceof PsiReferenceExpression) {
          IntentionAction action = getFixFactory().createRenameWrongRefFix((PsiReferenceExpression)ref);
          info.registerFix(action, null, null, null, null);
        }
        return info;
      }
    }

    if ((resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter) && !(resolved instanceof ImplicitVariable)) {
      return HighlightControlFlowUtil.checkVariableMustBeFinal((PsiVariable)resolved, ref, languageLevel);
    }

    if (resolved instanceof PsiClass &&
        ((PsiClass)resolved).getContainingClass() == null &&
        PsiUtil.isFromDefaultPackage(resolved) &&
        (PsiTreeUtil.getParentOfType(ref, PsiImportStatementBase.class) != null ||
         PsiUtil.isModuleFile(containingFile) ||
         !PsiUtil.isFromDefaultPackage(containingFile))) {
      String description = JavaErrorBundle.message("class.in.default.package", ((PsiClass)resolved).getName());
      return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description);
    }

    return null;
  }
static HighlightInfo.Builder checkReference(@NotNull PsiJavaCodeReferenceElement ref,
                                              @NotNull JavaResolveResult result,
                                              @NotNull PsiFile containingFile,
                                              @NotNull LanguageLevel languageLevel)