implemented using the CGLIB bytecode generation library
| Method from org.hibernate.proxy.pojo.cglib.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;
Class factory = getProxyFactory(persistentClass, interfaces);
proxy = getProxyInstance(factory, instance);
instance.constructed = true;
return proxy;
}
catch (Throwable t) {
LoggerFactory.getLogger( BasicLazyInitializer.class )
.error( "CGLIB Enhancement failed: " + entityName, t );
throw new HibernateException( "CGLIB Enhancement failed: " + entityName, t );
}
}
|
public static HibernateProxy getProxy(Class 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;
try {
proxy = getProxyInstance(factory, instance);
}
catch (Exception e) {
throw new HibernateException( "CGLIB Enhancement failed: " + persistentClass.getName(), e );
}
instance.constructed = true;
return proxy;
}
|
public static Class getProxyFactory(Class persistentClass,
Class[] interfaces) throws HibernateException {
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();
}
|
public Object invoke(Object proxy,
Method method,
Object[] args) throws Throwable {
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 Object serializableProxy() {
return new SerializableProxy(
getEntityName(),
persistentClass,
interfaces,
getIdentifier(),
getIdentifierMethod,
setIdentifierMethod,
componentIdType
);
}
|