1 | /* |
2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Sun designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Sun in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
23 | * have any questions. |
24 | */ |
25 | |
26 | package javax.lang.model.util; |
27 | |
28 | import javax.lang.model.element.*; |
29 | import static javax.lang.model.element.ElementKind.*; |
30 | import javax.annotation.processing.SupportedSourceVersion; |
31 | import static javax.lang.model.SourceVersion.*; |
32 | import javax.lang.model.SourceVersion; |
33 | |
34 | |
35 | /** |
36 | * A visitor of program elements based on their {@linkplain |
37 | * ElementKind kind} with default behavior appropriate for the {@link |
38 | * SourceVersion#RELEASE_6 RELEASE_6} source version. For {@linkplain |
39 | * Element elements} <tt><i>XYZ</i></tt> that may have more than one |
40 | * kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate |
41 | * to the <tt>visit<i>XYZKind</i></tt> method corresponding to the |
42 | * first argument's kind. The <tt>visit<i>XYZKind</i></tt> methods |
43 | * call {@link #defaultAction defaultAction}, passing their arguments |
44 | * to {@code defaultAction}'s corresponding parameters. |
45 | * |
46 | * <p> Methods in this class may be overridden subject to their |
47 | * general contract. Note that annotating methods in concrete |
48 | * subclasses with {@link java.lang.Override @Override} will help |
49 | * ensure that methods are overridden as intended. |
50 | * |
51 | * <p> <b>WARNING:</b> The {@code ElementVisitor} interface |
52 | * implemented by this class may have methods added to it or the |
53 | * {@code ElementKind} {@code enum} used in this case may have |
54 | * constants added to it in the future to accommodate new, currently |
55 | * unknown, language structures added to future versions of the |
56 | * Java™ programming language. Therefore, methods whose names |
57 | * begin with {@code "visit"} may be added to this class in the |
58 | * future; to avoid incompatibilities, classes which extend this class |
59 | * should not declare any instance methods with names beginning with |
60 | * {@code "visit"}. |
61 | * |
62 | * <p>When such a new visit method is added, the default |
63 | * implementation in this class will be to call the {@link |
64 | * #visitUnknown visitUnknown} method. A new abstract element kind |
65 | * visitor class will also be introduced to correspond to the new |
66 | * language level; this visitor will have different default behavior |
67 | * for the visit method in question. When the new visitor is |
68 | * introduced, all or portions of this visitor may be deprecated. |
69 | * |
70 | * @param <R> the return type of this visitor's methods. Use {@link |
71 | * Void} for visitors that do not need to return results. |
72 | * @param <P> the type of the additional parameter to this visitor's |
73 | * methods. Use {@code Void} for visitors that do not need an |
74 | * additional parameter. |
75 | * |
76 | * @author Joseph D. Darcy |
77 | * @author Scott Seligman |
78 | * @author Peter von der Ahé |
79 | * @since 1.6 |
80 | */ |
81 | @SupportedSourceVersion(RELEASE_6) |
82 | public class ElementKindVisitor6<R, P> |
83 | extends SimpleElementVisitor6<R, P> { |
84 | /** |
85 | * Constructor for concrete subclasses; uses {@code null} for the |
86 | * default value. |
87 | */ |
88 | protected ElementKindVisitor6() { |
89 | super(null); |
90 | } |
91 | |
92 | /** |
93 | * Constructor for concrete subclasses; uses the argument for the |
94 | * default value. |
95 | * |
96 | * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} |
97 | */ |
98 | protected ElementKindVisitor6(R defaultValue) { |
99 | super(defaultValue); |
100 | } |
101 | |
102 | /** |
103 | * {@inheritDoc} |
104 | * |
105 | * The element argument has kind {@code PACKAGE}. |
106 | * |
107 | * @param e {@inheritDoc} |
108 | * @param p {@inheritDoc} |
109 | * @return {@inheritDoc} |
110 | */ |
111 | @Override |
112 | public R visitPackage(PackageElement e, P p) { |
113 | assert e.getKind() == PACKAGE: "Bad kind on PackageElement"; |
114 | return defaultAction(e, p); |
115 | } |
116 | |
117 | /** |
118 | * Visits a type element, dispatching to the visit method for the |
119 | * specific {@linkplain ElementKind kind} of type, {@code |
120 | * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code |
121 | * INTERFACE}. |
122 | * |
123 | * @param e {@inheritDoc} |
124 | * @param p {@inheritDoc} |
125 | * @return the result of the kind-specific visit method |
126 | */ |
127 | @Override |
128 | public R visitType(TypeElement e, P p) { |
129 | ElementKind k = e.getKind(); |
130 | switch(k) { |
131 | case ANNOTATION_TYPE: |
132 | return visitTypeAsAnnotationType(e, p); |
133 | |
134 | case CLASS: |
135 | return visitTypeAsClass(e, p); |
136 | |
137 | case ENUM: |
138 | return visitTypeAsEnum(e, p); |
139 | |
140 | case INTERFACE: |
141 | return visitTypeAsInterface(e, p); |
142 | |
143 | default: |
144 | throw new AssertionError("Bad kind " + k + " for TypeElement" + e); |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * Visits an {@code ANNOTATION_TYPE} type element by calling |
150 | * {@code defaultAction}. |
151 | * |
152 | * @param e the element to visit |
153 | * @param p a visitor-specified parameter |
154 | * @return the result of {@code defaultAction} |
155 | */ |
156 | public R visitTypeAsAnnotationType(TypeElement e, P p) { |
157 | return defaultAction(e, p); |
158 | } |
159 | |
160 | /** |
161 | * Visits a {@code CLASS} type element by calling {@code |
162 | * defaultAction}. |
163 | * |
164 | * @param e the element to visit |
165 | * @param p a visitor-specified parameter |
166 | * @return the result of {@code defaultAction} |
167 | */ |
168 | public R visitTypeAsClass(TypeElement e, P p) { |
169 | return defaultAction(e, p); |
170 | } |
171 | |
172 | /** |
173 | * Visits an {@code ENUM} type element by calling {@code |
174 | * defaultAction}. |
175 | * |
176 | * @param e the element to visit |
177 | * @param p a visitor-specified parameter |
178 | * @return the result of {@code defaultAction} |
179 | */ |
180 | public R visitTypeAsEnum(TypeElement e, P p) { |
181 | return defaultAction(e, p); |
182 | } |
183 | |
184 | /** |
185 | * Visits an {@code INTERFACE} type element by calling {@code |
186 | * defaultAction}. |
187 | *. |
188 | * @param e the element to visit |
189 | * @param p a visitor-specified parameter |
190 | * @return the result of {@code defaultAction} |
191 | */ |
192 | public R visitTypeAsInterface(TypeElement e, P p) { |
193 | return defaultAction(e, p); |
194 | } |
195 | |
196 | /** |
197 | * Visits a variable element, dispatching to the visit method for |
198 | * the specific {@linkplain ElementKind kind} of variable, {@code |
199 | * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD}, |
200 | * {@code LOCAL_VARIABLE}, or {@code PARAMETER}. |
201 | * @param e {@inheritDoc} |
202 | * @param p {@inheritDoc} |
203 | * @return the result of the kind-specific visit method |
204 | */ |
205 | @Override |
206 | public R visitVariable(VariableElement e, P p) { |
207 | ElementKind k = e.getKind(); |
208 | switch(k) { |
209 | case ENUM_CONSTANT: |
210 | return visitVariableAsEnumConstant(e, p); |
211 | |
212 | case EXCEPTION_PARAMETER: |
213 | return visitVariableAsExceptionParameter(e, p); |
214 | |
215 | case FIELD: |
216 | return visitVariableAsField(e, p); |
217 | |
218 | case LOCAL_VARIABLE: |
219 | return visitVariableAsLocalVariable(e, p); |
220 | |
221 | case PARAMETER: |
222 | return visitVariableAsParameter(e, p); |
223 | |
224 | default: |
225 | throw new AssertionError("Bad kind " + k + " for VariableElement" + e); |
226 | } |
227 | |
228 | } |
229 | |
230 | /** |
231 | * Visits an {@code ENUM_CONSTANT} variable element by calling |
232 | * {@code defaultAction}. |
233 | * |
234 | * @param e the element to visit |
235 | * @param p a visitor-specified parameter |
236 | * @return the result of {@code defaultAction} |
237 | */ |
238 | public R visitVariableAsEnumConstant(VariableElement e, P p) { |
239 | return defaultAction(e, p); |
240 | } |
241 | |
242 | /** |
243 | * Visits an {@code EXCEPTION_PARAMETER} variable element by calling |
244 | * {@code defaultAction}. |
245 | * |
246 | * @param e the element to visit |
247 | * @param p a visitor-specified parameter |
248 | * @return the result of {@code defaultAction} |
249 | */ |
250 | public R visitVariableAsExceptionParameter(VariableElement e, P p) { |
251 | return defaultAction(e, p); |
252 | } |
253 | |
254 | /** |
255 | * Visits a {@code FIELD} variable element by calling |
256 | * {@code defaultAction}. |
257 | * |
258 | * @param e the element to visit |
259 | * @param p a visitor-specified parameter |
260 | * @return the result of {@code defaultAction} |
261 | */ |
262 | public R visitVariableAsField(VariableElement e, P p) { |
263 | return defaultAction(e, p); |
264 | } |
265 | |
266 | /** |
267 | * Visits a {@code LOCAL_VARIABLE} variable element by calling |
268 | * {@code defaultAction}. |
269 | * |
270 | * @param e the element to visit |
271 | * @param p a visitor-specified parameter |
272 | * @return the result of {@code defaultAction} |
273 | */ |
274 | public R visitVariableAsLocalVariable(VariableElement e, P p) { |
275 | return defaultAction(e, p); |
276 | } |
277 | |
278 | /** |
279 | * Visits a {@code PARAMETER} variable element by calling |
280 | * {@code defaultAction}. |
281 | * |
282 | * @param e the element to visit |
283 | * @param p a visitor-specified parameter |
284 | * @return the result of {@code defaultAction} |
285 | */ |
286 | public R visitVariableAsParameter(VariableElement e, P p) { |
287 | return defaultAction(e, p); |
288 | } |
289 | |
290 | /** |
291 | * Visits an executable element, dispatching to the visit method |
292 | * for the specific {@linkplain ElementKind kind} of executable, |
293 | * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or |
294 | * {@code STATIC_INIT}. |
295 | * |
296 | * @param e {@inheritDoc} |
297 | * @param p {@inheritDoc} |
298 | * @return the result of the kind-specific visit method |
299 | */ |
300 | @Override |
301 | public R visitExecutable(ExecutableElement e, P p) { |
302 | ElementKind k = e.getKind(); |
303 | switch(k) { |
304 | case CONSTRUCTOR: |
305 | return visitExecutableAsConstructor(e, p); |
306 | |
307 | case INSTANCE_INIT: |
308 | return visitExecutableAsInstanceInit(e, p); |
309 | |
310 | case METHOD: |
311 | return visitExecutableAsMethod(e, p); |
312 | |
313 | case STATIC_INIT: |
314 | return visitExecutableAsStaticInit(e, p); |
315 | |
316 | default: |
317 | throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e); |
318 | } |
319 | } |
320 | |
321 | /** |
322 | * Visits a {@code CONSTRUCTOR} executable element by calling |
323 | * {@code defaultAction}. |
324 | * |
325 | * @param e the element to visit |
326 | * @param p a visitor-specified parameter |
327 | * @return the result of {@code defaultAction} |
328 | */ |
329 | public R visitExecutableAsConstructor(ExecutableElement e, P p) { |
330 | return defaultAction(e, p); |
331 | } |
332 | |
333 | /** |
334 | * Visits an {@code INSTANCE_INIT} executable element by calling |
335 | * {@code defaultAction}. |
336 | * |
337 | * @param e the element to visit |
338 | * @param p a visitor-specified parameter |
339 | * @return the result of {@code defaultAction} |
340 | */ |
341 | public R visitExecutableAsInstanceInit(ExecutableElement e, P p) { |
342 | return defaultAction(e, p); |
343 | } |
344 | |
345 | /** |
346 | * Visits a {@code METHOD} executable element by calling |
347 | * {@code defaultAction}. |
348 | * |
349 | * @param e the element to visit |
350 | * @param p a visitor-specified parameter |
351 | * @return the result of {@code defaultAction} |
352 | */ |
353 | public R visitExecutableAsMethod(ExecutableElement e, P p) { |
354 | return defaultAction(e, p); |
355 | } |
356 | |
357 | /** |
358 | * Visits a {@code STATIC_INIT} executable element by calling |
359 | * {@code defaultAction}. |
360 | * |
361 | * @param e the element to visit |
362 | * @param p a visitor-specified parameter |
363 | * @return the result of {@code defaultAction} |
364 | */ |
365 | public R visitExecutableAsStaticInit(ExecutableElement e, P p) { |
366 | return defaultAction(e, p); |
367 | } |
368 | |
369 | |
370 | /** |
371 | * {@inheritDoc} |
372 | * |
373 | * The element argument has kind {@code TYPE_PARAMETER}. |
374 | * |
375 | * @param e {@inheritDoc} |
376 | * @param p {@inheritDoc} |
377 | * @return {@inheritDoc} |
378 | */ |
379 | @Override |
380 | public R visitTypeParameter(TypeParameterElement e, P p) { |
381 | assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement"; |
382 | return defaultAction(e, p); |
383 | } |
384 | } |