FileDocCategorySizeDatePackage
JavassistLazyInitializer.javaAPI DocHibernate 3.2.56537Mon Nov 13 10:44:44 GMT 2006org.hibernate.proxy.pojo.javassist

JavassistLazyInitializer

public class JavassistLazyInitializer extends org.hibernate.proxy.pojo.BasicLazyInitializer implements MethodHandler
A Javassist-based lazy initializer proxy.
author
Muga Nishizawa

Fields Summary
private static final MethodFilter
FINALIZE_FILTER
private Class[]
interfaces
private boolean
constructed
Constructors Summary
private JavassistLazyInitializer(String entityName, Class persistentClass, Class[] interfaces, Serializable id, Method getIdentifierMethod, Method setIdentifierMethod, org.hibernate.type.AbstractComponentType componentIdType, org.hibernate.engine.SessionImplementor session)


	 
			  
	          
	          
	          
	          
	          
	          
	           
		super( entityName, persistentClass, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session );
		this.interfaces = interfaces;
	
Methods Summary
public static org.hibernate.proxy.HibernateProxygetProxy(java.lang.String entityName, java.lang.Class persistentClass, java.lang.Class[] interfaces, java.lang.reflect.Method getIdentifierMethod, java.lang.reflect.Method setIdentifierMethod, org.hibernate.type.AbstractComponentType componentIdType, java.io.Serializable id, org.hibernate.engine.SessionImplementor session)

		// note: interface is assumed to already contain HibernateProxy.class
		try {
			final JavassistLazyInitializer instance = new JavassistLazyInitializer(
					entityName,
			        persistentClass,
			        interfaces,
			        id,
			        getIdentifierMethod,
			        setIdentifierMethod,
			        componentIdType,
			        session
			);
			ProxyFactory factory = new ProxyFactory();
			factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
			factory.setInterfaces( interfaces );
			factory.setFilter( FINALIZE_FILTER );
			Class cl = factory.createClass();
			final HibernateProxy proxy = ( HibernateProxy ) cl.newInstance();
			( ( ProxyObject ) proxy ).setHandler( instance );
			instance.constructed = true;
			return proxy;
		}
		catch ( Throwable t ) {
			LogFactory.getLog( BasicLazyInitializer.class ).error(
					"Javassist Enhancement failed: " + entityName, t
			);
			throw new HibernateException(
					"Javassist Enhancement failed: "
					+ entityName, t
			);
		}
	
public static org.hibernate.proxy.HibernateProxygetProxy(java.lang.Class factory, java.lang.String entityName, java.lang.Class persistentClass, java.lang.Class[] interfaces, java.lang.reflect.Method getIdentifierMethod, java.lang.reflect.Method setIdentifierMethod, org.hibernate.type.AbstractComponentType componentIdType, java.io.Serializable id, org.hibernate.engine.SessionImplementor session)


		final JavassistLazyInitializer instance = new JavassistLazyInitializer(
				entityName,
		        persistentClass,
		        interfaces, id,
		        getIdentifierMethod,
		        setIdentifierMethod,
		        componentIdType,
		        session
		);

		final HibernateProxy proxy;
		try {
			proxy = ( HibernateProxy ) factory.newInstance();
		}
		catch ( Exception e ) {
			throw new HibernateException(
					"Javassist Enhancement failed: "
					+ persistentClass.getName(), e
			);
		}
		( ( ProxyObject ) proxy ).setHandler( instance );
		instance.constructed = true;
		return proxy;
	
public static java.lang.ClassgetProxyFactory(java.lang.Class persistentClass, java.lang.Class[] interfaces)

		// note: interfaces is assumed to already contain HibernateProxy.class

		try {
			ProxyFactory factory = new ProxyFactory();
			factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
			factory.setInterfaces( interfaces );
			factory.setFilter( FINALIZE_FILTER );
			return factory.createClass();
		}
		catch ( Throwable t ) {
			LogFactory.getLog( BasicLazyInitializer.class ).error(
					"Javassist Enhancement failed: "
					+ persistentClass.getName(), t
			);
			throw new HibernateException(
					"Javassist Enhancement failed: "
					+ persistentClass.getName(), t
			);
		}
	
public java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method thisMethod, java.lang.reflect.Method proceed, java.lang.Object[] args)

		if ( this.constructed ) {
			Object result;
			try {
				result = this.invoke( thisMethod, args, proxy );
			}
			catch ( Throwable t ) {
				throw new Exception( t.getCause() );
			}
			if ( result == INVOKE_IMPLEMENTATION ) {
				Object target = getImplementation();
				final Object returnValue;
				try {
                    if ( ReflectHelper.isPublic( persistentClass, thisMethod ) ) {
						if ( ! thisMethod.getDeclaringClass().isInstance( target ) ) {
                    		throw new ClassCastException( target.getClass().getName() );
						}
                    	returnValue = thisMethod.invoke( target, args );
                    }
                    else {
                    	if ( !thisMethod.isAccessible() ) {
                    		thisMethod.setAccessible( true );
                    	}
                    	returnValue = thisMethod.invoke( target, args );
                    }
                    return returnValue == target ? proxy : returnValue;
                }
				catch ( InvocationTargetException ite ) {
                    throw ite.getTargetException();
                }
			}
			else {
				return result;
			}
		}
		else {
			// while constructor is running
			if ( thisMethod.getName().equals( "getHibernateLazyInitializer" ) ) {
				return this;
			}
			else {
				return proceed.invoke( proxy, args );
			}
		}
	
protected java.lang.ObjectserializableProxy()

		return new SerializableProxy(
				getEntityName(),
		        persistentClass,
		        interfaces,
		        getIdentifier(),
		        getIdentifierMethod,
		        setIdentifierMethod,
		        componentIdType
		);