1 /* 2 * Hibernate, Relational Persistence for Idiomatic Java 3 * 4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as 5 * indicated by the @author tags or express copyright attribution 6 * statements applied by the authors. All third-party contributions are 7 * distributed under license by Red Hat Middleware LLC. 8 * 9 * This copyrighted material is made available to anyone wishing to use, modify, 10 * copy, or redistribute it subject to the terms and conditions of the GNU 11 * Lesser General Public License, as published by the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 16 * for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License 19 * along with this distribution; if not, write to: 20 * Free Software Foundation, Inc. 21 * 51 Franklin Street, Fifth Floor 22 * Boston, MA 02110-1301 USA 23 * 24 */ 25 package org.hibernate.intercept.cglib; 26 27 import java.io.Serializable; 28 import java.util.Set; 29 30 import net.sf.cglib.transform.impl.InterceptFieldCallback; 31 32 import org.hibernate.intercept.AbstractFieldInterceptor; 33 import org.hibernate.engine.SessionImplementor; 34 import org.hibernate.proxy.HibernateProxy; 35 import org.hibernate.proxy.LazyInitializer; 36 37 /** 38 * A field-level interceptor that initializes lazily fetched properties. 39 * This interceptor can be attached to classes instrumented by CGLIB. 40 * Note that this implementation assumes that the instance variable 41 * name is the same as the name of the persistent property that must 42 * be loaded. 43 * 44 * @author Gavin King 45 */ 46 public final class FieldInterceptorImpl extends AbstractFieldInterceptor implements InterceptFieldCallback, Serializable { 47 48 /** 49 * Package-protected constructor 50 * 51 * @param session The Hibernate session 52 * @param uninitializedFields Names of the fields we need to initialize on load 53 * @param entityName The entity name to which we are being bound 54 */ 55 FieldInterceptorImpl(SessionImplementor session, Set uninitializedFields, String entityName) { 56 super( session, uninitializedFields, entityName ); 57 } 58 59 public boolean readBoolean(Object target, String name, boolean oldValue) { 60 return ( ( Boolean ) intercept( target, name, oldValue ? Boolean.TRUE : Boolean.FALSE ) ) 61 .booleanValue(); 62 } 63 64 public byte readByte(Object target, String name, byte oldValue) { 65 return ( ( Byte ) intercept( target, name, new Byte( oldValue ) ) ).byteValue(); 66 } 67 68 public char readChar(Object target, String name, char oldValue) { 69 return ( ( Character ) intercept( target, name, new Character( oldValue ) ) ) 70 .charValue(); 71 } 72 73 public double readDouble(Object target, String name, double oldValue) { 74 return ( ( Double ) intercept( target, name, new Double( oldValue ) ) ) 75 .doubleValue(); 76 } 77 78 public float readFloat(Object target, String name, float oldValue) { 79 return ( ( Float ) intercept( target, name, new Float( oldValue ) ) ) 80 .floatValue(); 81 } 82 83 public int readInt(Object target, String name, int oldValue) { 84 return ( ( Integer ) intercept( target, name, new Integer( oldValue ) ) ) 85 .intValue(); 86 } 87 88 public long readLong(Object target, String name, long oldValue) { 89 return ( ( Long ) intercept( target, name, new Long( oldValue ) ) ).longValue(); 90 } 91 92 public short readShort(Object target, String name, short oldValue) { 93 return ( ( Short ) intercept( target, name, new Short( oldValue ) ) ) 94 .shortValue(); 95 } 96 97 public Object readObject(Object target, String name, Object oldValue) { 98 Object value = intercept( target, name, oldValue ); 99 if (value instanceof HibernateProxy) { 100 LazyInitializer li = ( (HibernateProxy) value ).getHibernateLazyInitializer(); 101 if ( li.isUnwrap() ) { 102 value = li.getImplementation(); 103 } 104 } 105 return value; 106 } 107 108 public boolean writeBoolean(Object target, String name, boolean oldValue, boolean newValue) { 109 dirty(); 110 intercept( target, name, oldValue ? Boolean.TRUE : Boolean.FALSE ); 111 return newValue; 112 } 113 114 public byte writeByte(Object target, String name, byte oldValue, byte newValue) { 115 dirty(); 116 intercept( target, name, new Byte( oldValue ) ); 117 return newValue; 118 } 119 120 public char writeChar(Object target, String name, char oldValue, char newValue) { 121 dirty(); 122 intercept( target, name, new Character( oldValue ) ); 123 return newValue; 124 } 125 126 public double writeDouble(Object target, String name, double oldValue, double newValue) { 127 dirty(); 128 intercept( target, name, new Double( oldValue ) ); 129 return newValue; 130 } 131 132 public float writeFloat(Object target, String name, float oldValue, float newValue) { 133 dirty(); 134 intercept( target, name, new Float( oldValue ) ); 135 return newValue; 136 } 137 138 public int writeInt(Object target, String name, int oldValue, int newValue) { 139 dirty(); 140 intercept( target, name, new Integer( oldValue ) ); 141 return newValue; 142 } 143 144 public long writeLong(Object target, String name, long oldValue, long newValue) { 145 dirty(); 146 intercept( target, name, new Long( oldValue ) ); 147 return newValue; 148 } 149 150 public short writeShort(Object target, String name, short oldValue, short newValue) { 151 dirty(); 152 intercept( target, name, new Short( oldValue ) ); 153 return newValue; 154 } 155 156 public Object writeObject(Object target, String name, Object oldValue, Object newValue) { 157 dirty(); 158 intercept( target, name, oldValue ); 159 return newValue; 160 } 161 162 public String toString() { 163 return "FieldInterceptorImpl(" + 164 "entityName=" + getEntityName() + 165 ",dirty=" + isDirty() + 166 ",uninitializedFields=" + getUninitializedFields() + 167 ')'; 168 } 169 170 }