Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/lang/model/util/ElementScanner6.java
38899 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.lang.model.util;2627import javax.lang.model.element.*;28import javax.annotation.processing.SupportedSourceVersion;29import javax.lang.model.SourceVersion;30import static javax.lang.model.SourceVersion.*;313233/**34* A scanning visitor of program elements with default behavior35* appropriate for the {@link SourceVersion#RELEASE_6 RELEASE_6}36* source version. The <tt>visit<i>XYZ</i></tt> methods in this37* class scan their component elements by calling {@code scan} on38* their {@linkplain Element#getEnclosedElements enclosed elements},39* {@linkplain ExecutableElement#getParameters parameters}, etc., as40* indicated in the individual method specifications. A subclass can41* control the order elements are visited by overriding the42* <tt>visit<i>XYZ</i></tt> methods. Note that clients of a scanner43* may get the desired behavior be invoking {@code v.scan(e, p)} rather44* than {@code v.visit(e, p)} on the root objects of interest.45*46* <p>When a subclass overrides a <tt>visit<i>XYZ</i></tt> method, the47* new method can cause the enclosed elements to be scanned in the48* default way by calling <tt>super.visit<i>XYZ</i></tt>. In this49* fashion, the concrete visitor can control the ordering of traversal50* over the component elements with respect to the additional51* processing; for example, consistently calling52* <tt>super.visit<i>XYZ</i></tt> at the start of the overridden53* methods will yield a preorder traversal, etc. If the component54* elements should be traversed in some other order, instead of55* calling <tt>super.visit<i>XYZ</i></tt>, an overriding visit method56* should call {@code scan} with the elements in the desired order.57*58* <p> Methods in this class may be overridden subject to their59* general contract. Note that annotating methods in concrete60* subclasses with {@link java.lang.Override @Override} will help61* ensure that methods are overridden as intended.62*63* <p> <b>WARNING:</b> The {@code ElementVisitor} interface64* implemented by this class may have methods added to it in the65* future to accommodate new, currently unknown, language structures66* added to future versions of the Java™ programming language.67* Therefore, methods whose names begin with {@code "visit"} may be68* added to this class in the future; to avoid incompatibilities,69* classes which extend this class should not declare any instance70* methods with names beginning with {@code "visit"}.71*72* <p>When such a new visit method is added, the default73* implementation in this class will be to call the {@link74* #visitUnknown visitUnknown} method. A new element scanner visitor75* class will also be introduced to correspond to the new language76* level; this visitor will have different default behavior for the77* visit method in question. When the new visitor is introduced, all78* or portions of this visitor may be deprecated.79*80* @param <R> the return type of this visitor's methods. Use {@link81* Void} for visitors that do not need to return results.82* @param <P> the type of the additional parameter to this visitor's83* methods. Use {@code Void} for visitors that do not need an84* additional parameter.85*86* @author Joseph D. Darcy87* @author Scott Seligman88* @author Peter von der Ahé89*90* @see ElementScanner791* @see ElementScanner892* @since 1.693*/94@SupportedSourceVersion(RELEASE_6)95public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {96/**97* The specified default value.98*/99protected final R DEFAULT_VALUE;100101/**102* Constructor for concrete subclasses; uses {@code null} for the103* default value.104*/105protected ElementScanner6(){106DEFAULT_VALUE = null;107}108109/**110* Constructor for concrete subclasses; uses the argument for the111* default value.112*113* @param defaultValue the default value114*/115protected ElementScanner6(R defaultValue){116DEFAULT_VALUE = defaultValue;117}118119/**120* Iterates over the given elements and calls {@link121* #scan(Element, Object) scan(Element, P)} on each one. Returns122* the result of the last call to {@code scan} or {@code123* DEFAULT_VALUE} for an empty iterable.124*125* @param iterable the elements to scan126* @param p additional parameter127* @return the scan of the last element or {@code DEFAULT_VALUE} if no elements128*/129public final R scan(Iterable<? extends Element> iterable, P p) {130R result = DEFAULT_VALUE;131for(Element e : iterable)132result = scan(e, p);133return result;134}135136/**137* Processes an element by calling {@code e.accept(this, p)};138* this method may be overridden by subclasses.139*140* @param e the element to scan141* @param p a scanner-specified parameter142* @return the result of visiting {@code e}.143*/144public R scan(Element e, P p) {145return e.accept(this, p);146}147148/**149* Convenience method equivalent to {@code v.scan(e, null)}.150*151* @param e the element to scan152* @return the result of scanning {@code e}.153*/154public final R scan(Element e) {155return scan(e, null);156}157158/**159* {@inheritDoc} This implementation scans the enclosed elements.160*161* @param e {@inheritDoc}162* @param p {@inheritDoc}163* @return the result of scanning164*/165public R visitPackage(PackageElement e, P p) {166return scan(e.getEnclosedElements(), p);167}168169/**170* {@inheritDoc} This implementation scans the enclosed elements.171*172* @param e {@inheritDoc}173* @param p {@inheritDoc}174* @return the result of scanning175*/176public R visitType(TypeElement e, P p) {177return scan(e.getEnclosedElements(), p);178}179180/**181* {@inheritDoc}182*183* This implementation scans the enclosed elements, unless the184* element is a {@code RESOURCE_VARIABLE} in which case {@code185* visitUnknown} is called.186*187* @param e {@inheritDoc}188* @param p {@inheritDoc}189* @return the result of scanning190*/191public R visitVariable(VariableElement e, P p) {192if (e.getKind() != ElementKind.RESOURCE_VARIABLE)193return scan(e.getEnclosedElements(), p);194else195return visitUnknown(e, p);196}197198/**199* {@inheritDoc} This implementation scans the parameters.200*201* @param e {@inheritDoc}202* @param p {@inheritDoc}203* @return the result of scanning204*/205public R visitExecutable(ExecutableElement e, P p) {206return scan(e.getParameters(), p);207}208209/**210* {@inheritDoc} This implementation scans the enclosed elements.211*212* @param e {@inheritDoc}213* @param p {@inheritDoc}214* @return the result of scanning215*/216public R visitTypeParameter(TypeParameterElement e, P p) {217return scan(e.getEnclosedElements(), p);218}219}220221222