| Home >> All >> org >> hibernate >> bytecode >> [ javassist Javadoc ] |
Source code: org/hibernate/bytecode/javassist/ProxyFactoryFactoryImpl.java
1 package org.hibernate.bytecode.javassist; 2 3 import org.hibernate.bytecode.ProxyFactoryFactory; 4 import org.hibernate.bytecode.BasicProxyFactory; 5 import org.hibernate.proxy.ProxyFactory; 6 import org.hibernate.proxy.pojo.javassist.JavassistProxyFactory; 7 import org.hibernate.AssertionFailure; 8 import org.hibernate.HibernateException; 9 import javassist.util.proxy.MethodFilter; 10 import javassist.util.proxy.ProxyObject; 11 import javassist.util.proxy.MethodHandler; 12 13 import java.lang.reflect.Method; 14 import java.util.HashMap; 15 16 /** 17 * A factory for Javassist-based {@link ProxyFactory} instances. 18 * 19 * @author Steve Ebersole 20 */ 21 public class ProxyFactoryFactoryImpl implements ProxyFactoryFactory { 22 23 /** 24 * Builds a Javassist-based proxy factory. 25 * 26 * @return a new Javassist-based proxy factory. 27 */ 28 public ProxyFactory buildProxyFactory() { 29 return new JavassistProxyFactory(); 30 } 31 32 public BasicProxyFactory buildBasicProxyFactory(Class superClass, Class[] interfaces) { 33 return new BasicProxyFactoryImpl( superClass, interfaces ); 34 } 35 36 private static class BasicProxyFactoryImpl implements BasicProxyFactory { 37 private final Class proxyClass; 38 39 public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) { 40 if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) { 41 throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" ); 42 } 43 javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory(); 44 factory.setFilter( FINALIZE_FILTER ); 45 if ( superClass != null ) { 46 factory.setSuperclass( superClass ); 47 } 48 if ( interfaces != null && interfaces.length > 0 ) { 49 factory.setInterfaces( interfaces ); 50 } 51 proxyClass = factory.createClass(); 52 } 53 54 public Object getProxy() { 55 try { 56 ProxyObject proxy = ( ProxyObject ) proxyClass.newInstance(); 57 proxy.setHandler( new PassThroughHandler( proxy, proxyClass.getName() ) ); 58 return proxy; 59 } 60 catch ( Throwable t ) { 61 throw new HibernateException( "Unable to instantiated proxy instance" ); 62 } 63 } 64 65 public boolean isInstance(Object object) { 66 return proxyClass.isInstance( object ); 67 } 68 } 69 70 private static final MethodFilter FINALIZE_FILTER = new MethodFilter() { 71 public boolean isHandled(Method m) { 72 // skip finalize methods 73 return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) ); 74 } 75 }; 76 77 private static class PassThroughHandler implements MethodHandler { 78 private HashMap data = new HashMap(); 79 private final Object proxiedObject; 80 private final String proxiedClassName; 81 82 public PassThroughHandler(Object proxiedObject, String proxiedClassName) { 83 this.proxiedObject = proxiedObject; 84 this.proxiedClassName = proxiedClassName; 85 } 86 87 public Object invoke( 88 Object object, 89 Method method, 90 Method method1, 91 Object[] args) throws Exception { 92 String name = method.getName(); 93 if ( "toString".equals( name ) ) { 94 return proxiedClassName + "@" + System.identityHashCode( object ); 95 } 96 else if ( "equals".equals( name ) ) { 97 return proxiedObject == object ? Boolean.TRUE : Boolean.FALSE; 98 } 99 else if ( "hashCode".equals( name ) ) { 100 return new Integer( System.identityHashCode( object ) ); 101 } 102 boolean hasGetterSignature = method.getParameterTypes().length == 0 && method.getReturnType() != null; 103 boolean hasSetterSignature = method.getParameterTypes().length == 1 && ( method.getReturnType() == null || method.getReturnType() == void.class ); 104 if ( name.startsWith( "get" ) && hasGetterSignature ) { 105 String propName = name.substring( 3 ); 106 return data.get( propName ); 107 } 108 else if ( name.startsWith( "is" ) && hasGetterSignature ) { 109 String propName = name.substring( 2 ); 110 return data.get( propName ); 111 } 112 else if ( name.startsWith( "set" ) && hasSetterSignature) { 113 String propName = name.substring( 3 ); 114 data.put( propName, args[0] ); 115 return null; 116 } 117 else { 118 // todo : what else to do here? 119 return null; 120 } 121 } 122 } 123 }