KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ElementKindVisitor6.java 1.5 06/08/07
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 static javax.lang.model.element.ElementKind.*;
12 import javax.annotation.processing.SupportedSourceVersion;
13 import static javax.lang.model.SourceVersion.*;
14 import javax.lang.model.SourceVersion;
15
16
17 /**
18  * A visitor of program elements based on their {@linkplain
19  * ElementKind kind} with default behavior appropriate for the {@link
20  * SourceVersion#RELEASE_6 RELEASE_6} source version. For {@linkplain
21  * Element elements} <tt><i>XYZ</i></tt> that may have more than one
22  * kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate
23  * to the <tt>visit<i>XYZKind</i></tt> method corresponding to the
24  * first argument's kind. The <tt>visit<i>XYZKind</i></tt> methods
25  * call {@link #defaultAction defaultAction}, passing their arguments
26  * to {@code defaultAction}'s corresponding parameters.
27  *
28  * <p> Methods in this class may be overridden subject to their
29  * general contract. Note that annotating methods in concrete
30  * subclasses with {@link java.lang.Override @Override} will help
31  * ensure that methods are overridden as intended.
32  *
33  * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
34  * implemented by this class may have methods added to it or the
35  * {@code ElementKind} {@code enum} used in this case may have
36  * constants added to it in the future to accommodate new, currently
37  * unknown, language structures added to future versions of the
38  * Java&trade; programming language. Therefore, methods whose names
39  * begin with {@code "visit"} may be added to this class in the
40  * future; to avoid incompatibilities, classes which extend this class
41  * should not declare any instance methods with names beginning with
42  * {@code "visit"}.
43  *
44  * <p>When such a new visit method is added, the default
45  * implementation in this class will be to call the {@link
46  * #visitUnknown visitUnknown} method. A new abstract element kind
47  * visitor class will also be introduced to correspond to the new
48  * language level; this visitor will have different default behavior
49  * for the visit method in question. When the new visitor is
50  * introduced, all or portions of this visitor may be deprecated.
51  *
52  * @param <R> the return type of this visitor's methods. Use {@link
53  * Void} for visitors that do not need to return results.
54  * @param <P> the type of the additional parameter to this visitor's
55  * methods. Use {@code Void} for visitors that do not need an
56  * additional parameter.
57  *
58  * @author Joseph D. Darcy
59  * @author Scott Seligman
60  * @author Peter von der Ah&eacute;
61  * @version 1.5 06/08/07
62  * @since 1.6
63  */

64 @SupportedSourceVersion(RELEASE_6)
65 public class ElementKindVisitor6<R, P>
66                   extends SimpleElementVisitor6<R, P> {
67     /**
68      * Constructor for concrete subclasses; uses {@code null} for the
69      * default value.
70      */

71     protected ElementKindVisitor6() {
72     super(null);
73     }
74
75     /**
76      * Constructor for concrete subclasses; uses the argument for the
77      * default value.
78      *
79      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
80      */

81     protected ElementKindVisitor6(R defaultValue) {
82     super(defaultValue);
83     }
84
85     /**
86      * {@inheritDoc}
87      *
88      * The element argument has kind {@code PACKAGE}.
89      *
90      * @param e {@inheritDoc}
91      * @param p {@inheritDoc}
92      * @return {@inheritDoc}
93      */

94     @Override JavaDoc
95     public R visitPackage(PackageElement e, P p) {
96     assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
97     return defaultAction(e, p);
98     }
99
100     /**
101      * Visits a type element, dispatching to the visit method for the
102      * specific {@linkplain ElementKind kind} of type, {@code
103      * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
104      * INTERFACE}.
105      *
106      * @param e {@inheritDoc}
107      * @param p {@inheritDoc}
108      * @return the result of the kind-specific visit method
109      */

110     @Override JavaDoc
111     public R visitType(TypeElement e, P p) {
112     ElementKind k = e.getKind();
113     switch(k) {
114     case ANNOTATION_TYPE:
115         return visitTypeAsAnnotationType(e, p);
116
117     case CLASS:
118         return visitTypeAsClass(e, p);
119
120     case ENUM:
121         return visitTypeAsEnum(e, p);
122         
123     case INTERFACE:
124         return visitTypeAsInterface(e, p);
125         
126     default:
127         throw new AssertionError JavaDoc("Bad kind " + k + " for TypeElement" + e);
128     }
129     }
130
131     /**
132      * Visits an {@code ANNOTATION_TYPE} type element by calling
133      * {@code defaultAction}.
134      *
135      * @param e the element to visit
136      * @param p a visitor-specified parameter
137      * @return the result of {@code defaultAction}
138      */

139     public R visitTypeAsAnnotationType(TypeElement e, P p) {
140     return defaultAction(e, p);
141     }
142
143     /**
144      * Visits a {@code CLASS} type element by calling {@code
145      * defaultAction}.
146      *
147      * @param e the element to visit
148      * @param p a visitor-specified parameter
149      * @return the result of {@code defaultAction}
150      */

151     public R visitTypeAsClass(TypeElement e, P p) {
152     return defaultAction(e, p);
153     }
154
155     /**
156      * Visits an {@code ENUM} type element by calling {@code
157      * defaultAction}.
158      *
159      * @param e the element to visit
160      * @param p a visitor-specified parameter
161      * @return the result of {@code defaultAction}
162      */

163     public R visitTypeAsEnum(TypeElement e, P p) {
164     return defaultAction(e, p);
165     }
166
167     /**
168      * Visits an {@code INTERFACE} type element by calling {@code
169      * defaultAction}.
170      *.
171      * @param e the element to visit
172      * @param p a visitor-specified parameter
173      * @return the result of {@code defaultAction}
174      */

175     public R visitTypeAsInterface(TypeElement e, P p) {
176     return defaultAction(e, p);
177     }
178
179     /**
180      * Visits a variable element, dispatching to the visit method for
181      * the specific {@linkplain ElementKind kind} of variable, {@code
182      * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
183      * {@code LOCAL_VARIABLE}, or {@code PARAMETER}.
184      * @param e {@inheritDoc}
185      * @param p {@inheritDoc}
186      * @return the result of the kind-specific visit method
187      */

188     @Override JavaDoc
189     public R visitVariable(VariableElement e, P p) {
190     ElementKind k = e.getKind();
191     switch(k) {
192     case ENUM_CONSTANT:
193         return visitVariableAsEnumConstant(e, p);
194
195     case EXCEPTION_PARAMETER:
196         return visitVariableAsExceptionParameter(e, p);
197
198     case FIELD:
199         return visitVariableAsField(e, p);
200
201     case LOCAL_VARIABLE:
202         return visitVariableAsLocalVariable(e, p);
203
204     case PARAMETER:
205         return visitVariableAsParameter(e, p);
206
207     default:
208         throw new AssertionError JavaDoc("Bad kind " + k + " for VariableElement" + e);
209     }
210
211     }
212
213     /**
214      * Visits an {@code ENUM_CONSTANT} variable element by calling
215      * {@code defaultAction}.
216      *
217      * @param e the element to visit
218      * @param p a visitor-specified parameter
219      * @return the result of {@code defaultAction}
220      */

221     public R visitVariableAsEnumConstant(VariableElement e, P p) {
222     return defaultAction(e, p);
223     }
224
225     /**
226      * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
227      * {@code defaultAction}.
228      *
229      * @param e the element to visit
230      * @param p a visitor-specified parameter
231      * @return the result of {@code defaultAction}
232      */

233     public R visitVariableAsExceptionParameter(VariableElement e, P p) {
234     return defaultAction(e, p);
235     }
236
237     /**
238      * Visits a {@code FIELD} variable element by calling
239      * {@code defaultAction}.
240      *
241      * @param e the element to visit
242      * @param p a visitor-specified parameter
243      * @return the result of {@code defaultAction}
244      */

245     public R visitVariableAsField(VariableElement e, P p) {
246     return defaultAction(e, p);
247     }
248
249     /**
250      * Visits a {@code LOCAL_VARIABLE} variable element by calling
251      * {@code defaultAction}.
252      *
253      * @param e the element to visit
254      * @param p a visitor-specified parameter
255      * @return the result of {@code defaultAction}
256      */

257     public R visitVariableAsLocalVariable(VariableElement e, P p) {
258     return defaultAction(e, p);
259     }
260
261     /**
262      * Visits a {@code PARAMETER} variable element by calling
263      * {@code defaultAction}.
264      *
265      * @param e the element to visit
266      * @param p a visitor-specified parameter
267      * @return the result of {@code defaultAction}
268      */

269     public R visitVariableAsParameter(VariableElement e, P p) {
270     return defaultAction(e, p);
271     }
272
273     /**
274      * Visits an executable element, dispatching to the visit method
275      * for the specific {@linkplain ElementKind kind} of executable,
276      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
277      * {@code STATIC_INIT}.
278      *
279      * @param e {@inheritDoc}
280      * @param p {@inheritDoc}
281      * @return the result of the kind-specific visit method
282      */

283     @Override JavaDoc
284     public R visitExecutable(ExecutableElement e, P p) {
285     ElementKind k = e.getKind();
286     switch(k) {
287     case CONSTRUCTOR:
288         return visitExecutableAsConstructor(e, p);
289
290     case INSTANCE_INIT:
291         return visitExecutableAsInstanceInit(e, p);
292
293     case METHOD:
294         return visitExecutableAsMethod(e, p);
295
296     case STATIC_INIT:
297         return visitExecutableAsStaticInit(e, p);
298
299     default:
300         throw new AssertionError JavaDoc("Bad kind " + k + " for ExecutableElement" + e);
301     }
302     }
303
304     /**
305      * Visits a {@code CONSTRUCTOR} executable element by calling
306      * {@code defaultAction}.
307      *
308      * @param e the element to visit
309      * @param p a visitor-specified parameter
310      * @return the result of {@code defaultAction}
311      */

312     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
313     return defaultAction(e, p);
314     }
315
316     /**
317      * Visits an {@code INSTANCE_INIT} executable element by calling
318      * {@code defaultAction}.
319      *
320      * @param e the element to visit
321      * @param p a visitor-specified parameter
322      * @return the result of {@code defaultAction}
323      */

324     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
325     return defaultAction(e, p);
326     }
327
328     /**
329      * Visits a {@code METHOD} executable element by calling
330      * {@code defaultAction}.
331      *
332      * @param e the element to visit
333      * @param p a visitor-specified parameter
334      * @return the result of {@code defaultAction}
335      */

336     public R visitExecutableAsMethod(ExecutableElement e, P p) {
337     return defaultAction(e, p);
338     }
339
340     /**
341      * Visits a {@code STATIC_INIT} executable element by calling
342      * {@code defaultAction}.
343      *
344      * @param e the element to visit
345      * @param p a visitor-specified parameter
346      * @return the result of {@code defaultAction}
347      */

348     public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
349     return defaultAction(e, p);
350     }
351
352
353     /**
354      * {@inheritDoc}
355      *
356      * The element argument has kind {@code TYPE_PARAMETER}.
357      *
358      * @param e {@inheritDoc}
359      * @param p {@inheritDoc}
360      * @return {@inheritDoc}
361      */

362     @Override JavaDoc
363     public R visitTypeParameter(TypeParameterElement e, P p) {
364     assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
365     return defaultAction(e, p);
366     }
367 }
368
Popular Tags