|
From: <one...@us...> - 2002-11-05 06:46:10
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy
In directory usw-pr-cvs1:/tmp/cvs-serv4847/hibernate/proxy
Modified Files:
HibernateProxy.java LazyInitializer.java
Log Message:
fixed minor problem with proxies for classes that override finalize
Index: HibernateProxy.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy/HibernateProxy.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** HibernateProxy.java 29 Oct 2002 07:50:41 -0000 1.4
--- HibernateProxy.java 5 Nov 2002 06:46:06 -0000 1.5
***************
*** 5,15 ****
public interface HibernateProxy extends Serializable {
- /*public Object getImplementation_() throws HibernateException, SQLException;
- public Serializable getIdentifier_();
- public SessionImplementor getSession_();
- public boolean isUninitialized_();
- public void setSession_(SessionImplementor session) throws HibernateException;
- public Class getClass_();*/
-
public Object writeReplace();
}
--- 5,8 ----
Index: LazyInitializer.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy/LazyInitializer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** LazyInitializer.java 31 Oct 2002 15:10:57 -0000 1.9
--- LazyInitializer.java 5 Nov 2002 06:46:06 -0000 1.10
***************
*** 8,12 ****
import org.apache.commons.logging.LogFactory;
- import cirrus.hibernate.AssertionFailure;
import cirrus.hibernate.HibernateException;
import cirrus.hibernate.LazyInitializationException;
--- 8,11 ----
***************
*** 20,53 ****
public abstract class LazyInitializer {
- //static final Method GET_IMPLEMENTATION;
- //static final Method GET_IDENTIFIER;
- //static final Method GET_SESSION;
- //static final Method GET_CLASS;
- //static final Method IS_UNINITIALIZED;
- //static final Method SET_SESSION;
- static final Method HASH_CODE;
- static final Method EQUALS;
- static final Method FINALIZE;
- //static final Method WRITE_REPLACE;
-
- static {
- try {
- //GET_IMPLEMENTATION = HibernateProxy.class.getDeclaredMethod("getImplementation_", null);
- //GET_CLASS = HibernateProxy.class.getDeclaredMethod("getClass_", null);
- //GET_IDENTIFIER = HibernateProxy.class.getDeclaredMethod("getIdentifier_", null);
- //GET_SESSION = HibernateProxy.class.getDeclaredMethod("getSession_", null);
- //WRITE_REPLACE = HibernateProxy.class.getDeclaredMethod("writeReplace", null);
- //IS_UNINITIALIZED = HibernateProxy.class.getDeclaredMethod("isUninitialized_", null);
- //SET_SESSION = HibernateProxy.class.getDeclaredMethod("setSession_", new Class[] { SessionImplementor.class } );
- HASH_CODE = Object.class.getDeclaredMethod("hashCode", null);
- EQUALS = Object.class.getDeclaredMethod("equals", new Class[] { Object.class } );
- FINALIZE = Object.class.getDeclaredMethod("finalize", null);
- }
- catch (Exception e) {
- //cannot occur
- throw new AssertionFailure( "Exception in static initializer of LazyInitializer", e );
- }
- }
-
protected Object target = null;
protected Serializable id;
--- 19,22 ----
***************
*** 93,133 ****
protected final Object invoke(Method method, Object[] args) throws Throwable {
! // TODO: if ( method.getDeclaringClass()!=Object or HibernateProxy )....
! if ( "writeReplace".equals( method.getName() ) ) {
! if (target==null && session!=null ) target = session.getEntity(
! new Key( id, session.getFactory().getPersister(persistentClass) )
! );
! if (target==null) {
! /*if ( session==null || !session.isOpen() ) {
! return session.getFactory().getPersister(persistentClass).instantiate(id); //A little "empty" object
}
! else {*/
! return serializableProxy();
! //}
}
! else {
! return target;
}
}
! else if ( !overridesEquals && method.equals(HASH_CODE) ) {
! // kinda dodgy, since it redefines the hashcode of the proxied object.
! // but necessary if we are to keep proxies in HashSets without
! // forcing them to be initialized
! return new Integer( id.hashCode() );
! }
! else if ( !overridesEquals && method.equals(EQUALS) ) {
! // less dodgy because Hibernate forces == to be same as identifier equals
! return new Boolean( id.equals( getIdentifierMethod.invoke( args[0], null ) ) );
! }
! else if ( method.equals(getIdentifierMethod) ) {
! return id;
! }
! else if ( method.equals(FINALIZE) ) {
! return null;
! }
! else {
! return method.invoke( getImplementation(), args );
}
}
--- 62,110 ----
protected final Object invoke(Method method, Object[] args) throws Throwable {
! String methodName = method.getName();
! int params = method.getParameterTypes().length;
! if ( params==0 ) {
!
! if ( "writeReplace".equals(methodName) ) {
!
! if (target==null && session!=null ) target = session.getEntity(
! new Key( id, session.getFactory().getPersister(persistentClass) )
! );
! if (target==null) {
! /*if ( session==null || !session.isOpen() ) {
! return session.getFactory().getPersister(persistentClass).instantiate(id); //A little "empty" object
! }
! else {*/
! return serializableProxy();
! //}
}
! else {
! return target;
! }
!
}
! else if ( !overridesEquals && "hashCode".equals(methodName) ) {
! // kinda dodgy, since it redefines the hashcode of the proxied object.
! // but necessary if we are to keep proxies in HashSets without
! // forcing them to be initialized
! return new Integer( id.hashCode() );
}
+ else if ( method.equals(getIdentifierMethod) ) {
+ return id;
+ }
+ else if ( "finalize".equals( method.getName() ) ) {
+ return null;
+ }
+
}
! else if ( params==1 && !overridesEquals && "equals".equals(methodName) ) {
! // less dodgy because Hibernate forces == to be same as identifier equals
! return new Boolean( id.equals( getIdentifierMethod.invoke( args[0], null ) ) );
}
+
+ // otherwise:
+ return method.invoke( getImplementation(), args );
+
}
|