docs: @SuppressWarnings("unchecked")
      public static <T> T asType(Closure cl, Class<T> clazz) {
          if (clazz.isInterface() && !(clazz.isInstance(cl))) {
              if (Traits.isTrait(clazz)) {
                  Method samMethod = CachedSAMClass.getSAMMethod(clazz);
                  if (samMethod!=null) {
                      Map impl = Collections.singletonMap(samMethod.getName(),cl);
                      return (T) ProxyGenerator.INSTANCE.instantiateAggregate(impl, Collections.<Class>singletonList(clazz));
                  }
              }
              return (T) Proxy.newProxyInstance(
                      clazz.getClassLoader(),
                      new Class[]{clazz},
                      new ConvertedClosure(cl));
          }
          try {
              return asType((Object) cl, clazz);
          } catch (GroovyCastException ce) {
              try {
                  return (T) ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(cl, clazz);
              } catch (GroovyRuntimeException cause) {
                  throw new GroovyCastException("Error casting closure to " + clazz.getName() +
                          ", Reason: " + cause.getMessage());
              }
          }
      }