Invocation API 1.1.0.Final

org.jboss.invocation.proxy
Class ProxyFactory<T>

java.lang.Object
  extended by org.jboss.invocation.proxy.AbstractClassFactory<T>
      extended by org.jboss.invocation.proxy.AbstractSubclassFactory<T>
          extended by org.jboss.invocation.proxy.AbstractProxyFactory<T>
              extended by 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 an InvocationHandler.

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
 class ProxyFactory.ProxyConstructorBodyCreator
          Generates a proxy constructor that delegates to super(), and then sets the constructed flag to true.
 class ProxyFactory.ProxyMethodBodyCreator
          Overrides superclass methods and forwards calls to the dispatcher.
 class ProxyFactory.WriteReplaceBodyCreator
          Generates the writeReplace method if advanced serialization is enabled.
 
Field Summary
static String CONSTRUCTED_GUARD
          this field on the generated class stores if the constructor has been completed yet.
static String INVOCATION_HANDLER_FIELD
          Name 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
ProxyFactory(ProxyConfiguration<T> proxyConfiguration)
          Construct a new instance.
 
Method Summary
protected  void generateClass()
          Generate the class.
 MethodBodyCreator getDefaultMethodOverride()
          Returns the default MethodBodyCreator to use when creating overridden methods.
 InvocationHandler getInvocationHandler(Object proxy)
          Returns the invocation handler for a proxy created from this factory.
static InvocationHandler getInvocationHandlerStatic(Object proxy)
          Gets the InvocationHandler for a given proxy instance.
 T newInstance(InvocationHandler handler)
          Create a new proxy, initialising it with the given invocation handler.
 void setInvocationHandler(Object proxy, InvocationHandler handler)
          Sets the invocation handler for a proxy created from this factory.
static void setInvocationHandlerStatic(Object proxy, InvocationHandler handler)
          Sets the invocation handler for a proxy.
 void setSerializableProxyClass(Class<? extends SerializableProxy> serializableProxyClass)
          Sets the SerializableProxy class 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, 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 accessible
InstantiationException - if instantiation failed due to an exception

generateClass

protected void generateClass()
Generate the class.

Specified by:
generateClass in class AbstractClassFactory<T>

setSerializableProxyClass

public void setSerializableProxyClass(Class<? extends SerializableProxy> serializableProxyClass)
Sets the SerializableProxy class 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 and InvocationHandler are 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: AbstractSubclassFactory
Returns the default MethodBodyCreator to use when creating overridden methods.

Overrides:
getDefaultMethodOverride in class AbstractSubclassFactory<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 modify
handler - 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 than setInvocationHandler(Object, InvocationHandler), however it will work on any proxy, not just proxies from a specific factory.

Parameters:
proxy - the proxy to modify
handler - the handler to use

getInvocationHandlerStatic

public static InvocationHandler getInvocationHandlerStatic(Object proxy)
Gets the InvocationHandler for a given proxy instance. This method is less efficient than getInvocationHandler(Object), however it will work for any proxy, not just proxies from a specific factory instance.

Parameters:
proxy - the proxy
Returns:
the invocation handler

Invocation API 1.1.0.Final

Copyright © 2011 JBoss, a division of Red Hat, Inc.. All Rights Reserved.