1 /* 2 * Copyright (c) 2005, 2011, 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 */ 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 * 78 * @see TypeKindVisitor7 79 * @see TypeKindVisitor8 80 * @since 1.6 81 */ 82 @SupportedSourceVersion(RELEASE_6) 83 public class TypeKindVisitor6<R, P> extends SimpleTypeVisitor6<R, P> { 84 /** 85 * Constructor for concrete subclasses to call; uses {@code null} 86 * for the default value. 87 */ 88 protected TypeKindVisitor6() { 89 super(null); 90 } 91 92 93 /** 94 * Constructor for concrete subclasses to call; uses the argument 95 * for the default value. 96 * 97 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 98 */ 99 protected TypeKindVisitor6(R defaultValue) { 100 super(defaultValue); 101 } 102 103 /** 104 * Visits a primitive type, dispatching to the visit method for 105 * the specific {@linkplain TypeKind kind} of primitive type: 106 * {@code BOOLEAN}, {@code BYTE}, etc. 107 * 108 * @param t {@inheritDoc} 109 * @param p {@inheritDoc} 110 * @return the result of the kind-specific visit method 111 */ 112 @Override 113 public R visitPrimitive(PrimitiveType t, P p) { 114 TypeKind k = t.getKind(); 115 switch (k) { 116 case BOOLEAN: 117 return visitPrimitiveAsBoolean(t, p); 118 119 case BYTE: 120 return visitPrimitiveAsByte(t, p); 121 122 case SHORT: 123 return visitPrimitiveAsShort(t, p); 124 125 case INT: 126 return visitPrimitiveAsInt(t, p); 127 128 case LONG: 129 return visitPrimitiveAsLong(t, p); 130 131 case CHAR: 132 return visitPrimitiveAsChar(t, p); 133 134 case FLOAT: 135 return visitPrimitiveAsFloat(t, p); 136 137 case DOUBLE: 138 return visitPrimitiveAsDouble(t, p); 139 140 default: 141 throw new AssertionError("Bad kind " + k + " for PrimitiveType" + t); 142 } 143 } 144 145 /** 146 * Visits a {@code BOOLEAN} primitive type by calling 147 * {@code defaultAction}. 148 * 149 * @param t the type to visit 150 * @param p a visitor-specified parameter 151 * @return the result of {@code defaultAction} 152 */ 153 public R visitPrimitiveAsBoolean(PrimitiveType t, P p) { 154 return defaultAction(t, p); 155 } 156 157 /** 158 * Visits a {@code BYTE} primitive type by calling 159 * {@code defaultAction}. 160 * 161 * @param t the type to visit 162 * @param p a visitor-specified parameter 163 * @return the result of {@code defaultAction} 164 */ 165 public R visitPrimitiveAsByte(PrimitiveType t, P p) { 166 return defaultAction(t, p); 167 } 168 169 /** 170 * Visits a {@code SHORT} primitive type by calling 171 * {@code defaultAction}. 172 * 173 * @param t the type to visit 174 * @param p a visitor-specified parameter 175 * @return the result of {@code defaultAction} 176 */ 177 public R visitPrimitiveAsShort(PrimitiveType t, P p) { 178 return defaultAction(t, p); 179 } 180 181 /** 182 * Visits an {@code INT} primitive type by calling 183 * {@code defaultAction}. 184 * 185 * @param t the type to visit 186 * @param p a visitor-specified parameter 187 * @return the result of {@code defaultAction} 188 */ 189 public R visitPrimitiveAsInt(PrimitiveType t, P p) { 190 return defaultAction(t, p); 191 } 192 193 /** 194 * Visits a {@code LONG} primitive type by calling 195 * {@code defaultAction}. 196 * 197 * @param t the type to visit 198 * @param p a visitor-specified parameter 199 * @return the result of {@code defaultAction} 200 */ 201 public R visitPrimitiveAsLong(PrimitiveType t, P p) { 202 return defaultAction(t, p); 203 } 204 205 /** 206 * Visits a {@code CHAR} primitive type by calling 207 * {@code defaultAction}. 208 * 209 * @param t the type to visit 210 * @param p a visitor-specified parameter 211 * @return the result of {@code defaultAction} 212 */ 213 public R visitPrimitiveAsChar(PrimitiveType t, P p) { 214 return defaultAction(t, p); 215 } 216 217 /** 218 * Visits a {@code FLOAT} primitive type by calling 219 * {@code defaultAction}. 220 * 221 * @param t the type to visit 222 * @param p a visitor-specified parameter 223 * @return the result of {@code defaultAction} 224 */ 225 public R visitPrimitiveAsFloat(PrimitiveType t, P p) { 226 return defaultAction(t, p); 227 } 228 229 /** 230 * Visits a {@code DOUBLE} primitive type by calling 231 * {@code defaultAction}. 232 * 233 * @param t the type to visit 234 * @param p a visitor-specified parameter 235 * @return the result of {@code defaultAction} 236 */ 237 public R visitPrimitiveAsDouble(PrimitiveType t, P p) { 238 return defaultAction(t, p); 239 } 240 241 /** 242 * Visits a {@link NoType} instance, dispatching to the visit method for 243 * the specific {@linkplain TypeKind kind} of pseudo-type: 244 * {@code VOID}, {@code PACKAGE}, or {@code NONE}. 245 * 246 * @param t {@inheritDoc} 247 * @param p {@inheritDoc} 248 * @return the result of the kind-specific visit method 249 */ 250 @Override 251 public R visitNoType(NoType t, P p) { 252 TypeKind k = t.getKind(); 253 switch (k) { 254 case VOID: 255 return visitNoTypeAsVoid(t, p); 256 257 case PACKAGE: 258 return visitNoTypeAsPackage(t, p); 259 260 case NONE: 261 return visitNoTypeAsNone(t, p); 262 263 default: 264 throw new AssertionError("Bad kind " + k + " for NoType" + t); 265 } 266 } 267 268 /** 269 * Visits a {@link TypeKind#VOID VOID} pseudo-type by calling 270 * {@code defaultAction}. 271 * 272 * @param t the type to visit 273 * @param p a visitor-specified parameter 274 * @return the result of {@code defaultAction} 275 */ 276 public R visitNoTypeAsVoid(NoType t, P p) { 277 return defaultAction(t, p); 278 } 279 280 /** 281 * Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type by calling 282 * {@code defaultAction}. 283 * 284 * @param t the type to visit 285 * @param p a visitor-specified parameter 286 * @return the result of {@code defaultAction} 287 */ 288 public R visitNoTypeAsPackage(NoType t, P p) { 289 return defaultAction(t, p); 290 } 291 292 /** 293 * Visits a {@link TypeKind#NONE NONE} pseudo-type by calling 294 * {@code defaultAction}. 295 * 296 * @param t the type to visit 297 * @param p a visitor-specified parameter 298 * @return the result of {@code defaultAction} 299 */ 300 public R visitNoTypeAsNone(NoType t, P p) { 301 return defaultAction(t, p); 302 } 303 }