implemented using the CGLIB bytecode generation library
| Method from org.hibernate.proxy.CGLIBLazyInitializer Detail: |
static HibernateProxy getProxy(String entityName,
Class persistentClass,
Class[] interfaces,
Method getIdentifierMethod,
Method setIdentifierMethod,
AbstractComponentType componentIdType,
Serializable id,
SessionImplementor session) throws HibernateException {
//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 = (HibernateProxy) Enhancer.create(
(interfaces.length==1) ?
persistentClass :
null,
interfaces,
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 HibernateProxy getProxy(Factory factory,
String entityName,
Class persistentClass,
Class[] interfaces,
Method getIdentifierMethod,
Method setIdentifierMethod,
AbstractComponentType componentIdType,
Serializable id,
SessionImplementor session) throws HibernateException {
final CGLIBLazyInitializer instance = new CGLIBLazyInitializer(
entityName,
persistentClass,
interfaces,
id,
getIdentifierMethod,
setIdentifierMethod,
componentIdType,
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(BasicLazyInitializer.class).error(
"CGLIB Enhancement failed: " +
persistentClass.getName(),
t
);
throw new HibernateException( "CGLIB Enhancement failed: " + persistentClass.getName(), t );
}
}
|
public Object intercept(Object proxy,
Method method,
Object[] args,
MethodProxy methodProxy) throws Throwable {
if (constructed) {
Object result = invoke(method, args, proxy);
if (result==INVOKE_IMPLEMENTATION) {
Object target = getImplementation();
final Object returnValue;
if ( ReflectHelper.isPublic(persistentClass, method) ) {
returnValue = methodProxy.invoke(target, args);
}
else {
if ( !method.isAccessible() ) method.setAccessible(true);
returnValue = method.invoke(target, args);
}
return returnValue==target ? proxy : returnValue;
}
else {
return result;
}
}
else {
//while constructor is running
if( method.getName().equals("getHibernateLazyInitializer") ) {
return this;
}
else {
return methodProxy.invokeSuper(proxy, args);
}
}
}
|
protected Object serializableProxy() {
return new SerializableProxy(
getEntityName(),
persistentClass,
interfaces,
getIdentifier(),
getIdentifierMethod,
setIdentifierMethod,
componentIdType
);
}
|