1 /*
   2  * Copyright (c) 2005, 2011, 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}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
 203      *
 204      * @param e {@inheritDoc}
 205      * @param p {@inheritDoc}
 206      * @return  the result of the kind-specific visit method
 207      */
 208     @Override
 209     public R visitVariable(VariableElement e, P p) {
 210         ElementKind k = e.getKind();
 211         switch(k) {
 212         case ENUM_CONSTANT:
 213             return visitVariableAsEnumConstant(e, p);
 214 
 215         case EXCEPTION_PARAMETER:
 216             return visitVariableAsExceptionParameter(e, p);
 217 
 218         case FIELD:
 219             return visitVariableAsField(e, p);
 220 
 221         case LOCAL_VARIABLE:
 222             return visitVariableAsLocalVariable(e, p);
 223 
 224         case PARAMETER:
 225             return visitVariableAsParameter(e, p);
 226 
 227         case RESOURCE_VARIABLE:
 228             return visitVariableAsResourceVariable(e, p);
 229 
 230         default:
 231             throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
 232         }
 233     }
 234 
 235     /**
 236      * Visits an {@code ENUM_CONSTANT} variable element by calling
 237      * {@code defaultAction}.
 238      *
 239      * @param e the element to visit
 240      * @param p a visitor-specified parameter
 241      * @return  the result of {@code defaultAction}
 242      */
 243     public R visitVariableAsEnumConstant(VariableElement e, P p) {
 244         return defaultAction(e, p);
 245     }
 246 
 247     /**
 248      * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
 249      * {@code defaultAction}.
 250      *
 251      * @param e the element to visit
 252      * @param p a visitor-specified parameter
 253      * @return  the result of {@code defaultAction}
 254      */
 255     public R visitVariableAsExceptionParameter(VariableElement e, P p) {
 256         return defaultAction(e, p);
 257     }
 258 
 259     /**
 260      * Visits a {@code FIELD} variable element by calling
 261      * {@code defaultAction}.
 262      *
 263      * @param e the element to visit
 264      * @param p a visitor-specified parameter
 265      * @return  the result of {@code defaultAction}
 266      */
 267     public R visitVariableAsField(VariableElement e, P p) {
 268         return defaultAction(e, p);
 269     }
 270 
 271     /**
 272      * Visits a {@code LOCAL_VARIABLE} variable element by calling
 273      * {@code defaultAction}.
 274      *
 275      * @param e the element to visit
 276      * @param p a visitor-specified parameter
 277      * @return  the result of {@code defaultAction}
 278      */
 279     public R visitVariableAsLocalVariable(VariableElement e, P p) {
 280         return defaultAction(e, p);
 281     }
 282 
 283     /**
 284      * Visits a {@code PARAMETER} variable element by calling
 285      * {@code defaultAction}.
 286      *
 287      * @param e the element to visit
 288      * @param p a visitor-specified parameter
 289      * @return  the result of {@code defaultAction}
 290      */
 291     public R visitVariableAsParameter(VariableElement e, P p) {
 292         return defaultAction(e, p);
 293     }
 294 
 295     /**
 296      * Visits a {@code RESOURCE_VARIABLE} variable element by calling
 297      * {@code visitUnknown}.
 298      *
 299      * @param e the element to visit
 300      * @param p a visitor-specified parameter
 301      * @return  the result of {@code visitUnknown}
 302      *
 303      * @since 1.7
 304      */
 305     public R visitVariableAsResourceVariable(VariableElement e, P p) {
 306         return visitUnknown(e, p);
 307     }
 308 
 309     /**
 310      * Visits an executable element, dispatching to the visit method
 311      * for the specific {@linkplain ElementKind kind} of executable,
 312      * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
 313      * {@code STATIC_INIT}.
 314      *
 315      * @param e {@inheritDoc}
 316      * @param p {@inheritDoc}
 317      * @return  the result of the kind-specific visit method
 318      */
 319     @Override
 320     public R visitExecutable(ExecutableElement e, P p) {
 321         ElementKind k = e.getKind();
 322         switch(k) {
 323         case CONSTRUCTOR:
 324             return visitExecutableAsConstructor(e, p);
 325 
 326         case INSTANCE_INIT:
 327             return visitExecutableAsInstanceInit(e, p);
 328 
 329         case METHOD:
 330             return visitExecutableAsMethod(e, p);
 331 
 332         case STATIC_INIT:
 333             return visitExecutableAsStaticInit(e, p);
 334 
 335         default:
 336             throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
 337         }
 338     }
 339 
 340     /**
 341      * Visits a {@code CONSTRUCTOR} executable element by calling
 342      * {@code defaultAction}.
 343      *
 344      * @param e the element to visit
 345      * @param p a visitor-specified parameter
 346      * @return  the result of {@code defaultAction}
 347      */
 348     public R visitExecutableAsConstructor(ExecutableElement e, P p) {
 349         return defaultAction(e, p);
 350     }
 351 
 352     /**
 353      * Visits an {@code INSTANCE_INIT} executable element by calling
 354      * {@code defaultAction}.
 355      *
 356      * @param e the element to visit
 357      * @param p a visitor-specified parameter
 358      * @return  the result of {@code defaultAction}
 359      */
 360     public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
 361         return defaultAction(e, p);
 362     }
 363 
 364     /**
 365      * Visits a {@code METHOD} executable element by calling
 366      * {@code defaultAction}.
 367      *
 368      * @param e the element to visit
 369      * @param p a visitor-specified parameter
 370      * @return  the result of {@code defaultAction}
 371      */
 372     public R visitExecutableAsMethod(ExecutableElement e, P p) {
 373         return defaultAction(e, p);
 374     }
 375 
 376     /**
 377      * Visits a {@code STATIC_INIT} executable element by calling
 378      * {@code defaultAction}.
 379      *
 380      * @param e the element to visit
 381      * @param p a visitor-specified parameter
 382      * @return  the result of {@code defaultAction}
 383      */
 384     public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
 385         return defaultAction(e, p);
 386     }
 387 
 388 
 389     /**
 390      * {@inheritDoc}
 391      *
 392      * The element argument has kind {@code TYPE_PARAMETER}.
 393      *
 394      * @param e {@inheritDoc}
 395      * @param p {@inheritDoc}
 396      * @return  {@inheritDoc}
 397      */
 398     @Override
 399     public R visitTypeParameter(TypeParameterElement e, P p) {
 400         assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
 401         return defaultAction(e, p);
 402     }
 403 }