001: //$Id: FieldInterceptorImpl.java 10209 2006-08-03 20:35:26Z steve.ebersole@jboss.com $
002: package org.hibernate.intercept.javassist;
003:
004: import java.io.Serializable;
005: import java.util.Set;
006:
007: import org.hibernate.bytecode.javassist.FieldHandler;
008: import org.hibernate.intercept.AbstractFieldInterceptor;
009: import org.hibernate.engine.SessionImplementor;
010: import org.hibernate.proxy.HibernateProxy;
011: import org.hibernate.proxy.LazyInitializer;
012:
013: /**
014: * A field-level interceptor that initializes lazily fetched properties.
015: * This interceptor can be attached to classes instrumented by Javassist.
016: * Note that this implementation assumes that the instance variable
017: * name is the same as the name of the persistent property that must
018: * be loaded.
019: * </p>
020: * Note: most of the interesting functionality here is farmed off
021: * to the super-class. The stuff here mainly acts as an adapter to the
022: * Javassist-specific functionality, routing interception through
023: * the super-class's intercept() method
024: *
025: * @author Steve Ebersole
026: */
027: public final class FieldInterceptorImpl extends
028: AbstractFieldInterceptor implements FieldHandler, Serializable {
029:
030: /**
031: * Package-protected constructor.
032: *
033: * @param session
034: * @param uninitializedFields
035: * @param entityName
036: */
037: FieldInterceptorImpl(SessionImplementor session,
038: Set uninitializedFields, String entityName) {
039: super (session, uninitializedFields, entityName);
040: }
041:
042: // FieldHandler impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
043:
044: public boolean readBoolean(Object target, String name,
045: boolean oldValue) {
046: return ((Boolean) intercept(target, name,
047: oldValue ? Boolean.TRUE : Boolean.FALSE))
048: .booleanValue();
049: }
050:
051: public byte readByte(Object target, String name, byte oldValue) {
052: return ((Byte) intercept(target, name, new Byte(oldValue)))
053: .byteValue();
054: }
055:
056: public char readChar(Object target, String name, char oldValue) {
057: return ((Character) intercept(target, name, new Character(
058: oldValue))).charValue();
059: }
060:
061: public double readDouble(Object target, String name, double oldValue) {
062: return ((Double) intercept(target, name, new Double(oldValue)))
063: .doubleValue();
064: }
065:
066: public float readFloat(Object target, String name, float oldValue) {
067: return ((Float) intercept(target, name, new Float(oldValue)))
068: .floatValue();
069: }
070:
071: public int readInt(Object target, String name, int oldValue) {
072: return ((Integer) intercept(target, name, new Integer(oldValue)))
073: .intValue();
074: }
075:
076: public long readLong(Object target, String name, long oldValue) {
077: return ((Long) intercept(target, name, new Long(oldValue)))
078: .longValue();
079: }
080:
081: public short readShort(Object target, String name, short oldValue) {
082: return ((Short) intercept(target, name, new Short(oldValue)))
083: .shortValue();
084: }
085:
086: public Object readObject(Object target, String name, Object oldValue) {
087: Object value = intercept(target, name, oldValue);
088: if (value instanceof HibernateProxy) {
089: LazyInitializer li = ((HibernateProxy) value)
090: .getHibernateLazyInitializer();
091: if (li.isUnwrap()) {
092: value = li.getImplementation();
093: }
094: }
095: return value;
096: }
097:
098: public boolean writeBoolean(Object target, String name,
099: boolean oldValue, boolean newValue) {
100: dirty();
101: intercept(target, name, oldValue ? Boolean.TRUE : Boolean.FALSE);
102: return newValue;
103: }
104:
105: public byte writeByte(Object target, String name, byte oldValue,
106: byte newValue) {
107: dirty();
108: intercept(target, name, new Byte(oldValue));
109: return newValue;
110: }
111:
112: public char writeChar(Object target, String name, char oldValue,
113: char newValue) {
114: dirty();
115: intercept(target, name, new Character(oldValue));
116: return newValue;
117: }
118:
119: public double writeDouble(Object target, String name,
120: double oldValue, double newValue) {
121: dirty();
122: intercept(target, name, new Double(oldValue));
123: return newValue;
124: }
125:
126: public float writeFloat(Object target, String name, float oldValue,
127: float newValue) {
128: dirty();
129: intercept(target, name, new Float(oldValue));
130: return newValue;
131: }
132:
133: public int writeInt(Object target, String name, int oldValue,
134: int newValue) {
135: dirty();
136: intercept(target, name, new Integer(oldValue));
137: return newValue;
138: }
139:
140: public long writeLong(Object target, String name, long oldValue,
141: long newValue) {
142: dirty();
143: intercept(target, name, new Long(oldValue));
144: return newValue;
145: }
146:
147: public short writeShort(Object target, String name, short oldValue,
148: short newValue) {
149: dirty();
150: intercept(target, name, new Short(oldValue));
151: return newValue;
152: }
153:
154: public Object writeObject(Object target, String name,
155: Object oldValue, Object newValue) {
156: dirty();
157: intercept(target, name, oldValue);
158: return newValue;
159: }
160:
161: public String toString() {
162: return "FieldInterceptorImpl(" + "entityName="
163: + getEntityName() + ",dirty=" + isDirty()
164: + ",uninitializedFields=" + getUninitializedFields()
165: + ')';
166: }
167:
168: }
|