A Javassist-based lazy initializer proxy.
| Method from org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer Detail: |
public static HibernateProxy getProxy(String entityName,
Class persistentClass,
Class[] interfaces,
Method getIdentifierMethod,
Method setIdentifierMethod,
AbstractComponentType componentIdType,
Serializable id,
SessionImplementor session) throws HibernateException {
// note: interface is assumed to already contain HibernateProxy.class
try {
final JavassistLazyInitializer instance = new JavassistLazyInitializer(
entityName,
persistentClass,
interfaces,
id,
getIdentifierMethod,
setIdentifierMethod,
componentIdType,
session
);
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
Class cl = factory.createClass();
final HibernateProxy proxy = ( HibernateProxy ) cl.newInstance();
( ( ProxyObject ) proxy ).setHandler( instance );
instance.constructed = true;
return proxy;
}
catch ( Throwable t ) {
LoggerFactory.getLogger( BasicLazyInitializer.class ).error(
"Javassist Enhancement failed: " + entityName, t
);
throw new HibernateException(
"Javassist 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 JavassistLazyInitializer instance = new JavassistLazyInitializer(
entityName,
persistentClass,
interfaces, id,
getIdentifierMethod,
setIdentifierMethod,
componentIdType,
session
);
final HibernateProxy proxy;
try {
proxy = ( HibernateProxy ) factory.newInstance();
}
catch ( Exception e ) {
throw new HibernateException(
"Javassist Enhancement failed: "
+ persistentClass.getName(), e
);
}
( ( ProxyObject ) proxy ).setHandler( instance );
instance.constructed = true;
return proxy;
}
|
public static Class getProxyFactory(Class persistentClass,
Class[] interfaces) throws HibernateException {
// note: interfaces is assumed to already contain HibernateProxy.class
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
return factory.createClass();
}
catch ( Throwable t ) {
LoggerFactory.getLogger( BasicLazyInitializer.class ).error(
"Javassist Enhancement failed: "
+ persistentClass.getName(), t
);
throw new HibernateException(
"Javassist Enhancement failed: "
+ persistentClass.getName(), t
);
}
}
|
public Object invoke(Object proxy,
Method thisMethod,
Method proceed,
Object[] args) throws Throwable {
if ( this.constructed ) {
Object result;
try {
result = this.invoke( thisMethod, args, proxy );
}
catch ( Throwable t ) {
throw new Exception( t.getCause() );
}
if ( result == INVOKE_IMPLEMENTATION ) {
Object target = getImplementation();
final Object returnValue;
try {
if ( ReflectHelper.isPublic( persistentClass, thisMethod ) ) {
if ( !thisMethod.getDeclaringClass().isInstance( target ) ) {
throw new ClassCastException( target.getClass().getName() );
}
returnValue = thisMethod.invoke( target, args );
}
else {
if ( !thisMethod.isAccessible() ) {
thisMethod.setAccessible( true );
}
returnValue = thisMethod.invoke( target, args );
}
return returnValue == target ? proxy : returnValue;
}
catch ( InvocationTargetException ite ) {
throw ite.getTargetException();
}
}
else {
return result;
}
}
else {
// while constructor is running
if ( thisMethod.getName().equals( "getHibernateLazyInitializer" ) ) {
return this;
}
else {
return proceed.invoke( proxy, args );
}
}
}
|
protected Object serializableProxy() {
return new SerializableProxy(
getEntityName(),
persistentClass,
interfaces,
getIdentifier(),
getIdentifierMethod,
setIdentifierMethod,
componentIdType
);
}
|