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.bytecode.cglib; 26 27 import org.hibernate.bytecode.ReflectionOptimizer; 28 import org.hibernate.PropertyAccessException; 29 import net.sf.cglib.beans.BulkBean; 30 import net.sf.cglib.beans.BulkBeanException; 31 32 import java.io.Serializable; 33 import java.io.ObjectOutputStream; 34 import java.io.IOException; 35 import java.io.ObjectInputStream; 36 37 /** 38 * The {@link ReflectionOptimizer.AccessOptimizer} implementation for CGLIB 39 * which simply acts as an adpater to the {@link BulkBean} class. 40 * 41 * @author Steve Ebersole 42 */ 43 public class AccessOptimizerAdapter implements ReflectionOptimizer.AccessOptimizer, Serializable { 44 45 public static final String PROPERTY_GET_EXCEPTION = 46 "exception getting property value with CGLIB (set hibernate.bytecode.use_reflection_optimizer=false for more info)"; 47 48 public static final String PROPERTY_SET_EXCEPTION = 49 "exception setting property value with CGLIB (set hibernate.bytecode.use_reflection_optimizer=false for more info)"; 50 51 private Class mappedClass; 52 private BulkBean bulkBean; 53 54 public AccessOptimizerAdapter(BulkBean bulkBean, Class mappedClass) { 55 this.bulkBean = bulkBean; 56 this.mappedClass = mappedClass; 57 } 58 59 public String[] getPropertyNames() { 60 return bulkBean.getGetters(); 61 } 62 63 public Object[] getPropertyValues(Object object) { 64 try { 65 return bulkBean.getPropertyValues( object ); 66 } 67 catch ( Throwable t ) { 68 throw new PropertyAccessException( 69 t, 70 PROPERTY_GET_EXCEPTION, 71 false, 72 mappedClass, 73 getterName( t, bulkBean ) 74 ); 75 } 76 } 77 78 public void setPropertyValues(Object object, Object[] values) { 79 try { 80 bulkBean.setPropertyValues( object, values ); 81 } 82 catch ( Throwable t ) { 83 throw new PropertyAccessException( 84 t, 85 PROPERTY_SET_EXCEPTION, 86 true, 87 mappedClass, 88 setterName( t, bulkBean ) 89 ); 90 } 91 } 92 93 private static String setterName(Throwable t, BulkBean optimizer) { 94 if ( t instanceof BulkBeanException ) { 95 return optimizer.getSetters()[( ( BulkBeanException ) t ).getIndex()]; 96 } 97 else { 98 return "?"; 99 } 100 } 101 102 private static String getterName(Throwable t, BulkBean optimizer) { 103 if ( t instanceof BulkBeanException ) { 104 return optimizer.getGetters()[( ( BulkBeanException ) t ).getIndex()]; 105 } 106 else { 107 return "?"; 108 } 109 } 110 111 private void writeObject(ObjectOutputStream out) throws IOException { 112 out.writeObject( mappedClass ); 113 out.writeObject( bulkBean.getGetters() ); 114 out.writeObject( bulkBean.getSetters() ); 115 out.writeObject( bulkBean.getPropertyTypes() ); 116 } 117 118 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 119 Class beanClass = ( Class ) in.readObject(); 120 String[] getters = ( String[] ) in.readObject(); 121 String[] setters = ( String[] ) in.readObject(); 122 Class[] types = ( Class[] ) in.readObject(); 123 bulkBean = BulkBean.create( beanClass, getters, setters, types ); 124 } 125 }