protected static HibernateProxy createProxyInstance(
     Class<?> proxyClass, final GroovyAwareJavassistLazyInitializer instance) {
   final HibernateProxy proxy;
   try {
     proxy = (HibernateProxy) proxyClass.newInstance();
   } catch (Exception e) {
     throw new HibernateException("Javassist Enhancement failed: " + proxyClass.getName(), e);
   }
   ((ProxyObject) proxy).setHandler(instance);
   instance.groovyObjectMethodHandler = new HibernateGroovyObjectMethodHandler(proxyClass, proxy);
   HibernateUtils.enhanceProxy(proxy);
   instance.constructed = true;
   return proxy;
 }
  public static Class<?> getProxyFactory(Class<?> persistentClass, Class<?>[] interfaces)
      throws HibernateException {
    // note: interfaces is assumed to already contain HibernateProxy.class

    try {
      Set<Class<?>> allInterfaces = new HashSet<Class<?>>();
      if (interfaces != null) {
        allInterfaces.addAll(Arrays.asList(interfaces));
      }
      allInterfaces.add(GroovyObject.class);
      ProxyFactory factory =
          createProxyFactory(
              persistentClass, allInterfaces.toArray(new Class<?>[allInterfaces.size()]));
      Class<?> proxyClass = factory.createClass();
      HibernateUtils.enhanceProxyClass(proxyClass);
      return proxyClass;
    } catch (Throwable t) {
      LOG.error("Javassist Enhancement failed: " + persistentClass.getName(), t);
      throw new HibernateException("Javassist Enhancement failed: " + persistentClass.getName(), t);
    }
  }