|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.ph.pr.parser.ReflectiveVisitor
public abstract class ReflectiveVisitor
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.
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 |
---|
public static final java.lang.String VISIT_METHOD_NAME
VISIT_METHOD_NAME_BRITISH
,
Constant Field Valuespublic static final java.lang.String VISIT_METHOD_NAME_BRITISH
VISIT_METHOD_NAME
,
Constant Field Valuesprivate boolean visitAll
private static final java.lang.reflect.Method[] dummy
getPolymorphicMethods(Object)
. Ah, the little Java tricks...
Constructor Detail |
---|
public ReflectiveVisitor(boolean visitAll)
visitAll
- when Hospitable
object 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
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 |
---|
private java.lang.reflect.Method[] getPolymorphicMethods(java.lang.Object o)
public void visit(java.lang.Object o)
visit
method.
RTFM
.
visit
in interface Visitor
o
- Object to visitpublic abstract void defaultVisit(java.lang.Object o)
o
- say, can you see?
java.lang.Throwable
- as far as it can...private static final java.util.Set getAllInterfaces(java.lang.Class klass)
klass
- Class to find out interfaces for.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |