com.iparelan.util
Class ProxyTools

java.lang.Object
  extended by com.iparelan.util.UtilObject
      extended by com.iparelan.util.ProxyTools

@Copyright(value="Copyright © 2008, Iparelan Solutions, LLC. All rights reserved.")
public final class ProxyTools
extends UtilObject

This utility class contains algorithms for proxy support.

Version:
July 2008
Author:
Greg Mattes

Field Summary
private static ValidationStrategy<Class<?>> CLASS_OR_INTERFACE_VALIDATOR
          A ValidationStrategy that ensures a Class describes either a class or an interface, not a primitive or Void.
 
Fields inherited from class com.iparelan.util.UtilObject
LOGGER, MSGS, UTILITIES_LOGGER_NAME
 
Constructor Summary
private ProxyTools()
          Not instantiable.
 
Method Summary
static
<T> T
createProxy(Class<?> proxyType, InvocationHandler invocationHandler, Class<?>... extraInterfaces)
          Produces a Proxy class based on the interfaces implemented by proxyType plus any optional extra interfaces.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_OR_INTERFACE_VALIDATOR

private static final ValidationStrategy<Class<?>> CLASS_OR_INTERFACE_VALIDATOR
A ValidationStrategy that ensures a Class describes either a class or an interface, not a primitive or Void. Also, null is considered to be invalid.

Constructor Detail

ProxyTools

private ProxyTools()
Not instantiable.

Method Detail

createProxy

public static <T> T createProxy(Class<?> proxyType,
                                InvocationHandler invocationHandler,
                                Class<?>... extraInterfaces)
Produces a Proxy class based on the interfaces implemented by proxyType plus any optional extra interfaces.

Methods of proxyType that are not specified by an implemented interface cannot participate in a dynamic proxy. Consider extracting an interface for the remaining methods from such types ("Refactoring," Martin Fowler, "Extract Interface").

Type Parameters:
T - The type upon with the produced proxy class is based.
Parameters:
proxyType - The type that determines the list of interfaces upon which the proxy class produced by this method is based. The interfaces have a particular order as described by Proxy. The following interfaces are included in the list:
  • If proxyType is an interface:
    1. proxyType itself as the first interface of the proxy class.
    2. proxyType is followed by all interfaces it extends in the order described by Class.getInterfaces().
  • If proxyType is a class:
    1. All interfaces implemented by all super classes of proxyType in the order given by Class.getInterfaces() for each super class, beginning with the most distant super class (immediate sub class of java.lang.Object), and ending with the immediate super class of proxyType. The interfaces implemented by proxyType follow all interfaces implemented by super classes.
    2. All interfaces implemented by proxyType, in the order described by Class.getInterfaces(), follow all super class interfaces.
  • The interfaces specified in the optional extraInterfaces argument of this method folllow all interfaces related to proxyType as described above.

May not be null.

invocationHandler - The InvocationHandler that handles all proxy method invocation. May not be null.
extraInterfaces - Optional, additional interfaces for the proxy class. The order of these interfaces does not change in the proxy class, however these interfaces appear after all interfaces of proxyType. May be null or an empty array.
Returns:
A proxy class based on proxyType. Never null.
Throws:
ValidationException - Iff proxyType is neither a class, nor an interface. Or invocationHandler is null.
IllegalArgumentException - Iff a valid proxy cannot be created. See the documentation for Proxy for more details.
To Do:
  • Cannot proxy public class methods that are not specified by an interface, generate some warning?
  • Can the generics be "improved" here? Can the unchecked operation be eliminated instead of suppressed? Class<T> proxyType instead of Class<?> proxyType?
  • Do we want to proxy all interfaces of the type and its supertypes? Do we instead want the ability to restrict which interfaces are proxied? Should invocationHandler know which interfaces it proxies? If invocationHandler can proxy interfaces are not in the set formed by combining proxyType and extraInterfaces, is that an error? warning?


Copyright © 2008 Iparelan Solutions, LLC. All rights reserved.