Path: blob/master/src/java.compiler/share/classes/javax/lang/model/util/ElementScanner14.java
41161 views
/*1* Copyright (c) 2019, 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 java.util.List;28import java.util.ArrayList;29import javax.lang.model.element.*;30import javax.annotation.processing.SupportedSourceVersion;31import javax.lang.model.SourceVersion;32import static javax.lang.model.SourceVersion.*;3334/**35* A scanning visitor of program elements with default behavior36* appropriate for the {@link SourceVersion#RELEASE_14 RELEASE_14}37* source version.38*39* The <code>visit<i>Xyz</i></code> methods in this40* class scan their component elements by calling {@code scan} on41* their {@linkplain Element#getEnclosedElements enclosed elements},42* {@linkplain ExecutableElement#getParameters parameters}, etc., as43* indicated in the individual method specifications. A subclass can44* control the order elements are visited by overriding the45* <code>visit<i>Xyz</i></code> methods. Note that clients of a scanner46* may get the desired behavior be invoking {@code v.scan(e, p)} rather47* than {@code v.visit(e, p)} on the root objects of interest.48*49* <p>When a subclass overrides a <code>visit<i>Xyz</i></code> method, the50* new method can cause the enclosed elements to be scanned in the51* default way by calling <code>super.visit<i>Xyz</i></code>. In this52* fashion, the concrete visitor can control the ordering of traversal53* over the component elements with respect to the additional54* processing; for example, consistently calling55* <code>super.visit<i>Xyz</i></code> at the start of the overridden56* methods will yield a preorder traversal, etc. If the component57* elements should be traversed in some other order, instead of58* calling <code>super.visit<i>Xyz</i></code>, an overriding visit method59* should call {@code scan} with the elements in the desired order.60*61* @apiNote62* Methods in this class may be overridden subject to their general63* contract.64*65* @param <R> the return type of this visitor's methods. Use {@link66* Void} for visitors that do not need to return results.67* @param <P> the type of the additional parameter to this visitor's68* methods. Use {@code Void} for visitors that do not need an69* additional parameter.70*71* @see <a href="ElementScanner6.html#note_for_subclasses"><strong>Compatibility note for subclasses</strong></a>72* @see ElementScanner673* @see ElementScanner774* @see ElementScanner875* @see ElementScanner976* @since 1677*/78@SupportedSourceVersion(RELEASE_17)79public class ElementScanner14<R, P> extends ElementScanner9<R, P> {80/**81* Constructor for concrete subclasses; uses {@code null} for the82* default value.83*/84protected ElementScanner14(){85super(null);86}8788/**89* Constructor for concrete subclasses; uses the argument for the90* default value.91*92* @param defaultValue the default value93*/94protected ElementScanner14(R defaultValue){95super(defaultValue);96}9798/**99* {@inheritDoc}100*101* @implSpec This implementation scans the type parameters, if102* any, and then the enclosed elements.103*104*105* @param e {@inheritDoc}106* @param p {@inheritDoc}107* @return the result of scanning108*/109@Override110public R visitType(TypeElement e, P p) {111return scan(createScanningList(e, e.getEnclosedElements()), p);112}113114/**115* {@inheritDoc}116*117* @implSpec This implementation first scans the type parameters, if any, and then118* the parameters.119*120* @param e {@inheritDoc}121* @param p {@inheritDoc}122* @return the result of scanning123*/124public R visitExecutable(ExecutableElement e, P p) {125return scan(createScanningList(e, e.getParameters()), p);126}127128private List<? extends Element> createScanningList(Parameterizable element,129List<? extends Element> toBeScanned) {130var typeParameters = element.getTypeParameters();131if (typeParameters.isEmpty()) {132return toBeScanned;133} else {134List<Element> scanningList = new ArrayList<>(typeParameters);135scanningList.addAll(toBeScanned);136return scanningList;137}138}139140/**141* {@inheritDoc}142*143* @implSpec This implementation scans the enclosed elements.144*145* @param e the element to visit146* @param p a visitor-specified parameter147* @return the result of the scan148*/149@Override150public R visitRecordComponent(RecordComponentElement e, P p) {151return scan(e.getEnclosedElements(), p);152}153}154155156