com.ph.pr.parser
Class ReflectiveVisitor

java.lang.Object
  extended by com.ph.pr.parser.ReflectiveVisitor
All Implemented Interfaces:
Visitor
Direct Known Subclasses:
VBSVisitor

public abstract class ReflectiveVisitor
extends java.lang.Object
implements Visitor

A guest prone to intellectualizing. Well, not really. This is a visitor employing reflection, based on a description presented by Inigo Montoya Surguy here , albeit with some modifications -- we allow for visiting not only the narrowest type, but every type possible , based on the visitAll flag passed to ReflectiveVisitor(boolean) constructor. This functionality comes in quite handy for tasks such as, for instance, validation. For example, if one has a complex class hierarchy (especially with multiple inheritance) and different types have different requirements for it to be valid, and a concrete instance may implement a number of those, these validations can be well separated out.

Author:
"Grisha Golberg"
See Also:
"Dr Heinz Kabutz (I just like mentioning this name) on Visiting Collection elements ", "Dr Heinz Kabutz on Depth- first polymorphism ", http://fish37.livejournal.com/3307.html

Field Summary
private static java.lang.reflect.Method[] dummy
          Just so we do not need to create another object to correctly cast a list in getPolymorphicMethods(Object).
static java.lang.String VISIT_METHOD_NAME
          Convention for the visiting method name.
static java.lang.String VISIT_METHOD_NAME_BRITISH
          How we visit in UK.
private  boolean visitAll
          Flag, specifying whether
 
Constructor Summary
ReflectiveVisitor(boolean visitAll)
          Creates a visitor.
 
Method Summary
abstract  void defaultVisit(java.lang.Object o)
          If all else fails...
private static java.util.Set getAllInterfaces(java.lang.Class klass)
          Get all interface s (and superclasses) the given klass implements.
private  java.lang.reflect.Method[] getPolymorphicMethods(java.lang.Object o)
          For explanation, see here
 void visit(java.lang.Object o)
          The polymorphic visit method.
 
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
Convention for the visiting method name. Yeah, we all know it's "visit", but just in case...

See Also:
VISIT_METHOD_NAME_BRITISH, Constant Field Values

VISIT_METHOD_NAME_BRITISH

public static final java.lang.String VISIT_METHOD_NAME_BRITISH
How we visit in UK. Don't forget your hat. Ok, ok, it's a lame joke...

See Also:
VISIT_METHOD_NAME, Constant Field Values

visitAll

private boolean visitAll
Flag, specifying whether


dummy

private static final java.lang.reflect.Method[] dummy
Just so we do not need to create another object to correctly cast a list in getPolymorphicMethods(Object). Ah, the little Java tricks...

Constructor Detail

ReflectiveVisitor

public ReflectiveVisitor(boolean visitAll)
Creates a visitor.

Parameters:
visitAll - when , specifies that all types that a Hospitableobject implements and that the given concrete Visitor can handle will be visited (the order of visitation is unspecified). For example, consider that
  • Foo extends Bar
  • Bar extends Baz
  • Baz implements Hospitable
  • SampleVisitor extends ReflectiveVisitor
  • SampleVisitor has visit() overloaded for parameters Foo, Bar and Baz
  • SampleVisitor, when constructed, called super(true)
  • foo is an instance of Foo, and now
  • foo.accept(new SampleVisitor()) is called
In this case, not just SampleVisitor.visit(Foo) will be executed, but also SampleVisitor.visit(Bar) and SampleVisitor.visit(Baz), in an unspecified order. Oooof! Now why would you need that is left as an exercise.
Method Detail

getPolymorphicMethods

private java.lang.reflect.Method[] getPolymorphicMethods(java.lang.Object o)
For explanation, see here

Returns:

visit

public void visit(java.lang.Object o)
The polymorphic visit method. RTFM.

Specified by:
visit in interface Visitor
Parameters:
o - Object to visit

defaultVisit

public abstract void defaultVisit(java.lang.Object o)
If all else fails...

Parameters:
o - say, can you see?
Throws:
java.lang.Throwable - as far as it can...

getAllInterfaces

private static final java.util.Set getAllInterfaces(java.lang.Class klass)
Get all interface s (and superclasses) the given klass implements.

Parameters:
klass - Class to find out interfaces for.
Returns:
a Set of Class objects.