private static @NotNull List<LookupElement> completeReference(@NotNull CompletionParameters parameters,
                                                                PsiJavaCodeReferenceElement ref,
                                                                JavaCompletionSession session,
                                                                Set<? extends ExpectedTypeInfo> expectedTypes,
                                                                Condition<? super String> nameCondition) {
    PsiElement position = parameters.getPosition();
    if (TOP_LEVEL_VAR_IN_MODULE.accepts(position)) return Collections.emptyList();
    ElementFilter filter = getReferenceFilter(position);
    if (filter == null) return Collections.emptyList();
    if (parameters.getInvocationCount() <= 1 && JavaClassNameCompletionContributor.AFTER_NEW.accepts(position)) {
      filter = new AndFilter(filter, new ElementFilter() {
        @Override
        public boolean isAcceptable(Object element, @Nullable PsiElement context) {
          return !JavaPsiClassReferenceElement.isInaccessibleConstructorSuggestion(position, ObjectUtils.tryCast(element, PsiClass.class));
        }

        @Override
        public boolean isClassAcceptable(Class<?> hintClass) {
          return true;
        }
      });
    }

    boolean smart = parameters.getCompletionType() == CompletionType.SMART;
    if (smart) {
      if (JavaSmartCompletionContributor.INSIDE_TYPECAST_EXPRESSION.accepts(position) || SmartCastProvider.inCastContext(parameters)) {
        return Collections.emptyList();
      }

      ElementFilter smartRestriction = ReferenceExpressionCompletionContributor.getReferenceFilter(position, false);
      if (smartRestriction != TrueFilter.INSTANCE) {
        filter = new AndFilter(filter, smartRestriction);
      }
    }

    boolean inSwitchLabel = IN_SWITCH_LABEL.accepts(position);
    TailType forcedTail = getTailType(smart, inSwitchLabel, position);

    List<LookupElement> items = new ArrayList<>();
    if (INSIDE_CONSTRUCTOR.accepts(position) &&
        (parameters.getInvocationCount() <= 1 || CheckInitialized.isInsideConstructorCall(position))) {
      filter = new AndFilter(filter, new CheckInitialized(position));
    }
    PsiFile originalFile = parameters.getOriginalFile();

    boolean first = parameters.getInvocationCount() <= 1;
    JavaCompletionProcessor.Options options =
      JavaCompletionProcessor.Options.DEFAULT_OPTIONS
        .withCheckAccess(first)
        .withFilterStaticAfterInstance(first)
        .withShowInstanceInStaticContext(!first && !smart);

    for (LookupElement element : JavaCompletionUtil.processJavaReference(position,
                                                                         ref,
                                                                         new ElementExtractorFilter(filter),
                                                                         options,
                                                                         nameCondition, parameters)) {
      if (session.alreadyProcessed(element)) {
        continue;
      }

      LookupItem<?> item = element.as(LookupItem.CLASS_CONDITION_KEY);

      if (forcedTail != null && !(element instanceof JavaPsiClassReferenceElement)) {
        element = new TailTypeDecorator<>(element) {
          @Override
          protected TailType computeTailType(InsertionContext context) {
            if (context.getCompletionChar() == ':' && forcedTail == JavaTailTypes.CASE_ARROW) {
              return TailTypes.caseColonType();
            }
            return forcedTail;
          }
        };
      }

      if (inSwitchLabel && !smart) {
        element = new IndentingDecorator(element);
      }
      if (originalFile instanceof PsiJavaCodeReferenceCodeFragment &&
          !((PsiJavaCodeReferenceCodeFragment)originalFile).isClassesAccepted() && item != null) {
        item.setTailType(TailTypes.noneType());
      }
      if (item instanceof JavaMethodCallElement call) {
        PsiMethod method = call.getObject();
        if (method.getTypeParameters().length > 0) {
          PsiType returned = TypeConversionUtil.erasure(method.getReturnType());
          ExpectedTypeInfo matchingExpectation = returned == null ? null : ContainerUtil.find(expectedTypes, info ->
            info.getDefaultType().isAssignableFrom(returned) ||
            AssignableFromFilter.isAcceptable(method, position, info.getDefaultType(), call.getSubstitutor()));
          if (matchingExpectation != null) {
            call.setInferenceSubstitutorFromExpectedType(position, matchingExpectation.getDefaultType());
          }
        }
      }
      items.add(element);

      ContainerUtil.addIfNotNull(items, ArrayMemberAccess.accessFirstElement(position, element));
    }

    if (parameters.getInvocationCount() > 0) {
      items.addAll(getInnerScopeVariables(parameters, position));
    }

    if (ref.getQualifier() instanceof PsiExpression qualifierExpression &&
        parameters.getInvocationCount() <= 2 &&
        AdvancedSettings.getBoolean("java.completion.qualifier.as.argument")) {
      JavaQualifierAsArgumentStaticMembersProcessor processor =
        new JavaQualifierAsArgumentStaticMembersProcessor(parameters, qualifierExpression);

      items = ContainerUtil.map(items, lookupItem -> {
        JavaMethodCallElement javaMethodCallElement = lookupItem.as(JavaMethodCallElement.class);
        if (javaMethodCallElement != null) {
          PsiMethod psiMethod = javaMethodCallElement.getObject();
          Ref<LookupElement> staticref = new Ref<>();
          processor.processStaticMember(staticItem -> staticref.set(staticItem), psiMethod, new HashSet<>());
          if (!staticref.isNull()) {
            PrioritizedLookupElement<?> prioritizedLookupElement = lookupItem.as(PrioritizedLookupElement.class);
            if (prioritizedLookupElement != null) {
              if (prioritizedLookupElement.getExplicitProximity() != 0) {
                return PrioritizedLookupElement.withExplicitProximity(staticref.get(), prioritizedLookupElement.getExplicitProximity());
              }
              else {
                return PrioritizedLookupElement.withPriority(staticref.get(), prioritizedLookupElement.getPriority());
              }
            }
            return staticref.get();
          }
        }
        return lookupItem;
      });
    }

    return items;
  }
private static @NotNull List<LookupElement> completeReference(@NotNull CompletionParameters parameters,
                                                                PsiJavaCodeReferenceElement ref,
                                                                JavaCompletionSession session,
                                                                Set<? extends ExpectedTypeInfo> expectedTypes,
                                                                Condition<? super String> nameCondition)