Java Reference
In-Depth Information
1. An extension of ReflectiveVisitor is instantiated, such as TypeChecking
at Marker
40
. We denote the instance of that visitor as
v
.
2. The invocation
v
.
visit(
root
) initiates the TypeCheckingvisitor's process-
ing of the AST at the
root
node of the AST.
3. With
root
processed as a generic AbstractNode, the invocation is matched
by single dispatchwith themethod
visit
(AbstractNode
n
)atMarker
35
.
4. At Marker
36
,the
method is called to accomplish the second
dispatch, by determining reflectively which particular
dispatch
visit
should be
invoked, as follows.
All methods in the actual visitor (TypeChecking) are examined, and the
visit
method that accepts a node type
most closely matched
to the supplied
node's
actual
type is invoked. The nature of this matching process is
described below.
If no suitablematch is found, then the method at Marker
37
is invoked as
a default action, and its behavior passes the visitor along to the children
of the supplied node.
Note that the
(Object
o
)atMarker
37
method can be re-
defined by a reflective visitor subclass, so this default behavior can be
customized.
default
V
isit
How do we find the
method in a reflective visitor
v
that is most appro-
priate for handling node
n
?
visit
•
If node
n
's type is
t
, then the method
( t ) is the exact match that
would have been found by the nonreflective visitor in Figure 7.23. If
v
contains such a method, then it is the best choice to handle node
n
.
visit
•
If no exact match is found, then the search widens to find a
visit
method
that can handle a
superclass
of
t
.
A class in C
allows
multiple inheritance
. Every class (except Object) has a unique superclass in
Java. However, classes in Java can implement any number of
interfaces
.Thus,
the search for
++
may lack a unique immediate superclass, because C
++
(
w
), where
w
is a wider type than
t
, does not necessarily
yield a unique result.
In practice, the reflective visitor is crafted so that the desired match is
clearly present in the visitor and easily found by the widening process, as
follows.
visit
•
The instantiable node type hierarchy is largely disregarded. If a node has
actual type
t
, then it is unlikely that a visitor will o
ff
er a method
visit
(
t
).