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.type.*;
029: import javax.annotation.processing.SupportedSourceVersion;
030: import static javax.lang.model.element.ElementKind.*;
031: import static javax.lang.model.SourceVersion.*;
032: import javax.lang.model.SourceVersion;
033:
034: /**
035: * A visitor of types based on their {@linkplain TypeKind kind} with
036: * default behavior appropriate for the {@link SourceVersion#RELEASE_6
037: * RELEASE_6} source version. For {@linkplain
038: * TypeMirror types} <tt><i>XYZ</i></tt> that may have more than one
039: * kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate
040: * to the <tt>visit<i>XYZKind</i></tt> method corresponding to the
041: * first argument's kind. The <tt>visit<i>XYZKind</i></tt> methods
042: * call {@link #defaultAction defaultAction}, passing their arguments
043: * to {@code defaultAction}'s corresponding parameters.
044: *
045: * <p> Methods in this class may be overridden subject to their
046: * general contract. Note that annotating methods in concrete
047: * subclasses with {@link java.lang.Override @Override} will help
048: * ensure that methods are overridden as intended.
049: *
050: * <p> <b>WARNING:</b> The {@code TypeVisitor} interface implemented
051: * by this class may have methods added to it in the future to
052: * accommodate new, currently unknown, language structures added to
053: * future versions of the Java™ programming language.
054: * Therefore, methods whose names begin with {@code "visit"} may be
055: * added to this class in the future; to avoid incompatibilities,
056: * classes which extend this class should not declare any instance
057: * methods with names beginning with {@code "visit"}.
058: *
059: * <p>When such a new visit method is added, the default
060: * implementation in this class will be to call the {@link
061: * #visitUnknown visitUnknown} method. A new type kind visitor class
062: * will also be introduced to correspond to the new language level;
063: * this visitor will have different default behavior for the visit
064: * method in question. When the new visitor is introduced, all or
065: * portions of this visitor may be deprecated.
066: *
067: * @param <R> the return type of this visitor's methods. Use {@link
068: * Void} for visitors that do not need to return results.
069: * @param <P> the type of the additional parameter to this visitor's
070: * methods. Use {@code Void} for visitors that do not need an
071: * additional parameter.
072: *
073: * @author Joseph D. Darcy
074: * @author Scott Seligman
075: * @author Peter von der Ahé
076: * @version 1.12 07/05/05
077: * @since 1.6
078: */
079: @SupportedSourceVersion(RELEASE_6)
080: public class TypeKindVisitor6<R, P> extends SimpleTypeVisitor6<R, P> {
081: /**
082: * Constructor for concrete subclasses to call; uses {@code null}
083: * for the default value.
084: */
085: protected TypeKindVisitor6() {
086: super (null);
087: }
088:
089: /**
090: * Constructor for concrete subclasses to call; uses the argument
091: * for the default value.
092: *
093: * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
094: */
095: protected TypeKindVisitor6(R defaultValue) {
096: super (defaultValue);
097: }
098:
099: /**
100: * Visits a primitive type, dispatching to the visit method for
101: * the specific {@linkplain TypeKind kind} of primitive type:
102: * {@code BOOLEAN}, {@code BYTE}, etc.
103: *
104: * @param t {@inheritDoc}
105: * @param p {@inheritDoc}
106: * @return the result of the kind-specific visit method
107: */
108: @Override
109: public R visitPrimitive(PrimitiveType t, P p) {
110: TypeKind k = t.getKind();
111: switch (k) {
112: case BOOLEAN:
113: return visitPrimitiveAsBoolean(t, p);
114:
115: case BYTE:
116: return visitPrimitiveAsByte(t, p);
117:
118: case SHORT:
119: return visitPrimitiveAsShort(t, p);
120:
121: case INT:
122: return visitPrimitiveAsInt(t, p);
123:
124: case LONG:
125: return visitPrimitiveAsLong(t, p);
126:
127: case CHAR:
128: return visitPrimitiveAsChar(t, p);
129:
130: case FLOAT:
131: return visitPrimitiveAsFloat(t, p);
132:
133: case DOUBLE:
134: return visitPrimitiveAsDouble(t, p);
135:
136: default:
137: throw new AssertionError("Bad kind " + k
138: + " for PrimitiveType" + t);
139: }
140: }
141:
142: /**
143: * Visits a {@code BOOLEAN} primitive type by calling
144: * {@code defaultAction}.
145: *
146: * @param t the type to visit
147: * @param p a visitor-specified parameter
148: * @return the result of {@code defaultAction}
149: */
150: public R visitPrimitiveAsBoolean(PrimitiveType t, P p) {
151: return defaultAction(t, p);
152: }
153:
154: /**
155: * Visits a {@code BYTE} primitive type by calling
156: * {@code defaultAction}.
157: *
158: * @param t the type to visit
159: * @param p a visitor-specified parameter
160: * @return the result of {@code defaultAction}
161: */
162: public R visitPrimitiveAsByte(PrimitiveType t, P p) {
163: return defaultAction(t, p);
164: }
165:
166: /**
167: * Visits a {@code SHORT} primitive type by calling
168: * {@code defaultAction}.
169: *
170: * @param t the type to visit
171: * @param p a visitor-specified parameter
172: * @return the result of {@code defaultAction}
173: */
174: public R visitPrimitiveAsShort(PrimitiveType t, P p) {
175: return defaultAction(t, p);
176: }
177:
178: /**
179: * Visits an {@code INT} primitive type by calling
180: * {@code defaultAction}.
181: *
182: * @param t the type to visit
183: * @param p a visitor-specified parameter
184: * @return the result of {@code defaultAction}
185: */
186: public R visitPrimitiveAsInt(PrimitiveType t, P p) {
187: return defaultAction(t, p);
188: }
189:
190: /**
191: * Visits a {@code LONG} primitive type by calling
192: * {@code defaultAction}.
193: *
194: * @param t the type to visit
195: * @param p a visitor-specified parameter
196: * @return the result of {@code defaultAction}
197: */
198: public R visitPrimitiveAsLong(PrimitiveType t, P p) {
199: return defaultAction(t, p);
200: }
201:
202: /**
203: * Visits a {@code CHAR} primitive type by calling
204: * {@code defaultAction}.
205: *
206: * @param t the type to visit
207: * @param p a visitor-specified parameter
208: * @return the result of {@code defaultAction}
209: */
210: public R visitPrimitiveAsChar(PrimitiveType t, P p) {
211: return defaultAction(t, p);
212: }
213:
214: /**
215: * Visits a {@code FLOAT} primitive type by calling
216: * {@code defaultAction}.
217: *
218: * @param t the type to visit
219: * @param p a visitor-specified parameter
220: * @return the result of {@code defaultAction}
221: */
222: public R visitPrimitiveAsFloat(PrimitiveType t, P p) {
223: return defaultAction(t, p);
224: }
225:
226: /**
227: * Visits a {@code DOUBLE} primitive type by calling
228: * {@code defaultAction}.
229: *
230: * @param t the type to visit
231: * @param p a visitor-specified parameter
232: * @return the result of {@code defaultAction}
233: */
234: public R visitPrimitiveAsDouble(PrimitiveType t, P p) {
235: return defaultAction(t, p);
236: }
237:
238: /**
239: * Visits a {@link NoType} instance, dispatching to the visit method for
240: * the specific {@linkplain TypeKind kind} of pseudo-type:
241: * {@code VOID}, {@code PACKAGE}, or {@code NONE}.
242: *
243: * @param t {@inheritDoc}
244: * @param p {@inheritDoc}
245: * @return the result of the kind-specific visit method
246: */
247: @Override
248: public R visitNoType(NoType t, P p) {
249: TypeKind k = t.getKind();
250: switch (k) {
251: case VOID:
252: return visitNoTypeAsVoid(t, p);
253:
254: case PACKAGE:
255: return visitNoTypeAsPackage(t, p);
256:
257: case NONE:
258: return visitNoTypeAsNone(t, p);
259:
260: default:
261: throw new AssertionError("Bad kind " + k + " for NoType"
262: + t);
263: }
264: }
265:
266: /**
267: * Visits a {@link TypeKind#VOID VOID} pseudo-type by calling
268: * {@code defaultAction}.
269: *
270: * @param t the type to visit
271: * @param p a visitor-specified parameter
272: * @return the result of {@code defaultAction}
273: */
274: public R visitNoTypeAsVoid(NoType t, P p) {
275: return defaultAction(t, p);
276: }
277:
278: /**
279: * Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type by calling
280: * {@code defaultAction}.
281: *
282: * @param t the type to visit
283: * @param p a visitor-specified parameter
284: * @return the result of {@code defaultAction}
285: */
286: public R visitNoTypeAsPackage(NoType t, P p) {
287: return defaultAction(t, p);
288: }
289:
290: /**
291: * Visits a {@link TypeKind#NONE NONE} pseudo-type by calling
292: * {@code defaultAction}.
293: *
294: * @param t the type to visit
295: * @param p a visitor-specified parameter
296: * @return the result of {@code defaultAction}
297: */
298: public R visitNoTypeAsNone(NoType t, P p) {
299: return defaultAction(t, p);
300: }
301: }
|