Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/intercept/javassist/FieldInterceptorImpl.java


1   //$Id: FieldInterceptorImpl.java 9210 2006-02-03 22:15:19Z steveebersole $
2   package org.hibernate.intercept.javassist;
3   
4   import java.io.Serializable;
5   import java.util.Set;
6   
7   import org.hibernate.tool.instrument.javassist.FieldHandler;
8   import org.hibernate.intercept.AbstractFieldInterceptor;
9   import org.hibernate.engine.SessionImplementor;
10  import org.hibernate.proxy.HibernateProxy;
11  import org.hibernate.proxy.LazyInitializer;
12  
13  /**
14   * A field-level interceptor that initializes lazily fetched properties.
15   * This interceptor can be attached to classes instrumented by Javassist.
16   * Note that this implementation assumes that the instance variable
17   * name is the same as the name of the persistent property that must
18   * be loaded.
19   * </p>
20   * Note: most of the interesting functionality here is farmed off
21   * to the super-class.  The stuff here mainly acts as an adapter to the
22   * Javassist-specific functionality, routing interception through
23   * the super-class's intercept() method
24   *
25   * @author Steve Ebersole
26   */
27  public final class FieldInterceptorImpl extends AbstractFieldInterceptor implements FieldHandler, Serializable {
28  
29    /**
30     * Package-protected constructor.
31     *
32     * @param session
33     * @param uninitializedFields
34     * @param entityName
35     */
36    FieldInterceptorImpl(SessionImplementor session, Set uninitializedFields, String entityName) {
37      super( session, uninitializedFields, entityName );
38    }
39  
40  
41    // FieldHandler impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  
43    public boolean readBoolean(Object target, String name, boolean oldValue) {
44      return ( ( Boolean ) intercept( target, name, oldValue  ? Boolean.TRUE : Boolean.FALSE ) )
45          .booleanValue();
46    }
47  
48    public byte readByte(Object target, String name, byte oldValue) {
49      return ( ( Byte ) intercept( target, name, new Byte( oldValue ) ) ).byteValue();
50    }
51  
52    public char readChar(Object target, String name, char oldValue) {
53      return ( ( Character ) intercept( target, name, new Character( oldValue ) ) )
54          .charValue();
55    }
56  
57    public double readDouble(Object target, String name, double oldValue) {
58      return ( ( Double ) intercept( target, name, new Double( oldValue ) ) )
59          .doubleValue();
60    }
61  
62    public float readFloat(Object target, String name, float oldValue) {
63      return ( ( Float ) intercept( target, name, new Float( oldValue ) ) )
64          .floatValue();
65    }
66  
67    public int readInt(Object target, String name, int oldValue) {
68      return ( ( Integer ) intercept( target, name, new Integer( oldValue ) ) )
69          .intValue();
70    }
71  
72    public long readLong(Object target, String name, long oldValue) {
73      return ( ( Long ) intercept( target, name, new Long( oldValue ) ) ).longValue();
74    }
75  
76    public short readShort(Object target, String name, short oldValue) {
77      return ( ( Short ) intercept( target, name, new Short( oldValue ) ) )
78          .shortValue();
79    }
80  
81    public Object readObject(Object target, String name, Object oldValue) {
82      Object value = intercept( target, name, oldValue );
83      if (value instanceof HibernateProxy) {
84        LazyInitializer li = ( (HibernateProxy) value ).getHibernateLazyInitializer();
85        if ( li.isUnwrap() ) {
86          value = li.getImplementation();
87        }
88      }
89      return value;
90    }
91  
92    public boolean writeBoolean(Object target, String name, boolean oldValue, boolean newValue) {
93      dirty();
94      intercept( target, name, oldValue ? Boolean.TRUE : Boolean.FALSE );
95      return newValue;
96    }
97  
98    public byte writeByte(Object target, String name, byte oldValue, byte newValue) {
99      dirty();
100     intercept( target, name, new Byte( oldValue ) );
101     return newValue;
102   }
103 
104   public char writeChar(Object target, String name, char oldValue, char newValue) {
105     dirty();
106     intercept( target, name, new Character( oldValue ) );
107     return newValue;
108   }
109 
110   public double writeDouble(Object target, String name, double oldValue, double newValue) {
111     dirty();
112     intercept( target, name, new Double( oldValue ) );
113     return newValue;
114   }
115 
116   public float writeFloat(Object target, String name, float oldValue, float newValue) {
117     dirty();
118     intercept( target, name, new Float( oldValue ) );
119     return newValue;
120   }
121 
122   public int writeInt(Object target, String name, int oldValue, int newValue) {
123     dirty();
124     intercept( target, name, new Integer( oldValue ) );
125     return newValue;
126   }
127 
128   public long writeLong(Object target, String name, long oldValue, long newValue) {
129     dirty();
130     intercept( target, name, new Long( oldValue ) );
131     return newValue;
132   }
133 
134   public short writeShort(Object target, String name, short oldValue, short newValue) {
135     dirty();
136     intercept( target, name, new Short( oldValue ) );
137     return newValue;
138   }
139 
140   public Object writeObject(Object target, String name, Object oldValue, Object newValue) {
141     dirty();
142     intercept( target, name, oldValue );
143     return newValue;
144   }
145 
146   public String toString() {
147     return "FieldInterceptorImpl(" +
148       "entityName=" + getEntityName() +
149       ",dirty=" + isDirty() +
150       ",uninitializedFields=" + getUninitializedFields() +
151       ')';
152   }
153 
154 }