1 /*
   2  * Copyright 2010 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 import javax.lang.model.type.*;
  29 import javax.annotation.processing.SupportedSourceVersion;
  30 import static javax.lang.model.element.ElementKind.*;
  31 import static javax.lang.model.SourceVersion.*;
  32 import javax.lang.model.SourceVersion;
  33 
  34 /**
  35  * Shared package private class to reduce code bloat.
  36  *
  37  * @param <R> the return type of this visitor's methods.  Use {@link
  38  *            Void} for visitors that do not need to return results.
  39  * @param <P> the type of the additional parameter to this visitor's
  40  *            methods.  Use {@code Void} for visitors that do not need an
  41  *            additional parameter.
  42  *
  43  * @since 1.7
  44  */
  45 class SharedTypeKindVisitor<R, P> extends SimpleTypeVisitor6<R, P> {
  46     /**
  47      * Constructor for concrete subclasses to call; uses {@code null}
  48      * for the default value.
  49      */
  50     protected SharedTypeKindVisitor() {
  51         super(null);
  52     }
  53 
  54     /**
  55      * Constructor for concrete subclasses to call; uses the argument
  56      * for the default value.
  57      *
  58      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
  59      */
  60     protected SharedTypeKindVisitor(R defaultValue) {
  61         super(defaultValue);
  62     }
  63 
  64     /**
  65      * Visits a primitive type, dispatching to the visit method for
  66      * the specific {@linkplain TypeKind kind} of primitive type:
  67      * {@code BOOLEAN}, {@code BYTE}, etc.
  68      *
  69      * @param t {@inheritDoc}
  70      * @param p {@inheritDoc}
  71      * @return  the result of the kind-specific visit method
  72      */
  73     @Override
  74     public R visitPrimitive(PrimitiveType t, P p) {
  75         TypeKind k = t.getKind();
  76         switch (k) {
  77         case BOOLEAN:
  78             return visitPrimitiveAsBoolean(t, p);
  79 
  80         case BYTE:
  81             return visitPrimitiveAsByte(t, p);
  82 
  83         case SHORT:
  84             return visitPrimitiveAsShort(t, p);
  85 
  86         case INT:
  87             return visitPrimitiveAsInt(t, p);
  88 
  89         case LONG:
  90             return visitPrimitiveAsLong(t, p);
  91 
  92         case CHAR:
  93             return visitPrimitiveAsChar(t, p);
  94 
  95         case FLOAT:
  96             return visitPrimitiveAsFloat(t, p);
  97 
  98         case DOUBLE:
  99             return visitPrimitiveAsDouble(t, p);
 100 
 101         default:
 102             throw new AssertionError("Bad kind " + k + " for PrimitiveType" + t);
 103         }
 104     }
 105 
 106     /**
 107      * Visits a {@code BOOLEAN} primitive type by calling
 108      * {@code defaultAction}.
 109      *
 110      * @param t the type to visit
 111      * @param p a visitor-specified parameter
 112      * @return  the result of {@code defaultAction}
 113      */
 114     public R visitPrimitiveAsBoolean(PrimitiveType t, P p) {
 115         return defaultAction(t, p);
 116     }
 117 
 118     /**
 119      * Visits a {@code BYTE} primitive type by calling
 120      * {@code defaultAction}.
 121      *
 122      * @param t the type to visit
 123      * @param p a visitor-specified parameter
 124      * @return  the result of {@code defaultAction}
 125      */
 126     public R visitPrimitiveAsByte(PrimitiveType t, P p) {
 127         return defaultAction(t, p);
 128     }
 129 
 130     /**
 131      * Visits a {@code SHORT} primitive type by calling
 132      * {@code defaultAction}.
 133      *
 134      * @param t the type to visit
 135      * @param p a visitor-specified parameter
 136      * @return  the result of {@code defaultAction}
 137      */
 138     public R visitPrimitiveAsShort(PrimitiveType t, P p) {
 139         return defaultAction(t, p);
 140     }
 141 
 142     /**
 143      * Visits an {@code INT} 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 visitPrimitiveAsInt(PrimitiveType t, P p) {
 151         return defaultAction(t, p);
 152     }
 153 
 154     /**
 155      * Visits a {@code LONG} 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 visitPrimitiveAsLong(PrimitiveType t, P p) {
 163         return defaultAction(t, p);
 164     }
 165 
 166     /**
 167      * Visits a {@code CHAR} 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 visitPrimitiveAsChar(PrimitiveType t, P p) {
 175         return defaultAction(t, p);
 176     }
 177 
 178     /**
 179      * Visits a {@code FLOAT} 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 visitPrimitiveAsFloat(PrimitiveType t, P p) {
 187         return defaultAction(t, p);
 188     }
 189 
 190     /**
 191      * Visits a {@code DOUBLE} 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 visitPrimitiveAsDouble(PrimitiveType t, P p) {
 199         return defaultAction(t, p);
 200     }
 201 
 202     /**
 203      * Visits a {@link NoType} instance, dispatching to the visit method for
 204      * the specific {@linkplain TypeKind kind} of pseudo-type:
 205      * {@code VOID}, {@code PACKAGE}, or {@code NONE}.
 206      *
 207      * @param t {@inheritDoc}
 208      * @param p {@inheritDoc}
 209      * @return  the result of the kind-specific visit method
 210      */
 211     @Override
 212     public R visitNoType(NoType t, P p) {
 213         TypeKind k = t.getKind();
 214         switch (k) {
 215         case VOID:
 216             return visitNoTypeAsVoid(t, p);
 217 
 218         case PACKAGE:
 219             return visitNoTypeAsPackage(t, p);
 220 
 221         case NONE:
 222             return visitNoTypeAsNone(t, p);
 223 
 224         default:
 225             throw new AssertionError("Bad kind " + k + " for NoType" + t);
 226         }
 227     }
 228 
 229     /**
 230      * Visits a {@link TypeKind#VOID VOID} pseudo-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 visitNoTypeAsVoid(NoType t, P p) {
 238         return defaultAction(t, p);
 239     }
 240 
 241     /**
 242      * Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type by calling
 243      * {@code defaultAction}.
 244      *
 245      * @param t the type to visit
 246      * @param p a visitor-specified parameter
 247      * @return  the result of {@code defaultAction}
 248      */
 249     public R visitNoTypeAsPackage(NoType t, P p) {
 250         return defaultAction(t, p);
 251     }
 252 
 253     /**
 254      * Visits a {@link TypeKind#NONE NONE} pseudo-type by calling
 255      * {@code defaultAction}.
 256      *
 257      * @param t the type to visit
 258      * @param p a visitor-specified parameter
 259      * @return  the result of {@code defaultAction}
 260      */
 261     public R visitNoTypeAsNone(NoType t, P p) {
 262         return defaultAction(t, p);
 263     }
 264 }