KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > lang > model > util > ElementScanner6


1 /*
2  * @(#)ElementScanner6.java 1.7 06/08/15
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.lang.model.util;
9
10 import javax.lang.model.element.*;
11 import javax.annotation.processing.SupportedSourceVersion;
12 import static javax.lang.model.element.ElementKind.*;
13 import javax.lang.model.SourceVersion;
14 import static javax.lang.model.SourceVersion.*;
15
16
17 /**
18  * A scanning visitor of program elements with default behavior
19  * appropriate for the {@link SourceVersion#RELEASE_6 RELEASE_6}
20  * source version. The <tt>visit<i>XYZ</i></tt> methods in this
21  * class scan their component elements by calling {@code scan} on
22  * their {@linkplain Element#getEnclosedElements enclosed elements},
23  * {@linkplain ExecutableElement#getParameters parameters}, etc., as
24  * indicated in the individual method specifications. A subclass can
25  * control the order elements are visited by overriding the
26  * <tt>visit<i>XYZ</i></tt> methods. Note that clients of a scanner
27  * may get the desired behavior be invoking {@code v.scan(e, p)} rather
28  * than {@code v.visit(e, p)} on the root objects of interest.
29  *
30  * <p>When a subclass overrides a <tt>visit<i>XYZ</i></tt> method, the
31  * new method can cause the enclosed elements to be scanned in the
32  * default way by calling <tt>super.visit<i>XYZ</i></tt>. In this
33  * fashion, the concrete visitor can control the ordering of traversal
34  * over the component elements with respect to the additional
35  * processing; for example, consistently calling
36  * <tt>super.visit<i>XYZ</i></tt> at the start of the overridden
37  * methods will yield a preorder traversal, etc. If the component
38  * elements should be traversed in some other order, instead of
39  * calling <tt>super.visit<i>XYZ</i></tt>, an overriding visit method
40  * should call {@code scan} with the elements in the desired order.
41  *
42  * <p> Methods in this class may be overridden subject to their
43  * general contract. Note that annotating methods in concrete
44  * subclasses with {@link java.lang.Override @Override} will help
45  * ensure that methods are overridden as intended.
46  *
47  * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
48  * implemented by this class may have methods added to it in the
49  * future to accommodate new, currently unknown, language structures
50  * added to future versions of the Java&trade; programming language.
51  * Therefore, methods whose names begin with {@code "visit"} may be
52  * added to this class in the future; to avoid incompatibilities,
53  * classes which extend this class should not declare any instance
54  * methods with names beginning with {@code "visit"}.
55  *
56  * <p>When such a new visit method is added, the default
57  * implementation in this class will be to call the {@link
58  * #visitUnknown visitUnknown} method. A new element scanner visitor
59  * class will also be introduced to correspond to the new language
60  * level; this visitor will have different default behavior for the
61  * visit method in question. When the new visitor is introduced, all
62  * or portions of this visitor may be deprecated.
63  *
64  * @param <R> the return type of this visitor's methods. Use {@link
65  * Void} for visitors that do not need to return results.
66  * @param <P> the type of the additional parameter to this visitor's
67  * methods. Use {@code Void} for visitors that do not need an
68  * additional parameter.
69  *
70  * @author Joseph D. Darcy
71  * @author Scott Seligman
72  * @author Peter von der Ah&eacute;
73  * @version 1.7 06/08/15
74  * @since 1.6
75  */

76 @SupportedSourceVersion(RELEASE_6)
77 public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {
78     /**
79      * The specified default value.
80      */

81     protected final R DEFAULT_VALUE;
82
83     /**
84      * Constructor for concrete subclasses; uses {@code null} for the
85      * default value.
86      */

87     protected ElementScanner6(){
88     DEFAULT_VALUE = null;
89     }
90
91     /**
92      * Constructor for concrete subclasses; uses the argument for the
93      * default value.
94      */

95     protected ElementScanner6(R defaultValue){
96     DEFAULT_VALUE = defaultValue;
97     }
98
99     /**
100      * Iterates over the given elements and calls {@link
101      * #scan(Element, Object) scan(Element, P)} on each one. Returns
102      * the result of the last call to {@code scan} or {@code
103      * DEFAULT_VALUE} for an empty iterable.
104      *
105      * @param iterable the elements to scan
106      * @param p additional parameter
107      * @return the scan of the last element or {@code DEFAULT_VALUE} if no elements
108      */

109     public final R scan(Iterable JavaDoc<? extends Element> iterable, P p) {
110     R result = DEFAULT_VALUE;
111     for(Element e : iterable)
112         result = scan(e, p);
113     return result;
114     }
115
116     /**
117      * Processes an element by calling {@code e.accept(this, p)};
118      * this method may be overridden by subclasses.
119      * @return the result of visiting {@code e}.
120      */

121     public R scan(Element e, P p) {
122     return e.accept(this, p);
123     }
124
125     /**
126      * Convenience method equivalent to {@code v.scan(e, null)}.
127      * @return the result of scanning {@code e}.
128      */

129     public final R scan(Element e) {
130     return scan(e, null);
131     }
132
133     /**
134      * {@inheritDoc} This implementation scans the enclosed elements.
135      *
136      * @param e the element to visit
137      * @param p a visitor-specified parameter
138      * @return the result of scanning
139      */

140     public R visitPackage(PackageElement e, P p) {
141     return scan(e.getEnclosedElements(), p);
142     }
143
144     /**
145      * {@inheritDoc} This implementation scans the enclosed elements.
146      *
147      * @param e the element to visit
148      * @param p a visitor-specified parameter
149      * @return the result of scanning
150      */

151     public R visitType(TypeElement e, P p) {
152     return scan(e.getEnclosedElements(), p);
153     }
154
155     /**
156      * {@inheritDoc} This implementation scans the enclosed elements.
157      *
158      * @param e the element to visit
159      * @param p a visitor-specified parameter
160      * @return the result of scanning
161      */

162     public R visitVariable(VariableElement e, P p) {
163     return scan(e.getEnclosedElements(), p);
164     }
165
166     /**
167      * {@inheritDoc} This implementation scans the parameters.
168      *
169      * @param e the element to visit
170      * @param p a visitor-specified parameter
171      * @return the result of scanning
172      */

173     public R visitExecutable(ExecutableElement e, P p) {
174     return scan(e.getParameters(), p);
175     }
176
177     /**
178      * {@inheritDoc} This implementation scans the enclosed elements.
179      *
180      * @param e the element to visit
181      * @param p a visitor-specified parameter
182      * @return the result of scanning
183      */

184     public R visitTypeParameter(TypeParameterElement e, P p) {
185     return scan(e.getEnclosedElements(), p);
186     }
187 }
188
Popular Tags