Path: blob/master/src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor6.java
42759 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.lang.model.util;2627import javax.annotation.processing.SupportedSourceVersion;28import javax.lang.model.SourceVersion;29import javax.lang.model.type.*;30import static javax.lang.model.SourceVersion.*;3132/**33* A visitor of types based on their {@linkplain TypeKind kind} with34* default behavior appropriate for the {@link SourceVersion#RELEASE_635* RELEASE_6} source version. For {@linkplain36* TypeMirror types} <code><i>Xyz</i></code> that may have more than one37* kind, the <code>visit<i>Xyz</i></code> methods in this class delegate38* to the <code>visit<i>Xyz</i>As<i>Kind</i></code> method corresponding to the39* first argument's kind. The <code>visit<i>Xyz</i>As<i>Kind</i></code> methods40* call {@link #defaultAction defaultAction}, passing their arguments41* to {@code defaultAction}'s corresponding parameters.42*43* @apiNote44* Methods in this class may be overridden subject to their general45* contract.46*47* <p id=note_for_subclasses><strong>WARNING:</strong> The {@code48* TypeVisitor} interface implemented by this class may have methods49* added to it or the {@link TypeKind TypeKind enum} used in this50* class may have constants added to it in the future to accommodate51* new, currently unknown, language structures added to future52* versions of the Java programming language. Therefore,53* methods whose names begin with {@code "visit"} may be added to this54* class in the future; to avoid incompatibilities, classes and55* subclasses which extend this class should not declare any instance56* methods with names beginning with {@code "visit"}.</p>57*58* <p>When such a new visit method is added, the default59* implementation in this class will be to directly or indirectly call60* the {@link #visitUnknown visitUnknown} method. A new type kind61* visitor class will also be introduced to correspond to the new62* language level; this visitor will have different default behavior63* for the visit method in question. When a new visitor is64* introduced, portions of this visitor class may be deprecated,65* including its constructors.66*67* @param <R> the return type of this visitor's methods. Use {@link68* Void} for visitors that do not need to return results.69* @param <P> the type of the additional parameter to this visitor's70* methods. Use {@code Void} for visitors that do not need an71* additional parameter.72*73* @author Joseph D. Darcy74* @author Scott Seligman75* @author Peter von der Ahé76*77* @see TypeKindVisitor778* @see TypeKindVisitor879* @see TypeKindVisitor980* @see TypeKindVisitor1481* @since 1.682*/83@SupportedSourceVersion(RELEASE_6)84public class TypeKindVisitor6<R, P> extends SimpleTypeVisitor6<R, P> {85/**86* Constructor for concrete subclasses to call; uses {@code null}87* for the default value.88* @deprecated Release 6 is obsolete; update to a visitor for a newer89* release level.90*/91@Deprecated(since="9")92protected TypeKindVisitor6() {93super(null);94}959697/**98* Constructor for concrete subclasses to call; uses the argument99* for the default value.100*101* @param defaultValue the value to assign to {@link #DEFAULT_VALUE}102* @deprecated Release 6 is obsolete; update to a visitor for a newer103* release level.104*/105@Deprecated(since="9")106protected TypeKindVisitor6(R defaultValue) {107super(defaultValue);108}109110/**111* {@inheritDoc}112*113* @implSpec This implementation dispatches to the visit method for114* the specific {@linkplain TypeKind kind} of primitive type:115* {@code BOOLEAN}, {@code BYTE}, etc.116*117* @param t {@inheritDoc}118* @param p {@inheritDoc}119* @return the result of the kind-specific visit method120*/121@Override122public R visitPrimitive(PrimitiveType t, P p) {123TypeKind k = t.getKind();124switch (k) {125case BOOLEAN:126return visitPrimitiveAsBoolean(t, p);127128case BYTE:129return visitPrimitiveAsByte(t, p);130131case SHORT:132return visitPrimitiveAsShort(t, p);133134case INT:135return visitPrimitiveAsInt(t, p);136137case LONG:138return visitPrimitiveAsLong(t, p);139140case CHAR:141return visitPrimitiveAsChar(t, p);142143case FLOAT:144return visitPrimitiveAsFloat(t, p);145146case DOUBLE:147return visitPrimitiveAsDouble(t, p);148149default:150throw new AssertionError("Bad kind " + k + " for PrimitiveType" + t);151}152}153154/**155* Visits a {@code BOOLEAN} primitive type.156*157* @implSpec This implementation calls {@code defaultAction}.158*159* @param t the type to visit160* @param p a visitor-specified parameter161* @return the result of {@code defaultAction}162*/163public R visitPrimitiveAsBoolean(PrimitiveType t, P p) {164return defaultAction(t, p);165}166167/**168* Visits a {@code BYTE} primitive type.169*170* @implSpec This implementation calls {@code defaultAction}.171*172* @param t the type to visit173* @param p a visitor-specified parameter174* @return the result of {@code defaultAction}175*/176public R visitPrimitiveAsByte(PrimitiveType t, P p) {177return defaultAction(t, p);178}179180/**181* Visits a {@code SHORT} primitive type.182*183* @implSpec This implementation calls {@code defaultAction}.184*185* @param t the type to visit186* @param p a visitor-specified parameter187* @return the result of {@code defaultAction}188*/189public R visitPrimitiveAsShort(PrimitiveType t, P p) {190return defaultAction(t, p);191}192193/**194* Visits an {@code INT} primitive type.195*196* @implSpec This implementation calls {@code defaultAction}.197*198* @param t the type to visit199* @param p a visitor-specified parameter200* @return the result of {@code defaultAction}201*/202public R visitPrimitiveAsInt(PrimitiveType t, P p) {203return defaultAction(t, p);204}205206/**207* Visits a {@code LONG} primitive type.208*209* @implSpec This implementation calls {@code defaultAction}.210*211* @param t the type to visit212* @param p a visitor-specified parameter213* @return the result of {@code defaultAction}214*/215public R visitPrimitiveAsLong(PrimitiveType t, P p) {216return defaultAction(t, p);217}218219/**220* Visits a {@code CHAR} primitive type.221*222* @implSpec This implementation calls {@code defaultAction}.223*224* @param t the type to visit225* @param p a visitor-specified parameter226* @return the result of {@code defaultAction}227*/228public R visitPrimitiveAsChar(PrimitiveType t, P p) {229return defaultAction(t, p);230}231232/**233* Visits a {@code FLOAT} primitive type.234*235* @implSpec This implementation calls {@code defaultAction}.236*237* @param t the type to visit238* @param p a visitor-specified parameter239* @return the result of {@code defaultAction}240*/241public R visitPrimitiveAsFloat(PrimitiveType t, P p) {242return defaultAction(t, p);243}244245/**246* Visits a {@code DOUBLE} primitive type.247*248* @implSpec This implementation calls {@code defaultAction}.249*250* @param t the type to visit251* @param p a visitor-specified parameter252* @return the result of {@code defaultAction}253*/254public R visitPrimitiveAsDouble(PrimitiveType t, P p) {255return defaultAction(t, p);256}257258/**259* {@inheritDoc}260*261* @implSpec This implementation dispatches to the visit method for262* the specific {@linkplain TypeKind kind} of pseudo-type:263* {@code VOID}, {@code PACKAGE}, {@code MODULE}, or {@code NONE}.264*265* @param t {@inheritDoc}266* @param p {@inheritDoc}267* @return the result of the kind-specific visit method268*/269@Override270public R visitNoType(NoType t, P p) {271TypeKind k = t.getKind();272switch (k) {273case VOID:274return visitNoTypeAsVoid(t, p);275276case PACKAGE:277return visitNoTypeAsPackage(t, p);278279case MODULE:280return visitNoTypeAsModule(t, p);281282case NONE:283return visitNoTypeAsNone(t, p);284285default:286throw new AssertionError("Bad kind " + k + " for NoType" + t);287}288}289290/**291* Visits a {@link TypeKind#VOID VOID} pseudo-type.292*293* @implSpec This implementation calls {@code defaultAction}.294*295* @param t the type to visit296* @param p a visitor-specified parameter297* @return the result of {@code defaultAction}298*/299public R visitNoTypeAsVoid(NoType t, P p) {300return defaultAction(t, p);301}302303/**304* Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type.305*306* @implSpec This implementation calls {@code defaultAction}.307*308* @param t the type to visit309* @param p a visitor-specified parameter310* @return the result of {@code defaultAction}311*/312public R visitNoTypeAsPackage(NoType t, P p) {313return defaultAction(t, p);314}315316/**317* Visits a {@link TypeKind#MODULE MODULE} pseudo-type.318*319* @implSpec This implementation calls {@code visitUnknown}.320*321* @param t the type to visit322* @param p a visitor-specified parameter323* @return the result of {@code visitUnknown}324*325* @since 10326*/327public R visitNoTypeAsModule(NoType t, P p) {328return visitUnknown(t, p);329}330331/**332* Visits a {@link TypeKind#NONE NONE} pseudo-type.333*334* @implSpec This implementation calls {@code defaultAction}.335*336* @param t the type to visit337* @param p a visitor-specified parameter338* @return the result of {@code defaultAction}339*/340public R visitNoTypeAsNone(NoType t, P p) {341return defaultAction(t, p);342}343}344345346