1 /*
   2  * Copyright (c) 2005, 2010, 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} <tt><i>XYZ</i></tt> that may have more than one
  40  * kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate
  41  * to the <tt>visit<i>XYZKind</i></tt> method corresponding to the
  42  * first argument's kind.  The <tt>visit<i>XYZKind</i></tt> 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  * @since 1.6
  82  */
  83 @SupportedSourceVersion(RELEASE_6)
  84 public class ElementKindVisitor6<R, P>
  85                   extends SimpleElementVisitor6<R, P> {
  86     /**
  87      * Constructor for concrete subclasses; uses {@code null} for the
  88      * default value.
  89      */
  90     protected ElementKindVisitor6() {
  91         super(null);
  92     }
  93 
  94     /**
  95      * Constructor for concrete subclasses; uses the argument for the
  96      * default value.
  97      *
  98      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
  99      */
 100     protected ElementKindVisitor6(R defaultValue) {
 101         super(defaultValue);
 102     }
 103 
 104     /**
 105      * {@inheritDoc}
 106      *
 107      * The element argument has kind {@code PACKAGE}.
 108      *
 109      * @param e {@inheritDoc}
 110      * @param p {@inheritDoc}
 111      * @return  {@inheritDoc}
 112      */
 113     @Override
 114     public R visitPackage(PackageElement e, P p) {
 115         assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
 116         return defaultAction(e, p);
 117     }
 118 
 119     /**
 120      * Visits a type element, dispatching to the visit method for the
 121      * specific {@linkplain ElementKind kind} of type, {@code
 122      * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
 123      * INTERFACE}.
 124      *
 125      * @param e {@inheritDoc}
 126      * @param p {@inheritDoc}
 127      * @return  the result of the kind-specific visit method
 128      */
 129     @Override
 130     public R visitType(TypeElement e, P p) {
 131         ElementKind k = e.getKind();
 132         switch(k) {
 133         case ANNOTATION_TYPE:
 134             return visitTypeAsAnnotationType(e, p);
 135 
 136         case CLASS:
 137             return visitTypeAsClass(e, p);
 138 
 139         case ENUM:
 140             return visitTypeAsEnum(e, p);
 141 
 142         case INTERFACE:
 143             return visitTypeAsInterface(e, p);
 144 
 145         default:
 146             throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
 147         }
 148     }
 149 
 150     /**
 151      * Visits an {@code ANNOTATION_TYPE} type element by calling
 152      * {@code defaultAction}.
 153      *
 154      * @param e the element to visit
 155      * @param p a visitor-specified parameter
 156      * @return  the result of {@code defaultAction}
 157      */
 158     public R visitTypeAsAnnotationType(TypeElement e, P p) {
 159         return defaultAction(e, p);
 160     }
 161 
 162     /**
 163      * Visits a {@code CLASS} type element by calling {@code
 164      * defaultAction}.
 165      *
 166      * @param e the element to visit
 167      * @param p a visitor-specified parameter
 168      * @return  the result of {@code defaultAction}
 169      */
 170     public R visitTypeAsClass(TypeElement e, P p) {
 171         return defaultAction(e, p);
 172     }
 173 
 174     /**
 175      * Visits an {@code ENUM} type element by calling {@code
 176      * defaultAction}.
 177      *
 178      * @param e the element to visit
 179      * @param p a visitor-specified parameter
 180      * @return  the result of {@code defaultAction}
 181      */
 182     public R visitTypeAsEnum(TypeElement e, P p) {
 183         return defaultAction(e, p);
 184     }
 185 
 186     /**
 187      * Visits an {@code INTERFACE} type element by calling {@code
 188      * defaultAction}.
 189      *.
 190      * @param e the element to visit
 191      * @param p a visitor-specified parameter
 192      * @return  the result of {@code defaultAction}
 193      */
 194     public R visitTypeAsInterface(TypeElement e, P p) {
 195         return defaultAction(e, p);
 196     }
 197 
 198     /**
 199      * Visits a variable element, dispatching to the visit method for
 200      * the specific {@linkplain ElementKind kind} of variable, {@code
 201      * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
 202      * {@code LOCAL_VARIABLE}, or {@code PARAMETER}.
 203      * @param e {@inheritDoc}
 204      * @param p {@inheritDoc}
 205      * @return  the result of the kind-specific visit method
 206      */
 207     @Override
 208     public R visitVariable(VariableElement e, P p) {
 209         ElementKind k = e.getKind();
 210         switch(k) {
 211         case ENUM_CONSTANT:
 212             return visitVariableAsEnumConstant(e, p);
 213 
 214         case EXCEPTION_PARAMETER:
 215             return visitVariableAsExceptionParameter(e, p);
 216 
 217         case FIELD:
 218             return visitVariableAsField(e, p);
 219 
 220         case LOCAL_VARIABLE:
 221             return visitVariableAsLocalVariable(e, p);
 222 
 223         case PARAMETER:
 224             return visitVariableAsParameter(e, p);
 225 
 226         default:
 227             throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
 228         }
 229 
 230     }
 231 
 232     /**
 233      * Visits an {@code ENUM_CONSTANT} variable element by calling
 234      * {@code defaultAction}.
 235      *
 236      * @param e the element to visit
 237      * @param p a visitor-specified parameter
 238      * @return  the result of {@code defaultAction}
 239      */
 240     public R visitVariableAsEnumConstant(VariableElement e, P p) {
 241         return defaultAction(e, p);
 242     }
 243 
 244     /**
 245      * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
 246      * {@code defaultAction}.
 247      *
 248      * @param e the element to visit
 249      * @param p a visitor-specified parameter
 250      * @return  the result of {@code defaultAction}
 251      */
 252     public R visitVariableAsExceptionParameter(VariableElement e, P p) {
 253         return defaultAction(e, p);
 254     }
 255 
 256     /**
 257      * Visits a {@code FIELD} variable element by calling
 258      * {@code defaultAction}.
 259      *
 260      * @param e the element to visit
 261      * @param p a visitor-specified parameter
 262      * @return  the result of {@code defaultAction}
 263      */
 264     public R visitVariableAsField(VariableElement e, P p) {
 265         return defaultAction(e, p);
 266     }
 267 
 268     /**
 269      * Visits a {@code LOCAL_VARIABLE} variable element by calling
 270      * {@code defaultAction}.
 271      *
 272      * @param e the element to visit
 273      * @param p a visitor-specified parameter
 274      * @return  the result of {@code defaultAction}
 275      */
 276     public R visitVariableAsLocalVariable(VariableElement e, P p) {
 277         return defaultAction(e, p);
 278     }
 279 
 280     /**
 281      * Visits a {@code PARAMETER} variable element by calling
 282      * {@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 visitVariableAsParameter(VariableElement e, P p) {
 289         return defaultAction(e, p);
 290     }
 291 
 292     /**
 293      * Visits an executable element, dispatching to the visit method
 294      * for the specific {@linkplain ElementKind kind} of executable,
 295      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
 296      * {@code STATIC_INIT}.
 297      *
 298      * @param e {@inheritDoc}
 299      * @param p {@inheritDoc}
 300      * @return  the result of the kind-specific visit method
 301      */
 302     @Override
 303     public R visitExecutable(ExecutableElement e, P p) {
 304         ElementKind k = e.getKind();
 305         switch(k) {
 306         case CONSTRUCTOR:
 307             return visitExecutableAsConstructor(e, p);
 308 
 309         case INSTANCE_INIT:
 310             return visitExecutableAsInstanceInit(e, p);
 311 
 312         case METHOD:
 313             return visitExecutableAsMethod(e, p);
 314 
 315         case STATIC_INIT:
 316             return visitExecutableAsStaticInit(e, p);
 317 
 318         default:
 319             throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
 320         }
 321     }
 322 
 323     /**
 324      * Visits a {@code CONSTRUCTOR} executable element by calling
 325      * {@code defaultAction}.
 326      *
 327      * @param e the element to visit
 328      * @param p a visitor-specified parameter
 329      * @return  the result of {@code defaultAction}
 330      */
 331     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
 332         return defaultAction(e, p);
 333     }
 334 
 335     /**
 336      * Visits an {@code INSTANCE_INIT} executable element by calling
 337      * {@code defaultAction}.
 338      *
 339      * @param e the element to visit
 340      * @param p a visitor-specified parameter
 341      * @return  the result of {@code defaultAction}
 342      */
 343     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
 344         return defaultAction(e, p);
 345     }
 346 
 347     /**
 348      * Visits a {@code METHOD} executable element by calling
 349      * {@code defaultAction}.
 350      *
 351      * @param e the element to visit
 352      * @param p a visitor-specified parameter
 353      * @return  the result of {@code defaultAction}
 354      */
 355     public R visitExecutableAsMethod(ExecutableElement e, P p) {
 356         return defaultAction(e, p);
 357     }
 358 
 359     /**
 360      * Visits a {@code STATIC_INIT} executable element by calling
 361      * {@code defaultAction}.
 362      *
 363      * @param e the element to visit
 364      * @param p a visitor-specified parameter
 365      * @return  the result of {@code defaultAction}
 366      */
 367     public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
 368         return defaultAction(e, p);
 369     }
 370 
 371 
 372     /**
 373      * {@inheritDoc}
 374      *
 375      * The element argument has kind {@code TYPE_PARAMETER}.
 376      *
 377      * @param e {@inheritDoc}
 378      * @param p {@inheritDoc}
 379      * @return  {@inheritDoc}
 380      */
 381     @Override
 382     public R visitTypeParameter(TypeParameterElement e, P p) {
 383         assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
 384         return defaultAction(e, p);
 385     }
 386 }