org.unitils.mock.core.proxy
Class ProxyFactory

java.lang.Object
  extended by org.unitils.mock.core.proxy.ProxyFactory

public class ProxyFactory
extends Object

Utility class to create and work with proxy objects.

Author:
Kenny Claes, Filip Neven, Tim Ducheyne

Constructor Summary
ProxyFactory()
           
 
Method Summary
protected static
<T> Class<T>
createEnhancedClass(Class<T> proxiedClass, Class<?>... implementedInterfaces)
           
static
<T> T
createInitializedOrUninitializedInstanceOfType(Class<T> clazz)
          Creates an instance of the given type.
protected static
<T> T
createProxy(String mockName, boolean initialize, ProxyInvocationHandler invocationHandler, Class<T> proxiedClass, Class<?>... implementedInterfaces)
          Creates a proxy object for the given type.
static
<T> T
createProxy(String mockName, ProxyInvocationHandler invocationHandler, Class<T> proxiedClass, Class<?>... implementedInterfaces)
          Creates a proxy object for the given type.
static
<T> T
createUninitializedInstanceOfType(Class<T> clazz)
          Creates an instance of the given type.
static
<T> T
createUninitializedProxy(String mockName, ProxyInvocationHandler invocationHandler, Class<T> proxiedClass, Class<?>... implementedInterfaces)
          Creates a proxy object for the given type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProxyFactory

public ProxyFactory()
Method Detail

createProxy

public static <T> T createProxy(String mockName,
                                ProxyInvocationHandler invocationHandler,
                                Class<T> proxiedClass,
                                Class<?>... implementedInterfaces)
Creates a proxy object for the given type. All method invocations will be passed to the given invocation handler. If possible, the default constructor (can be private) will be used. If there is no default constructor, no constructor will be called.

Parameters:
mockName - The name of the mock, not null
invocationHandler - The handler that will handle the method invocations of the proxy, not null.
proxiedClass - The type to proxy, not null
implementedInterfaces - Additional interfaces that the proxy must implement
Returns:
The proxy object, not null

createUninitializedProxy

public static <T> T createUninitializedProxy(String mockName,
                                             ProxyInvocationHandler invocationHandler,
                                             Class<T> proxiedClass,
                                             Class<?>... implementedInterfaces)
Creates a proxy object for the given type. All method invocations will be passed to the given invocation handler. No constructor or class-initialization will be called.

Parameters:
mockName - The name of the mock, not null
invocationHandler - The handler that will handle the method invocations of the proxy, not null.
proxiedClass - The type to proxy, not null
implementedInterfaces - Additional interfaces that the proxy must implement
Returns:
The proxy object, not null

createProxy

protected static <T> T createProxy(String mockName,
                                   boolean initialize,
                                   ProxyInvocationHandler invocationHandler,
                                   Class<T> proxiedClass,
                                   Class<?>... implementedInterfaces)
Creates a proxy object for the given type. All method invocations will be passed to the given invocation handler.

Parameters:
mockName - The name of the mock, not null
initialize - If possible, use the default constructor and initialize all fields
implementedInterfaces - Additional interfaces that the proxy must implement
proxiedClass - The type to proxy, not null
invocationHandler - The handler that will handle the method invocations of the proxy, not null.
Returns:
The proxy object, not null

createInitializedOrUninitializedInstanceOfType

public static <T> T createInitializedOrUninitializedInstanceOfType(Class<T> clazz)
Creates an instance of the given type. First we try to create an instance using the default constructor. If this doesn't work, eg if there is no default constructor, we try using objenesis. This way the class doesn't have to offer an empty constructor in order for this method to succeed.

Type Parameters:
T - The type of the instance
Parameters:
clazz - The class for which an instance is requested
Returns:
An instance of the given class

createUninitializedInstanceOfType

public static <T> T createUninitializedInstanceOfType(Class<T> clazz)
Creates an instance of the given type. First we try to create an instance using the default constructor. No constructor or class-initialization will be called.

Type Parameters:
T - The type of the instance
Parameters:
clazz - The class for which an instance is requested
Returns:
An instance of the given class

createEnhancedClass

protected static <T> Class<T> createEnhancedClass(Class<T> proxiedClass,
                                                  Class<?>... implementedInterfaces)


Copyright © 2011. All Rights Reserved.