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      * @implSpec This implementation calls {@code defaultAction}.
 118      *
 119      * @param e {@inheritDoc}
 120      * @param p {@inheritDoc}
 121      * @return  {@inheritDoc}
 122      */
 123     @Override
 124     public R visitPackage(PackageElement e, P p) {
 125         assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
 126         return defaultAction(e, p);
 127     }
 128 
 129     /**
 130      * {@inheritDoc}
 131      *
 132      * @implSpec This implementation dispatches to the visit method for the
 133      * specific {@linkplain ElementKind kind} of type, {@code
 134      * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
 135      * INTERFACE}.
 136      *
 137      * @param e {@inheritDoc}
 138      * @param p {@inheritDoc}
 139      * @return  the result of the kind-specific visit method
 140      */
 141     @Override
 142     public R visitType(TypeElement e, P p) {
 143         ElementKind k = e.getKind();
 144         switch(k) {
 145         case ANNOTATION_TYPE:
 146             return visitTypeAsAnnotationType(e, p);
 147 
 148         case CLASS:
 149             return visitTypeAsClass(e, p);
 150 
 151         case ENUM:
 152             return visitTypeAsEnum(e, p);
 153 
 154         case INTERFACE:
 155             return visitTypeAsInterface(e, p);
 156 
 157         default:
 158             throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
 159         }
 160     }
 161 
 162     /**
 163      * Visits an {@code ANNOTATION_TYPE} type element.
 164      *
 165      * @implSpec This implementation calls {@code defaultAction}.
 166      *
 167      * @param e the element to visit
 168      * @param p a visitor-specified parameter
 169      * @return  the result of {@code defaultAction}
 170      */
 171     public R visitTypeAsAnnotationType(TypeElement e, P p) {
 172         return defaultAction(e, p);
 173     }
 174 
 175     /**
 176      * Visits a {@code CLASS} type element.
 177      *
 178      * @implSpec This implementation calls {@code defaultAction}.
 179      *
 180      * @param e the element to visit
 181      * @param p a visitor-specified parameter
 182      * @return  the result of {@code defaultAction}
 183      */
 184     public R visitTypeAsClass(TypeElement e, P p) {
 185         return defaultAction(e, p);
 186     }
 187 
 188     /**
 189      * Visits an {@code ENUM} type element.
 190      *
 191      * @implSpec This implementation calls {@code defaultAction}.
 192      *
 193      * @param e the element to visit
 194      * @param p a visitor-specified parameter
 195      * @return  the result of {@code defaultAction}
 196      */
 197     public R visitTypeAsEnum(TypeElement e, P p) {
 198         return defaultAction(e, p);
 199     }
 200 
 201     /**
 202      * Visits an {@code INTERFACE} type element.
 203      *
 204      * @implSpec This implementation calls {@code defaultAction}.
 205      *.
 206      * @param e the element to visit
 207      * @param p a visitor-specified parameter
 208      * @return  the result of {@code defaultAction}
 209      */
 210     public R visitTypeAsInterface(TypeElement e, P p) {
 211         return defaultAction(e, p);
 212     }
 213 
 214     /**
 215      * Visits a variable element
 216      *
 217      * @implSpec This implementation dispatches to the visit method for
 218      * the specific {@linkplain ElementKind kind} of variable, {@code
 219      * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
 220      * {@code LOCAL_VARIABLE}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
 221      *
 222      * @param e {@inheritDoc}
 223      * @param p {@inheritDoc}
 224      * @return  the result of the kind-specific visit method
 225      */
 226     @Override
 227     public R visitVariable(VariableElement e, P p) {
 228         ElementKind k = e.getKind();
 229         switch(k) {
 230         case ENUM_CONSTANT:
 231             return visitVariableAsEnumConstant(e, p);
 232 
 233         case EXCEPTION_PARAMETER:
 234             return visitVariableAsExceptionParameter(e, p);
 235 
 236         case FIELD:
 237             return visitVariableAsField(e, p);
 238 
 239         case LOCAL_VARIABLE:
 240             return visitVariableAsLocalVariable(e, p);
 241 
 242         case PARAMETER:
 243             return visitVariableAsParameter(e, p);
 244 
 245         case RESOURCE_VARIABLE:
 246             return visitVariableAsResourceVariable(e, p);
 247 
 248         default:
 249             throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
 250         }
 251     }
 252 
 253     /**
 254      * Visits an {@code ENUM_CONSTANT} variable element.
 255      *
 256      * @implSpec This implementation calls {@code defaultAction}.
 257      *.
 258      * @param e the element to visit
 259      * @param p a visitor-specified parameter
 260      * @return  the result of {@code defaultAction}
 261      */
 262     public R visitVariableAsEnumConstant(VariableElement e, P p) {
 263         return defaultAction(e, p);
 264     }
 265 
 266     /**
 267      * Visits an {@code EXCEPTION_PARAMETER} variable element.
 268      *
 269      * @implSpec This implementation calls {@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 visitVariableAsExceptionParameter(VariableElement e, P p) {
 276         return defaultAction(e, p);
 277     }
 278 
 279     /**
 280      * Visits a {@code FIELD} variable element.
 281      *
 282      * @implSpec This implementation calls {@code defaultAction}.
 283      *.
 284      * @param e the element to visit
 285      * @param p a visitor-specified parameter
 286      * @return  the result of {@code defaultAction}
 287      */
 288     public R visitVariableAsField(VariableElement e, P p) {
 289         return defaultAction(e, p);
 290     }
 291 
 292     /**
 293      * Visits a {@code LOCAL_VARIABLE} variable element.
 294      *
 295      * @implSpec This implementation calls {@code defaultAction}.
 296      *
 297      * @param e the element to visit
 298      * @param p a visitor-specified parameter
 299      * @return  the result of {@code defaultAction}
 300      */
 301     public R visitVariableAsLocalVariable(VariableElement e, P p) {
 302         return defaultAction(e, p);
 303     }
 304 
 305     /**
 306      * Visits a {@code PARAMETER} variable element.
 307      *
 308      * @implSpec This implementation calls {@code defaultAction}.
 309      *
 310      * @param e the element to visit
 311      * @param p a visitor-specified parameter
 312      * @return  the result of {@code defaultAction}
 313      */
 314     public R visitVariableAsParameter(VariableElement e, P p) {
 315         return defaultAction(e, p);
 316     }
 317 
 318     /**
 319      * Visits a {@code RESOURCE_VARIABLE} variable element.
 320      *
 321      * @implSpec This implementation calls {@code visitUnknown}.
 322      *
 323      * @param e the element to visit
 324      * @param p a visitor-specified parameter
 325      * @return  the result of {@code visitUnknown}
 326      *
 327      * @since 1.7
 328      */
 329     public R visitVariableAsResourceVariable(VariableElement e, P p) {
 330         return visitUnknown(e, p);
 331     }
 332 
 333     /**
 334      * {@inheritDoc}
 335      *
 336      * @implSpec This implementation dispatches to the visit method
 337      * for the specific {@linkplain ElementKind kind} of executable,
 338      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
 339      * {@code STATIC_INIT}.
 340      *
 341      * @param e {@inheritDoc}
 342      * @param p {@inheritDoc}
 343      * @return  the result of the kind-specific visit method
 344      */
 345     @Override
 346     public R visitExecutable(ExecutableElement e, P p) {
 347         ElementKind k = e.getKind();
 348         switch(k) {
 349         case CONSTRUCTOR:
 350             return visitExecutableAsConstructor(e, p);
 351 
 352         case INSTANCE_INIT:
 353             return visitExecutableAsInstanceInit(e, p);
 354 
 355         case METHOD:
 356             return visitExecutableAsMethod(e, p);
 357 
 358         case STATIC_INIT:
 359             return visitExecutableAsStaticInit(e, p);
 360 
 361         default:
 362             throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
 363         }
 364     }
 365 
 366     /**
 367      * Visits a {@code CONSTRUCTOR} executable element.
 368      *
 369      * @implSpec This implementation calls {@code defaultAction}.
 370      *
 371      * @param e the element to visit
 372      * @param p a visitor-specified parameter
 373      * @return  the result of {@code defaultAction}
 374      */
 375     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
 376         return defaultAction(e, p);
 377     }
 378 
 379     /**
 380      * Visits an {@code INSTANCE_INIT} executable element.
 381      *
 382      * @implSpec This implementation calls {@code defaultAction}.
 383      *
 384      * @param e the element to visit
 385      * @param p a visitor-specified parameter
 386      * @return  the result of {@code defaultAction}
 387      */
 388     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
 389         return defaultAction(e, p);
 390     }
 391 
 392     /**
 393      * Visits a {@code METHOD} executable element.
 394      *
 395      * @implSpec This implementation calls {@code defaultAction}.
 396      *
 397      * @param e the element to visit
 398      * @param p a visitor-specified parameter
 399      * @return  the result of {@code defaultAction}
 400      */
 401     public R visitExecutableAsMethod(ExecutableElement e, P p) {
 402         return defaultAction(e, p);
 403     }
 404 
 405     /**
 406      * Visits a {@code STATIC_INIT} executable element.
 407      *
 408      * @implSpec This implementation calls {@code defaultAction}.
 409      *
 410      * @param e the element to visit
 411      * @param p a visitor-specified parameter
 412      * @return  the result of {@code defaultAction}
 413      */
 414     public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
 415         return defaultAction(e, p);
 416     }
 417 
 418     /**
 419      * {@inheritDoc}
 420      *
 421      * The element argument has kind {@code TYPE_PARAMETER}.
 422      *
 423      * @implSpec This implementation calls {@code defaultAction}.
 424      *
 425      * @param e {@inheritDoc}
 426      * @param p {@inheritDoc}
 427      * @return  {@inheritDoc}
 428      */
 429     @Override
 430     public R visitTypeParameter(TypeParameterElement e, P p) {
 431         assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
 432         return defaultAction(e, p);
 433     }
 434 }