com.iotabits.revisitor
Class BasicReflectiveVisitor

java.lang.Object
  extended bycom.iotabits.revisitor.BasicReflectiveVisitor
All Implemented Interfaces:
iReflectiveVisitor
Direct Known Subclasses:
BinaryTreeNode.BinaryTreeNodePrinter, GraphNode.GraphPrinter

public class BasicReflectiveVisitor
extends java.lang.Object
implements iReflectiveVisitor

This class an abstract class that implements the Visitor pattern via reflection. The visit method will look for a public method in the class that matches most closely the types of the arguments passed to it.

Subclasses will need to override the implementation of the 'body' methods that handle visiting when no other methods are found. By default, the body methods (named defaultVisit(...)) throw a ReflectiveDispatchException.

Subclasses should implement "visit(arg)" methods for the types of objects that are going to be encountered.


Field Summary
static java.lang.String VISIT_METHOD_NAME
           
 
Constructor Summary
BasicReflectiveVisitor()
           
 
Method Summary
protected  void callAccept(java.lang.Object visiting)
          Override this if you want the revisitor to control traversal.
protected  void defaultVisit(java.lang.Object visiting)
          Override this to provide different default behavior for the default visit method.
 void dispatchToVisit(java.lang.Object visiting)
          Find a public method that most closely matches the given types and call it.
protected  java.lang.reflect.Method findMethod(java.lang.Object visiting)
          Look through the class and find a method that matches the classes of the parameters.
 void visit(java.lang.Object visiting)
          This implements the default visit method, simply calling defaultVisit(Object):void.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VISIT_METHOD_NAME

public static final java.lang.String VISIT_METHOD_NAME
See Also:
Constant Field Values
Constructor Detail

BasicReflectiveVisitor

public BasicReflectiveVisitor()
Method Detail

findMethod

protected java.lang.reflect.Method findMethod(java.lang.Object visiting)
                                       throws java.lang.NoSuchMethodException
Look through the class and find a method that matches the classes of the parameters.

Override this if you want to control how methods are found. By default, this uses ReflectionUtils.findCovariantMethod().

Throws:
java.lang.NoSuchMethodException

callAccept

protected void callAccept(java.lang.Object visiting)
                   throws java.lang.Exception
Override this if you want the revisitor to control traversal.

Parameters:
visiting -
Throws:
java.lang.Exception

defaultVisit

protected void defaultVisit(java.lang.Object visiting)
                     throws java.lang.Exception
Override this to provide different default behavior for the default visit method.

Parameters:
visiting -
Throws:
java.lang.Exception

dispatchToVisit

public final void dispatchToVisit(java.lang.Object visiting)
                           throws java.lang.Exception
Find a public method that most closely matches the given types and call it.

This is a template method - subclasses can alter the behavior by implementing findMethod(), callAccept(), and defaultVisit().

Specified by:
dispatchToVisit in interface iReflectiveVisitor
Parameters:
visiting -
Throws:
java.lang.Exception - The visit method threw an exception.

visit

public final void visit(java.lang.Object visiting)
                 throws java.lang.Exception
This implements the default visit method, simply calling defaultVisit(Object):void.

Specified by:
visit in interface iReflectiveVisitor
Parameters:
visiting - The object being visited.
Throws:
java.lang.Exception