1 /*
   2  * Copyright (c) 2005, 2014, 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>XYZKind</i></code> method corresponding to the
  42  * first argument's kind.  The <code>visit<i>XYZKind</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  * <p>Note that adding a default implementation of a new visit method
  71  * in a visitor class will occur instead of adding a <em>default
  72  * method</em> directly in the visitor interface since a Java SE 8
  73  * language feature cannot be used to this version of the API since
  74  * this version is required to be runnable on Java SE 7
  75  * implementations.  Future versions of the API that are only required
  76  * to run on Java SE 8 and later may take advantage of default methods
  77  * in this situation.
  78  *
  79  * @param <R> the return type of this visitor's methods.  Use {@link
  80  *            Void} for visitors that do not need to return results.
  81  * @param <P> the type of the additional parameter to this visitor's
  82  *            methods.  Use {@code Void} for visitors that do not need an
  83  *            additional parameter.
  84  *
  85  * @author Joseph D. Darcy
  86  * @author Scott Seligman
  87  * @author Peter von der Ah&eacute;
  88  *
  89  * @see ElementKindVisitor7
  90  * @see ElementKindVisitor8
  91  * @see ElementKindVisitor9
  92  * @since 1.6
  93  * @deprecated Release 6 is obsolete; update to a visitor for a newer
  94  * release level.
  95  */
  96 @Deprecated
  97 @SupportedSourceVersion(RELEASE_6)
  98 public class ElementKindVisitor6<R, P>
  99                   extends SimpleElementVisitor6<R, P> {
 100     /**
 101      * Constructor for concrete subclasses; uses {@code null} for the
 102      * default value.
 103      */
 104     protected ElementKindVisitor6() {
 105         super(null);
 106     }
 107 
 108     /**
 109      * Constructor for concrete subclasses; uses the argument for the
 110      * default value.
 111      *
 112      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
 113      */
 114     protected ElementKindVisitor6(R defaultValue) {
 115         super(defaultValue);
 116     }
 117 
 118     /**
 119      * {@inheritDoc}
 120      *
 121      * The element argument has kind {@code PACKAGE}.
 122      *
 123      * @param e {@inheritDoc}
 124      * @param p {@inheritDoc}
 125      * @return  {@inheritDoc}
 126      */
 127     @Override
 128     public R visitPackage(PackageElement e, P p) {
 129         assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
 130         return defaultAction(e, p);
 131     }
 132 
 133     /**
 134      * Visits a type element, dispatching to the visit method for the
 135      * specific {@linkplain ElementKind kind} of type, {@code
 136      * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
 137      * INTERFACE}.
 138      *
 139      * @param e {@inheritDoc}
 140      * @param p {@inheritDoc}
 141      * @return  the result of the kind-specific visit method
 142      */
 143     @Override
 144     public R visitType(TypeElement e, P p) {
 145         ElementKind k = e.getKind();
 146         switch(k) {
 147         case ANNOTATION_TYPE:
 148             return visitTypeAsAnnotationType(e, p);
 149 
 150         case CLASS:
 151             return visitTypeAsClass(e, p);
 152 
 153         case ENUM:
 154             return visitTypeAsEnum(e, p);
 155 
 156         case INTERFACE:
 157             return visitTypeAsInterface(e, p);
 158 
 159         default:
 160             throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
 161         }
 162     }
 163 
 164     /**
 165      * Visits an {@code ANNOTATION_TYPE} type element by calling
 166      * {@code defaultAction}.
 167      *
 168      * @param e the element to visit
 169      * @param p a visitor-specified parameter
 170      * @return  the result of {@code defaultAction}
 171      */
 172     public R visitTypeAsAnnotationType(TypeElement e, P p) {
 173         return defaultAction(e, p);
 174     }
 175 
 176     /**
 177      * Visits a {@code CLASS} type element by calling {@code
 178      * 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 by calling {@code
 190      * defaultAction}.
 191      *
 192      * @param e the element to visit
 193      * @param p a visitor-specified parameter
 194      * @return  the result of {@code defaultAction}
 195      */
 196     public R visitTypeAsEnum(TypeElement e, P p) {
 197         return defaultAction(e, p);
 198     }
 199 
 200     /**
 201      * Visits an {@code INTERFACE} type element by calling {@code
 202      * defaultAction}.
 203      *.
 204      * @param e the element to visit
 205      * @param p a visitor-specified parameter
 206      * @return  the result of {@code defaultAction}
 207      */
 208     public R visitTypeAsInterface(TypeElement e, P p) {
 209         return defaultAction(e, p);
 210     }
 211 
 212     /**
 213      * Visits a variable element, dispatching to the visit method for
 214      * the specific {@linkplain ElementKind kind} of variable, {@code
 215      * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
 216      * {@code LOCAL_VARIABLE}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
 217      *
 218      * @param e {@inheritDoc}
 219      * @param p {@inheritDoc}
 220      * @return  the result of the kind-specific visit method
 221      */
 222     @Override
 223     public R visitVariable(VariableElement e, P p) {
 224         ElementKind k = e.getKind();
 225         switch(k) {
 226         case ENUM_CONSTANT:
 227             return visitVariableAsEnumConstant(e, p);
 228 
 229         case EXCEPTION_PARAMETER:
 230             return visitVariableAsExceptionParameter(e, p);
 231 
 232         case FIELD:
 233             return visitVariableAsField(e, p);
 234 
 235         case LOCAL_VARIABLE:
 236             return visitVariableAsLocalVariable(e, p);
 237 
 238         case PARAMETER:
 239             return visitVariableAsParameter(e, p);
 240 
 241         case RESOURCE_VARIABLE:
 242             return visitVariableAsResourceVariable(e, p);
 243 
 244         default:
 245             throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
 246         }
 247     }
 248 
 249     /**
 250      * Visits an {@code ENUM_CONSTANT} variable element by calling
 251      * {@code defaultAction}.
 252      *
 253      * @param e the element to visit
 254      * @param p a visitor-specified parameter
 255      * @return  the result of {@code defaultAction}
 256      */
 257     public R visitVariableAsEnumConstant(VariableElement e, P p) {
 258         return defaultAction(e, p);
 259     }
 260 
 261     /**
 262      * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
 263      * {@code defaultAction}.
 264      *
 265      * @param e the element to visit
 266      * @param p a visitor-specified parameter
 267      * @return  the result of {@code defaultAction}
 268      */
 269     public R visitVariableAsExceptionParameter(VariableElement e, P p) {
 270         return defaultAction(e, p);
 271     }
 272 
 273     /**
 274      * Visits a {@code FIELD} variable element by calling
 275      * {@code defaultAction}.
 276      *
 277      * @param e the element to visit
 278      * @param p a visitor-specified parameter
 279      * @return  the result of {@code defaultAction}
 280      */
 281     public R visitVariableAsField(VariableElement e, P p) {
 282         return defaultAction(e, p);
 283     }
 284 
 285     /**
 286      * Visits a {@code LOCAL_VARIABLE} variable element by calling
 287      * {@code defaultAction}.
 288      *
 289      * @param e the element to visit
 290      * @param p a visitor-specified parameter
 291      * @return  the result of {@code defaultAction}
 292      */
 293     public R visitVariableAsLocalVariable(VariableElement e, P p) {
 294         return defaultAction(e, p);
 295     }
 296 
 297     /**
 298      * Visits a {@code PARAMETER} variable element by calling
 299      * {@code defaultAction}.
 300      *
 301      * @param e the element to visit
 302      * @param p a visitor-specified parameter
 303      * @return  the result of {@code defaultAction}
 304      */
 305     public R visitVariableAsParameter(VariableElement e, P p) {
 306         return defaultAction(e, p);
 307     }
 308 
 309     /**
 310      * Visits a {@code RESOURCE_VARIABLE} variable element by calling
 311      * {@code visitUnknown}.
 312      *
 313      * @param e the element to visit
 314      * @param p a visitor-specified parameter
 315      * @return  the result of {@code visitUnknown}
 316      *
 317      * @since 1.7
 318      */
 319     public R visitVariableAsResourceVariable(VariableElement e, P p) {
 320         return visitUnknown(e, p);
 321     }
 322 
 323     /**
 324      * Visits an executable element, dispatching to the visit method
 325      * for the specific {@linkplain ElementKind kind} of executable,
 326      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
 327      * {@code STATIC_INIT}.
 328      *
 329      * @param e {@inheritDoc}
 330      * @param p {@inheritDoc}
 331      * @return  the result of the kind-specific visit method
 332      */
 333     @Override
 334     public R visitExecutable(ExecutableElement e, P p) {
 335         ElementKind k = e.getKind();
 336         switch(k) {
 337         case CONSTRUCTOR:
 338             return visitExecutableAsConstructor(e, p);
 339 
 340         case INSTANCE_INIT:
 341             return visitExecutableAsInstanceInit(e, p);
 342 
 343         case METHOD:
 344             return visitExecutableAsMethod(e, p);
 345 
 346         case STATIC_INIT:
 347             return visitExecutableAsStaticInit(e, p);
 348 
 349         default:
 350             throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
 351         }
 352     }
 353 
 354     /**
 355      * Visits a {@code CONSTRUCTOR} executable element by calling
 356      * {@code defaultAction}.
 357      *
 358      * @param e the element to visit
 359      * @param p a visitor-specified parameter
 360      * @return  the result of {@code defaultAction}
 361      */
 362     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
 363         return defaultAction(e, p);
 364     }
 365 
 366     /**
 367      * Visits an {@code INSTANCE_INIT} executable element by calling
 368      * {@code defaultAction}.
 369      *
 370      * @param e the element to visit
 371      * @param p a visitor-specified parameter
 372      * @return  the result of {@code defaultAction}
 373      */
 374     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
 375         return defaultAction(e, p);
 376     }
 377 
 378     /**
 379      * Visits a {@code METHOD} executable element by calling
 380      * {@code defaultAction}.
 381      *
 382      * @param e the element to visit
 383      * @param p a visitor-specified parameter
 384      * @return  the result of {@code defaultAction}
 385      */
 386     public R visitExecutableAsMethod(ExecutableElement e, P p) {
 387         return defaultAction(e, p);
 388     }
 389 
 390     /**
 391      * Visits a {@code STATIC_INIT} executable element by calling
 392      * {@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 visitExecutableAsStaticInit(ExecutableElement e, P p) {
 399         return defaultAction(e, p);
 400     }
 401 
 402 
 403     /**
 404      * {@inheritDoc}
 405      *
 406      * The element argument has kind {@code TYPE_PARAMETER}.
 407      *
 408      * @param e {@inheritDoc}
 409      * @param p {@inheritDoc}
 410      * @return  {@inheritDoc}
 411      */
 412     @Override
 413     public R visitTypeParameter(TypeParameterElement e, P p) {
 414         assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
 415         return defaultAction(e, p);
 416     }
 417 }