Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/source/util/DocTreeScanner.java
38899 views
/*1* Copyright (c) 2011, 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 com.sun.source.util;2627import com.sun.source.doctree.*;282930/**31* A TreeVisitor that visits all the child tree nodes.32* To visit nodes of a particular type, just override the33* corresponding visitXYZ method.34* Inside your method, call super.visitXYZ to visit descendant35* nodes.36*37* <p>The default implementation of the visitXYZ methods will determine38* a result as follows:39* <ul>40* <li>If the node being visited has no children, the result will be null.41* <li>If the node being visited has one child, the result will be the42* result of calling {@code scan} on that child. The child may be a simple node43* or itself a list of nodes.44* <li> If the node being visited has more than one child, the result will45* be determined by calling {@code scan} each child in turn, and then combining the46* result of each scan after the first with the cumulative result47* so far, as determined by the {@link #reduce} method. Each child may be either48* a simple node of a list of nodes. The default behavior of the {@code reduce}49* method is such that the result of the visitXYZ method will be the result of50* the last child scanned.51* </ul>52*53* <p>Here is an example to count the number of erroneous nodes in a tree:54* <pre>55* class CountErrors extends DocTreeScanner<Integer,Void> {56* {@literal @}Override57* public Integer visitErroneous(ErroneousTree node, Void p) {58* return 1;59* }60* {@literal @}Override61* public Integer reduce(Integer r1, Integer r2) {62* return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);63* }64* }65* </pre>66*67* @since 1.868*/69@jdk.Exported70public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {7172/**73* Scan a single node.74*/75public R scan(DocTree node, P p) {76return (node == null) ? null : node.accept(this, p);77}7879private R scanAndReduce(DocTree node, P p, R r) {80return reduce(scan(node, p), r);81}8283/**84* Scan a list of nodes.85*/86public R scan(Iterable<? extends DocTree> nodes, P p) {87R r = null;88if (nodes != null) {89boolean first = true;90for (DocTree node : nodes) {91r = (first ? scan(node, p) : scanAndReduce(node, p, r));92first = false;93}94}95return r;96}9798private R scanAndReduce(Iterable<? extends DocTree> nodes, P p, R r) {99return reduce(scan(nodes, p), r);100}101102/**103* Reduces two results into a combined result.104* The default implementation is to return the first parameter.105* The general contract of the method is that it may take any action whatsoever.106*/107public R reduce(R r1, R r2) {108return r1;109}110111112/* ***************************************************************************113* Visitor methods114****************************************************************************/115116@Override117public R visitAttribute(AttributeTree node, P p) {118return null;119}120121@Override122public R visitAuthor(AuthorTree node, P p) {123return scan(node.getName(), p);124}125126@Override127public R visitComment(CommentTree node, P p) {128return null;129}130131@Override132public R visitDeprecated(DeprecatedTree node, P p) {133return scan(node.getBody(), p);134}135136@Override137public R visitDocComment(DocCommentTree node, P p) {138R r = scan(node.getFirstSentence(), p);139r = scanAndReduce(node.getBody(), p, r);140r = scanAndReduce(node.getBlockTags(), p, r);141return r;142}143144@Override145public R visitDocRoot(DocRootTree node, P p) {146return null;147}148149@Override150public R visitEndElement(EndElementTree node, P p) {151return null;152}153154@Override155public R visitEntity(EntityTree node, P p) {156return null;157}158159@Override160public R visitErroneous(ErroneousTree node, P p) {161return null;162}163164@Override165public R visitIdentifier(IdentifierTree node, P p) {166return null;167}168169@Override170public R visitInheritDoc(InheritDocTree node, P p) {171return null;172}173174@Override175public R visitLink(LinkTree node, P p) {176R r = scan(node.getReference(), p);177r = scanAndReduce(node.getLabel(), p, r);178return r;179}180181@Override182public R visitLiteral(LiteralTree node, P p) {183return null;184}185186@Override187public R visitParam(ParamTree node, P p) {188R r = scan(node.getName(), p);189r = scanAndReduce(node.getDescription(), p, r);190return r;191}192193@Override194public R visitReference(ReferenceTree node, P p) {195return null;196}197198@Override199public R visitReturn(ReturnTree node, P p) {200return scan(node.getDescription(), p);201}202203@Override204public R visitSee(SeeTree node, P p) {205return scan(node.getReference(), p);206}207208@Override209public R visitSerial(SerialTree node, P p) {210return scan(node.getDescription(), p);211}212213@Override214public R visitSerialData(SerialDataTree node, P p) {215return scan(node.getDescription(), p);216}217218@Override219public R visitSerialField(SerialFieldTree node, P p) {220R r = scan(node.getName(), p);221r = scanAndReduce(node.getType(), p, r);222r = scanAndReduce(node.getDescription(), p, r);223return r;224}225226@Override227public R visitSince(SinceTree node, P p) {228return scan(node.getBody(), p);229}230231@Override232public R visitStartElement(StartElementTree node, P p) {233return scan(node.getAttributes(), p);234}235236@Override237public R visitText(TextTree node, P p) {238return null;239}240241@Override242public R visitThrows(ThrowsTree node, P p) {243R r = scan(node.getExceptionName(), p);244r = scanAndReduce(node.getDescription(), p, r);245return r;246}247248@Override249public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) {250return scan(node.getContent(), p);251}252253@Override254public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) {255return scan(node.getContent(), p);256}257258@Override259public R visitValue(ValueTree node, P p) {260return scan(node.getReference(), p);261}262263@Override264public R visitVersion(VersionTree node, P p) {265return scan(node.getBody(), p);266}267268@Override269public R visitOther(DocTree node, P p) {270return null;271}272273}274275276