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 java.lang.reflect.Modifier; 28 29 import net.sf.cglib.beans.BulkBean; 30 import net.sf.cglib.beans.BulkBeanException; 31 import net.sf.cglib.reflect.FastClass; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 import org.hibernate.bytecode.BytecodeProvider; 35 import org.hibernate.bytecode.ProxyFactoryFactory; 36 import org.hibernate.bytecode.ReflectionOptimizer; 37 import org.hibernate.bytecode.util.FieldFilter; 38 import org.hibernate.util.StringHelper; 39 40 /** 41 * Bytecode provider implementation for CGLIB. 42 * 43 * @author Steve Ebersole 44 */ 45 public class BytecodeProviderImpl implements BytecodeProvider { 46 47 private static final Logger log = LoggerFactory.getLogger( BytecodeProviderImpl.class ); 48 49 public BytecodeProviderImpl() { 50 log.warn( "The CGLIB BytecodeProvider impl is considered deprecated and not recommended for use" ); 51 } 52 53 public ProxyFactoryFactory getProxyFactoryFactory() { 54 return new ProxyFactoryFactoryImpl(); 55 } 56 57 public ReflectionOptimizer getReflectionOptimizer( 58 Class clazz, 59 String[] getterNames, 60 String[] setterNames, 61 Class[] types) { 62 FastClass fastClass; 63 BulkBean bulkBean; 64 try { 65 fastClass = FastClass.create( clazz ); 66 bulkBean = BulkBean.create( clazz, getterNames, setterNames, types ); 67 if ( !clazz.isInterface() && !Modifier.isAbstract( clazz.getModifiers() ) ) { 68 if ( fastClass == null ) { 69 bulkBean = null; 70 } 71 else { 72 //test out the optimizer: 73 Object instance = fastClass.newInstance(); 74 bulkBean.setPropertyValues( instance, bulkBean.getPropertyValues( instance ) ); 75 } 76 } 77 } 78 catch( Throwable t ) { 79 fastClass = null; 80 bulkBean = null; 81 String message = "reflection optimizer disabled for: " + 82 clazz.getName() + 83 " [" + 84 StringHelper.unqualify( t.getClass().getName() ) + 85 ": " + 86 t.getMessage(); 87 88 if (t instanceof BulkBeanException ) { 89 int index = ( (BulkBeanException) t ).getIndex(); 90 if (index >= 0) { 91 message += " (property " + setterNames[index] + ")"; 92 } 93 } 94 95 log.debug( message ); 96 } 97 98 if ( fastClass != null && bulkBean != null ) { 99 return new ReflectionOptimizerImpl( 100 new InstantiationOptimizerAdapter( fastClass ), 101 new AccessOptimizerAdapter( bulkBean, clazz ) 102 ); 103 } 104 else { 105 return null; 106 } 107 } 108 109 public org.hibernate.bytecode.ClassTransformer getTransformer(org.hibernate.bytecode.util.ClassFilter classFilter, FieldFilter fieldFilter) { 110 return new CglibClassTransformer( classFilter, fieldFilter ); 111 } 112 113 }