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 29 import javax.lang.model.type.*; 30 import javax.annotation.processing.SupportedSourceVersion; 31 import static javax.lang.model.element.ElementKind.*; 32 import static javax.lang.model.SourceVersion.*; 33 import javax.lang.model.SourceVersion; 34 35 /** 36 * A visitor of types based on their {@linkplain TypeKind kind} with 37 * default behavior appropriate for the {@link SourceVersion#RELEASE_6 38 * RELEASE_6} source version. For {@linkplain 39 * TypeMirror types} <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 TypeVisitor} interface implemented 52 * by this class may have methods added to it in the future to 53 * accommodate new, currently unknown, language structures added to 54 * future versions of the Java™ programming language. 55 * Therefore, methods whose names begin with {@code "visit"} may be 56 * added to this class in the future; to avoid incompatibilities, 57 * classes which extend this class should not declare any instance 58 * methods with names beginning with {@code "visit"}. 59 * 60 * <p>When such a new visit method is added, the default 61 * implementation in this class will be to call the {@link 62 * #visitUnknown visitUnknown} method. A new type kind visitor class 63 * will also be introduced to correspond to the new language level; 64 * this visitor will have different default behavior for the visit 65 * method in question. When the new visitor is introduced, all or 66 * portions of this visitor may be deprecated. 67 * 68 * @param <R> the return type of this visitor's methods. Use {@link 69 * Void} for visitors that do not need to return results. 70 * @param <P> the type of the additional parameter to this visitor's 71 * methods. Use {@code Void} for visitors that do not need an 72 * additional parameter. 73 * 74 * @author Joseph D. Darcy 75 * @author Scott Seligman 76 * @author Peter von der Ahé 77 * @since 1.6 78 */ 79 @SupportedSourceVersion(RELEASE_6) 80 public class TypeKindVisitor6<R, P> extends SimpleTypeVisitor6<R, P> { 81 /** 82 * Constructor for concrete subclasses to call; uses {@code null} 83 * for the default value. 84 */ 85 protected TypeKindVisitor6() { 86 super(null); 87 } 88 89 90 /** 91 * Constructor for concrete subclasses to call; uses the argument 92 * for the default value. 93 * 94 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 95 */ 96 protected TypeKindVisitor6(R defaultValue) { 97 super(defaultValue); 98 } 99 100 /** 101 * Visits a primitive type, dispatching to the visit method for 102 * the specific {@linkplain TypeKind kind} of primitive type: 103 * {@code BOOLEAN}, {@code BYTE}, etc. 104 * 105 * @param t {@inheritDoc} 106 * @param p {@inheritDoc} 107 * @return the result of the kind-specific visit method 108 */ 109 @Override 110 public R visitPrimitive(PrimitiveType t, P p) { 111 TypeKind k = t.getKind(); 112 switch (k) { 113 case BOOLEAN: 114 return visitPrimitiveAsBoolean(t, p); 115 116 case BYTE: 117 return visitPrimitiveAsByte(t, p); 118 119 case SHORT: 120 return visitPrimitiveAsShort(t, p); 121 122 case INT: 123 return visitPrimitiveAsInt(t, p); 124 125 case LONG: 126 return visitPrimitiveAsLong(t, p); 127 128 case CHAR: 129 return visitPrimitiveAsChar(t, p); 130 131 case FLOAT: 132 return visitPrimitiveAsFloat(t, p); 133 134 case DOUBLE: 135 return visitPrimitiveAsDouble(t, p); 136 137 default: 138 throw new AssertionError("Bad kind " + k + " 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" + t); 262 } 263 } 264 265 /** 266 * Visits a {@link TypeKind#VOID VOID} pseudo-type by calling 267 * {@code defaultAction}. 268 * 269 * @param t the type to visit 270 * @param p a visitor-specified parameter 271 * @return the result of {@code defaultAction} 272 */ 273 public R visitNoTypeAsVoid(NoType t, P p) { 274 return defaultAction(t, p); 275 } 276 277 /** 278 * Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type by calling 279 * {@code defaultAction}. 280 * 281 * @param t the type to visit 282 * @param p a visitor-specified parameter 283 * @return the result of {@code defaultAction} 284 */ 285 public R visitNoTypeAsPackage(NoType t, P p) { 286 return defaultAction(t, p); 287 } 288 289 /** 290 * Visits a {@link TypeKind#NONE NONE} pseudo-type by calling 291 * {@code defaultAction}. 292 * 293 * @param t the type to visit 294 * @param p a visitor-specified parameter 295 * @return the result of {@code defaultAction} 296 */ 297 public R visitNoTypeAsNone(NoType t, P p) { 298 return defaultAction(t, p); 299 } 300 }