private PsiClass @NotNull [] calcClassesToImport() {
    ApplicationManager.getApplication().assertIsNonDispatchThread();
    PsiFile file = myContainingFile;
    if (file == null) {
      return PsiClass.EMPTY_ARRAY;
    }
    VirtualFile virtualFile = file.getVirtualFile();
    myInContent = virtualFile != null && ModuleUtilCore.projectContainsFile(file.getProject(), virtualFile, false);

    extensionsAllowToChangeFileSilently = virtualFile == null ? ThreeState.UNSURE : SilentChangeVetoer.extensionsAllowToChangeFileSilently(file.getProject(), virtualFile);

    PsiElement referenceElement;
    if (!myReferenceElement.isValid() || (referenceElement = myReference.getElement()) != myReferenceElement && !referenceElement.isValid()) {
      return PsiClass.EMPTY_ARRAY;
    }
    if (myReference instanceof PsiJavaReference) {
      JavaResolveResult result = ((PsiJavaReference)myReference).advancedResolve(true);
      PsiElement element = result.getElement();
      // already imported
      // can happen when e.g., class name happened to be in a method position
      if (result.isValidResult() || element instanceof PsiClass && result.getCurrentFileResolveScope() instanceof PsiImportStatement) {
        return PsiClass.EMPTY_ARRAY;
      }
    }

    String name = getReferenceName(myReference);
    if (name == null) {
      return PsiClass.EMPTY_ARRAY;
    }

    if (!canReferenceClass(myReference)) {
      return PsiClass.EMPTY_ARRAY;
    }

    Project project = file.getProject();

    PsiElement parent = myReferenceElement.getParent();
    if (parent instanceof PsiNewExpression && ((PsiNewExpression)parent).getQualifier() != null) {
      return PsiClass.EMPTY_ARRAY;
    }

    if (parent instanceof PsiReferenceExpression) {
      PsiExpression expression = ((PsiReferenceExpression)parent).getQualifierExpression();
      if (expression != null && expression != myReferenceElement) {
        return PsiClass.EMPTY_ARRAY;
      }
    }

    if (file instanceof PsiJavaCodeReferenceCodeFragment && !((PsiJavaCodeReferenceCodeFragment)file).isClassesAccepted()) {
      return PsiClass.EMPTY_ARRAY;
    }

    GlobalSearchScope scope = file.getResolveScope();
    PsiClass[] classes = PsiShortNamesCache.getInstance(project).getClassesByName(name, scope);
    if (classes.length == 0) return PsiClass.EMPTY_ARRAY;
    List<PsiClass> classList = new ArrayList<>(classes.length);
    boolean isAnnotationReference = myReferenceElement.getParent() instanceof PsiAnnotation;
    for (PsiClass aClass : classes) {
      if (isAnnotationReference && !aClass.isAnnotationType()) continue;
      if (qualifiedNameAllowsAutoImport(file, aClass)) {
        classList.add(aClass);
      }
    }
    boolean anyAccessibleFound = ContainerUtil.exists(classList, aClass -> isAccessible(aClass, myReferenceElement));
    JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    classList.removeIf(aClass -> (anyAccessibleFound ||
                                  !BaseIntentionAction.canModify(aClass) ||
                                  facade.arePackagesTheSame(aClass, myReferenceElement) ||
                                  PsiTreeUtil.getParentOfType(aClass, PsiImplicitClass.class) != null) &&
                                 !isAccessible(aClass, myReferenceElement));

    filterByRequiredMemberName(classList);

    Collection<PsiClass> filtered = filterByContext(classList, myReferenceElement);
    if (!filtered.isEmpty()) {
      classList = new ArrayList<>(filtered);
    }

    filerByPackageName(classList, file);

    filterAlreadyImportedButUnresolved(classList, file);

    if (classList.isEmpty() || isReferenceNameForbiddenForAutoImport()) {
      return PsiClass.EMPTY_ARRAY;
    }

    if (classList.size() > 1) {
      reduceSuggestedClassesBasedOnDependencyRuleViolation(classList, file);
    }

    PsiClass[] array = classList.toArray(PsiClass.EMPTY_ARRAY);
    CodeInsightUtil.sortIdenticalShortNamedMembers(array, myReference);
    return array;
  }
private PsiClass @NotNull [] calcClassesToImport()