1 package org.hibernate.proxy; 3 4 import java.io.Serializable ; 5 import java.lang.reflect.Method ; 6 7 import org.hibernate.engine.EntityKey; 8 import org.hibernate.engine.SessionImplementor; 9 import org.hibernate.type.AbstractComponentType; 10 import org.hibernate.util.MarkerObject; 11 import org.hibernate.util.ReflectHelper; 12 13 17 public abstract class BasicLazyInitializer extends AbstractLazyInitializer { 18 19 protected static final Object INVOKE_IMPLEMENTATION = new MarkerObject("INVOKE_IMPLEMENTATION"); 20 21 protected Class persistentClass; 22 protected Method getIdentifierMethod; 23 protected Method setIdentifierMethod; 24 protected boolean overridesEquals; 25 private Object replacement; 26 protected AbstractComponentType componentIdType; 27 28 protected BasicLazyInitializer( 29 String entityName, 30 Class persistentClass, 31 Serializable id, 32 Method getIdentifierMethod, 33 Method setIdentifierMethod, 34 AbstractComponentType componentIdType, 35 SessionImplementor session) { 36 super(entityName, id, session); 37 this.persistentClass = persistentClass; 38 this.getIdentifierMethod = getIdentifierMethod; 39 this.setIdentifierMethod = setIdentifierMethod; 40 this.componentIdType = componentIdType; 41 overridesEquals = ReflectHelper.overridesEquals(persistentClass); 42 } 43 44 protected abstract Object serializableProxy(); 45 46 protected final Object invoke(Method method, Object [] args, Object proxy) throws Throwable { 47 48 String methodName = method.getName(); 49 int params = method.getParameterTypes().length; 50 51 if ( params==0 ) { 52 53 if ( "writeReplace".equals(methodName) ) { 54 55 final Object target = getTarget(); 56 final SessionImplementor session = getSession(); 57 if ( target==null && session!=null ) { 58 final EntityKey key = new EntityKey( 59 getIdentifier(), 60 session.getFactory().getEntityPersister( getEntityName() ), 61 session.getEntityMode() 62 ); 63 setTarget( session.getPersistenceContext().getEntity(key) ); 64 } 65 if (target==null) { 66 if (replacement==null) replacement = serializableProxy(); 67 return replacement; 68 } 69 else { 70 return target; 71 } 72 76 77 } 78 else if ( !overridesEquals && "hashCode".equals(methodName) ) { 79 return new Integer ( System.identityHashCode(proxy) ); 80 } 81 else if ( isUninitialized() && method.equals(getIdentifierMethod) ) { 82 return getIdentifier(); 83 } 84 else if ( "finalize".equals(methodName) ) { 85 return null; 86 } 87 else if ( "getHibernateLazyInitializer".equals(methodName) ) { 88 return this; 89 } 90 91 } 92 else if ( params==1 ) { 93 94 if ( !overridesEquals && "equals".equals(methodName) ) { 95 return args[0]==proxy ? Boolean.TRUE : Boolean.FALSE; 96 } 97 else if ( method.equals(setIdentifierMethod) ) { 98 initialize(); 99 setIdentifier( (Serializable ) args[0] ); 100 return INVOKE_IMPLEMENTATION; 101 } 102 103 } 104 105 if ( componentIdType!=null && componentIdType.isMethodOf(method) ) { 107 return method.invoke( getIdentifier(), args ); 108 } 109 110 return INVOKE_IMPLEMENTATION; 112 113 } 114 115 public final Class getPersistentClass() { 116 return persistentClass; 117 } 118 119 } 120 | Popular Tags |