Thu, 31 Aug 2017 15:17:03 +0800
merge
1 /*
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package javax.lang.model.util;
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;
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 * <p>Note that adding a default implementation of a new visit method
71 * in a visitor class will occur instead of adding a <em>default
72 * method</em> directly in the visitor interface since a Java SE 8
73 * language feature cannot be used to this version of the API since
74 * this version is required to be runnable on Java SE 7
75 * implementations. Future versions of the API that are only required
76 * to run on Java SE 8 and later may take advantage of default methods
77 * in this situation.
78 *
79 * @param <R> the return type of this visitor's methods. Use {@link
80 * Void} for visitors that do not need to return results.
81 * @param <P> the type of the additional parameter to this visitor's
82 * methods. Use {@code Void} for visitors that do not need an
83 * additional parameter.
84 *
85 * @author Joseph D. Darcy
86 * @author Scott Seligman
87 * @author Peter von der Ahé
88 *
89 * @see ElementKindVisitor7
90 * @see ElementKindVisitor8
91 * @since 1.6
92 */
93 @SupportedSourceVersion(RELEASE_6)
94 public class ElementKindVisitor6<R, P>
95 extends SimpleElementVisitor6<R, P> {
96 /**
97 * Constructor for concrete subclasses; uses {@code null} for the
98 * default value.
99 */
100 protected ElementKindVisitor6() {
101 super(null);
102 }
104 /**
105 * Constructor for concrete subclasses; uses the argument for the
106 * default value.
107 *
108 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
109 */
110 protected ElementKindVisitor6(R defaultValue) {
111 super(defaultValue);
112 }
114 /**
115 * {@inheritDoc}
116 *
117 * The element argument has kind {@code PACKAGE}.
118 *
119 * @param e {@inheritDoc}
120 * @param p {@inheritDoc}
121 * @return {@inheritDoc}
122 */
123 @Override
124 public R visitPackage(PackageElement e, P p) {
125 assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
126 return defaultAction(e, p);
127 }
129 /**
130 * Visits a type element, dispatching to the visit method for the
131 * specific {@linkplain ElementKind kind} of type, {@code
132 * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
133 * INTERFACE}.
134 *
135 * @param e {@inheritDoc}
136 * @param p {@inheritDoc}
137 * @return the result of the kind-specific visit method
138 */
139 @Override
140 public R visitType(TypeElement e, P p) {
141 ElementKind k = e.getKind();
142 switch(k) {
143 case ANNOTATION_TYPE:
144 return visitTypeAsAnnotationType(e, p);
146 case CLASS:
147 return visitTypeAsClass(e, p);
149 case ENUM:
150 return visitTypeAsEnum(e, p);
152 case INTERFACE:
153 return visitTypeAsInterface(e, p);
155 default:
156 throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
157 }
158 }
160 /**
161 * Visits an {@code ANNOTATION_TYPE} type element by calling
162 * {@code 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 visitTypeAsAnnotationType(TypeElement e, P p) {
169 return defaultAction(e, p);
170 }
172 /**
173 * Visits a {@code CLASS} 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 visitTypeAsClass(TypeElement e, P p) {
181 return defaultAction(e, p);
182 }
184 /**
185 * Visits an {@code ENUM} 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 visitTypeAsEnum(TypeElement e, P p) {
193 return defaultAction(e, p);
194 }
196 /**
197 * Visits an {@code INTERFACE} type element by calling {@code
198 * defaultAction}.
199 *.
200 * @param e the element to visit
201 * @param p a visitor-specified parameter
202 * @return the result of {@code defaultAction}
203 */
204 public R visitTypeAsInterface(TypeElement e, P p) {
205 return defaultAction(e, p);
206 }
208 /**
209 * Visits a variable element, dispatching to the visit method for
210 * the specific {@linkplain ElementKind kind} of variable, {@code
211 * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
212 * {@code LOCAL_VARIABLE}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
213 *
214 * @param e {@inheritDoc}
215 * @param p {@inheritDoc}
216 * @return the result of the kind-specific visit method
217 */
218 @Override
219 public R visitVariable(VariableElement e, P p) {
220 ElementKind k = e.getKind();
221 switch(k) {
222 case ENUM_CONSTANT:
223 return visitVariableAsEnumConstant(e, p);
225 case EXCEPTION_PARAMETER:
226 return visitVariableAsExceptionParameter(e, p);
228 case FIELD:
229 return visitVariableAsField(e, p);
231 case LOCAL_VARIABLE:
232 return visitVariableAsLocalVariable(e, p);
234 case PARAMETER:
235 return visitVariableAsParameter(e, p);
237 case RESOURCE_VARIABLE:
238 return visitVariableAsResourceVariable(e, p);
240 default:
241 throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
242 }
243 }
245 /**
246 * Visits an {@code ENUM_CONSTANT} variable element by calling
247 * {@code defaultAction}.
248 *
249 * @param e the element to visit
250 * @param p a visitor-specified parameter
251 * @return the result of {@code defaultAction}
252 */
253 public R visitVariableAsEnumConstant(VariableElement e, P p) {
254 return defaultAction(e, p);
255 }
257 /**
258 * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
259 * {@code defaultAction}.
260 *
261 * @param e the element to visit
262 * @param p a visitor-specified parameter
263 * @return the result of {@code defaultAction}
264 */
265 public R visitVariableAsExceptionParameter(VariableElement e, P p) {
266 return defaultAction(e, p);
267 }
269 /**
270 * Visits a {@code FIELD} variable element by calling
271 * {@code defaultAction}.
272 *
273 * @param e the element to visit
274 * @param p a visitor-specified parameter
275 * @return the result of {@code defaultAction}
276 */
277 public R visitVariableAsField(VariableElement e, P p) {
278 return defaultAction(e, p);
279 }
281 /**
282 * Visits a {@code LOCAL_VARIABLE} variable element by calling
283 * {@code defaultAction}.
284 *
285 * @param e the element to visit
286 * @param p a visitor-specified parameter
287 * @return the result of {@code defaultAction}
288 */
289 public R visitVariableAsLocalVariable(VariableElement e, P p) {
290 return defaultAction(e, p);
291 }
293 /**
294 * Visits a {@code PARAMETER} variable element by calling
295 * {@code defaultAction}.
296 *
297 * @param e the element to visit
298 * @param p a visitor-specified parameter
299 * @return the result of {@code defaultAction}
300 */
301 public R visitVariableAsParameter(VariableElement e, P p) {
302 return defaultAction(e, p);
303 }
305 /**
306 * Visits a {@code RESOURCE_VARIABLE} variable element by calling
307 * {@code visitUnknown}.
308 *
309 * @param e the element to visit
310 * @param p a visitor-specified parameter
311 * @return the result of {@code visitUnknown}
312 *
313 * @since 1.7
314 */
315 public R visitVariableAsResourceVariable(VariableElement e, P p) {
316 return visitUnknown(e, p);
317 }
319 /**
320 * Visits an executable element, dispatching to the visit method
321 * for the specific {@linkplain ElementKind kind} of executable,
322 * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
323 * {@code STATIC_INIT}.
324 *
325 * @param e {@inheritDoc}
326 * @param p {@inheritDoc}
327 * @return the result of the kind-specific visit method
328 */
329 @Override
330 public R visitExecutable(ExecutableElement e, P p) {
331 ElementKind k = e.getKind();
332 switch(k) {
333 case CONSTRUCTOR:
334 return visitExecutableAsConstructor(e, p);
336 case INSTANCE_INIT:
337 return visitExecutableAsInstanceInit(e, p);
339 case METHOD:
340 return visitExecutableAsMethod(e, p);
342 case STATIC_INIT:
343 return visitExecutableAsStaticInit(e, p);
345 default:
346 throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
347 }
348 }
350 /**
351 * Visits a {@code CONSTRUCTOR} executable element by calling
352 * {@code defaultAction}.
353 *
354 * @param e the element to visit
355 * @param p a visitor-specified parameter
356 * @return the result of {@code defaultAction}
357 */
358 public R visitExecutableAsConstructor(ExecutableElement e, P p) {
359 return defaultAction(e, p);
360 }
362 /**
363 * Visits an {@code INSTANCE_INIT} executable element by calling
364 * {@code defaultAction}.
365 *
366 * @param e the element to visit
367 * @param p a visitor-specified parameter
368 * @return the result of {@code defaultAction}
369 */
370 public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
371 return defaultAction(e, p);
372 }
374 /**
375 * Visits a {@code METHOD} executable element by calling
376 * {@code defaultAction}.
377 *
378 * @param e the element to visit
379 * @param p a visitor-specified parameter
380 * @return the result of {@code defaultAction}
381 */
382 public R visitExecutableAsMethod(ExecutableElement e, P p) {
383 return defaultAction(e, p);
384 }
386 /**
387 * Visits a {@code STATIC_INIT} executable element by calling
388 * {@code defaultAction}.
389 *
390 * @param e the element to visit
391 * @param p a visitor-specified parameter
392 * @return the result of {@code defaultAction}
393 */
394 public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
395 return defaultAction(e, p);
396 }
399 /**
400 * {@inheritDoc}
401 *
402 * The element argument has kind {@code TYPE_PARAMETER}.
403 *
404 * @param e {@inheritDoc}
405 * @param p {@inheritDoc}
406 * @return {@inheritDoc}
407 */
408 @Override
409 public R visitTypeParameter(TypeParameterElement e, P p) {
410 assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
411 return defaultAction(e, p);
412 }
413 }