qflib 0.98.1

de.qfs.lib.util
Class Visitor

java.lang.Object
  |
  +--de.qfs.lib.util.Visitor

public abstract class Visitor
extends java.lang.Object

This is a generic multi-method implementation of the Visitor pattern. It is completely dynamic, requiring no compile time information.

A Visitor is any Object that implements any method of the signature:
public Object visit (<someClass>) throws ...
This method can be called indirectly via Visitor.visit, eg:

 class DoSomethingVisitor
 {
     // String visitor
     public Object visit (String string)
     {
         return doSomething(string);
     }

     // Number visitor
     public Object visit (Number num)
     {
         return doSomething(num);
     }

     // Fallback method
     public Object visit (Object object)
     {
         return null;
     }
 }

 // visit a String
 try {
     Object result = Visitor.visit(new DoSomethingVisitor (), someString);
 } catch (Exception ex) {
     ...
 }

 
Method lookup is cached for improved speed.

Version:
$Revision: 1.7 $
Author:
Gregor Schmid

Inner Class Summary
static class Visitor.UnitTest
          Test cases for the Visitor class.
 
Constructor Summary
Visitor()
           
 
Method Summary
static java.lang.Object safeVisit(java.lang.Object visitor, java.lang.Object object)
          This method is identical to visit except for Exception handling.
static java.lang.Object visit(java.lang.Object visitor, java.lang.Object object)
          Call the visitor's visit method, that most closely matches the visited object's class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Visitor

public Visitor()
Method Detail

visit

public static java.lang.Object visit(java.lang.Object visitor,
                                     java.lang.Object object)
                              throws java.lang.reflect.InvocationTargetException,
                                     java.lang.NoSuchMethodException
Call the visitor's visit method, that most closely matches the visited object's class. The visit method is determined by first looking for one that expects the Object's class as argument, then one that expects the Object's superclass and so on.
Parameters:
visitor - The visitor whose visit method is called.
object - The object to visit.
Returns:
The result of the visitor's matching visit method.
Throws:
java.lang.reflect.InvocationTargetException - If the visitor's visit method declares and throws any checked exceptions, they will be wrapped in an InvocationTargetException. Undeclared RuntimeExceptions will be passed on directly.
java.lang.NoSuchMethodException - If the visitor doesn't implement any matching visit method.

safeVisit

public static java.lang.Object safeVisit(java.lang.Object visitor,
                                         java.lang.Object object)
This method is identical to visit except for Exception handling. If the visitor doesn't have a matching visit method, an error is logged and null is returned. It is also expected, but not verified, that the visitor's visit method does not declare any exceptions. If it does throw a declared exception anyway, it is logged as an error and null is returned. RuntimeExceptions are passed on as usual.
Parameters:
visitor - The visitor whose visit method is called.
object - The object to visit.
Returns:
The result of the visitor's matching visit method.

qflib 0.98.1