com.perisic.shadow
Class Shadowable

java.lang.Object
  extended by com.perisic.shadow.Shadowable
Direct Known Subclasses:
NiceFellow

public class Shadowable
extends java.lang.Object

A common base class for all shadowable objects. Basically adding a shadow to an object obj means that a method call to obj will be executed in the shadow instead in obj. If the shadow does not define the method it will be passed to obj. If obj is shadowed by more than one shadow the most recently added shadow will be tried first.

This package partly mimics the behaviour of shadows known from LPC, see for example http://genesis.cs.chalmers.se/coding/lpcdoc/lpc_58.html


Constructor Summary
Shadowable()
          When constructed the instances of the class shadows of this class and all parent classes are added to this object.
 
Method Summary
static void addShadow(java.lang.Class owner, java.lang.Class shadow)
          An instance of shadow is added whenever an ownerobject is instantiated.
 void addShadow(Shadow shadow)
          Add a shadow to this object.
static java.lang.Class[] allShadowedClasses()
          Gets all classes that have been shadowed.
 java.lang.Object call(java.lang.String methodName, java.lang.Object... args)
          Executes the method methodName with the arguments args.
static java.util.Vector<java.lang.Class> getClassShadows(java.lang.Class owner)
          Gets a vector of all shadows that belong to the class owner.
 java.util.Vector<Shadow> getShadows()
          Gets all shadows that shadow this object.
static boolean removeShadow(java.lang.Class owner, java.lang.Class shadow)
          Removes the class shadow that has been added to the class owner.
 boolean removeShadow(Shadow shadow)
          Removes a shadow from this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Shadowable

public Shadowable()
When constructed the instances of the class shadows of this class and all parent classes are added to this object.

Method Detail

addShadow

public void addShadow(Shadow shadow)
Add a shadow to this object. A shadow receives all method calls (via #call) sent to this object. If the shadow defines the method it will be processed in the shadow.

Parameters:
shadow - The shadow that is added to this object.
See Also:
call(java.lang.String, java.lang.Object...)

addShadow

public static void addShadow(java.lang.Class owner,
                             java.lang.Class shadow)
An instance of shadow is added whenever an ownerobject is instantiated. The order in that the shadows are added to the object is the same order as the shadow classes are added to owner.

Parameters:
owner - The class that is to be shadowed
shadow - The class that shadows owner

removeShadow

public static boolean removeShadow(java.lang.Class owner,
                                   java.lang.Class shadow)
Removes the class shadow that has been added to the class owner. Note: the shadow objects that have been already added to instances of owner are not removed. To remove shadows from objects use removeShadow(Shadow)

Parameters:
owner - the class that is shadowed
shadow - the shadow to be removed from the class
Returns:
true if owner has been shadowed by shadow

getClassShadows

public static java.util.Vector<java.lang.Class> getClassShadows(java.lang.Class owner)
Gets a vector of all shadows that belong to the class owner. This may be used to manipulate the class shadows manually by changing the the vector.

Parameters:
owner - the class whose shadows are returned
Returns:
all classes added to owner

allShadowedClasses

public static java.lang.Class[] allShadowedClasses()
Gets all classes that have been shadowed.

Returns:
an array of all classes that are shadowed.

removeShadow

public boolean removeShadow(Shadow shadow)
Removes a shadow from this object.

Returns:
true if this object has been shadowed by shadow

getShadows

public java.util.Vector<Shadow> getShadows()
Gets all shadows that shadow this object. Modification of the returned vector will modify the shadows.

Returns:
a vector of all shadows of this object

call

public java.lang.Object call(java.lang.String methodName,
                             java.lang.Object... args)
Executes the method methodName with the arguments args. When the method is shadowed (that means when the object has a Shadow object added and the Shadow object defines the method), then the method defined in the shadow object is executed instead of the method in this object.

If an object has more than one shadow added, the last shadow added to the method is checked first.

If the method is not found in the shadows and in the object itself a ShadowException with a NoSuchMethodException as cause is thrown.

It the method is found but throws an exception, a ShadowException is thrown where the cause is the exception originally thrown by the method.