Path: blob/master/src/java.compiler/share/classes/javax/lang/model/util/ElementScanner6.java
41161 views
/*1* Copyright (c) 2005, 2020, 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 <code>visit<i>Xyz</i></code> 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* <code>visit<i>Xyz</i></code> 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 <code>visit<i>Xyz</i></code> method, the47* new method can cause the enclosed elements to be scanned in the48* default way by calling <code>super.visit<i>Xyz</i></code>. 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* <code>super.visit<i>Xyz</i></code> 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 <code>super.visit<i>Xyz</i></code>, an overriding visit method56* should call {@code scan} with the elements in the desired order.57*58* @apiNote59* Methods in this class may be overridden subject to their general60* contract.61*62* <p id=note_for_subclasses><strong>WARNING:</strong> The {@code ElementVisitor} interface63* implemented by this class may have methods added to it in the64* future to accommodate new, currently unknown, language structures65* added to future versions of the Java programming language.66* Therefore, methods whose names begin with {@code "visit"} may be67* added to this class in the future; to avoid incompatibilities,68* classes which extend this class should not declare any instance69* methods with names beginning with {@code "visit"}.</p>70*71* <p>When such a new visit method is added, the default72* implementation in this class will be to directly or indirectly call the {@link73* #visitUnknown visitUnknown} method. A new element scanner visitor74* class will also be introduced to correspond to the new language75* level; this visitor will have different default behavior for the76* visit method in question. When a new visitor is introduced,77* portions of this visitor class may be deprecated, including its constructors.78*79* @param <R> the return type of this visitor's methods. Use {@link80* Void} for visitors that do not need to return results.81* @param <P> the type of the additional parameter to this visitor's82* methods. Use {@code Void} for visitors that do not need an83* additional parameter.84*85* @author Joseph D. Darcy86* @author Scott Seligman87* @author Peter von der Ahé88*89* @see ElementScanner790* @see ElementScanner891* @see ElementScanner992* @see ElementScanner1493* @since 1.694*/95@SupportedSourceVersion(RELEASE_6)96public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {97/**98* The specified default value.99*/100protected final R DEFAULT_VALUE;101102/**103* Constructor for concrete subclasses; uses {@code null} for the104* default value.105* @deprecated Release 6 is obsolete; update to a visitor for a newer106* release level.107*/108@Deprecated(since="9")109protected ElementScanner6(){110DEFAULT_VALUE = null;111}112113/**114* Constructor for concrete subclasses; uses the argument for the115* default value.116*117* @param defaultValue the default value118* @deprecated Release 6 is obsolete; update to a visitor for a newer119* release level.120*/121@Deprecated(since="9")122protected ElementScanner6(R defaultValue){123DEFAULT_VALUE = defaultValue;124}125126/**127* Iterates over the given elements and calls {@link128* #scan(Element, Object) scan(Element, P)} on each one. Returns129* the result of the last call to {@code scan} or {@code130* DEFAULT_VALUE} for an empty iterable.131*132* @param iterable the elements to scan133* @param p additional parameter134* @return the scan of the last element or {@code DEFAULT_VALUE} if no elements135*/136public final R scan(Iterable<? extends Element> iterable, P p) {137R result = DEFAULT_VALUE;138for(Element e : iterable)139result = scan(e, p);140return result;141}142143/**144* Processes an element by calling {@code e.accept(this, p)};145* this method may be overridden by subclasses.146*147* @param e the element to scan148* @param p a scanner-specified parameter149* @return the result of visiting {@code e}.150*/151public R scan(Element e, P p) {152return e.accept(this, p);153}154155/**156* Convenience method equivalent to {@code v.scan(e, null)}.157*158* @param e the element to scan159* @return the result of scanning {@code e}.160*/161public final R scan(Element e) {162return scan(e, null);163}164165/**166* {@inheritDoc}167*168* @implSpec This implementation scans the enclosed elements.169*170* @param e {@inheritDoc}171* @param p {@inheritDoc}172* @return the result of scanning173*/174public R visitPackage(PackageElement e, P p) {175return scan(e.getEnclosedElements(), p);176}177178/**179* {@inheritDoc}180*181* @implSpec This implementation scans the enclosed elements.182* Note that type parameters are <em>not</em> scanned by this183* implementation since type parameters are not considered to be184* {@linkplain TypeElement#getEnclosedElements enclosed elements185* of a type}.186*187* @param e {@inheritDoc}188* @param p {@inheritDoc}189* @return the result of scanning190*/191public R visitType(TypeElement e, P p) {192return scan(e.getEnclosedElements(), p);193}194195/**196* {@inheritDoc}197*198* @implSpec This implementation scans the enclosed elements, unless the199* element is a {@code RESOURCE_VARIABLE} in which case {@code200* visitUnknown} is called.201*202* @param e {@inheritDoc}203* @param p {@inheritDoc}204* @return the result of scanning205*/206public R visitVariable(VariableElement e, P p) {207if (e.getKind() != ElementKind.RESOURCE_VARIABLE)208return scan(e.getEnclosedElements(), p);209else210return visitUnknown(e, p);211}212213/**214* {@inheritDoc}215*216* @implSpec This implementation scans the parameters.217* Note that type parameters are <em>not</em> scanned by this218* implementation.219*220* @param e {@inheritDoc}221* @param p {@inheritDoc}222* @return the result of scanning223*/224public R visitExecutable(ExecutableElement e, P p) {225return scan(e.getParameters(), p);226}227228/**229* {@inheritDoc}230*231* @implSpec This implementation scans the enclosed elements.232*233* @param e {@inheritDoc}234* @param p {@inheritDoc}235* @return the result of scanning236*/237public R visitTypeParameter(TypeParameterElement e, P p) {238return scan(e.getEnclosedElements(), p);239}240}241242243