1 /* 2 * Copyright (c) 2005, 2020, 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 import javax.annotation.processing.SupportedSourceVersion; 29 import javax.lang.model.SourceVersion; 30 import javax.lang.model.type.*; 31 import static javax.lang.model.SourceVersion.*; 32 33 /** 34 * A visitor of types based on their {@linkplain TypeKind kind} with 35 * default behavior appropriate for the {@link SourceVersion#RELEASE_6 36 * RELEASE_6} source version. For {@linkplain 37 * TypeMirror types} <code><i>Xyz</i></code> that may have more than one 38 * kind, the <code>visit<i>Xyz</i></code> methods in this class delegate 39 * to the <code>visit<i>Xyz</i>As<i>Kind</i></code> method corresponding to the 40 * first argument's kind. The <code>visit<i>Xyz</i>As<i>Kind</i></code> methods 41 * call {@link #defaultAction defaultAction}, passing their arguments 42 * to {@code defaultAction}'s corresponding parameters. 43 * 44 * @apiNote 45 * Methods in this class may be overridden subject to their general 46 * contract. 47 * 48 * <p id=note_for_subclasses><strong>WARNING:</strong> The {@code 49 * TypeVisitor} interface implemented by this class may have methods 50 * added to it or the {@link TypeKind TypeKind enum} used in this 51 * class may have constants added to it in the future to accommodate 52 * new, currently unknown, language structures added to future 53 * versions of the Java™ programming language. Therefore, 54 * methods whose names begin with {@code "visit"} may be added to this 55 * class in the future; to avoid incompatibilities, classes and 56 * subclasses which extend this class should not declare any instance 57 * methods with names beginning with {@code "visit"}.</p> 58 * 59 * <p>When such a new visit method is added, the default 60 * implementation in this class will be to directly or indirectly call 61 * the {@link #visitUnknown visitUnknown} method. A new type kind 62 * visitor class will also be introduced to correspond to the new 63 * language level; this visitor will have different default behavior 64 * for the visit method in question. When a new visitor is 65 * introduced, portions of this visitor class may be deprecated, 66 * including its constructors. 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 * @see TypeKindVisitor9 81 * @see TypeKindVisitor14 82 * @since 1.6 83 */ 84 @SupportedSourceVersion(RELEASE_6) 85 public class TypeKindVisitor6<R, P> extends SimpleTypeVisitor6<R, P> { 86 /** 87 * Constructor for concrete subclasses to call; uses {@code null} 88 * for the default value. 89 * @deprecated Release 6 is obsolete; update to a visitor for a newer 90 * release level. 91 */ 92 @Deprecated(since="9") 93 protected TypeKindVisitor6() { 94 super(null); 95 } 96 97 98 /** 99 * Constructor for concrete subclasses to call; uses the argument 100 * for the default value. 101 * 102 * @param defaultValue the value to assign to {@link #DEFAULT_VALUE} 103 * @deprecated Release 6 is obsolete; update to a visitor for a newer 104 * release level. 105 */ 106 @Deprecated(since="9") 107 protected TypeKindVisitor6(R defaultValue) { 108 super(defaultValue); 109 } 110 111 /** 112 * {@inheritDoc} 113 * 114 * @implSpec This implementation dispatches to the visit method for 115 * the specific {@linkplain TypeKind kind} of primitive type: 116 * {@code BOOLEAN}, {@code BYTE}, etc. 117 * 118 * @param t {@inheritDoc} 119 * @param p {@inheritDoc} 120 * @return the result of the kind-specific visit method 121 */ 122 @Override 123 public R visitPrimitive(PrimitiveType t, P p) { 124 TypeKind k = t.getKind(); 125 switch (k) { 126 case BOOLEAN: 127 return visitPrimitiveAsBoolean(t, p); 128 129 case BYTE: 130 return visitPrimitiveAsByte(t, p); 131 132 case SHORT: 133 return visitPrimitiveAsShort(t, p); 134 135 case INT: 136 return visitPrimitiveAsInt(t, p); 137 138 case LONG: 139 return visitPrimitiveAsLong(t, p); 140 141 case CHAR: 142 return visitPrimitiveAsChar(t, p); 143 144 case FLOAT: 145 return visitPrimitiveAsFloat(t, p); 146 147 case DOUBLE: 148 return visitPrimitiveAsDouble(t, p); 149 150 default: 151 throw new AssertionError("Bad kind " + k + " for PrimitiveType" + t); 152 } 153 } 154 155 /** 156 * Visits a {@code BOOLEAN} primitive type. 157 * 158 * @implSpec This implementation calls {@code defaultAction}. 159 * 160 * @param t the type to visit 161 * @param p a visitor-specified parameter 162 * @return the result of {@code defaultAction} 163 */ 164 public R visitPrimitiveAsBoolean(PrimitiveType t, P p) { 165 return defaultAction(t, p); 166 } 167 168 /** 169 * Visits a {@code BYTE} primitive type. 170 * 171 * @implSpec This implementation calls {@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 visitPrimitiveAsByte(PrimitiveType t, P p) { 178 return defaultAction(t, p); 179 } 180 181 /** 182 * Visits a {@code SHORT} primitive type. 183 * 184 * @implSpec This implementation calls {@code defaultAction}. 185 * 186 * @param t the type to visit 187 * @param p a visitor-specified parameter 188 * @return the result of {@code defaultAction} 189 */ 190 public R visitPrimitiveAsShort(PrimitiveType t, P p) { 191 return defaultAction(t, p); 192 } 193 194 /** 195 * Visits an {@code INT} primitive type. 196 * 197 * @implSpec This implementation calls {@code defaultAction}. 198 * 199 * @param t the type to visit 200 * @param p a visitor-specified parameter 201 * @return the result of {@code defaultAction} 202 */ 203 public R visitPrimitiveAsInt(PrimitiveType t, P p) { 204 return defaultAction(t, p); 205 } 206 207 /** 208 * Visits a {@code LONG} primitive type. 209 * 210 * @implSpec This implementation calls {@code defaultAction}. 211 * 212 * @param t the type to visit 213 * @param p a visitor-specified parameter 214 * @return the result of {@code defaultAction} 215 */ 216 public R visitPrimitiveAsLong(PrimitiveType t, P p) { 217 return defaultAction(t, p); 218 } 219 220 /** 221 * Visits a {@code CHAR} primitive type. 222 * 223 * @implSpec This implementation calls {@code defaultAction}. 224 * 225 * @param t the type to visit 226 * @param p a visitor-specified parameter 227 * @return the result of {@code defaultAction} 228 */ 229 public R visitPrimitiveAsChar(PrimitiveType t, P p) { 230 return defaultAction(t, p); 231 } 232 233 /** 234 * Visits a {@code FLOAT} primitive type. 235 * 236 * @implSpec This implementation calls {@code defaultAction}. 237 * 238 * @param t the type to visit 239 * @param p a visitor-specified parameter 240 * @return the result of {@code defaultAction} 241 */ 242 public R visitPrimitiveAsFloat(PrimitiveType t, P p) { 243 return defaultAction(t, p); 244 } 245 246 /** 247 * Visits a {@code DOUBLE} primitive type. 248 * 249 * @implSpec This implementation calls {@code defaultAction}. 250 * 251 * @param t the type to visit 252 * @param p a visitor-specified parameter 253 * @return the result of {@code defaultAction} 254 */ 255 public R visitPrimitiveAsDouble(PrimitiveType t, P p) { 256 return defaultAction(t, p); 257 } 258 259 /** 260 * {@inheritDoc} 261 * 262 * @implSpec This implementation dispatches to the visit method for 263 * the specific {@linkplain TypeKind kind} of pseudo-type: 264 * {@code VOID}, {@code PACKAGE}, {@code MODULE}, or {@code NONE}. 265 * 266 * @param t {@inheritDoc} 267 * @param p {@inheritDoc} 268 * @return the result of the kind-specific visit method 269 */ 270 @Override 271 public R visitNoType(NoType t, P p) { 272 TypeKind k = t.getKind(); 273 switch (k) { 274 case VOID: 275 return visitNoTypeAsVoid(t, p); 276 277 case PACKAGE: 278 return visitNoTypeAsPackage(t, p); 279 280 case MODULE: 281 return visitNoTypeAsModule(t, p); 282 283 case NONE: 284 return visitNoTypeAsNone(t, p); 285 286 default: 287 throw new AssertionError("Bad kind " + k + " for NoType" + t); 288 } 289 } 290 291 /** 292 * Visits a {@link TypeKind#VOID VOID} pseudo-type. 293 * 294 * @implSpec This implementation calls {@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 visitNoTypeAsVoid(NoType t, P p) { 301 return defaultAction(t, p); 302 } 303 304 /** 305 * Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type. 306 * 307 * @implSpec This implementation calls {@code defaultAction}. 308 * 309 * @param t the type to visit 310 * @param p a visitor-specified parameter 311 * @return the result of {@code defaultAction} 312 */ 313 public R visitNoTypeAsPackage(NoType t, P p) { 314 return defaultAction(t, p); 315 } 316 317 /** 318 * Visits a {@link TypeKind#MODULE MODULE} pseudo-type. 319 * 320 * @implSpec This implementation calls {@code visitUnknown}. 321 * 322 * @param t the type to visit 323 * @param p a visitor-specified parameter 324 * @return the result of {@code visitUnknown} 325 * 326 * @since 10 327 */ 328 public R visitNoTypeAsModule(NoType t, P p) { 329 return visitUnknown(t, p); 330 } 331 332 /** 333 * Visits a {@link TypeKind#NONE NONE} pseudo-type. 334 * 335 * @implSpec This implementation calls {@code defaultAction}. 336 * 337 * @param t the type to visit 338 * @param p a visitor-specified parameter 339 * @return the result of {@code defaultAction} 340 */ 341 public R visitNoTypeAsNone(NoType t, P p) { 342 return defaultAction(t, p); 343 } 344 }