public static void setupGenericParameters(PsiClass targetClass, PsiJavaCodeReferenceElement ref) {
    int numParams = ref.getTypeParameters().length;
    if (numParams == 0) return;
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(ref.getProject());
    final Set<String> typeParamNames = new HashSet<>();
    for (PsiType type : ref.getTypeParameters()) {
      final PsiClass psiClass = PsiUtil.resolveClassInType(type);
      if (psiClass instanceof PsiTypeParameter) {
        typeParamNames.add(psiClass.getName());
      }
    }
    int idx = 0;
    PsiTypeParameterList typeParameterList = Objects.requireNonNull(targetClass.getTypeParameterList());
    for (PsiType type : ref.getTypeParameters()) {
      final PsiClass psiClass = PsiUtil.resolveClassInType(type);
      if (psiClass instanceof PsiTypeParameter) {
        typeParameterList.add(factory.createTypeParameterFromText(psiClass.getName(), null));
      } else {
        while (true) {
          final @NonNls String paramName = idx > 0 ? "T" + idx : "T";
          if (typeParamNames.add(paramName)) {
            typeParameterList.add(factory.createTypeParameterFromText(paramName, null));
            break;
          }
          idx++;
        }
      }
    }
  }
public static void setupGenericParameters(PsiClass targetClass, PsiJavaCodeReferenceElement ref)