⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgliblazyinitializer.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
字号:
//$Id: CGLIBLazyInitializer.java,v 1.9.2.7 2003/11/19 15:45:37 oneovthafew Exp $package net.sf.hibernate.proxy;import java.io.Serializable;import java.lang.reflect.Method;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.Factory;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;import net.sf.hibernate.HibernateException;import net.sf.hibernate.engine.SessionImplementor;import org.apache.commons.logging.LogFactory;/** * A <tt>LazyInitializer</tt> implemented using the CGLIB bytecode generation library */public final class CGLIBLazyInitializer extends LazyInitializer implements MethodInterceptor {		private Class[] interfaces;		public static HibernateProxy getProxy(Class persistentClass, Class[] interfaces, Method getIdentifierMethod, Method setIdentifierMethod, Serializable id, SessionImplementor session) throws HibernateException {		//note: interfaces is assumed to already contain HibernateProxy.class		try {			return (HibernateProxy) Enhancer.create(				(interfaces.length==1) ?					persistentClass :					null,				interfaces,				new CGLIBLazyInitializer(persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, session)			);		}		catch (Throwable t) {			LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t);			throw new HibernateException( "CGLIB Enhancement failed", t );		}	}		public static HibernateProxy getProxy(Factory factory, Class persistentClass, Class[] interfaces, Method getIdentifierMethod, Method setIdentifierMethod, Serializable id, SessionImplementor session) throws HibernateException {		return (HibernateProxy) factory.newInstance( 			new CGLIBLazyInitializer(persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, session) 		);	}		public static Factory getProxyFactory(Class persistentClass, Class[] interfaces) throws HibernateException {		//note: interfaces is assumed to already contain HibernateProxy.class		try {			return (Factory) Enhancer.create(				(interfaces.length==1) ?					persistentClass :					null,				interfaces,				NULL_METHOD_INTERCEPTOR			);		}		catch (Throwable t) {			LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t);			throw new HibernateException( "CGLIB Enhancement failed", t );		}	}		private CGLIBLazyInitializer(Class persistentClass, Class[] interfaces, Serializable id, Method getIdentifierMethod, Method setIdentifierMethod, SessionImplementor session) {		super(persistentClass, id, getIdentifierMethod, setIdentifierMethod, session);		this.interfaces = interfaces;	}		public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {		Object result = invoke(method, args, obj);		if (result==INVOKE_IMPLEMENTATION) {			return proxy.invoke( getImplementation(), args );		}		else {			return result;		}	}		protected Object serializableProxy() {		return new SerializableProxy(persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod);	}		private static final MethodInterceptor NULL_METHOD_INTERCEPTOR = new MethodInterceptor() {		public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { return null; }	};}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -