Package org.jboss.weld.bean.proxy
Class ProxyFactory<T>
- java.lang.Object
-
- org.jboss.weld.bean.proxy.ProxyFactory<T>
-
- All Implemented Interfaces:
PrivilegedAction<T>
- Direct Known Subclasses:
ClientProxyFactory
,DecoratorProxyFactory
,InterceptedProxyFactory
,InterceptedSubclassFactory
public class ProxyFactory<T> extends Object implements PrivilegedAction<T>
Main factory to produce proxy classes and instances for Weld beans. This implementation creates proxies which forward non-static method invocations to aBeanInstance
. All proxies implement theProxy
interface.- Author:
- David Allen, Stuart Douglas, Marius Bogoevici, Ales Justin
-
-
Field Summary
Fields Modifier and Type Field Description static String
CONSTRUCTED_FLAG_NAME
protected static BytecodeMethodResolver
DEFAULT_METHOD_RESOLVER
static String
DEFAULT_PROXY_PACKAGE
protected static String
INIT_METHOD_NAME
protected static String
INVOKE_METHOD_NAME
protected static String
LBEAN_IDENTIFIER
protected static String
LJAVA_LANG_BYTE
protected static String
LJAVA_LANG_CLASS
protected static String
LJAVA_LANG_OBJECT
protected static String
LJAVA_LANG_REFLECT_METHOD
protected static String
LJAVA_LANG_STRING
protected static String
LJAVA_LANG_THREAD_LOCAL
protected static String
METHOD_HANDLER_FIELD_NAME
static String
PROXY_SUFFIX
static String
WELD_PROXY_PREFIX
-
Constructor Summary
Constructors Constructor Description ProxyFactory(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, jakarta.enterprise.inject.spi.Bean<?> bean)
created a new proxy factory from a bean instance.ProxyFactory(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, jakarta.enterprise.inject.spi.Bean<?> bean, boolean forceSuperClass)
ProxyFactory(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, String proxyName, jakarta.enterprise.inject.spi.Bean<?> bean)
Creates a new proxy factory when the name of the proxy class is already known, such as during de-serializationProxyFactory(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, String proxyName, jakarta.enterprise.inject.spi.Bean<?> bean, boolean forceSuperClass)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addAdditionalInterfaces(Set<Class<?>> interfaces)
Sub classes may override to specify additional interfaces the proxy should implementprotected void
addConstructedGuardToMethodBody(org.jboss.classfilewriter.ClassMethod classMethod)
protected void
addConstructedGuardToMethodBody(org.jboss.classfilewriter.ClassMethod classMethod, String className)
Adds the following code to a delegating method:protected void
addConstructors(org.jboss.classfilewriter.ClassFile proxyClassType, List<DeferredBytecode> initialValueBytecode)
Adds a constructor for the proxy for each constructor declared by the base bean type.protected void
addFields(org.jboss.classfilewriter.ClassFile proxyClassType, List<DeferredBytecode> initialValueBytecode)
void
addInterface(Class<?> newInterface)
Adds an additional interface that the proxy should implement.void
addInterfacesFromTypeClosure(Set<? extends Type> typeClosure, Class<?> proxiedBeanType)
protected void
addMethods(org.jboss.classfilewriter.ClassFile proxyClassType, org.jboss.classfilewriter.ClassMethod staticConstructor)
protected void
addMethodsFromClass(org.jboss.classfilewriter.ClassFile proxyClassType, org.jboss.classfilewriter.ClassMethod staticConstructor)
protected void
addSerializationSupport(org.jboss.classfilewriter.ClassFile proxyClassType)
Adds special serialization code.protected void
addSpecialMethods(org.jboss.classfilewriter.ClassFile proxyClassType, org.jboss.classfilewriter.ClassMethod staticConstructor)
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 void
createForwardingMethodBody(org.jboss.classfilewriter.ClassMethod classMethod, MethodInformation method, org.jboss.classfilewriter.ClassMethod staticConstructor)
protected void
createInterceptorBody(org.jboss.classfilewriter.ClassMethod classMethod, MethodInformation method, org.jboss.classfilewriter.ClassMethod staticConstructor)
Creates the given method on the proxy class where the implementation forwards the call directly to the method handler.protected void
createSpecialMethodBody(org.jboss.classfilewriter.ClassMethod proxyClassType, MethodInformation method, org.jboss.classfilewriter.ClassMethod staticConstructor)
protected void
generateEqualsMethod(org.jboss.classfilewriter.ClassFile proxyClassType)
Generate the body of the proxies equals method.protected void
generateGetMethodHandlerBody(org.jboss.classfilewriter.ClassMethod method)
protected void
generateHashCodeMethod(org.jboss.classfilewriter.ClassFile proxyClassType)
Generate the body of the proxies hashCode method.protected void
generateSetMethodHandlerBody(org.jboss.classfilewriter.ClassMethod method)
Set<Class<?>>
getAdditionalInterfaces()
protected String
getBaseProxyName()
Returns the package and base name for the proxy class.jakarta.enterprise.inject.spi.Bean<?>
getBean()
Class<?>
getBeanType()
String
getContextId()
protected void
getMethodHandlerField(org.jboss.classfilewriter.ClassFile file, org.jboss.classfilewriter.code.CodeAttribute b)
protected Class<? extends MethodHandler>
getMethodHandlerType()
protected Class<?>
getProxiedBeanType()
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 Class<?>
getProxySuperclass()
protected void
invokeMethodHandler(org.jboss.classfilewriter.ClassMethod classMethod, MethodInformation method, boolean addReturnInstruction, BytecodeMethodResolver bytecodeMethodResolver, org.jboss.classfilewriter.ClassMethod staticConstructor)
calls methodHandler.invoke for a given methodprotected boolean
isMethodAccepted(Method method, Class<?> proxySuperclass)
protected boolean
isUsingProxyInstantiator()
T
run()
static <T> void
setBeanInstance(String contextId, T proxy, BeanInstance beanInstance, jakarta.enterprise.inject.spi.Bean<?> bean)
Convenience method to set the underlying bean instance for a proxy.protected Class<?>
toClass(org.jboss.classfilewriter.ClassFile ct, Class<?> originalClass, org.jboss.weld.serialization.spi.ProxyServices proxyServices, ProtectionDomain domain)
Delegates proxy creation viaProxyServices
to the integrator or to our own implementation.
-
-
-
Field Detail
-
PROXY_SUFFIX
public static final String PROXY_SUFFIX
- See Also:
- Constant Field Values
-
WELD_PROXY_PREFIX
public static final String WELD_PROXY_PREFIX
- 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
-
LJAVA_LANG_REFLECT_METHOD
protected static final String LJAVA_LANG_REFLECT_METHOD
- See Also:
- Constant Field Values
-
LJAVA_LANG_BYTE
protected static final String LJAVA_LANG_BYTE
- See Also:
- Constant Field Values
-
LJAVA_LANG_CLASS
protected static final String LJAVA_LANG_CLASS
- See Also:
- Constant Field Values
-
LJAVA_LANG_OBJECT
protected static final String LJAVA_LANG_OBJECT
- See Also:
- Constant Field Values
-
LBEAN_IDENTIFIER
protected static final String LBEAN_IDENTIFIER
- See Also:
- Constant Field Values
-
LJAVA_LANG_STRING
protected static final String LJAVA_LANG_STRING
- See Also:
- Constant Field Values
-
LJAVA_LANG_THREAD_LOCAL
protected static final String LJAVA_LANG_THREAD_LOCAL
- See Also:
- Constant Field Values
-
INIT_METHOD_NAME
protected static final String INIT_METHOD_NAME
- See Also:
- Constant Field Values
-
INVOKE_METHOD_NAME
protected static final String INVOKE_METHOD_NAME
- See Also:
- Constant Field Values
-
METHOD_HANDLER_FIELD_NAME
protected static final String METHOD_HANDLER_FIELD_NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProxyFactory
public ProxyFactory(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, jakarta.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(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, jakarta.enterprise.inject.spi.Bean<?> bean, boolean forceSuperClass)
-
ProxyFactory
public ProxyFactory(String contextId, Class<?> proxiedBeanType, Set<? extends Type> typeClosure, String proxyName, jakarta.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 classtypeClosure
- the bean types of the beanproxyName
- the name of the proxy class
-
-
Method Detail
-
setBeanInstance
public static <T> void setBeanInstance(String contextId, T proxy, BeanInstance beanInstance, jakarta.enterprise.inject.spi.Bean<?> bean)
Convenience method to set the underlying bean instance for a proxy.- Parameters:
proxy
- the proxy instancebeanInstance
- the instance of the bean
-
addInterfacesFromTypeClosure
public void addInterfacesFromTypeClosure(Set<? extends Type> typeClosure, Class<?> proxiedBeanType)
-
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
-
run
public T run()
- Specified by:
run
in interfacePrivilegedAction<T>
-
getProxyClass
public Class<T> getProxyClass()
Produces or returns the existing proxy class. The operation is thread-safe.- 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
-
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(org.jboss.classfilewriter.ClassFile proxyClassType, List<DeferredBytecode> initialValueBytecode)
Adds a constructor for the proxy for each constructor declared by the base bean type.- Parameters:
proxyClassType
- the Javassist class for the proxyinitialValueBytecode
-
-
addFields
protected void addFields(org.jboss.classfilewriter.ClassFile proxyClassType, List<DeferredBytecode> initialValueBytecode)
-
getMethodHandlerType
protected Class<? extends MethodHandler> getMethodHandlerType()
-
addMethods
protected void addMethods(org.jboss.classfilewriter.ClassFile proxyClassType, org.jboss.classfilewriter.ClassMethod staticConstructor)
-
addSerializationSupport
protected void addSerializationSupport(org.jboss.classfilewriter.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(org.jboss.classfilewriter.ClassFile proxyClassType, org.jboss.classfilewriter.ClassMethod staticConstructor)
-
generateHashCodeMethod
protected void generateHashCodeMethod(org.jboss.classfilewriter.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 void generateEqualsMethod(org.jboss.classfilewriter.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 void createSpecialMethodBody(org.jboss.classfilewriter.ClassMethod proxyClassType, MethodInformation method, org.jboss.classfilewriter.ClassMethod staticConstructor)
-
addConstructedGuardToMethodBody
protected void addConstructedGuardToMethodBody(org.jboss.classfilewriter.ClassMethod classMethod)
-
addConstructedGuardToMethodBody
protected void addConstructedGuardToMethodBody(org.jboss.classfilewriter.ClassMethod classMethod, String className)
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 void createForwardingMethodBody(org.jboss.classfilewriter.ClassMethod classMethod, MethodInformation method, org.jboss.classfilewriter.ClassMethod staticConstructor)
-
createInterceptorBody
protected void createInterceptorBody(org.jboss.classfilewriter.ClassMethod classMethod, MethodInformation method, org.jboss.classfilewriter.ClassMethod staticConstructor)
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:
classMethod
- the class methodmethod
- any JLR method
-
invokeMethodHandler
protected void invokeMethodHandler(org.jboss.classfilewriter.ClassMethod classMethod, MethodInformation method, boolean addReturnInstruction, BytecodeMethodResolver bytecodeMethodResolver, org.jboss.classfilewriter.ClassMethod staticConstructor)
calls methodHandler.invoke for a given method- Parameters:
method
- The method informationaddReturnInstruction
- set to true you want to return the result of the method invocationbytecodeMethodResolver
- The resolver that returns the method to invoke
-
addSpecialMethods
protected void addSpecialMethods(org.jboss.classfilewriter.ClassFile proxyClassType, org.jboss.classfilewriter.ClassMethod staticConstructor)
Adds methods requiring special implementations rather than just delegation.- Parameters:
proxyClassType
- the Javassist class description for the proxy type
-
generateSetMethodHandlerBody
protected void generateSetMethodHandlerBody(org.jboss.classfilewriter.ClassMethod method)
-
generateGetMethodHandlerBody
protected void generateGetMethodHandlerBody(org.jboss.classfilewriter.ClassMethod method)
-
getBeanType
public Class<?> getBeanType()
-
getBean
public jakarta.enterprise.inject.spi.Bean<?> getBean()
-
getContextId
public String getContextId()
-
getProxiedBeanType
protected Class<?> getProxiedBeanType()
-
getMethodHandlerField
protected void getMethodHandlerField(org.jboss.classfilewriter.ClassFile file, org.jboss.classfilewriter.code.CodeAttribute b)
-
getProxySuperclass
protected Class<?> getProxySuperclass()
-
isUsingProxyInstantiator
protected boolean isUsingProxyInstantiator()
- Returns:
true
ifProxyInstantiator
is used to instantiate proxy instances
-
toClass
protected Class<?> toClass(org.jboss.classfilewriter.ClassFile ct, Class<?> originalClass, org.jboss.weld.serialization.spi.ProxyServices proxyServices, ProtectionDomain domain)
Delegates proxy creation viaProxyServices
to the integrator or to our own implementation.
-
-