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  * @see ElementKindVisitor14
  84  * @since 1.6
  85  */
  86 @SupportedSourceVersion(RELEASE_6)
  87 public class ElementKindVisitor6<R, P>
  88                   extends SimpleElementVisitor6<R, P> {
  89     /**
  90      * Constructor for concrete subclasses; uses {@code null} for the
  91      * default value.
  92      * @deprecated Release 6 is obsolete; update to a visitor for a newer
  93      * release level.
  94      */
  95     @Deprecated(since="9")
  96     protected ElementKindVisitor6() {
  97         super(null);
  98     }
  99 
 100     /**
 101      * Constructor for concrete subclasses; uses the argument for the
 102      * default value.
 103      *
 104      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
 105      * @deprecated Release 6 is obsolete; update to a visitor for a newer
 106      * release level.
 107      */
 108     @Deprecated(since="9")
 109     protected ElementKindVisitor6(R defaultValue) {
 110         super(defaultValue);
 111     }
 112 
 113     /**
 114      * {@inheritDoc}
 115      *
 116      * The element argument has kind {@code PACKAGE}.
 117      *
 118      * @implSpec This implementation calls {@code defaultAction}.
 119      *
 120      * @param e {@inheritDoc}
 121      * @param p {@inheritDoc}
 122      * @return  {@inheritDoc}
 123      */
 124     @Override
 125     public R visitPackage(PackageElement e, P p) {
 126         assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
 127         return defaultAction(e, p);
 128     }
 129 
 130     /**
 131      * {@inheritDoc}
 132      *
 133      * @implSpec This implementation dispatches to the visit method for the
 134      * specific {@linkplain ElementKind kind} of type, {@code
 135      * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
 136      * INTERFACE}.
 137      *
 138      * @param e {@inheritDoc}
 139      * @param p {@inheritDoc}
 140      * @return  the result of the kind-specific visit method
 141      */
 142     @Override
 143     public R visitType(TypeElement e, P p) {
 144         ElementKind k = e.getKind();
 145         switch(k) {
 146         case ANNOTATION_TYPE:
 147             return visitTypeAsAnnotationType(e, p);
 148 
 149         case CLASS:
 150             return visitTypeAsClass(e, p);
 151 
 152         case ENUM:
 153             return visitTypeAsEnum(e, p);
 154 
 155         case INTERFACE:
 156             return visitTypeAsInterface(e, p);
 157 
 158         case RECORD:
 159             return visitTypeAsRecord(e, p);
 160 
 161         default:
 162             throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
 163         }
 164     }
 165 
 166     /**
 167      * Visits an {@code ANNOTATION_TYPE} type element.
 168      *
 169      * @implSpec This implementation calls {@code defaultAction}.
 170      *
 171      * @param e the element to visit
 172      * @param p a visitor-specified parameter
 173      * @return  the result of {@code defaultAction}
 174      */
 175     public R visitTypeAsAnnotationType(TypeElement e, P p) {
 176         return defaultAction(e, p);
 177     }
 178 
 179     /**
 180      * Visits a {@code CLASS} type element.
 181      *
 182      * @implSpec This implementation calls {@code defaultAction}.
 183      *
 184      * @param e the element to visit
 185      * @param p a visitor-specified parameter
 186      * @return  the result of {@code defaultAction}
 187      */
 188     public R visitTypeAsClass(TypeElement e, P p) {
 189         return defaultAction(e, p);
 190     }
 191 
 192     /**
 193      * Visits an {@code ENUM} type element.
 194      *
 195      * @implSpec This implementation calls {@code defaultAction}.
 196      *
 197      * @param e the element to visit
 198      * @param p a visitor-specified parameter
 199      * @return  the result of {@code defaultAction}
 200      */
 201     public R visitTypeAsEnum(TypeElement e, P p) {
 202         return defaultAction(e, p);
 203     }
 204 
 205     /**
 206      * Visits an {@code INTERFACE} type element.
 207      *
 208      * @implSpec This implementation calls {@code defaultAction}.
 209      *.
 210      * @param e the element to visit
 211      * @param p a visitor-specified parameter
 212      * @return  the result of {@code defaultAction}
 213      */
 214     public R visitTypeAsInterface(TypeElement e, P p) {
 215         return defaultAction(e, p);
 216     }
 217 
 218     /**
 219      * Visits a {@code RECORD} type element.
 220      *
 221      * @implSpec This implementation calls {@code visitUnknown}.
 222      *.
 223      * @param e the element to visit
 224      * @param p a visitor-specified parameter
 225      * @return  the result of {@code visitUnknown}
 226      *
 227      * @since 14
 228      * @deprecated This method is part of a preview feature and may be removed
 229      * if the preview feature is removed.
 230      */
 231     @Deprecated(forRemoval=true, since="14")
 232     @SuppressWarnings("removal")
 233     public R visitTypeAsRecord(TypeElement e, P p) {
 234         return visitUnknown(e, p);
 235     }
 236 
 237     /**
 238      * Visits a variable element
 239      *
 240      * @implSpec This implementation dispatches to the visit method for
 241      * the specific {@linkplain ElementKind kind} of variable, {@code
 242      * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
 243      * {@code LOCAL_VARIABLE}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
 244      *
 245      * @param e {@inheritDoc}
 246      * @param p {@inheritDoc}
 247      * @return  the result of the kind-specific visit method
 248      */
 249     @Override
 250     public R visitVariable(VariableElement e, P p) {
 251         ElementKind k = e.getKind();
 252         switch(k) {
 253         case ENUM_CONSTANT:
 254             return visitVariableAsEnumConstant(e, p);
 255 
 256         case EXCEPTION_PARAMETER:
 257             return visitVariableAsExceptionParameter(e, p);
 258 
 259         case FIELD:
 260             return visitVariableAsField(e, p);
 261 
 262         case LOCAL_VARIABLE:
 263             return visitVariableAsLocalVariable(e, p);
 264 
 265         case PARAMETER:
 266             return visitVariableAsParameter(e, p);
 267 
 268         case RESOURCE_VARIABLE:
 269             return visitVariableAsResourceVariable(e, p);
 270 
 271         default:
 272             throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
 273         }
 274     }
 275 
 276     /**
 277      * Visits an {@code ENUM_CONSTANT} variable element.
 278      *
 279      * @implSpec This implementation calls {@code defaultAction}.
 280      *.
 281      * @param e the element to visit
 282      * @param p a visitor-specified parameter
 283      * @return  the result of {@code defaultAction}
 284      */
 285     public R visitVariableAsEnumConstant(VariableElement e, P p) {
 286         return defaultAction(e, p);
 287     }
 288 
 289     /**
 290      * Visits an {@code EXCEPTION_PARAMETER} variable element.
 291      *
 292      * @implSpec This implementation calls {@code defaultAction}.
 293      *.
 294      * @param e the element to visit
 295      * @param p a visitor-specified parameter
 296      * @return  the result of {@code defaultAction}
 297      */
 298     public R visitVariableAsExceptionParameter(VariableElement e, P p) {
 299         return defaultAction(e, p);
 300     }
 301 
 302     /**
 303      * Visits a {@code FIELD} variable element.
 304      *
 305      * @implSpec This implementation calls {@code defaultAction}.
 306      *.
 307      * @param e the element to visit
 308      * @param p a visitor-specified parameter
 309      * @return  the result of {@code defaultAction}
 310      */
 311     public R visitVariableAsField(VariableElement e, P p) {
 312         return defaultAction(e, p);
 313     }
 314 
 315     /**
 316      * Visits a {@code LOCAL_VARIABLE} variable element.
 317      *
 318      * @implSpec This implementation calls {@code defaultAction}.
 319      *
 320      * @param e the element to visit
 321      * @param p a visitor-specified parameter
 322      * @return  the result of {@code defaultAction}
 323      */
 324     public R visitVariableAsLocalVariable(VariableElement e, P p) {
 325         return defaultAction(e, p);
 326     }
 327 
 328     /**
 329      * Visits a {@code PARAMETER} variable element.
 330      *
 331      * @implSpec This implementation calls {@code defaultAction}.
 332      *
 333      * @param e the element to visit
 334      * @param p a visitor-specified parameter
 335      * @return  the result of {@code defaultAction}
 336      */
 337     public R visitVariableAsParameter(VariableElement e, P p) {
 338         return defaultAction(e, p);
 339     }
 340 
 341     /**
 342      * Visits a {@code RESOURCE_VARIABLE} variable element.
 343      *
 344      * @implSpec This implementation calls {@code visitUnknown}.
 345      *
 346      * @param e the element to visit
 347      * @param p a visitor-specified parameter
 348      * @return  the result of {@code visitUnknown}
 349      *
 350      * @since 1.7
 351      */
 352     public R visitVariableAsResourceVariable(VariableElement e, P p) {
 353         return visitUnknown(e, p);
 354     }
 355 
 356     /**
 357      * {@inheritDoc}
 358      *
 359      * @implSpec This implementation dispatches to the visit method
 360      * for the specific {@linkplain ElementKind kind} of executable,
 361      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
 362      * {@code STATIC_INIT}.
 363      *
 364      * @param e {@inheritDoc}
 365      * @param p {@inheritDoc}
 366      * @return  the result of the kind-specific visit method
 367      */
 368     @Override
 369     public R visitExecutable(ExecutableElement e, P p) {
 370         ElementKind k = e.getKind();
 371         switch(k) {
 372         case CONSTRUCTOR:
 373             return visitExecutableAsConstructor(e, p);
 374 
 375         case INSTANCE_INIT:
 376             return visitExecutableAsInstanceInit(e, p);
 377 
 378         case METHOD:
 379             return visitExecutableAsMethod(e, p);
 380 
 381         case STATIC_INIT:
 382             return visitExecutableAsStaticInit(e, p);
 383 
 384         default:
 385             throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
 386         }
 387     }
 388 
 389     /**
 390      * Visits a {@code CONSTRUCTOR} executable element.
 391      *
 392      * @implSpec This implementation calls {@code defaultAction}.
 393      *
 394      * @param e the element to visit
 395      * @param p a visitor-specified parameter
 396      * @return  the result of {@code defaultAction}
 397      */
 398     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
 399         return defaultAction(e, p);
 400     }
 401 
 402     /**
 403      * Visits an {@code INSTANCE_INIT} executable element.
 404      *
 405      * @implSpec This implementation calls {@code defaultAction}.
 406      *
 407      * @param e the element to visit
 408      * @param p a visitor-specified parameter
 409      * @return  the result of {@code defaultAction}
 410      */
 411     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
 412         return defaultAction(e, p);
 413     }
 414 
 415     /**
 416      * Visits a {@code METHOD} executable element.
 417      *
 418      * @implSpec This implementation calls {@code defaultAction}.
 419      *
 420      * @param e the element to visit
 421      * @param p a visitor-specified parameter
 422      * @return  the result of {@code defaultAction}
 423      */
 424     public R visitExecutableAsMethod(ExecutableElement e, P p) {
 425         return defaultAction(e, p);
 426     }
 427 
 428     /**
 429      * Visits a {@code STATIC_INIT} executable element.
 430      *
 431      * @implSpec This implementation calls {@code defaultAction}.
 432      *
 433      * @param e the element to visit
 434      * @param p a visitor-specified parameter
 435      * @return  the result of {@code defaultAction}
 436      */
 437     public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
 438         return defaultAction(e, p);
 439     }
 440 
 441     /**
 442      * {@inheritDoc}
 443      *
 444      * The element argument has kind {@code TYPE_PARAMETER}.
 445      *
 446      * @implSpec This implementation calls {@code defaultAction}.
 447      *
 448      * @param e {@inheritDoc}
 449      * @param p {@inheritDoc}
 450      * @return  {@inheritDoc}
 451      */
 452     @Override
 453     public R visitTypeParameter(TypeParameterElement e, P p) {
 454         assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
 455         return defaultAction(e, p);
 456     }
 457 }