@NotNull
  public static SimpleFunctionDescriptor createSamAdapterFunction(
      @NotNull SimpleFunctionDescriptor original) {
    SimpleFunctionDescriptorImpl result =
        new SimpleFunctionDescriptorImpl(
            original.getContainingDeclaration(),
            original.getAnnotations(),
            original.getName(),
            CallableMemberDescriptor.Kind.SYNTHESIZED);

    TypeParameters typeParameters =
        recreateAndInitializeTypeParameters(original.getTypeParameters(), result);

    JetType returnTypeUnsubstituted = original.getReturnType();
    assert returnTypeUnsubstituted != null : original;
    JetType returnType =
        typeParameters.substitutor.substitute(returnTypeUnsubstituted, Variance.OUT_VARIANCE);
    assert returnType != null
        : "couldn't substitute type: "
            + returnType
            + ", substitutor = "
            + typeParameters.substitutor;

    List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
    for (ValueParameterDescriptor originalParam : original.getValueParameters()) {
      JetType originalType = originalParam.getType();
      JetType newTypeUnsubstituted =
          isSamType(originalType) ? getFunctionTypeForSamType(originalType) : originalType;
      JetType newType =
          typeParameters.substitutor.substitute(newTypeUnsubstituted, Variance.IN_VARIANCE);
      assert newType != null
          : "couldn't substitute type: "
              + newTypeUnsubstituted
              + ", substitutor = "
              + typeParameters.substitutor;

      ValueParameterDescriptor newParam =
          new ValueParameterDescriptorImpl(
              result,
              originalParam.getIndex(),
              originalParam.getAnnotations(),
              originalParam.getName(),
              newType,
              false,
              null);
      valueParameters.add(newParam);
    }

    result.initialize(
        null,
        original.getExpectedThisObject(),
        typeParameters.descriptors,
        valueParameters,
        returnType,
        original.getModality(),
        original.getVisibility(),
        false);

    return result;
  }
 public static boolean isSamAdapterNecessary(@NotNull SimpleFunctionDescriptor fun) {
   for (ValueParameterDescriptor param : fun.getValueParameters()) {
     if (isSamType(param.getType())) {
       return true;
     }
   }
   return false;
 }