private static @Nullable PsiClassType getSubstitutorWithWildcardsCaptured(
    @NotNull PsiElement context, PsiClassType.@NotNull ClassResolveResult result) {
    PsiClass aClass = result.getElement();
    if (aClass == null) return null;
    PsiSubstitutor substitutor = result.getSubstitutor();

    PsiSubstitutor captureSubstitutor = substitutor;
    for (PsiTypeParameter typeParameter : typeParametersIterable(aClass)) {
      PsiType substituted = substitutor.substitute(typeParameter);
      if (substituted instanceof PsiWildcardType) {
        captureSubstitutor =
          captureSubstitutor.put(typeParameter, PsiCapturedWildcardType.create((PsiWildcardType)substituted, context, typeParameter));
      }
    }

    if (captureSubstitutor == substitutor) return null;
    Map<PsiTypeParameter, PsiType> substitutionMap = null;
    for (PsiTypeParameter typeParameter : typeParametersIterable(aClass)) {
      PsiType substituted = substitutor.substitute(typeParameter);
      if (substituted instanceof PsiWildcardType) {
        if (substitutionMap == null) substitutionMap = new HashMap<>(substitutor.getSubstitutionMap());
        PsiCapturedWildcardType capturedWildcard = (PsiCapturedWildcardType)captureSubstitutor.substitute(typeParameter);
        LOG.assertTrue(capturedWildcard != null);
        PsiType upperBound = PsiCapturedWildcardType.captureUpperBound(typeParameter, (PsiWildcardType)substituted, captureSubstitutor);
        if (upperBound != null) {
          capturedWildcard.setUpperBound(upperBound);
        }
        substitutionMap.put(typeParameter, capturedWildcard);
      }
    }

    if (substitutionMap == null) return null;
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(aClass.getProject());
    return factory.createType(aClass, factory.createSubstitutor(substitutionMap));
  }
private static @Nullable PsiClassType getSubstitutorWithWildcardsCaptured(
    @NotNull PsiElement context, PsiClassType.@NotNull ClassResolveResult result)