0N/A/*
850N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage javax.lang.model.util;
0N/A
0N/Aimport javax.lang.model.element.*;
0N/Aimport javax.annotation.processing.SupportedSourceVersion;
0N/Aimport static javax.lang.model.element.ElementKind.*;
0N/Aimport javax.lang.model.SourceVersion;
0N/Aimport static javax.lang.model.SourceVersion.*;
0N/A
0N/A
0N/A/**
0N/A * A scanning visitor of program elements with default behavior
0N/A * appropriate for the {@link SourceVersion#RELEASE_6 RELEASE_6}
0N/A * source version. The <tt>visit<i>XYZ</i></tt> methods in this
0N/A * class scan their component elements by calling {@code scan} on
0N/A * their {@linkplain Element#getEnclosedElements enclosed elements},
0N/A * {@linkplain ExecutableElement#getParameters parameters}, etc., as
0N/A * indicated in the individual method specifications. A subclass can
0N/A * control the order elements are visited by overriding the
0N/A * <tt>visit<i>XYZ</i></tt> methods. Note that clients of a scanner
0N/A * may get the desired behavior be invoking {@code v.scan(e, p)} rather
0N/A * than {@code v.visit(e, p)} on the root objects of interest.
0N/A *
0N/A * <p>When a subclass overrides a <tt>visit<i>XYZ</i></tt> method, the
0N/A * new method can cause the enclosed elements to be scanned in the
0N/A * default way by calling <tt>super.visit<i>XYZ</i></tt>. In this
0N/A * fashion, the concrete visitor can control the ordering of traversal
0N/A * over the component elements with respect to the additional
0N/A * processing; for example, consistently calling
0N/A * <tt>super.visit<i>XYZ</i></tt> at the start of the overridden
0N/A * methods will yield a preorder traversal, etc. If the component
0N/A * elements should be traversed in some other order, instead of
0N/A * calling <tt>super.visit<i>XYZ</i></tt>, an overriding visit method
0N/A * should call {@code scan} with the elements in the desired order.
0N/A *
0N/A * <p> Methods in this class may be overridden subject to their
0N/A * general contract. Note that annotating methods in concrete
0N/A * subclasses with {@link java.lang.Override @Override} will help
0N/A * ensure that methods are overridden as intended.
0N/A *
0N/A * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
0N/A * implemented by this class may have methods added to it in the
0N/A * future to accommodate new, currently unknown, language structures
0N/A * added to future versions of the Java&trade; programming language.
0N/A * Therefore, methods whose names begin with {@code "visit"} may be
0N/A * added to this class in the future; to avoid incompatibilities,
0N/A * classes which extend this class should not declare any instance
0N/A * methods with names beginning with {@code "visit"}.
0N/A *
0N/A * <p>When such a new visit method is added, the default
0N/A * implementation in this class will be to call the {@link
0N/A * #visitUnknown visitUnknown} method. A new element scanner visitor
0N/A * class will also be introduced to correspond to the new language
0N/A * level; this visitor will have different default behavior for the
0N/A * visit method in question. When the new visitor is introduced, all
0N/A * or portions of this visitor may be deprecated.
0N/A *
0N/A * @param <R> the return type of this visitor's methods. Use {@link
0N/A * Void} for visitors that do not need to return results.
0N/A * @param <P> the type of the additional parameter to this visitor's
0N/A * methods. Use {@code Void} for visitors that do not need an
0N/A * additional parameter.
0N/A *
0N/A * @author Joseph D. Darcy
0N/A * @author Scott Seligman
0N/A * @author Peter von der Ah&eacute;
574N/A *
574N/A * @see ElementScanner7
0N/A * @since 1.6
0N/A */
0N/A@SupportedSourceVersion(RELEASE_6)
0N/Apublic class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {
0N/A /**
0N/A * The specified default value.
0N/A */
0N/A protected final R DEFAULT_VALUE;
0N/A
0N/A /**
0N/A * Constructor for concrete subclasses; uses {@code null} for the
0N/A * default value.
0N/A */
0N/A protected ElementScanner6(){
0N/A DEFAULT_VALUE = null;
0N/A }
0N/A
0N/A /**
0N/A * Constructor for concrete subclasses; uses the argument for the
0N/A * default value.
0N/A */
0N/A protected ElementScanner6(R defaultValue){
0N/A DEFAULT_VALUE = defaultValue;
0N/A }
0N/A
0N/A /**
0N/A * Iterates over the given elements and calls {@link
0N/A * #scan(Element, Object) scan(Element, P)} on each one. Returns
0N/A * the result of the last call to {@code scan} or {@code
0N/A * DEFAULT_VALUE} for an empty iterable.
0N/A *
0N/A * @param iterable the elements to scan
0N/A * @param p additional parameter
0N/A * @return the scan of the last element or {@code DEFAULT_VALUE} if no elements
0N/A */
0N/A public final R scan(Iterable<? extends Element> iterable, P p) {
0N/A R result = DEFAULT_VALUE;
0N/A for(Element e : iterable)
0N/A result = scan(e, p);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Processes an element by calling {@code e.accept(this, p)};
0N/A * this method may be overridden by subclasses.
0N/A * @return the result of visiting {@code e}.
0N/A */
0N/A public R scan(Element e, P p) {
0N/A return e.accept(this, p);
0N/A }
0N/A
0N/A /**
0N/A * Convenience method equivalent to {@code v.scan(e, null)}.
0N/A * @return the result of scanning {@code e}.
0N/A */
0N/A public final R scan(Element e) {
0N/A return scan(e, null);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc} This implementation scans the enclosed elements.
0N/A *
850N/A * @param e {@inheritDoc}
850N/A * @param p {@inheritDoc}
0N/A * @return the result of scanning
0N/A */
0N/A public R visitPackage(PackageElement e, P p) {
0N/A return scan(e.getEnclosedElements(), p);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc} This implementation scans the enclosed elements.
0N/A *
850N/A * @param e {@inheritDoc}
850N/A * @param p {@inheritDoc}
0N/A * @return the result of scanning
0N/A */
0N/A public R visitType(TypeElement e, P p) {
0N/A return scan(e.getEnclosedElements(), p);
0N/A }
0N/A
0N/A /**
850N/A * {@inheritDoc}
0N/A *
850N/A * This implementation scans the enclosed elements, unless the
850N/A * element is a {@code RESOURCE_VARIABLE} in which case {@code
850N/A * visitUnknown} is called.
850N/A *
850N/A * @param e {@inheritDoc}
850N/A * @param p {@inheritDoc}
0N/A * @return the result of scanning
0N/A */
0N/A public R visitVariable(VariableElement e, P p) {
850N/A if (e.getKind() != ElementKind.RESOURCE_VARIABLE)
850N/A return scan(e.getEnclosedElements(), p);
850N/A else
850N/A return visitUnknown(e, p);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc} This implementation scans the parameters.
0N/A *
850N/A * @param e {@inheritDoc}
850N/A * @param p {@inheritDoc}
0N/A * @return the result of scanning
0N/A */
0N/A public R visitExecutable(ExecutableElement e, P p) {
0N/A return scan(e.getParameters(), p);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc} This implementation scans the enclosed elements.
0N/A *
850N/A * @param e {@inheritDoc}
850N/A * @param p {@inheritDoc}
0N/A * @return the result of scanning
0N/A */
0N/A public R visitTypeParameter(TypeParameterElement e, P p) {
0N/A return scan(e.getEnclosedElements(), p);
0N/A }
0N/A}