cgliblazyinitializer.java
来自「人力资源管理系统主要包括:人员管理、招聘管理、培训管理、奖惩管理和薪金管理五大管」· Java 代码 · 共 130 行
JAVA
130 行
//$Id: CGLIBLazyInitializer.java,v 1.9.2.12 2004/01/27 07:35:06 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; private boolean constructed = false; static HibernateProxy getProxy( final Class persistentClass, final Class[] interfaces, final Method getIdentifierMethod, final Method setIdentifierMethod, final Serializable id, final SessionImplementor session) throws HibernateException { //note: interfaces is assumed to already contain HibernateProxy.class try { final CGLIBLazyInitializer instance = new CGLIBLazyInitializer( persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, session ); final HibernateProxy proxy = (HibernateProxy) Enhancer.create( (interfaces.length==1) ? persistentClass : null, interfaces, instance ); instance.constructed = true; return proxy; } catch (Throwable t) { LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t); throw new HibernateException( "CGLIB Enhancement failed", t ); } } public static HibernateProxy getProxy( final Factory factory, final Class persistentClass, final Class[] interfaces, final Method getIdentifierMethod, final Method setIdentifierMethod, final Serializable id, final SessionImplementor session) throws HibernateException { final CGLIBLazyInitializer instance = new CGLIBLazyInitializer( persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, session ); final HibernateProxy proxy = (HibernateProxy) factory.newInstance(instance); instance.constructed = true; return proxy; } 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( final Class persistentClass, final Class[] interfaces, final Serializable id, final Method getIdentifierMethod, final Method setIdentifierMethod, final SessionImplementor session) { super(persistentClass, id, getIdentifierMethod, setIdentifierMethod, session); this.interfaces = interfaces; } public Object intercept( final Object obj, final Method method, final Object[] args, final MethodProxy proxy) throws Throwable { if (constructed) { Object result = invoke(method, args, obj); if (result==INVOKE_IMPLEMENTATION) { return proxy.invoke( getImplementation(), args ); } else { return result; } } else { //while constructor is running return proxy.invokeSuper(obj, args); } } 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 proxy.invokeSuper(obj, args); } };}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?