src/share/classes/javax/lang/model/util/ElementKindVisitor6.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/javax/lang/model/util/ElementKindVisitor6.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,384 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package javax.lang.model.util;
    1.30 +
    1.31 +import javax.lang.model.element.*;
    1.32 +import static javax.lang.model.element.ElementKind.*;
    1.33 +import javax.annotation.processing.SupportedSourceVersion;
    1.34 +import static javax.lang.model.SourceVersion.*;
    1.35 +import javax.lang.model.SourceVersion;
    1.36 +
    1.37 +
    1.38 +/**
    1.39 + * A visitor of program elements based on their {@linkplain
    1.40 + * ElementKind kind} with default behavior appropriate for the {@link
    1.41 + * SourceVersion#RELEASE_6 RELEASE_6} source version.  For {@linkplain
    1.42 + * Element elements} <tt><i>XYZ</i></tt> that may have more than one
    1.43 + * kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate
    1.44 + * to the <tt>visit<i>XYZKind</i></tt> method corresponding to the
    1.45 + * first argument's kind.  The <tt>visit<i>XYZKind</i></tt> methods
    1.46 + * call {@link #defaultAction defaultAction}, passing their arguments
    1.47 + * to {@code defaultAction}'s corresponding parameters.
    1.48 + *
    1.49 + * <p> Methods in this class may be overridden subject to their
    1.50 + * general contract.  Note that annotating methods in concrete
    1.51 + * subclasses with {@link java.lang.Override @Override} will help
    1.52 + * ensure that methods are overridden as intended.
    1.53 + *
    1.54 + * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
    1.55 + * implemented by this class may have methods added to it or the
    1.56 + * {@code ElementKind} {@code enum} used in this case may have
    1.57 + * constants added to it in the future to accommodate new, currently
    1.58 + * unknown, language structures added to future versions of the
    1.59 + * Java&trade; programming language.  Therefore, methods whose names
    1.60 + * begin with {@code "visit"} may be added to this class in the
    1.61 + * future; to avoid incompatibilities, classes which extend this class
    1.62 + * should not declare any instance methods with names beginning with
    1.63 + * {@code "visit"}.
    1.64 + *
    1.65 + * <p>When such a new visit method is added, the default
    1.66 + * implementation in this class will be to call the {@link
    1.67 + * #visitUnknown visitUnknown} method.  A new abstract element kind
    1.68 + * visitor class will also be introduced to correspond to the new
    1.69 + * language level; this visitor will have different default behavior
    1.70 + * for the visit method in question.  When the new visitor is
    1.71 + * introduced, all or portions of this visitor may be deprecated.
    1.72 + *
    1.73 + * @param <R> the return type of this visitor's methods.  Use {@link
    1.74 + *            Void} for visitors that do not need to return results.
    1.75 + * @param <P> the type of the additional parameter to this visitor's
    1.76 + *            methods.  Use {@code Void} for visitors that do not need an
    1.77 + *            additional parameter.
    1.78 + *
    1.79 + * @author Joseph D. Darcy
    1.80 + * @author Scott Seligman
    1.81 + * @author Peter von der Ah&eacute;
    1.82 + * @since 1.6
    1.83 + */
    1.84 +@SupportedSourceVersion(RELEASE_6)
    1.85 +public class ElementKindVisitor6<R, P>
    1.86 +                  extends SimpleElementVisitor6<R, P> {
    1.87 +    /**
    1.88 +     * Constructor for concrete subclasses; uses {@code null} for the
    1.89 +     * default value.
    1.90 +     */
    1.91 +    protected ElementKindVisitor6() {
    1.92 +        super(null);
    1.93 +    }
    1.94 +
    1.95 +    /**
    1.96 +     * Constructor for concrete subclasses; uses the argument for the
    1.97 +     * default value.
    1.98 +     *
    1.99 +     * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
   1.100 +     */
   1.101 +    protected ElementKindVisitor6(R defaultValue) {
   1.102 +        super(defaultValue);
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * {@inheritDoc}
   1.107 +     *
   1.108 +     * The element argument has kind {@code PACKAGE}.
   1.109 +     *
   1.110 +     * @param e {@inheritDoc}
   1.111 +     * @param p {@inheritDoc}
   1.112 +     * @return  {@inheritDoc}
   1.113 +     */
   1.114 +    @Override
   1.115 +    public R visitPackage(PackageElement e, P p) {
   1.116 +        assert e.getKind() == PACKAGE: "Bad kind on PackageElement";
   1.117 +        return defaultAction(e, p);
   1.118 +    }
   1.119 +
   1.120 +    /**
   1.121 +     * Visits a type element, dispatching to the visit method for the
   1.122 +     * specific {@linkplain ElementKind kind} of type, {@code
   1.123 +     * ANNOTATION_TYPE}, {@code CLASS}, {@code ENUM}, or {@code
   1.124 +     * INTERFACE}.
   1.125 +     *
   1.126 +     * @param e {@inheritDoc}
   1.127 +     * @param p {@inheritDoc}
   1.128 +     * @return  the result of the kind-specific visit method
   1.129 +     */
   1.130 +    @Override
   1.131 +    public R visitType(TypeElement e, P p) {
   1.132 +        ElementKind k = e.getKind();
   1.133 +        switch(k) {
   1.134 +        case ANNOTATION_TYPE:
   1.135 +            return visitTypeAsAnnotationType(e, p);
   1.136 +
   1.137 +        case CLASS:
   1.138 +            return visitTypeAsClass(e, p);
   1.139 +
   1.140 +        case ENUM:
   1.141 +            return visitTypeAsEnum(e, p);
   1.142 +
   1.143 +        case INTERFACE:
   1.144 +            return visitTypeAsInterface(e, p);
   1.145 +
   1.146 +        default:
   1.147 +            throw new AssertionError("Bad kind " + k + " for TypeElement" + e);
   1.148 +        }
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Visits an {@code ANNOTATION_TYPE} type element by calling
   1.153 +     * {@code defaultAction}.
   1.154 +     *
   1.155 +     * @param e the element to visit
   1.156 +     * @param p a visitor-specified parameter
   1.157 +     * @return  the result of {@code defaultAction}
   1.158 +     */
   1.159 +    public R visitTypeAsAnnotationType(TypeElement e, P p) {
   1.160 +        return defaultAction(e, p);
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * Visits a {@code CLASS} type element by calling {@code
   1.165 +     * defaultAction}.
   1.166 +     *
   1.167 +     * @param e the element to visit
   1.168 +     * @param p a visitor-specified parameter
   1.169 +     * @return  the result of {@code defaultAction}
   1.170 +     */
   1.171 +    public R visitTypeAsClass(TypeElement e, P p) {
   1.172 +        return defaultAction(e, p);
   1.173 +    }
   1.174 +
   1.175 +    /**
   1.176 +     * Visits an {@code ENUM} type element by calling {@code
   1.177 +     * defaultAction}.
   1.178 +     *
   1.179 +     * @param e the element to visit
   1.180 +     * @param p a visitor-specified parameter
   1.181 +     * @return  the result of {@code defaultAction}
   1.182 +     */
   1.183 +    public R visitTypeAsEnum(TypeElement e, P p) {
   1.184 +        return defaultAction(e, p);
   1.185 +    }
   1.186 +
   1.187 +    /**
   1.188 +     * Visits an {@code INTERFACE} type element by calling {@code
   1.189 +     * defaultAction}.
   1.190 +     *.
   1.191 +     * @param e the element to visit
   1.192 +     * @param p a visitor-specified parameter
   1.193 +     * @return  the result of {@code defaultAction}
   1.194 +     */
   1.195 +    public R visitTypeAsInterface(TypeElement e, P p) {
   1.196 +        return defaultAction(e, p);
   1.197 +    }
   1.198 +
   1.199 +    /**
   1.200 +     * Visits a variable element, dispatching to the visit method for
   1.201 +     * the specific {@linkplain ElementKind kind} of variable, {@code
   1.202 +     * ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
   1.203 +     * {@code LOCAL_VARIABLE}, or {@code PARAMETER}.
   1.204 +     * @param e {@inheritDoc}
   1.205 +     * @param p {@inheritDoc}
   1.206 +     * @return  the result of the kind-specific visit method
   1.207 +     */
   1.208 +    @Override
   1.209 +    public R visitVariable(VariableElement e, P p) {
   1.210 +        ElementKind k = e.getKind();
   1.211 +        switch(k) {
   1.212 +        case ENUM_CONSTANT:
   1.213 +            return visitVariableAsEnumConstant(e, p);
   1.214 +
   1.215 +        case EXCEPTION_PARAMETER:
   1.216 +            return visitVariableAsExceptionParameter(e, p);
   1.217 +
   1.218 +        case FIELD:
   1.219 +            return visitVariableAsField(e, p);
   1.220 +
   1.221 +        case LOCAL_VARIABLE:
   1.222 +            return visitVariableAsLocalVariable(e, p);
   1.223 +
   1.224 +        case PARAMETER:
   1.225 +            return visitVariableAsParameter(e, p);
   1.226 +
   1.227 +        default:
   1.228 +            throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
   1.229 +        }
   1.230 +
   1.231 +    }
   1.232 +
   1.233 +    /**
   1.234 +     * Visits an {@code ENUM_CONSTANT} variable element by calling
   1.235 +     * {@code defaultAction}.
   1.236 +     *
   1.237 +     * @param e the element to visit
   1.238 +     * @param p a visitor-specified parameter
   1.239 +     * @return  the result of {@code defaultAction}
   1.240 +     */
   1.241 +    public R visitVariableAsEnumConstant(VariableElement e, P p) {
   1.242 +        return defaultAction(e, p);
   1.243 +    }
   1.244 +
   1.245 +    /**
   1.246 +     * Visits an {@code EXCEPTION_PARAMETER} variable element by calling
   1.247 +     * {@code defaultAction}.
   1.248 +     *
   1.249 +     * @param e the element to visit
   1.250 +     * @param p a visitor-specified parameter
   1.251 +     * @return  the result of {@code defaultAction}
   1.252 +     */
   1.253 +    public R visitVariableAsExceptionParameter(VariableElement e, P p) {
   1.254 +        return defaultAction(e, p);
   1.255 +    }
   1.256 +
   1.257 +    /**
   1.258 +     * Visits a {@code FIELD} variable element by calling
   1.259 +     * {@code defaultAction}.
   1.260 +     *
   1.261 +     * @param e the element to visit
   1.262 +     * @param p a visitor-specified parameter
   1.263 +     * @return  the result of {@code defaultAction}
   1.264 +     */
   1.265 +    public R visitVariableAsField(VariableElement e, P p) {
   1.266 +        return defaultAction(e, p);
   1.267 +    }
   1.268 +
   1.269 +    /**
   1.270 +     * Visits a {@code LOCAL_VARIABLE} variable element by calling
   1.271 +     * {@code defaultAction}.
   1.272 +     *
   1.273 +     * @param e the element to visit
   1.274 +     * @param p a visitor-specified parameter
   1.275 +     * @return  the result of {@code defaultAction}
   1.276 +     */
   1.277 +    public R visitVariableAsLocalVariable(VariableElement e, P p) {
   1.278 +        return defaultAction(e, p);
   1.279 +    }
   1.280 +
   1.281 +    /**
   1.282 +     * Visits a {@code PARAMETER} variable element by calling
   1.283 +     * {@code defaultAction}.
   1.284 +     *
   1.285 +     * @param e the element to visit
   1.286 +     * @param p a visitor-specified parameter
   1.287 +     * @return  the result of {@code defaultAction}
   1.288 +     */
   1.289 +    public R visitVariableAsParameter(VariableElement e, P p) {
   1.290 +        return defaultAction(e, p);
   1.291 +    }
   1.292 +
   1.293 +    /**
   1.294 +     * Visits an executable element, dispatching to the visit method
   1.295 +     * for the specific {@linkplain ElementKind kind} of executable,
   1.296 +     * {@code CONSTRUCTOR}, {@code INSTANCE_INIT}, {@code METHOD}, or
   1.297 +     * {@code STATIC_INIT}.
   1.298 +     *
   1.299 +     * @param e {@inheritDoc}
   1.300 +     * @param p {@inheritDoc}
   1.301 +     * @return  the result of the kind-specific visit method
   1.302 +     */
   1.303 +    @Override
   1.304 +    public R visitExecutable(ExecutableElement e, P p) {
   1.305 +        ElementKind k = e.getKind();
   1.306 +        switch(k) {
   1.307 +        case CONSTRUCTOR:
   1.308 +            return visitExecutableAsConstructor(e, p);
   1.309 +
   1.310 +        case INSTANCE_INIT:
   1.311 +            return visitExecutableAsInstanceInit(e, p);
   1.312 +
   1.313 +        case METHOD:
   1.314 +            return visitExecutableAsMethod(e, p);
   1.315 +
   1.316 +        case STATIC_INIT:
   1.317 +            return visitExecutableAsStaticInit(e, p);
   1.318 +
   1.319 +        default:
   1.320 +            throw new AssertionError("Bad kind " + k + " for ExecutableElement" + e);
   1.321 +        }
   1.322 +    }
   1.323 +
   1.324 +    /**
   1.325 +     * Visits a {@code CONSTRUCTOR} executable element by calling
   1.326 +     * {@code defaultAction}.
   1.327 +     *
   1.328 +     * @param e the element to visit
   1.329 +     * @param p a visitor-specified parameter
   1.330 +     * @return  the result of {@code defaultAction}
   1.331 +     */
   1.332 +    public R visitExecutableAsConstructor(ExecutableElement e, P p) {
   1.333 +        return defaultAction(e, p);
   1.334 +    }
   1.335 +
   1.336 +    /**
   1.337 +     * Visits an {@code INSTANCE_INIT} executable element by calling
   1.338 +     * {@code defaultAction}.
   1.339 +     *
   1.340 +     * @param e the element to visit
   1.341 +     * @param p a visitor-specified parameter
   1.342 +     * @return  the result of {@code defaultAction}
   1.343 +     */
   1.344 +    public R visitExecutableAsInstanceInit(ExecutableElement e, P p) {
   1.345 +        return defaultAction(e, p);
   1.346 +    }
   1.347 +
   1.348 +    /**
   1.349 +     * Visits a {@code METHOD} executable element by calling
   1.350 +     * {@code defaultAction}.
   1.351 +     *
   1.352 +     * @param e the element to visit
   1.353 +     * @param p a visitor-specified parameter
   1.354 +     * @return  the result of {@code defaultAction}
   1.355 +     */
   1.356 +    public R visitExecutableAsMethod(ExecutableElement e, P p) {
   1.357 +        return defaultAction(e, p);
   1.358 +    }
   1.359 +
   1.360 +    /**
   1.361 +     * Visits a {@code STATIC_INIT} executable element by calling
   1.362 +     * {@code defaultAction}.
   1.363 +     *
   1.364 +     * @param e the element to visit
   1.365 +     * @param p a visitor-specified parameter
   1.366 +     * @return  the result of {@code defaultAction}
   1.367 +     */
   1.368 +    public R visitExecutableAsStaticInit(ExecutableElement e, P p) {
   1.369 +        return defaultAction(e, p);
   1.370 +    }
   1.371 +
   1.372 +
   1.373 +    /**
   1.374 +     * {@inheritDoc}
   1.375 +     *
   1.376 +     * The element argument has kind {@code TYPE_PARAMETER}.
   1.377 +     *
   1.378 +     * @param e {@inheritDoc}
   1.379 +     * @param p {@inheritDoc}
   1.380 +     * @return  {@inheritDoc}
   1.381 +     */
   1.382 +    @Override
   1.383 +    public R visitTypeParameter(TypeParameterElement e, P p) {
   1.384 +        assert e.getKind() == TYPE_PARAMETER: "Bad kind on TypeParameterElement";
   1.385 +        return defaultAction(e, p);
   1.386 +    }
   1.387 +}

mercurial