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 javax.annotation.processing.SupportedSourceVersion;
030 import static javax.lang.model.element.ElementKind.*;
031 import javax.lang.model.SourceVersion;
032 import static javax.lang.model.SourceVersion.*;
033
034
035 /**
036 * A simple visitor of program elements with default behavior
037 * appropriate for the {@link SourceVersion#RELEASE_6 RELEASE_6}
038 * source version.
039 *
040 * Visit methods corresponding to {@code RELEASE_6} language
041 * constructs call {@link #defaultAction}, passing their arguments to
042 * {@code defaultAction}'s corresponding parameters.
043 *
044 * <p> Methods in this class may be overridden subject to their
045 * general contract. Note that annotating methods in concrete
046 * subclasses with {@link java.lang.Override @Override} will help
047 * ensure that methods are overridden as intended.
048 *
049 * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
050 * implemented by this class may have methods added to it in the
051 * future to accommodate new, currently unknown, language structures
052 * added to future versions of the Java™ programming language.
053 * Therefore, methods whose names begin with {@code "visit"} may be
054 * added to this class in the future; to avoid incompatibilities,
055 * classes which extend this class should not declare any instance
056 * methods with names beginning with {@code "visit"}.
057 *
058 * <p>When such a new visit method is added, the default
059 * implementation in this class will be to call the {@link
060 * #visitUnknown visitUnknown} method. A new simple element visitor
061 * class will also be introduced to correspond to the new language
062 * level; this visitor will have different default behavior for the
063 * visit method in question. When the new visitor is introduced, all
064 * or portions of this visitor may be deprecated.
065 *
066 * @param <R> the return type of this visitor's methods. Use {@code Void}
067 * for visitors that do not need to return results.
068 * @param <P> the type of the additional parameter to this visitor's methods. Use {@code Void}
069 * for visitors that do not need an additional parameter.
070 *
071 * @author Joseph D. Darcy
072 * @author Scott Seligman
073 * @author Peter von der Ahé
074 * @since 1.6
075 */
076 @SupportedSourceVersion(RELEASE_6)
077 public class SimpleElementVisitor6<R, P> extends AbstractElementVisitor6<R, P> {
078 /**
079 * Default value to be returned; {@link #defaultAction
080 * defaultAction} returns this value unless the method is
081 * overridden.
082 */
083 protected final R DEFAULT_VALUE;
084
085 /**
086 * Constructor for concrete subclasses; uses {@code null} for the
087 * default value.
088 */
089 protected SimpleElementVisitor6(){
090 DEFAULT_VALUE = null;
091 }
092
093 /**
094 * Constructor for concrete subclasses; uses the argument for the
095 * default value.
096 *
097 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
098 */
099 protected SimpleElementVisitor6(R defaultValue){
100 DEFAULT_VALUE = defaultValue;
101 }
102 /**
103 * The default action for visit methods. The implementation in
104 * this class just returns {@link #DEFAULT_VALUE}; subclasses will
105 * commonly override this method.
106 *
107 * @param e the element to process
108 * @param p a visitor-specified parameter
109 * @return {@code DEFAULT_VALUE} unless overridden
110 */
111 protected R defaultAction(Element e, P p) {
112 return DEFAULT_VALUE;
113 }
114
115 /**
116 * {@inheritDoc} This implementation calls {@code defaultAction}.
117 *
118 * @param e {@inheritDoc}
119 * @param p {@inheritDoc}
120 * @return the result of {@code defaultAction}
121 */
122 public R visitPackage(PackageElement e, P p) {
123 return defaultAction(e, p);
124 }
125
126 /**
127 * {@inheritDoc} This implementation calls {@code defaultAction}.
128 *
129 * @param e {@inheritDoc}
130 * @param p {@inheritDoc}
131 * @return the result of {@code defaultAction}
132 */
133 public R visitType(TypeElement e, P p) {
134 return defaultAction(e, p);
135 }
136
137 /**
138 * {@inheritDoc} This implementation calls {@code defaultAction}.
139 *
140 * @param e {@inheritDoc}
141 * @param p {@inheritDoc}
142 * @return the result of {@code defaultAction}
143 */
144 public R visitVariable(VariableElement e, P p) {
145 return defaultAction(e, p);
146 }
147
148 /**
149 * {@inheritDoc} This implementation calls {@code defaultAction}.
150 *
151 * @param e {@inheritDoc}
152 * @param p {@inheritDoc}
153 * @return the result of {@code defaultAction}
154 */
155 public R visitExecutable(ExecutableElement e, P p) {
156 return defaultAction(e, p);
157 }
158
159 /**
160 * {@inheritDoc} This implementation calls {@code defaultAction}.
161 *
162 * @param e {@inheritDoc}
163 * @param p {@inheritDoc}
164 * @return the result of {@code defaultAction}
165 */
166 public R visitTypeParameter(TypeParameterElement e, P p) {
167 return defaultAction(e, p);
168 }
169 }