001    /*
002     * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package javax.lang.model.util;
027    
028    import javax.lang.model.element.*;
029    import static javax.lang.model.element.ElementKind.*;
030    import javax.annotation.processing.SupportedSourceVersion;
031    import static javax.lang.model.SourceVersion.*;
032    import javax.lang.model.SourceVersion;
033    
034    
035    /**
036     * A visitor of program elements based on their {@linkplain
037     * ElementKind kind} with default behavior appropriate for the {@link
038     * SourceVersion#RELEASE_6 RELEASE_6} source version.  For {@linkplain
039     * Element elements} <tt><i>XYZ</i></tt> that may have more than one
040     * kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate
041     * to the <tt>visit<i>XYZKind</i></tt> method corresponding to the
042     * first argument's kind.  The <tt>visit<i>XYZKind</i></tt> methods
043     * call {@link #defaultAction defaultAction}, passing their arguments
044     * to {@code defaultAction}'s corresponding parameters.
045     *
046     * <p> Methods in this class may be overridden subject to their
047     * general contract.  Note that annotating methods in concrete
048     * subclasses with {@link java.lang.Override @Override} will help
049     * ensure that methods are overridden as intended.
050     *
051     * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
052     * implemented by this class may have methods added to it or the
053     * {@code ElementKind} {@code enum} used in this case may have
054     * constants added to it in the future to accommodate new, currently
055     * unknown, language structures added to future versions of the
056     * Java&trade; programming language.  Therefore, methods whose names
057     * begin with {@code "visit"} may be added to this class in the
058     * future; to avoid incompatibilities, classes which extend this class
059     * should not declare any instance methods with names beginning with
060     * {@code "visit"}.
061     *
062     * <p>When such a new visit method is added, the default
063     * implementation in this class will be to call the {@link
064     * #visitUnknown visitUnknown} method.  A new abstract element kind
065     * visitor class will also be introduced to correspond to the new
066     * language level; this visitor will have different default behavior
067     * for the visit method in question.  When the new visitor is
068     * introduced, all or portions of this visitor may be deprecated.
069     *
070     * @param <R> the return type of this visitor's methods.  Use {@link
071     *            Void} for visitors that do not need to return results.
072     * @param <P> the type of the additional parameter to this visitor's
073     *            methods.  Use {@code Void} for visitors that do not need an
074     *            additional parameter.
075     *
076     * @author Joseph D. Darcy
077     * @author Scott Seligman
078     * @author Peter von der Ah&eacute;
079     * @since 1.6
080     */
081    @SupportedSourceVersion(RELEASE_6)
082    public class ElementKindVisitor6<R, P>
083                      extends SimpleElementVisitor6<R, P> {
084        /**
085         * Constructor for concrete subclasses; uses {@code null} for the
086         * default value.
087         */
088        protected ElementKindVisitor6() {
089            super(null);
090        }
091    
092        /**
093         * Constructor for concrete subclasses; uses the argument for the
094         * default value.
095         *
096         * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
097         */
098        protected ElementKindVisitor6(R defaultValue) {
099            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    }