de.kruis.padoclet.util
Class HalfDynamicProxy

java.lang.Object
  extended by de.kruis.padoclet.util.HalfDynamicProxy
All Implemented Interfaces:
InvocationHandlerWithTarget, java.lang.reflect.InvocationHandler
Direct Known Subclasses:
FilterDocletBase.HandlerBase

public class HalfDynamicProxy
extends java.lang.Object
implements InvocationHandlerWithTarget

This class is a quite universaly usable base class for a dynamic proxy.


Nested Class Summary
private static class HalfDynamicProxy.HDPState
          Holds the state of a HalfDynamicProxy.
static interface HalfDynamicProxy.MessageInterface
          Used to emit a message.
 
Field Summary
private static java.lang.Class<?>[][] proxyClassTable
          Holds a mapping table, that determinates the invocation handler class for a proxy target class.
private  HalfDynamicProxy.HDPState state
          holds the state
protected  java.lang.Object target
          holds the target.
 
Constructor Summary
HalfDynamicProxy()
           
 
Method Summary
 java.lang.reflect.Proxy dynamicProxyInstance()
          Get the dynamic proxy for this object.
 boolean equals(java.lang.Object obj)
          This equals-method calls the equals for the unwrapped proxy target objects.
protected  java.lang.Object getHDPProxy(java.lang.Object obj, java.lang.Class<?> expect)
          Get the proxy for an object using the same state as this proxy uses.
static java.lang.Object getHDPProxy(java.lang.Object obj, java.lang.Class<?> expect, HalfDynamicProxy.HDPState state)
          Get the dynamic proxy for an object.
protected  java.lang.Object getHDPStateUserObject()
          Get the user object.
 java.lang.Object getInvocationTarget()
          Get the target object, for which this invocation handler is a proxy for.
static java.lang.Class<?>[][] getProxyClassTable()
           
 int hashCode()
          Calls the hashCode method of the proxy target.
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
           
static void setProxyClassTable(java.lang.Class<?>[][] proxyClassTable)
          Set the proxy class table.
 void setupInvocationHandler(java.lang.Object target, java.lang.Object state)
          Initialize this invocation handler.
static HalfDynamicProxy.HDPState stateFactory(java.lang.Object userState, HalfDynamicProxy.MessageInterface reciver)
          Create a new state object.
 java.lang.String toString()
          Calls the toString method of the proxy target.
protected static java.lang.Object unwrap(java.lang.Object proxy)
          Get the unwrapped proxy target.
 

Field Detail

proxyClassTable

private static java.lang.Class<?>[][] proxyClassTable
Holds a mapping table, that determinates the invocation handler class for a proxy target class.


target

protected java.lang.Object target
holds the target.


state

private HalfDynamicProxy.HDPState state
holds the state

Constructor Detail

HalfDynamicProxy

public HalfDynamicProxy()
Method Detail

stateFactory

public static HalfDynamicProxy.HDPState stateFactory(java.lang.Object userState,
                                                     HalfDynamicProxy.MessageInterface reciver)
Create a new state object.

Parameters:
userState - an arbitrary object provided by the caller. May be null.
reciver - where to send messages to. May be null.
Returns:
a new initialized HDPState object

getProxyClassTable

public static java.lang.Class<?>[][] getProxyClassTable()
Returns:
Returns the proxyClassTable.

setProxyClassTable

public static void setProxyClassTable(java.lang.Class<?>[][] proxyClassTable)
Set the proxy class table. The proxyClassTable is used to select the InvocationHandlerWithTarget, to be used for a given proxy target class. Each entry of the table is a Class[], that contains a class implementing InvocationHandlerWithTarget as its first entry and zero or more interfaces as additional entries, that must be implemented by the object to create a proxy for. The first matching invocation handler is used.

Parameters:
proxyClassTable - The proxyClassTable to set.

setupInvocationHandler

public final void setupInvocationHandler(java.lang.Object target,
                                         java.lang.Object state)
Description copied from interface: InvocationHandlerWithTarget
Initialize this invocation handler.

Specified by:
setupInvocationHandler in interface InvocationHandlerWithTarget
Parameters:
target - object, for which this object is a proxy.
state - an arbitrary object used to hold state information

getInvocationTarget

public final java.lang.Object getInvocationTarget()
Description copied from interface: InvocationHandlerWithTarget
Get the target object, for which this invocation handler is a proxy for.

Specified by:
getInvocationTarget in interface InvocationHandlerWithTarget
Returns:
Returns the invovation target.

dynamicProxyInstance

public java.lang.reflect.Proxy dynamicProxyInstance()
Get the dynamic proxy for this object.

Returns:
an instance of Proxy.

getHDPStateUserObject

protected java.lang.Object getHDPStateUserObject()
Get the user object.

Returns:
the user object, that was provided to the call of stateFactory(Object, HalfDynamicProxy.MessageInterface).

getHDPProxy

protected java.lang.Object getHDPProxy(java.lang.Object obj,
                                       java.lang.Class<?> expect)
Get the proxy for an object using the same state as this proxy uses. If obj is an array, this method is called recursively on each element of the array.

Parameters:
obj - the object to create a proxy for
expect - the expected type of the object, if obj is an array. If expect is Object or null, then the type of obj is used instead of expect.
Returns:
the proxy object, or obj itself.
See Also:
getHDPProxy(Object, Class, HalfDynamicProxy.HDPState)

getHDPProxy

public static java.lang.Object getHDPProxy(java.lang.Object obj,
                                           java.lang.Class<?> expect,
                                           HalfDynamicProxy.HDPState state)
Get the dynamic proxy for an object. If obj is an array, this method is called recursively on each element of the array. No proxy is generated, if obj is null or primitive or, if obj is already a dynamic proxy, or, if no InvocationHandlerWithTarget implementation is found in the proxyClassTable (see setProxyClassTable(Class[][])).

Parameters:
obj - the object to create a proxy for.
expect - the expected type of the object, if obj is an array. If expect is Object or null, then the type of obj is used instead of expect.
state - the state object for the proxy
Returns:
the proxy object, or obj itself.
See Also:
setProxyClassTable(Class[][])

invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws java.lang.Throwable
Throws:
java.lang.Throwable

unwrap

protected static java.lang.Object unwrap(java.lang.Object proxy)
Get the unwrapped proxy target.

Parameters:
proxy - an object
Returns:
proxy, unless proxy is a dynamic proxy and its invocation handler implements the InvocationHandlerWithTarget interface. In that case the target object will be returned.

equals

public boolean equals(java.lang.Object obj)
This equals-method calls the equals for the unwrapped proxy target objects.

Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Calls the hashCode method of the proxy target.

Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()

toString

public java.lang.String toString()
Calls the toString method of the proxy target.

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()