Package org.jboss.invocation.proxy
Class ProxyFactory<T>
- java.lang.Object
-
- org.jboss.invocation.proxy.AbstractClassFactory<T>
-
- org.jboss.invocation.proxy.AbstractSubclassFactory<T>
-
- org.jboss.invocation.proxy.AbstractProxyFactory<T>
-
- org.jboss.invocation.proxy.ProxyFactory<T>
-
- Type Parameters:
T- the superclass type
public class ProxyFactory<T> extends AbstractProxyFactory<T>
Proxy Factory that generates proxies that delegate all calls to anInvocationHandler.Typical usage looks like:
ProxyFactory<SimpleClass> proxyFactory = new ProxyFactory<SimpleClass>(SimpleClass.class); SimpleClass instance = proxyFactory.newInstance(new SimpleDispatcher());
This will create a proxy for SimpleClass, and return a new instance that handles invocations using the InvocationDispatcher SimpleDispatcher.Invocations on these proxies are very efficient, as no reflection is involved.
- Author:
- Stuart Douglas
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classProxyFactory.ProxyConstructorBodyCreatorGenerates a proxy constructor that delegates to super(), and then sets the constructed flag to true.classProxyFactory.ProxyMethodBodyCreatorOverrides superclass methods and forwards calls to the dispatcher.classProxyFactory.WriteReplaceBodyCreatorGenerates the writeReplace method if advanced serialization is enabled.
-
Field Summary
Fields Modifier and Type Field Description static StringCONSTRUCTED_GUARDthis field on the generated class stores if the constructor has been completed yet.static StringINVOCATION_HANDLER_FIELDName of the field that holds the generated dispatcher on the generated proxy-
Fields inherited from class org.jboss.invocation.proxy.AbstractSubclassFactory
reflectionMetadataSource
-
Fields inherited from class org.jboss.invocation.proxy.AbstractClassFactory
classFile
-
-
Constructor Summary
Constructors Constructor Description ProxyFactory(ProxyConfiguration<T> proxyConfiguration)Construct a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidgenerateClass()Generate the class.MethodBodyCreatorgetDefaultMethodOverride()Returns the defaultMethodBodyCreatorto use when creating overridden methods.InvocationHandlergetInvocationHandler(Object proxy)Returns the invocation handler for a proxy created from this factory.static InvocationHandlergetInvocationHandlerStatic(Object proxy)Gets theInvocationHandlerfor a given proxy instance.TnewInstance(InvocationHandler handler)Create a new proxy, initialising it with the given invocation handler.voidsetInvocationHandler(Object proxy, InvocationHandler handler)Sets the invocation handler for a proxy created from this factory.static voidsetInvocationHandlerStatic(Object proxy, InvocationHandler handler)Sets the invocation handler for a proxy.voidsetSerializableProxyClass(Class<? extends SerializableProxy> serializableProxyClass)Sets theSerializableProxyclass to emit from the proxies writeReplace method.-
Methods inherited from class org.jboss.invocation.proxy.AbstractProxyFactory
afterClassLoad, cleanup, finalizeStaticConstructor, getCachedMethods, loadMethodIdentifier
-
Methods inherited from class org.jboss.invocation.proxy.AbstractSubclassFactory
addInterface, addInterface, createConstructorDelegates, createConstructorDelegates, getDefaultConstructorOverride, overrideAllMethods, overrideAllMethods, overrideClone, overrideClone, overrideEquals, overrideEquals, overrideFinalize, overrideFinalize, overrideHashcode, overrideHashcode, overrideMethod, overrideMethod, overridePublicMethods, overridePublicMethods, overrideToString, overrideToString
-
Methods inherited from class org.jboss.invocation.proxy.AbstractClassFactory
buildClassDefinition, defineClass, getClassLoader, getClassName, getProtectionDomain, getSuperClass, getSuperClassName, isProxyClassDefined, isProxyClassDefined, newInstance
-
-
-
-
Field Detail
-
INVOCATION_HANDLER_FIELD
public static final String INVOCATION_HANDLER_FIELD
Name of the field that holds the generated dispatcher on the generated proxy- See Also:
- Constant Field Values
-
CONSTRUCTED_GUARD
public static final String CONSTRUCTED_GUARD
this field on the generated class stores if the constructor has been completed yet. No methods will be delegated to the dispatcher until the constructor has finished. This prevents virtual methods called from the constructor being delegated to a handler that is null.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProxyFactory
public ProxyFactory(ProxyConfiguration<T> proxyConfiguration)
Construct a new instance.- Parameters:
proxyConfiguration- The configuration to use to build the proxy
-
-
Method Detail
-
newInstance
public T newInstance(InvocationHandler handler) throws InstantiationException, IllegalAccessException
Create a new proxy, initialising it with the given invocation handler.- Parameters:
handler- the invocation handler to use- Returns:
- the new proxy instance
- Throws:
IllegalAccessException- if the constructor is not accessibleInstantiationException- if instantiation failed due to an exception
-
generateClass
protected void generateClass()
Generate the class.- Specified by:
generateClassin classAbstractClassFactory<T>
-
setSerializableProxyClass
public void setSerializableProxyClass(Class<? extends SerializableProxy> serializableProxyClass)
Sets theSerializableProxyclass to emit from the proxies writeReplace method. If this is set to null (the default) then no writeReplace method will be generated. The proxy may still be serializable, providing that the superclass andInvocationHandlerare both serializable.- Parameters:
serializableProxyClass- the proxy class- Throws:
IllegalStateException- If the proxy class has already been generated- See Also:
SerializableProxy,DefaultSerializableProxy
-
getDefaultMethodOverride
public MethodBodyCreator getDefaultMethodOverride()
Description copied from class:AbstractSubclassFactoryReturns the defaultMethodBodyCreatorto use when creating overridden methods.- Overrides:
getDefaultMethodOverridein classAbstractSubclassFactory<T>- Returns:
- the default method body creator
-
setInvocationHandler
public void setInvocationHandler(Object proxy, InvocationHandler handler)
Sets the invocation handler for a proxy created from this factory.- Parameters:
proxy- the proxy to modifyhandler- the handler to use
-
getInvocationHandler
public InvocationHandler getInvocationHandler(Object proxy)
Returns the invocation handler for a proxy created from this factory.- Parameters:
proxy- the proxy- Returns:
- the invocation handler
-
setInvocationHandlerStatic
public static void setInvocationHandlerStatic(Object proxy, InvocationHandler handler)
Sets the invocation handler for a proxy. This method is less efficient thansetInvocationHandler(Object, InvocationHandler), however it will work on any proxy, not just proxies from a specific factory.- Parameters:
proxy- the proxy to modifyhandler- the handler to use
-
getInvocationHandlerStatic
public static InvocationHandler getInvocationHandlerStatic(Object proxy)
Gets theInvocationHandlerfor a given proxy instance. This method is less efficient thangetInvocationHandler(Object), however it will work for any proxy, not just proxies from a specific factory instance.- Parameters:
proxy- the proxy- Returns:
- the invocation handler
-
-