1 /*
   2  * Copyright (c) 2005, 2017, 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.lang.model.element.*;
  29 import static javax.lang.model.element.ElementKind.*;
  30 import javax.annotation.processing.SupportedSourceVersion;
  31 import static javax.lang.model.SourceVersion.*;
  32 import javax.lang.model.SourceVersion;
  33 
  34 
  35 /**
  36  * A visitor of program elements based on their {@linkplain
  37  * ElementKind kind} with default behavior appropriate for the {@link
  38  * SourceVersion#RELEASE_6 RELEASE_6} source version.  For {@linkplain
  39  * Element elements} <code><i>Xyz</i></code> that may have more than one
  40  * kind, the <code>visit<i>Xyz</i></code> methods in this class delegate
  41  * to the <code>visit<i>Xyz</i>As<i>Kind</i></code> method corresponding to the
  42  * first argument's kind.  The <code>visit<i>Xyz</i>As<i>Kind</i></code> 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 ElementVisitor} interface
  52  * implemented by this class may have methods added to it or the
  53  * {@code ElementKind} {@code enum} used in this case may have
  54  * constants added to it in the future to accommodate new, currently
  55  * unknown, language structures added to future versions of the
  56  * Java&trade; programming language.  Therefore, methods whose names
  57  * begin with {@code "visit"} may be added to this class in the
  58  * future; to avoid incompatibilities, classes which extend this class
  59  * should not declare any instance methods with names beginning with
  60  * {@code "visit"}.
  61  *
  62  * <p>When such a new visit method is added, the default
  63  * implementation in this class will be to call the {@link
  64  * #visitUnknown visitUnknown} method.  A new abstract element kind
  65  * visitor class will also be introduced to correspond to the new
  66  * language level; this visitor will have different default behavior
  67  * for the visit method in question.  When the new visitor is
  68  * introduced, all or portions of this visitor may be deprecated.
  69  *
  70  * @param <R> the return type of this visitor's methods.  Use {@link
  71  *            Void} for visitors that do not need to return results.
  72  * @param <P> the type of the additional parameter to this visitor's
  73  *            methods.  Use {@code Void} for visitors that do not need an
  74  *            additional parameter.
  75  *
  76  * @author Joseph D. Darcy
  77  * @author Scott Seligman
  78  * @author Peter von der Ah&eacute;
  79  *
  80  * @see ElementKindVisitor7
  81  * @see ElementKindVisitor8
  82  * @see ElementKindVisitor9
  83  * @since 1.6
  84  */
  85 @SupportedSourceVersion(RELEASE_6)
  86 public class ElementKindVisitor6<R, P>
  87                   extends SimpleElementVisitor6<R, P> {
  88     /**
  89      * Constructor for concrete subclasses; uses {@code null} for the
  90      * default value.
  91      * @deprecated Release 6 is obsolete; update to a visitor for a newer
  92      * release level.
  93      */
  94     @Deprecated(since="9")
  95     protected ElementKindVisitor6() {
  96         super(null);
  97     }
  98 
  99     /**
 100      * Constructor for concrete subclasses; uses the argument for the
 101      * default value.
 102      *
 103      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
 104      * @deprecated Release 6 is obsolete; update to a visitor for a newer
 105      * release level.
 106      */
 107     @Deprecated(since="9")
 108     protected ElementKindVisitor6(R defaultValue) {
 109         super(defaultValue);
 110     }
 111 
 112     /**
 113      * {@inheritDoc}
 114      *
 115      * The element argument has kind {@code PACKAGE}.
 116      *
 117      * @param e {@inheritDoc}
 118      * @param p {@inheritDoc}
 119      * @return  {@inheritDoc}
 120      */
 121     @Override
 122     public R visitPackage(PackageElement e, P p) {
 123         assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
 124         return defaultAction(e, p);
 125     }
 126 
 127     /**
 128      * Visits a type element, dispatching to the visit method for the
 129      * specific {@linkplain ElementKind kind} of type, {@code
 130      * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
 131      * INTERFACE}.
 132      *
 133      * @param e {@inheritDoc}
 134      * @param p {@inheritDoc}
 135      * @return  the result of the kind-specific visit method
 136      */
 137     @Override
 138     public R visitType(TypeElement e, P p) {
 139         ElementKind k = e.getKind();
 140         switch(k) {
 141         case ANNOTATION_TYPE:
 142             return visitTypeAsAnnotationType(e, p);
 143 
 144         case CLASS:
 145             return visitTypeAsClass(e, p);
 146 
 147         case ENUM:
 148             return visitTypeAsEnum(e, p);
 149 
 150         case INTERFACE:
 151             return visitTypeAsInterface(e, p);
 152 
 153         default:
 154             throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
 155         }
 156     }
 157 
 158     /**
 159      * Visits an {@code ANNOTATION_TYPE} type element by calling
 160      * {@code defaultAction}.
 161      *
 162      * @param e the element to visit
 163      * @param p a visitor-specified parameter
 164      * @return  the result of {@code defaultAction}
 165      */
 166     public R visitTypeAsAnnotationType(TypeElement e, P p) {
 167         return defaultAction(e, p);
 168     }
 169 
 170     /**
 171      * Visits a {@code CLASS} type element by calling {@code
 172      * defaultAction}.
 173      *
 174      * @param e the element to visit
 175      * @param p a visitor-specified parameter
 176      * @return  the result of {@code defaultAction}
 177      */
 178     public R visitTypeAsClass(TypeElement e, P p) {
 179         return defaultAction(e, p);
 180     }
 181 
 182     /**
 183      * Visits an {@code ENUM} type element by calling {@code
 184      * defaultAction}.
 185      *
 186      * @param e the element to visit
 187      * @param p a visitor-specified parameter
 188      * @return  the result of {@code defaultAction}
 189      */
 190     public R visitTypeAsEnum(TypeElement e, P p) {
 191         return defaultAction(e, p);
 192     }
 193 
 194     /**
 195      * Visits an {@code INTERFACE} type element by calling {@code
 196      * defaultAction}.
 197      *.
 198      * @param e the element to visit
 199      * @param p a visitor-specified parameter
 200      * @return  the result of {@code defaultAction}
 201      */
 202     public R visitTypeAsInterface(TypeElement e, P p) {
 203         return defaultAction(e, p);
 204     }
 205 
 206     /**
 207      * Visits a variable element, dispatching to the visit method for
 208      * the specific {@linkplain ElementKind kind} of variable, {@code
 209      * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
 210      * {@code LOCAL_VARIABLE}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
 211      *
 212      * @param e {@inheritDoc}
 213      * @param p {@inheritDoc}
 214      * @return  the result of the kind-specific visit method
 215      */
 216     @Override
 217     public R visitVariable(VariableElement e, P p) {
 218         ElementKind k = e.getKind();
 219         switch(k) {
 220         case ENUM_CONSTANT:
 221             return visitVariableAsEnumConstant(e, p);
 222 
 223         case EXCEPTION_PARAMETER:
 224             return visitVariableAsExceptionParameter(e, p);
 225 
 226         case FIELD:
 227             return visitVariableAsField(e, p);
 228 
 229         case LOCAL_VARIABLE:
 230             return visitVariableAsLocalVariable(e, p);
 231 
 232         case PARAMETER:
 233             return visitVariableAsParameter(e, p);
 234 
 235         case RESOURCE_VARIABLE:
 236             return visitVariableAsResourceVariable(e, p);
 237 
 238         default:
 239             throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
 240         }
 241     }
 242 
 243     /**
 244      * Visits an {@code ENUM_CONSTANT} variable element by calling
 245      * {@code defaultAction}.
 246      *
 247      * @param e the element to visit
 248      * @param p a visitor-specified parameter
 249      * @return  the result of {@code defaultAction}
 250      */
 251     public R visitVariableAsEnumConstant(VariableElement e, P p) {
 252         return defaultAction(e, p);
 253     }
 254 
 255     /**
 256      * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
 257      * {@code defaultAction}.
 258      *
 259      * @param e the element to visit
 260      * @param p a visitor-specified parameter
 261      * @return  the result of {@code defaultAction}
 262      */
 263     public R visitVariableAsExceptionParameter(VariableElement e, P p) {
 264         return defaultAction(e, p);
 265     }
 266 
 267     /**
 268      * Visits a {@code FIELD} variable element by calling
 269      * {@code defaultAction}.
 270      *
 271      * @param e the element to visit
 272      * @param p a visitor-specified parameter
 273      * @return  the result of {@code defaultAction}
 274      */
 275     public R visitVariableAsField(VariableElement e, P p) {
 276         return defaultAction(e, p);
 277     }
 278 
 279     /**
 280      * Visits a {@code LOCAL_VARIABLE} variable element by calling
 281      * {@code defaultAction}.
 282      *
 283      * @param e the element to visit
 284      * @param p a visitor-specified parameter
 285      * @return  the result of {@code defaultAction}
 286      */
 287     public R visitVariableAsLocalVariable(VariableElement e, P p) {
 288         return defaultAction(e, p);
 289     }
 290 
 291     /**
 292      * Visits a {@code PARAMETER} variable element by calling
 293      * {@code defaultAction}.
 294      *
 295      * @param e the element to visit
 296      * @param p a visitor-specified parameter
 297      * @return  the result of {@code defaultAction}
 298      */
 299     public R visitVariableAsParameter(VariableElement e, P p) {
 300         return defaultAction(e, p);
 301     }
 302 
 303     /**
 304      * Visits a {@code RESOURCE_VARIABLE} variable element by calling
 305      * {@code visitUnknown}.
 306      *
 307      * @param e the element to visit
 308      * @param p a visitor-specified parameter
 309      * @return  the result of {@code visitUnknown}
 310      *
 311      * @since 1.7
 312      */
 313     public R visitVariableAsResourceVariable(VariableElement e, P p) {
 314         return visitUnknown(e, p);
 315     }
 316 
 317     /**
 318      * Visits an executable element, dispatching to the visit method
 319      * for the specific {@linkplain ElementKind kind} of executable,
 320      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
 321      * {@code STATIC_INIT}.
 322      *
 323      * @param e {@inheritDoc}
 324      * @param p {@inheritDoc}
 325      * @return  the result of the kind-specific visit method
 326      */
 327     @Override
 328     public R visitExecutable(ExecutableElement e, P p) {
 329         ElementKind k = e.getKind();
 330         switch(k) {
 331         case CONSTRUCTOR:
 332             return visitExecutableAsConstructor(e, p);
 333 
 334         case INSTANCE_INIT:
 335             return visitExecutableAsInstanceInit(e, p);
 336 
 337         case METHOD:
 338             return visitExecutableAsMethod(e, p);
 339 
 340         case STATIC_INIT:
 341             return visitExecutableAsStaticInit(e, p);
 342 
 343         default:
 344             throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
 345         }
 346     }
 347 
 348     /**
 349      * Visits a {@code CONSTRUCTOR} executable element by calling
 350      * {@code defaultAction}.
 351      *
 352      * @param e the element to visit
 353      * @param p a visitor-specified parameter
 354      * @return  the result of {@code defaultAction}
 355      */
 356     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
 357         return defaultAction(e, p);
 358     }
 359 
 360     /**
 361      * Visits an {@code INSTANCE_INIT} executable element by calling
 362      * {@code defaultAction}.
 363      *
 364      * @param e the element to visit
 365      * @param p a visitor-specified parameter
 366      * @return  the result of {@code defaultAction}
 367      */
 368     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
 369         return defaultAction(e, p);
 370     }
 371 
 372     /**
 373      * Visits a {@code METHOD} executable element by calling
 374      * {@code defaultAction}.
 375      *
 376      * @param e the element to visit
 377      * @param p a visitor-specified parameter
 378      * @return  the result of {@code defaultAction}
 379      */
 380     public R visitExecutableAsMethod(ExecutableElement e, P p) {
 381         return defaultAction(e, p);
 382     }
 383 
 384     /**
 385      * Visits a {@code STATIC_INIT} executable element by calling
 386      * {@code defaultAction}.
 387      *
 388      * @param e the element to visit
 389      * @param p a visitor-specified parameter
 390      * @return  the result of {@code defaultAction}
 391      */
 392     public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
 393         return defaultAction(e, p);
 394     }
 395 
 396 
 397     /**
 398      * {@inheritDoc}
 399      *
 400      * The element argument has kind {@code TYPE_PARAMETER}.
 401      *
 402      * @param e {@inheritDoc}
 403      * @param p {@inheritDoc}
 404      * @return  {@inheritDoc}
 405      */
 406     @Override
 407     public R visitTypeParameter(TypeParameterElement e, P p) {
 408         assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
 409         return defaultAction(e, p);
 410     }
 411 }