Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/source/util/TreeScanner.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 com.sun.source.util;2627import com.sun.source.tree.*;2829/**30* A TreeVisitor that visits all the child tree nodes.31* To visit nodes of a particular type, just override the32* corresponding visitXYZ method.33* Inside your method, call super.visitXYZ to visit descendant34* nodes.35*36* <p>The default implementation of the visitXYZ methods will determine37* a result as follows:38* <ul>39* <li>If the node being visited has no children, the result will be null.40* <li>If the node being visited has one child, the result will be the41* result of calling {@code scan} on that child. The child may be a simple node42* or itself a list of nodes.43* <li> If the node being visited has more than one child, the result will44* be determined by calling {@code scan} each child in turn, and then combining the45* result of each scan after the first with the cumulative result46* so far, as determined by the {@link #reduce} method. Each child may be either47* a simple node of a list of nodes. The default behavior of the {@code reduce}48* method is such that the result of the visitXYZ method will be the result of49* the last child scanned.50* </ul>51*52* <p>Here is an example to count the number of identifier nodes in a tree:53* <pre>54* class CountIdentifiers extends TreeScanner<Integer,Void> {55* {@literal @}Override56* public Integer visitIdentifier(IdentifierTree node, Void p) {57* return 1;58* }59* {@literal @}Override60* public Integer reduce(Integer r1, Integer r2) {61* return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);62* }63* }64* </pre>65*66* @author Peter von der Ahé67* @author Jonathan Gibbons68* @since 1.669*/70@jdk.Exported71public class TreeScanner<R,P> implements TreeVisitor<R,P> {7273/** Scan a single node.74*/75public R scan(Tree node, P p) {76return (node == null) ? null : node.accept(this, p);77}7879private R scanAndReduce(Tree node, P p, R r) {80return reduce(scan(node, p), r);81}8283/** Scan a list of nodes.84*/85public R scan(Iterable<? extends Tree> nodes, P p) {86R r = null;87if (nodes != null) {88boolean first = true;89for (Tree node : nodes) {90r = (first ? scan(node, p) : scanAndReduce(node, p, r));91first = false;92}93}94return r;95}9697private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {98return reduce(scan(nodes, p), r);99}100101/**102* Reduces two results into a combined result.103* The default implementation is to return the first parameter.104* The general contract of the method is that it may take any action whatsoever.105*/106public R reduce(R r1, R r2) {107return r1;108}109110111/* ***************************************************************************112* Visitor methods113****************************************************************************/114115public R visitCompilationUnit(CompilationUnitTree node, P p) {116R r = scan(node.getPackageAnnotations(), p);117r = scanAndReduce(node.getPackageName(), p, r);118r = scanAndReduce(node.getImports(), p, r);119r = scanAndReduce(node.getTypeDecls(), p, r);120return r;121}122123public R visitImport(ImportTree node, P p) {124return scan(node.getQualifiedIdentifier(), p);125}126127public R visitClass(ClassTree node, P p) {128R r = scan(node.getModifiers(), p);129r = scanAndReduce(node.getTypeParameters(), p, r);130r = scanAndReduce(node.getExtendsClause(), p, r);131r = scanAndReduce(node.getImplementsClause(), p, r);132r = scanAndReduce(node.getMembers(), p, r);133return r;134}135136public R visitMethod(MethodTree node, P p) {137R r = scan(node.getModifiers(), p);138r = scanAndReduce(node.getReturnType(), p, r);139r = scanAndReduce(node.getTypeParameters(), p, r);140r = scanAndReduce(node.getParameters(), p, r);141r = scanAndReduce(node.getReceiverParameter(), p, r);142r = scanAndReduce(node.getThrows(), p, r);143r = scanAndReduce(node.getBody(), p, r);144r = scanAndReduce(node.getDefaultValue(), p, r);145return r;146}147148public R visitVariable(VariableTree node, P p) {149R r = scan(node.getModifiers(), p);150r = scanAndReduce(node.getType(), p, r);151r = scanAndReduce(node.getNameExpression(), p, r);152r = scanAndReduce(node.getInitializer(), p, r);153return r;154}155156public R visitEmptyStatement(EmptyStatementTree node, P p) {157return null;158}159160public R visitBlock(BlockTree node, P p) {161return scan(node.getStatements(), p);162}163164public R visitDoWhileLoop(DoWhileLoopTree node, P p) {165R r = scan(node.getStatement(), p);166r = scanAndReduce(node.getCondition(), p, r);167return r;168}169170public R visitWhileLoop(WhileLoopTree node, P p) {171R r = scan(node.getCondition(), p);172r = scanAndReduce(node.getStatement(), p, r);173return r;174}175176public R visitForLoop(ForLoopTree node, P p) {177R r = scan(node.getInitializer(), p);178r = scanAndReduce(node.getCondition(), p, r);179r = scanAndReduce(node.getUpdate(), p, r);180r = scanAndReduce(node.getStatement(), p, r);181return r;182}183184public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {185R r = scan(node.getVariable(), p);186r = scanAndReduce(node.getExpression(), p, r);187r = scanAndReduce(node.getStatement(), p, r);188return r;189}190191public R visitLabeledStatement(LabeledStatementTree node, P p) {192return scan(node.getStatement(), p);193}194195public R visitSwitch(SwitchTree node, P p) {196R r = scan(node.getExpression(), p);197r = scanAndReduce(node.getCases(), p, r);198return r;199}200201public R visitCase(CaseTree node, P p) {202R r = scan(node.getExpression(), p);203r = scanAndReduce(node.getStatements(), p, r);204return r;205}206207public R visitSynchronized(SynchronizedTree node, P p) {208R r = scan(node.getExpression(), p);209r = scanAndReduce(node.getBlock(), p, r);210return r;211}212213public R visitTry(TryTree node, P p) {214R r = scan(node.getResources(), p);215r = scanAndReduce(node.getBlock(), p, r);216r = scanAndReduce(node.getCatches(), p, r);217r = scanAndReduce(node.getFinallyBlock(), p, r);218return r;219}220221public R visitCatch(CatchTree node, P p) {222R r = scan(node.getParameter(), p);223r = scanAndReduce(node.getBlock(), p, r);224return r;225}226227public R visitConditionalExpression(ConditionalExpressionTree node, P p) {228R r = scan(node.getCondition(), p);229r = scanAndReduce(node.getTrueExpression(), p, r);230r = scanAndReduce(node.getFalseExpression(), p, r);231return r;232}233234public R visitIf(IfTree node, P p) {235R r = scan(node.getCondition(), p);236r = scanAndReduce(node.getThenStatement(), p, r);237r = scanAndReduce(node.getElseStatement(), p, r);238return r;239}240241public R visitExpressionStatement(ExpressionStatementTree node, P p) {242return scan(node.getExpression(), p);243}244245public R visitBreak(BreakTree node, P p) {246return null;247}248249public R visitContinue(ContinueTree node, P p) {250return null;251}252253public R visitReturn(ReturnTree node, P p) {254return scan(node.getExpression(), p);255}256257public R visitThrow(ThrowTree node, P p) {258return scan(node.getExpression(), p);259}260261public R visitAssert(AssertTree node, P p) {262R r = scan(node.getCondition(), p);263r = scanAndReduce(node.getDetail(), p, r);264return r;265}266267public R visitMethodInvocation(MethodInvocationTree node, P p) {268R r = scan(node.getTypeArguments(), p);269r = scanAndReduce(node.getMethodSelect(), p, r);270r = scanAndReduce(node.getArguments(), p, r);271return r;272}273274public R visitNewClass(NewClassTree node, P p) {275R r = scan(node.getEnclosingExpression(), p);276r = scanAndReduce(node.getIdentifier(), p, r);277r = scanAndReduce(node.getTypeArguments(), p, r);278r = scanAndReduce(node.getArguments(), p, r);279r = scanAndReduce(node.getClassBody(), p, r);280return r;281}282283public R visitNewArray(NewArrayTree node, P p) {284R r = scan(node.getType(), p);285r = scanAndReduce(node.getDimensions(), p, r);286r = scanAndReduce(node.getInitializers(), p, r);287r = scanAndReduce(node.getAnnotations(), p, r);288for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {289r = scanAndReduce(dimAnno, p, r);290}291return r;292}293294public R visitLambdaExpression(LambdaExpressionTree node, P p) {295R r = scan(node.getParameters(), p);296r = scanAndReduce(node.getBody(), p, r);297return r;298}299300public R visitParenthesized(ParenthesizedTree node, P p) {301return scan(node.getExpression(), p);302}303304public R visitAssignment(AssignmentTree node, P p) {305R r = scan(node.getVariable(), p);306r = scanAndReduce(node.getExpression(), p, r);307return r;308}309310public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {311R r = scan(node.getVariable(), p);312r = scanAndReduce(node.getExpression(), p, r);313return r;314}315316public R visitUnary(UnaryTree node, P p) {317return scan(node.getExpression(), p);318}319320public R visitBinary(BinaryTree node, P p) {321R r = scan(node.getLeftOperand(), p);322r = scanAndReduce(node.getRightOperand(), p, r);323return r;324}325326public R visitTypeCast(TypeCastTree node, P p) {327R r = scan(node.getType(), p);328r = scanAndReduce(node.getExpression(), p, r);329return r;330}331332public R visitInstanceOf(InstanceOfTree node, P p) {333R r = scan(node.getExpression(), p);334r = scanAndReduce(node.getType(), p, r);335return r;336}337338public R visitArrayAccess(ArrayAccessTree node, P p) {339R r = scan(node.getExpression(), p);340r = scanAndReduce(node.getIndex(), p, r);341return r;342}343344public R visitMemberSelect(MemberSelectTree node, P p) {345return scan(node.getExpression(), p);346}347348public R visitMemberReference(MemberReferenceTree node, P p) {349R r = scan(node.getQualifierExpression(), p);350r = scanAndReduce(node.getTypeArguments(), p, r);351return r;352}353354public R visitIdentifier(IdentifierTree node, P p) {355return null;356}357358public R visitLiteral(LiteralTree node, P p) {359return null;360}361362public R visitPrimitiveType(PrimitiveTypeTree node, P p) {363return null;364}365366public R visitArrayType(ArrayTypeTree node, P p) {367return scan(node.getType(), p);368}369370public R visitParameterizedType(ParameterizedTypeTree node, P p) {371R r = scan(node.getType(), p);372r = scanAndReduce(node.getTypeArguments(), p, r);373return r;374}375376public R visitUnionType(UnionTypeTree node, P p) {377return scan(node.getTypeAlternatives(), p);378}379380public R visitIntersectionType(IntersectionTypeTree node, P p) {381return scan(node.getBounds(), p);382}383384public R visitTypeParameter(TypeParameterTree node, P p) {385R r = scan(node.getAnnotations(), p);386r = scanAndReduce(node.getBounds(), p, r);387return r;388}389390public R visitWildcard(WildcardTree node, P p) {391return scan(node.getBound(), p);392}393394public R visitModifiers(ModifiersTree node, P p) {395return scan(node.getAnnotations(), p);396}397398public R visitAnnotation(AnnotationTree node, P p) {399R r = scan(node.getAnnotationType(), p);400r = scanAndReduce(node.getArguments(), p, r);401return r;402}403404public R visitAnnotatedType(AnnotatedTypeTree node, P p) {405R r = scan(node.getAnnotations(), p);406r = scanAndReduce(node.getUnderlyingType(), p, r);407return r;408}409410public R visitOther(Tree node, P p) {411return null;412}413414public R visitErroneous(ErroneousTree node, P p) {415return null;416}417}418419420