org.pfsw.extender
Class ExtendedObject<TDelegate>

java.lang.Object
  extended by org.pfsw.extender.DelegatingInvocationHandler<TDelegate>
      extended by org.pfsw.extender.ExtendedObject<TDelegate>
All Implemented Interfaces:
java.lang.reflect.InvocationHandler

public class ExtendedObject<TDelegate>
extends DelegatingInvocationHandler<TDelegate>

This is the core of the extendible objects framework. This handler is responsible for holding extensions and mix-ins and to dispatch method calls to the correct receiver.


Constructor Summary
ExtendedObject(TDelegate object)
           
 
Method Summary
protected  boolean containsMethod(java.lang.reflect.Method[] methods, java.lang.reflect.Method method)
          Returns true if the given method array contains the specified method.
protected  java.lang.Object findTargetOf(java.lang.reflect.Method method)
           
 java.lang.Object getExtension(java.lang.String name)
          Returns the extension object for the given name or null if not available.
protected  java.util.Map<java.lang.String,java.lang.Object> getExtensions()
           
<I> I
getImplementor(java.lang.Class<I> interfaceClass)
          Returns the implementing object of the given interface or null if none is found.
<I> I
getMixin(java.lang.Class<I> interfaceClass)
           
protected  java.util.Map<java.lang.Class,java.lang.Object> getMixins()
           
protected  boolean implementsMethod(java.lang.Class interfaceOrClass, java.lang.reflect.Method method)
          Returns true if the given class is no interface and is implementing the given method itself, that is not inheriting it.
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
          Executes the given method with the specified args against the internal delegate.
protected  boolean isSameMethod(java.lang.reflect.Method method1, java.lang.reflect.Method method2)
          Returns true if the two given methods are the same except that they do not have to be declared by the same class or interface.
 void onExtensionsDo(ExtensionProcessor processor)
          The given processor will be called on all extensions registered in this extended object until the processor returns false or all extensions have been iterated through.
 void onMixinsDo(MixinProcessor processor)
          The given processor will be called on all mixins registered in this extended object until the processor returns false or all mixins have been iterated through.
 java.lang.Object removeExtension(java.lang.String name)
          Remove and return the extension object for the given name or return null if not available.
<I> I
removeMixin(java.lang.Class<I> interfaceClass)
          Removes the mix-in implementation for the given interface and returns it.
 void setExtension(java.lang.String name, java.lang.Object extension)
          Attach the given extension to the object this invocation handler stands for.
<I,T extends I>
void
setMixin(java.lang.reflect.Proxy proxy, java.lang.Class<I> interfaceClass, T implementation)
          Sets the implementation instance for the specified interface.
protected  boolean understandsMethod(java.lang.Class interfaceOrClass, java.lang.reflect.Method method)
          Returns true if the given interface or class is understanding the given method, either by declaring it itself or by inheritance.
protected  boolean understandsMethod(java.lang.Object object, java.lang.reflect.Method method)
           
 
Methods inherited from class org.pfsw.extender.DelegatingInvocationHandler
getDelegate, setDelegate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtendedObject

public ExtendedObject(TDelegate object)
Method Detail

invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws java.lang.Throwable
Description copied from class: DelegatingInvocationHandler
Executes the given method with the specified args against the internal delegate.

Specified by:
invoke in interface java.lang.reflect.InvocationHandler
Overrides:
invoke in class DelegatingInvocationHandler<TDelegate>
Throws:
java.lang.Throwable

setExtension

public void setExtension(java.lang.String name,
                         java.lang.Object extension)
Attach the given extension to the object this invocation handler stands for.

Parameters:
name - The name of the extension (must not be null)
extension - Any object that must be attached (must not be null)

getExtension

public java.lang.Object getExtension(java.lang.String name)
Returns the extension object for the given name or null if not available.


removeExtension

public java.lang.Object removeExtension(java.lang.String name)
Remove and return the extension object for the given name or return null if not available.


setMixin

public <I,T extends I> void setMixin(java.lang.reflect.Proxy proxy,
                                     java.lang.Class<I> interfaceClass,
                                     T implementation)
Sets the implementation instance for the specified interface. If there is another implementation set for this interface already, it will be removed.

The given proxy object will be set to all fields of the implementation object that are annotated by @Self.

If any of the given parameters is null, nothing will be set at all.


getMixin

public <I> I getMixin(java.lang.Class<I> interfaceClass)

removeMixin

public <I> I removeMixin(java.lang.Class<I> interfaceClass)
Removes the mix-in implementation for the given interface and returns it. If null is returned, no mix-in for this interface was found.


getImplementor

public <I> I getImplementor(java.lang.Class<I> interfaceClass)
Returns the implementing object of the given interface or null if none is found. The result could be the underlying delegate as well as a mix-in.


onExtensionsDo

public void onExtensionsDo(ExtensionProcessor processor)
The given processor will be called on all extensions registered in this extended object until the processor returns false or all extensions have been iterated through.

Parameters:
processor - The processor that will be called per extension (must not be null).

onMixinsDo

public void onMixinsDo(MixinProcessor processor)
The given processor will be called on all mixins registered in this extended object until the processor returns false or all mixins have been iterated through.

Parameters:
processor - The processor that will be called per mixin (must not be null).

findTargetOf

protected java.lang.Object findTargetOf(java.lang.reflect.Method method)

understandsMethod

protected boolean understandsMethod(java.lang.Object object,
                                    java.lang.reflect.Method method)

understandsMethod

protected boolean understandsMethod(java.lang.Class interfaceOrClass,
                                    java.lang.reflect.Method method)
Returns true if the given interface or class is understanding the given method, either by declaring it itself or by inheritance.


implementsMethod

protected boolean implementsMethod(java.lang.Class interfaceOrClass,
                                   java.lang.reflect.Method method)
Returns true if the given class is no interface and is implementing the given method itself, that is not inheriting it.


containsMethod

protected boolean containsMethod(java.lang.reflect.Method[] methods,
                                 java.lang.reflect.Method method)
Returns true if the given method array contains the specified method.


isSameMethod

protected boolean isSameMethod(java.lang.reflect.Method method1,
                               java.lang.reflect.Method method2)
Returns true if the two given methods are the same except that they do not have to be declared by the same class or interface.


getExtensions

protected java.util.Map<java.lang.String,java.lang.Object> getExtensions()

getMixins

protected java.util.Map<java.lang.Class,java.lang.Object> getMixins()