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