org.jboss.weld.bean.proxy
Class ProxyFactory<T>

java.lang.Object
  extended by org.jboss.weld.bean.proxy.ProxyFactory<T>
Direct Known Subclasses:
ClientProxyFactory, DecoratorProxyFactory, EnterpriseProxyFactory, InterceptedSubclassFactory

public class ProxyFactory<T>
extends Object

Main factory to produce proxy classes and instances for Weld beans. This implementation creates proxies which forward non-static method invocations to a BeanInstance. All proxies implement the Proxy interface.

Author:
David Allen, Stuart Douglas, Marius Bogoevici, Ales Justin

Field Summary
static String CONSTRUCTED_FLAG_NAME
           
protected static BytecodeMethodResolver DEFAULT_METHOD_RESOLVER
           
static String DEFAULT_PROXY_PACKAGE
           
protected static org.slf4j.cal10n.LocLogger log
           
static String PROXY_SUFFIX
           
 
Constructor Summary
ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> typeClosure, javax.enterprise.inject.spi.Bean<?> bean)
          created a new proxy factory from a bean instance.
ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> typeClosure, String proxyName, javax.enterprise.inject.spi.Bean<?> bean)
          Creates a new proxy factory when the name of the proxy class is already known, such as during de-serialization
 
Method Summary
protected  void addAdditionalInterfaces(Set<Class<?>> interfaces)
          Sub classes may override to specify additional interfaces the proxy should implement
protected  javassist.bytecode.Bytecode addConstructedGuardToMethodBody(javassist.bytecode.ClassFile proxyClassType, javassist.bytecode.Bytecode existingMethod, MethodInformation method)
          Adds the following code to a delegating method:

if(!this.constructed) return super.thisMethod()

This means that the proxy will not start to delegate to the underlying bean instance until after the constructor has finished.

protected  void addConstructors(javassist.bytecode.ClassFile proxyClassType, javassist.bytecode.Bytecode initialValueBytecode)
          Adds a constructor for the proxy for each constructor declared by the base bean type.
protected  void addFields(javassist.bytecode.ClassFile proxyClassType, javassist.bytecode.Bytecode initialValueBytecode)
           
 void addInterface(Class<?> newInterface)
          Adds an additional interface that the proxy should implement.
protected  void addMethods(javassist.bytecode.ClassFile proxyClassType)
           
protected  void addMethodsFromClass(javassist.bytecode.ClassFile proxyClassType)
           
protected  void addSerializationSupport(javassist.bytecode.ClassFile proxyClassType)
          Adds special serialization code.
protected  void addSpecialMethods(javassist.bytecode.ClassFile proxyClassType)
          Adds methods requiring special implementations rather than just delegation.
 T create(BeanInstance beanInstance)
          Method to create a new proxy that wraps the bean instance.
protected  javassist.bytecode.Bytecode createForwardingMethodBody(javassist.bytecode.ClassFile proxyClassType, MethodInformation method)
           
protected  javassist.bytecode.Bytecode createInterceptorBody(javassist.bytecode.ClassFile file, MethodInformation method)
          Creates the given method on the proxy class where the implementation forwards the call directly to the method handler.
protected  javassist.bytecode.Bytecode createSpecialMethodBody(javassist.bytecode.ClassFile proxyClassType, MethodInformation method)
           
protected  javassist.bytecode.MethodInfo generateEqualsMethod(javassist.bytecode.ClassFile proxyClassType)
          Generate the body of the proxies equals method.
protected  javassist.bytecode.MethodInfo generateHashCodeMethod(javassist.bytecode.ClassFile proxyClassType)
          Generate the body of the proxies hashCode method.
 Set<Class<?>> getAdditionalInterfaces()
           
protected  String getBaseProxyName()
          Returns the package and base name for the proxy class.
 javax.enterprise.inject.spi.Bean<?> getBean()
           
 Class<?> getBeanType()
           
 Class<T> getProxyClass()
          Produces or returns the existing proxy class.
protected  String getProxyNameSuffix()
          Returns a suffix to append to the name of the proxy class.
protected static void invokeMethodHandler(javassist.bytecode.ClassFile file, javassist.bytecode.Bytecode b, MethodInformation method, boolean addReturnInstruction, BytecodeMethodResolver bytecodeMethodResolver)
          calls methodHandler.invoke for a given method
static boolean isProxy(Object proxySuspect)
          Convenience method to determine if an object is a proxy generated by this factory or any derived factory.
static ClassLoader resolveClassLoaderForBeanProxy(javax.enterprise.inject.spi.Bean<?> bean)
           
static ClassLoader resolveClassLoaderForBeanProxy(javax.enterprise.inject.spi.Bean<?> bean, Proxies.TypeInfo typeInfo)
          Figures out the correct class loader to use for a proxy for a given bean
static
<T> void
setBeanInstance(T proxy, BeanInstance beanInstance, javax.enterprise.inject.spi.Bean<?> bean)
          Convenience method to set the underlying bean instance for a proxy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.slf4j.cal10n.LocLogger log

PROXY_SUFFIX

public static final String PROXY_SUFFIX
See Also:
Constant Field Values

DEFAULT_PROXY_PACKAGE

public static final String DEFAULT_PROXY_PACKAGE
See Also:
Constant Field Values

CONSTRUCTED_FLAG_NAME

public static final String CONSTRUCTED_FLAG_NAME
See Also:
Constant Field Values

DEFAULT_METHOD_RESOLVER

protected static final BytecodeMethodResolver DEFAULT_METHOD_RESOLVER
Constructor Detail

ProxyFactory

public ProxyFactory(Class<?> proxiedBeanType,
                    Set<? extends Type> typeClosure,
                    javax.enterprise.inject.spi.Bean<?> bean)
created a new proxy factory from a bean instance. The proxy name is generated from the bean id


ProxyFactory

public ProxyFactory(Class<?> proxiedBeanType,
                    Set<? extends Type> typeClosure,
                    String proxyName,
                    javax.enterprise.inject.spi.Bean<?> bean)
Creates a new proxy factory when the name of the proxy class is already known, such as during de-serialization

Parameters:
proxiedBeanType - the super-class for this proxy class
typeClosure - the bean types of the bean
proxyName - the name of the proxy class
Method Detail

addInterface

public void addInterface(Class<?> newInterface)
Adds an additional interface that the proxy should implement. The default implementation will be to forward invocations to the bean instance.

Parameters:
newInterface - an interface

create

public T create(BeanInstance beanInstance)
Method to create a new proxy that wraps the bean instance.

Parameters:
beanInstance - the bean instance
Returns:
a new proxy object

getProxyClass

public Class<T> getProxyClass()
Produces or returns the existing proxy class.

Returns:
always the class of the proxy

getBaseProxyName

protected String getBaseProxyName()
Returns the package and base name for the proxy class.

Returns:
base name without suffixes

isProxy

public static boolean isProxy(Object proxySuspect)
Convenience method to determine if an object is a proxy generated by this factory or any derived factory.

Parameters:
proxySuspect - the object suspected of being a proxy
Returns:
true only if it is a proxy object

setBeanInstance

public static <T> void setBeanInstance(T proxy,
                                       BeanInstance beanInstance,
                                       javax.enterprise.inject.spi.Bean<?> bean)
Convenience method to set the underlying bean instance for a proxy.

Parameters:
proxy - the proxy instance
beanInstance - the instance of the bean

getProxyNameSuffix

protected String getProxyNameSuffix()
Returns a suffix to append to the name of the proxy class. The name already consists of _$$_Weld, to which the suffix is added. This allows the creation of different types of proxies for the same class.

Returns:
a name suffix

addAdditionalInterfaces

protected void addAdditionalInterfaces(Set<Class<?>> interfaces)
Sub classes may override to specify additional interfaces the proxy should implement


addConstructors

protected void addConstructors(javassist.bytecode.ClassFile proxyClassType,
                               javassist.bytecode.Bytecode initialValueBytecode)
Adds a constructor for the proxy for each constructor declared by the base bean type.

Parameters:
proxyClassType - the Javassist class for the proxy
initialValueBytecode -

addFields

protected void addFields(javassist.bytecode.ClassFile proxyClassType,
                         javassist.bytecode.Bytecode initialValueBytecode)

addMethods

protected void addMethods(javassist.bytecode.ClassFile proxyClassType)

addSerializationSupport

protected void addSerializationSupport(javassist.bytecode.ClassFile proxyClassType)
Adds special serialization code. By default this is a nop

Parameters:
proxyClassType - the Javassist class for the proxy class

addMethodsFromClass

protected void addMethodsFromClass(javassist.bytecode.ClassFile proxyClassType)

generateHashCodeMethod

protected javassist.bytecode.MethodInfo generateHashCodeMethod(javassist.bytecode.ClassFile proxyClassType)
Generate the body of the proxies hashCode method.

If this method returns null, the method will not be added, and the hashCode on the superclass will be used as per normal virtual method resolution rules


generateEqualsMethod

protected javassist.bytecode.MethodInfo generateEqualsMethod(javassist.bytecode.ClassFile proxyClassType)
Generate the body of the proxies equals method.

If this method returns null, the method will not be added, and the hashCode on the superclass will be used as per normal virtual method resolution rules

Parameters:
proxyClassType - The class file

createSpecialMethodBody

protected javassist.bytecode.Bytecode createSpecialMethodBody(javassist.bytecode.ClassFile proxyClassType,
                                                              MethodInformation method)
                                                       throws javassist.NotFoundException
Throws:
javassist.NotFoundException

addConstructedGuardToMethodBody

protected javassist.bytecode.Bytecode addConstructedGuardToMethodBody(javassist.bytecode.ClassFile proxyClassType,
                                                                      javassist.bytecode.Bytecode existingMethod,
                                                                      MethodInformation method)
Adds the following code to a delegating method:

if(!this.constructed) return super.thisMethod()

This means that the proxy will not start to delegate to the underlying bean instance until after the constructor has finished.


createForwardingMethodBody

protected javassist.bytecode.Bytecode createForwardingMethodBody(javassist.bytecode.ClassFile proxyClassType,
                                                                 MethodInformation method)
                                                          throws javassist.NotFoundException
Throws:
javassist.NotFoundException

createInterceptorBody

protected javassist.bytecode.Bytecode createInterceptorBody(javassist.bytecode.ClassFile file,
                                                            MethodInformation method)
                                                     throws javassist.NotFoundException
Creates the given method on the proxy class where the implementation forwards the call directly to the method handler.

the generated bytecode is equivalent to:

return (RetType) methodHandler.invoke(this,param1,param2);

Parameters:
file - the class file
method - any JLR method
Returns:
the method byte code
Throws:
javassist.NotFoundException

invokeMethodHandler

protected static void invokeMethodHandler(javassist.bytecode.ClassFile file,
                                          javassist.bytecode.Bytecode b,
                                          MethodInformation method,
                                          boolean addReturnInstruction,
                                          BytecodeMethodResolver bytecodeMethodResolver)
calls methodHandler.invoke for a given method

Parameters:
file - the current class file
b - the bytecode to add the methodHandler.invoke call to
method - The method information
addReturnInstruction - set to true you want to return the result of the method invocation
bytecodeMethodResolver - The resolver that returns the method to invoke

addSpecialMethods

protected void addSpecialMethods(javassist.bytecode.ClassFile proxyClassType)
Adds methods requiring special implementations rather than just delegation.

Parameters:
proxyClassType - the Javassist class description for the proxy type

getBeanType

public Class<?> getBeanType()

getAdditionalInterfaces

public Set<Class<?>> getAdditionalInterfaces()

getBean

public javax.enterprise.inject.spi.Bean<?> getBean()

resolveClassLoaderForBeanProxy

public static ClassLoader resolveClassLoaderForBeanProxy(javax.enterprise.inject.spi.Bean<?> bean,
                                                         Proxies.TypeInfo typeInfo)
Figures out the correct class loader to use for a proxy for a given bean


resolveClassLoaderForBeanProxy

public static ClassLoader resolveClassLoaderForBeanProxy(javax.enterprise.inject.spi.Bean<?> bean)


Copyright © 2013 Seam Framework. All Rights Reserved.