Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/bytecode/javassist/ProxyFactoryFactoryImpl.java
package org.hibernate.bytecode.javassist;

import org.hibernate.bytecode.ProxyFactoryFactory;
import org.hibernate.bytecode.BasicProxyFactory;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.proxy.pojo.javassist.JavassistProxyFactory;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.ProxyObject;
import javassist.util.proxy.MethodHandler;

import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * A factory for Javassist-based {@link ProxyFactory} instances.
 *
 @author Steve Ebersole
 */
public class ProxyFactoryFactoryImpl implements ProxyFactoryFactory {

  /**
   * Builds a Javassist-based proxy factory.
   *
   @return a new Javassist-based proxy factory.
   */
  public ProxyFactory buildProxyFactory() {
    return new JavassistProxyFactory();
  }

  public BasicProxyFactory buildBasicProxyFactory(Class superClass, Class[] interfaces) {
    return new BasicProxyFactoryImplsuperClass, interfaces );
  }

  private static class BasicProxyFactoryImpl implements BasicProxyFactory {
    private final Class proxyClass;

    public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) {
      if superClass == null && interfaces == null || interfaces.length < ) ) {
        throw new AssertionFailure"attempting to build proxy without any superclass or interfaces" );
      }
      javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory();
      factory.setFilterFINALIZE_FILTER );
      if superClass != null ) {
        factory.setSuperclasssuperClass );
      }
      if interfaces != null && interfaces.length > ) {
        factory.setInterfacesinterfaces );
      }
      proxyClass = factory.createClass();
    }

    public Object getProxy() {
      try {
        ProxyObject proxy = ProxyObject proxyClass.newInstance();
        proxy.setHandlernew PassThroughHandlerproxy, proxyClass.getName() ) );
        return proxy;
      }
      catch Throwable t ) {
        throw new HibernateException"Unable to instantiated proxy instance" );
      }
    }

    public boolean isInstance(Object object) {
      return proxyClass.isInstanceobject );
    }
  }

  private static final MethodFilter FINALIZE_FILTER = new MethodFilter() {
    public boolean isHandled(Method m) {
      // skip finalize methods
      return !m.getParameterTypes().length == && m.getName().equals"finalize" ) );
    }
  };

  private static class PassThroughHandler implements MethodHandler {
    private HashMap data = new HashMap();
    private final Object proxiedObject;
    private final String proxiedClassName;

    public PassThroughHandler(Object proxiedObject, String proxiedClassName) {
      this.proxiedObject = proxiedObject;
      this.proxiedClassName = proxiedClassName;
    }

    public Object invoke(
        Object object,
            Method method,
            Method method1,
            Object[] argsthrows Exception {
      String name = method.getName();
      if "toString".equalsname ) ) {
        return proxiedClassName + "@" + System.identityHashCodeobject );
      }
      else if "equals".equalsname ) ) {
        return proxiedObject == object ? Boolean.TRUE : Boolean.FALSE;
      }
      else if "hashCode".equalsname ) ) {
        return new IntegerSystem.identityHashCodeobject ) );
      }
      boolean hasGetterSignature = method.getParameterTypes().length == && method.getReturnType() != null;
      boolean hasSetterSignature = method.getParameterTypes().length == && method.getReturnType() == null || method.getReturnType() == void.class );
      if name.startsWith"get" && hasGetterSignature ) {
        String propName = name.substring);
        return data.getpropName );
      }
      else if name.startsWith"is" && hasGetterSignature ) {
        String propName = name.substring);
        return data.getpropName );
      }
      else if name.startsWith"set" && hasSetterSignature) {
        String propName = name.substring);
        data.putpropName, args[0] );
        return null;
      }
      else {
        // todo : what else to do here?
        return null;
      }
    }
  }
}