aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.lang.model.util; aoqi@0: aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.annotation.processing.SupportedSourceVersion; aoqi@0: import javax.lang.model.SourceVersion; aoqi@0: import static javax.lang.model.SourceVersion.*; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * A scanning visitor of program elements with default behavior aoqi@0: * appropriate for the {@link SourceVersion#RELEASE_6 RELEASE_6} aoqi@0: * source version. The visitXYZ methods in this aoqi@0: * class scan their component elements by calling {@code scan} on aoqi@0: * their {@linkplain Element#getEnclosedElements enclosed elements}, aoqi@0: * {@linkplain ExecutableElement#getParameters parameters}, etc., as aoqi@0: * indicated in the individual method specifications. A subclass can aoqi@0: * control the order elements are visited by overriding the aoqi@0: * visitXYZ methods. Note that clients of a scanner aoqi@0: * may get the desired behavior be invoking {@code v.scan(e, p)} rather aoqi@0: * than {@code v.visit(e, p)} on the root objects of interest. aoqi@0: * aoqi@0: *

When a subclass overrides a visitXYZ method, the aoqi@0: * new method can cause the enclosed elements to be scanned in the aoqi@0: * default way by calling super.visitXYZ. In this aoqi@0: * fashion, the concrete visitor can control the ordering of traversal aoqi@0: * over the component elements with respect to the additional aoqi@0: * processing; for example, consistently calling aoqi@0: * super.visitXYZ at the start of the overridden aoqi@0: * methods will yield a preorder traversal, etc. If the component aoqi@0: * elements should be traversed in some other order, instead of aoqi@0: * calling super.visitXYZ, an overriding visit method aoqi@0: * should call {@code scan} with the elements in the desired order. aoqi@0: * aoqi@0: *

Methods in this class may be overridden subject to their aoqi@0: * general contract. Note that annotating methods in concrete aoqi@0: * subclasses with {@link java.lang.Override @Override} will help aoqi@0: * ensure that methods are overridden as intended. aoqi@0: * aoqi@0: *

WARNING: The {@code ElementVisitor} interface aoqi@0: * implemented by this class may have methods added to it in the aoqi@0: * future to accommodate new, currently unknown, language structures aoqi@0: * added to future versions of the Java™ programming language. aoqi@0: * Therefore, methods whose names begin with {@code "visit"} may be aoqi@0: * added to this class in the future; to avoid incompatibilities, aoqi@0: * classes which extend this class should not declare any instance aoqi@0: * methods with names beginning with {@code "visit"}. aoqi@0: * aoqi@0: *

When such a new visit method is added, the default aoqi@0: * implementation in this class will be to call the {@link aoqi@0: * #visitUnknown visitUnknown} method. A new element scanner visitor aoqi@0: * class will also be introduced to correspond to the new language aoqi@0: * level; this visitor will have different default behavior for the aoqi@0: * visit method in question. When the new visitor is introduced, all aoqi@0: * or portions of this visitor may be deprecated. aoqi@0: * aoqi@0: * @param the return type of this visitor's methods. Use {@link aoqi@0: * Void} for visitors that do not need to return results. aoqi@0: * @param

the type of the additional parameter to this visitor's aoqi@0: * methods. Use {@code Void} for visitors that do not need an aoqi@0: * additional parameter. aoqi@0: * aoqi@0: * @author Joseph D. Darcy aoqi@0: * @author Scott Seligman aoqi@0: * @author Peter von der Ahé aoqi@0: * aoqi@0: * @see ElementScanner7 aoqi@0: * @see ElementScanner8 aoqi@0: * @since 1.6 aoqi@0: */ aoqi@0: @SupportedSourceVersion(RELEASE_6) aoqi@0: public class ElementScanner6 extends AbstractElementVisitor6 { aoqi@0: /** aoqi@0: * The specified default value. aoqi@0: */ aoqi@0: protected final R DEFAULT_VALUE; aoqi@0: aoqi@0: /** aoqi@0: * Constructor for concrete subclasses; uses {@code null} for the aoqi@0: * default value. aoqi@0: */ aoqi@0: protected ElementScanner6(){ aoqi@0: DEFAULT_VALUE = null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Constructor for concrete subclasses; uses the argument for the aoqi@0: * default value. aoqi@0: * aoqi@0: * @param defaultValue the default value aoqi@0: */ aoqi@0: protected ElementScanner6(R defaultValue){ aoqi@0: DEFAULT_VALUE = defaultValue; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Iterates over the given elements and calls {@link aoqi@0: * #scan(Element, Object) scan(Element, P)} on each one. Returns aoqi@0: * the result of the last call to {@code scan} or {@code aoqi@0: * DEFAULT_VALUE} for an empty iterable. aoqi@0: * aoqi@0: * @param iterable the elements to scan aoqi@0: * @param p additional parameter aoqi@0: * @return the scan of the last element or {@code DEFAULT_VALUE} if no elements aoqi@0: */ aoqi@0: public final R scan(Iterable iterable, P p) { aoqi@0: R result = DEFAULT_VALUE; aoqi@0: for(Element e : iterable) aoqi@0: result = scan(e, p); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Processes an element by calling {@code e.accept(this, p)}; aoqi@0: * this method may be overridden by subclasses. aoqi@0: * aoqi@0: * @param e the element to scan aoqi@0: * @param p a scanner-specified parameter aoqi@0: * @return the result of visiting {@code e}. aoqi@0: */ aoqi@0: public R scan(Element e, P p) { aoqi@0: return e.accept(this, p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convenience method equivalent to {@code v.scan(e, null)}. aoqi@0: * aoqi@0: * @param e the element to scan aoqi@0: * @return the result of scanning {@code e}. aoqi@0: */ aoqi@0: public final R scan(Element e) { aoqi@0: return scan(e, null); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@inheritDoc} This implementation scans the enclosed elements. aoqi@0: * aoqi@0: * @param e {@inheritDoc} aoqi@0: * @param p {@inheritDoc} aoqi@0: * @return the result of scanning aoqi@0: */ aoqi@0: public R visitPackage(PackageElement e, P p) { aoqi@0: return scan(e.getEnclosedElements(), p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@inheritDoc} This implementation scans the enclosed elements. aoqi@0: * aoqi@0: * @param e {@inheritDoc} aoqi@0: * @param p {@inheritDoc} aoqi@0: * @return the result of scanning aoqi@0: */ aoqi@0: public R visitType(TypeElement e, P p) { aoqi@0: return scan(e.getEnclosedElements(), p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@inheritDoc} aoqi@0: * aoqi@0: * This implementation scans the enclosed elements, unless the aoqi@0: * element is a {@code RESOURCE_VARIABLE} in which case {@code aoqi@0: * visitUnknown} is called. aoqi@0: * aoqi@0: * @param e {@inheritDoc} aoqi@0: * @param p {@inheritDoc} aoqi@0: * @return the result of scanning aoqi@0: */ aoqi@0: public R visitVariable(VariableElement e, P p) { aoqi@0: if (e.getKind() != ElementKind.RESOURCE_VARIABLE) aoqi@0: return scan(e.getEnclosedElements(), p); aoqi@0: else aoqi@0: return visitUnknown(e, p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@inheritDoc} This implementation scans the parameters. aoqi@0: * aoqi@0: * @param e {@inheritDoc} aoqi@0: * @param p {@inheritDoc} aoqi@0: * @return the result of scanning aoqi@0: */ aoqi@0: public R visitExecutable(ExecutableElement e, P p) { aoqi@0: return scan(e.getParameters(), p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@inheritDoc} This implementation scans the enclosed elements. aoqi@0: * aoqi@0: * @param e {@inheritDoc} aoqi@0: * @param p {@inheritDoc} aoqi@0: * @return the result of scanning aoqi@0: */ aoqi@0: public R visitTypeParameter(TypeParameterElement e, P p) { aoqi@0: return scan(e.getEnclosedElements(), p); aoqi@0: } aoqi@0: }