FileDocCategorySizeDatePackage
CGLIBLazyInitializer.javaAPI DocHibernate 3.2.56066Sat Mar 17 22:10:10 GMT 2007org.hibernate.proxy.pojo.cglib

CGLIBLazyInitializer

public final class CGLIBLazyInitializer extends org.hibernate.proxy.pojo.BasicLazyInitializer implements net.sf.cglib.proxy.InvocationHandler
A LazyInitializer implemented using the CGLIB bytecode generation library

Fields Summary
private static final net.sf.cglib.proxy.CallbackFilter
FINALIZE_FILTER
private Class[]
interfaces
private boolean
constructed
Constructors Summary
private CGLIBLazyInitializer(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
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: interfaces is assumed to already contain HibernateProxy.class
		
		try {
			final CGLIBLazyInitializer instance = new CGLIBLazyInitializer(
					entityName,
					persistentClass,
					interfaces,
					id,
					getIdentifierMethod,
					setIdentifierMethod,
					componentIdType,
					session 
				);
			
			final HibernateProxy proxy;
			Class factory = getProxyFactory(persistentClass,  interfaces);
			proxy = getProxyInstance(factory, instance);
			instance.constructed = true;
			return proxy;
		}
		catch (Throwable t) {
			LogFactory.getLog( BasicLazyInitializer.class )
				.error( "CGLIB Enhancement failed: " + entityName, t );
			throw new HibernateException( "CGLIB 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 CGLIBLazyInitializer instance = new CGLIBLazyInitializer(
				entityName,
				persistentClass,
				interfaces,
				id,
				getIdentifierMethod,
				setIdentifierMethod,
				componentIdType,
				session 
			);
		
		final HibernateProxy proxy;
		try {
			proxy = getProxyInstance(factory, instance);
		}
		catch (Exception e) {
			throw new HibernateException( "CGLIB Enhancement failed: " + persistentClass.getName(), e );
		}
		instance.constructed = true;

		return proxy;
	
public static java.lang.ClassgetProxyFactory(java.lang.Class persistentClass, java.lang.Class[] interfaces)

		Enhancer e = new Enhancer();
		e.setSuperclass( interfaces.length == 1 ? persistentClass : null );
		e.setInterfaces(interfaces);
		e.setCallbackTypes(new Class[]{
			InvocationHandler.class,
			NoOp.class,
	  		});
  		e.setCallbackFilter(FINALIZE_FILTER);
  		e.setUseFactory(false);
		e.setInterceptDuringConstruction( false );
		return e.createClass();
	
private static org.hibernate.proxy.HibernateProxygetProxyInstance(java.lang.Class factory, org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer instance)

		HibernateProxy proxy;
		try {
			Enhancer.registerCallbacks(factory, new Callback[]{ instance, null });
			proxy = (HibernateProxy)factory.newInstance();
		} finally {
			// HHH-2481 make sure the callback gets cleared, otherwise the instance stays in a static thread local.
			Enhancer.registerCallbacks(factory, null);
		}
		return proxy;
	
public java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)

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

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