1 /*
   2  * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.source.util;
  27 
  28 import com.sun.source.tree.*;
  29 
  30 /**
  31  * A TreeVisitor that visits all the child tree nodes.
  32  * To visit nodes of a particular type, just override the
  33  * corresponding visitXYZ method.
  34  * Inside your method, call super.visitXYZ to visit descendant
  35  * nodes.
  36  *
  37  * <p>The default implementation of the visitXYZ methods will determine
  38  * a result as follows:
  39  * <ul>
  40  * <li>If the node being visited has no children, the result will be {@code null}.
  41  * <li>If the node being visited has one child, the result will be the
  42  * result of calling {@code scan} on that child. The child may be a simple node
  43  * or itself a list of nodes.
  44  * <li> If the node being visited has more than one child, the result will
  45  * be determined by calling {@code scan} each child in turn, and then combining the
  46  * result of each scan after the first with the cumulative result
  47  * so far, as determined by the {@link #reduce} method. Each child may be either
  48  * 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 of
  50  * the last child scanned.
  51  * </ul>
  52  *
  53  * <p>Here is an example to count the number of identifier nodes in a tree:
  54  * <pre>
  55  *   class CountIdentifiers extends TreeScanner&lt;Integer,Void&gt; {
  56  *      {@literal @}Override
  57  *      public Integer visitIdentifier(IdentifierTree node, Void p) {
  58  *          return 1;
  59  *      }
  60  *      {@literal @}Override
  61  *      public Integer reduce(Integer r1, Integer r2) {
  62  *          return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
  63  *      }
  64  *   }
  65  * </pre>
  66  *
  67  * @param <R> the return type of this visitor's methods.  Use {@link
  68  *            Void} for visitors that do not need to return results.
  69  * @param <P> the type of the additional parameter to this visitor's
  70  *            methods.  Use {@code Void} for visitors that do not need an
  71  *            additional parameter.
  72  *
  73  * @author Peter von der Ah&eacute;
  74  * @author Jonathan Gibbons
  75  * @since 1.6
  76  */
  77 public class TreeScanner<R,P> implements TreeVisitor<R,P> {
  78 
  79     /**
  80      * Scans a single node.
  81      * @param tree the node to be scanned
  82      * @param p a parameter value passed to the visit method
  83      * @return the result value from the visit method
  84      */
  85     public R scan(Tree tree, P p) {
  86         return (tree == null) ? null : tree.accept(this, p);
  87     }
  88 
  89     private R scanAndReduce(Tree node, P p, R r) {
  90         return reduce(scan(node, p), r);
  91     }
  92 
  93     /**
  94      * Scans a sequence of nodes.
  95      * @param nodes the nodes to be scanned
  96      * @param p a parameter value to be passed to the visit method for each node
  97      * @return the combined return value from the visit methods.
  98      *      The values are combined using the {@link #reduce reduce} method.
  99      */
 100     public R scan(Iterable<? extends Tree> nodes, P p) {
 101         R r = null;
 102         if (nodes != null) {
 103             boolean first = true;
 104             for (Tree node : nodes) {
 105                 r = (first ? scan(node, p) : scanAndReduce(node, p, r));
 106                 first = false;
 107             }
 108         }
 109         return r;
 110     }
 111 
 112     private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {
 113         return reduce(scan(nodes, p), r);
 114     }
 115 
 116     /**
 117      * Reduces two results into a combined result.
 118      * The default implementation is to return the first parameter.
 119      * The general contract of the method is that it may take any action whatsoever.
 120      * @param r1 the first of the values to be combined
 121      * @param r2 the second of the values to be combined
 122      * @return the result of combining the two parameters
 123      */
 124     public R reduce(R r1, R r2) {
 125         return r1;
 126     }
 127 
 128 
 129 /* ***************************************************************************
 130  * Visitor methods
 131  ****************************************************************************/
 132 
 133     /**
 134      * {@inheritDoc} This implementation scans the children in left to right order.
 135      *
 136      * @param node  {@inheritDoc}
 137      * @param p  {@inheritDoc}
 138      * @return the result of scanning
 139      */
 140     @Override
 141     public R visitCompilationUnit(CompilationUnitTree node, P p) {
 142         R r = scan(node.getPackage(), p);
 143         r = scanAndReduce(node.getImports(), p, r);
 144         r = scanAndReduce(node.getTypeDecls(), p, r);
 145         return r;
 146     }
 147 
 148     /**
 149      * {@inheritDoc} This implementation scans the children in left to right order.
 150      *
 151      * @param node  {@inheritDoc}
 152      * @param p  {@inheritDoc}
 153      * @return the result of scanning
 154      */
 155     @Override
 156     public R visitPackage(PackageTree node, P p) {
 157         R r = scan(node.getAnnotations(), p);
 158         r = scanAndReduce(node.getPackageName(), p, r);
 159         return r;
 160     }
 161 
 162     /**
 163      * {@inheritDoc} This implementation scans the children in left to right order.
 164      *
 165      * @param node  {@inheritDoc}
 166      * @param p  {@inheritDoc}
 167      * @return the result of scanning
 168      */
 169     @Override
 170     public R visitImport(ImportTree node, P p) {
 171         return scan(node.getQualifiedIdentifier(), p);
 172     }
 173 
 174     /**
 175      * {@inheritDoc} This implementation scans the children in left to right order.
 176      *
 177      * @param node  {@inheritDoc}
 178      * @param p  {@inheritDoc}
 179      * @return the result of scanning
 180      */
 181     @Override
 182     public R visitClass(ClassTree node, P p) {
 183         R r = scan(node.getModifiers(), p);
 184         r = scanAndReduce(node.getTypeParameters(), p, r);
 185         r = scanAndReduce(node.getExtendsClause(), p, r);
 186         r = scanAndReduce(node.getImplementsClause(), p, r);
 187         r = scanAndReduce(node.getMembers(), p, r);
 188         return r;
 189     }
 190 
 191     /**
 192      * {@inheritDoc} This implementation scans the children in left to right order.
 193      *
 194      * @param node  {@inheritDoc}
 195      * @param p  {@inheritDoc}
 196      * @return the result of scanning
 197      */
 198     @Override
 199     public R visitMethod(MethodTree node, P p) {
 200         R r = scan(node.getModifiers(), p);
 201         r = scanAndReduce(node.getReturnType(), p, r);
 202         r = scanAndReduce(node.getTypeParameters(), p, r);
 203         r = scanAndReduce(node.getParameters(), p, r);
 204         r = scanAndReduce(node.getReceiverParameter(), p, r);
 205         r = scanAndReduce(node.getThrows(), p, r);
 206         r = scanAndReduce(node.getBody(), p, r);
 207         r = scanAndReduce(node.getDefaultValue(), p, r);
 208         return r;
 209     }
 210 
 211     /**
 212      * {@inheritDoc} This implementation scans the children in left to right order.
 213      *
 214      * @param node  {@inheritDoc}
 215      * @param p  {@inheritDoc}
 216      * @return the result of scanning
 217      */
 218     @Override
 219     public R visitVariable(VariableTree node, P p) {
 220         R r = scan(node.getModifiers(), p);
 221         r = scanAndReduce(node.getType(), p, r);
 222         r = scanAndReduce(node.getNameExpression(), p, r);
 223         r = scanAndReduce(node.getInitializer(), p, r);
 224         return r;
 225     }
 226 
 227     /**
 228      * {@inheritDoc} This implementation returns {@code null}.
 229      *
 230      * @param node  {@inheritDoc}
 231      * @param p  {@inheritDoc}
 232      * @return the result of scanning
 233      */
 234     @Override
 235     public R visitEmptyStatement(EmptyStatementTree node, P p) {
 236         return null;
 237     }
 238 
 239     /**
 240      * {@inheritDoc} This implementation scans the children in left to right order.
 241      *
 242      * @param node  {@inheritDoc}
 243      * @param p  {@inheritDoc}
 244      * @return the result of scanning
 245      */
 246     @Override
 247     public R visitBlock(BlockTree node, P p) {
 248         return scan(node.getStatements(), p);
 249     }
 250 
 251     /**
 252      * {@inheritDoc} This implementation scans the children in left to right order.
 253      *
 254      * @param node  {@inheritDoc}
 255      * @param p  {@inheritDoc}
 256      * @return the result of scanning
 257      */
 258     @Override
 259     public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
 260         R r = scan(node.getStatement(), p);
 261         r = scanAndReduce(node.getCondition(), p, r);
 262         return r;
 263     }
 264 
 265     /**
 266      * {@inheritDoc} This implementation scans the children in left to right order.
 267      *
 268      * @param node  {@inheritDoc}
 269      * @param p  {@inheritDoc}
 270      * @return the result of scanning
 271      */
 272     @Override
 273     public R visitWhileLoop(WhileLoopTree node, P p) {
 274         R r = scan(node.getCondition(), p);
 275         r = scanAndReduce(node.getStatement(), p, r);
 276         return r;
 277     }
 278 
 279     /**
 280      * {@inheritDoc} This implementation scans the children in left to right order.
 281      *
 282      * @param node  {@inheritDoc}
 283      * @param p  {@inheritDoc}
 284      * @return the result of scanning
 285      */
 286     @Override
 287     public R visitForLoop(ForLoopTree node, P p) {
 288         R r = scan(node.getInitializer(), p);
 289         r = scanAndReduce(node.getCondition(), p, r);
 290         r = scanAndReduce(node.getUpdate(), p, r);
 291         r = scanAndReduce(node.getStatement(), p, r);
 292         return r;
 293     }
 294 
 295     /**
 296      * {@inheritDoc} This implementation scans the children in left to right order.
 297      *
 298      * @param node  {@inheritDoc}
 299      * @param p  {@inheritDoc}
 300      * @return the result of scanning
 301      */
 302     @Override
 303     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
 304         R r = scan(node.getVariable(), p);
 305         r = scanAndReduce(node.getExpression(), p, r);
 306         r = scanAndReduce(node.getStatement(), p, r);
 307         return r;
 308     }
 309 
 310     /**
 311      * {@inheritDoc} This implementation scans the children in left to right order.
 312      *
 313      * @param node  {@inheritDoc}
 314      * @param p  {@inheritDoc}
 315      * @return the result of scanning
 316      */
 317     @Override
 318     public R visitLabeledStatement(LabeledStatementTree node, P p) {
 319         return scan(node.getStatement(), p);
 320     }
 321 
 322     /**
 323      * {@inheritDoc} This implementation scans the children in left to right order.
 324      *
 325      * @param node  {@inheritDoc}
 326      * @param p  {@inheritDoc}
 327      * @return the result of scanning
 328      */
 329     @Override
 330     public R visitSwitch(SwitchTree node, P p) {
 331         R r = scan(node.getExpression(), p);
 332         r = scanAndReduce(node.getCases(), p, r);
 333         return r;
 334     }
 335 
 336     /**
 337      * {@preview Associated with switch expressions, a preview feature of
 338      *           the Java language.
 339      *
 340      *           This method is associated with <i>switch expressions</i>, a preview
 341      *           feature of the Java language. Preview features
 342      *           may be removed in a future release, or upgraded to permanent
 343      *           features of the Java language.}
 344      *
 345      * {@inheritDoc} This implementation scans the children in left to right order.
 346      *
 347      * @param node  {@inheritDoc}
 348      * @param p  {@inheritDoc}
 349      * @return the result of scanning
 350      */
 351     @Override
 352     @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SWITCH_EXPRESSIONS)
 353     @SuppressWarnings("preview")
 354     public R visitSwitchExpression(SwitchExpressionTree node, P p) {
 355         R r = scan(node.getExpression(), p);
 356         r = scanAndReduce(node.getCases(), p, r);
 357         return r;
 358     }
 359 
 360     /**
 361      * {@inheritDoc} This implementation scans the children in left to right order.
 362      *
 363      * @param node  {@inheritDoc}
 364      * @param p  {@inheritDoc}
 365      * @return the result of scanning
 366      */
 367     @Override
 368     @SuppressWarnings("preview")
 369     public R visitCase(CaseTree node, P p) {
 370         R r = scan(node.getExpressions(), p);
 371         if (node.getCaseKind() == CaseTree.CaseKind.RULE)
 372             r = scanAndReduce(node.getBody(), p, r);
 373         else
 374             r = scanAndReduce(node.getStatements(), p, r);
 375         return r;
 376     }
 377 
 378     /**
 379      * {@inheritDoc} This implementation scans the children in left to right order.
 380      *
 381      * @param node  {@inheritDoc}
 382      * @param p  {@inheritDoc}
 383      * @return the result of scanning
 384      */
 385     @Override
 386     public R visitSynchronized(SynchronizedTree node, P p) {
 387         R r = scan(node.getExpression(), p);
 388         r = scanAndReduce(node.getBlock(), p, r);
 389         return r;
 390     }
 391 
 392     /**
 393      * {@inheritDoc} This implementation scans the children in left to right order.
 394      *
 395      * @param node  {@inheritDoc}
 396      * @param p  {@inheritDoc}
 397      * @return the result of scanning
 398      */
 399     @Override
 400     public R visitTry(TryTree node, P p) {
 401         R r = scan(node.getResources(), p);
 402         r = scanAndReduce(node.getBlock(), p, r);
 403         r = scanAndReduce(node.getCatches(), p, r);
 404         r = scanAndReduce(node.getFinallyBlock(), p, r);
 405         return r;
 406     }
 407 
 408     /**
 409      * {@inheritDoc} This implementation scans the children in left to right order.
 410      *
 411      * @param node  {@inheritDoc}
 412      * @param p  {@inheritDoc}
 413      * @return the result of scanning
 414      */
 415     @Override
 416     public R visitCatch(CatchTree node, P p) {
 417         R r = scan(node.getParameter(), p);
 418         r = scanAndReduce(node.getBlock(), p, r);
 419         return r;
 420     }
 421 
 422     /**
 423      * {@inheritDoc} This implementation scans the children in left to right order.
 424      *
 425      * @param node  {@inheritDoc}
 426      * @param p  {@inheritDoc}
 427      * @return the result of scanning
 428      */
 429     @Override
 430     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
 431         R r = scan(node.getCondition(), p);
 432         r = scanAndReduce(node.getTrueExpression(), p, r);
 433         r = scanAndReduce(node.getFalseExpression(), p, r);
 434         return r;
 435     }
 436 
 437     /**
 438      * {@inheritDoc} This implementation scans the children in left to right order.
 439      *
 440      * @param node  {@inheritDoc}
 441      * @param p  {@inheritDoc}
 442      * @return the result of scanning
 443      */
 444     @Override
 445     public R visitIf(IfTree node, P p) {
 446         R r = scan(node.getCondition(), p);
 447         r = scanAndReduce(node.getThenStatement(), p, r);
 448         r = scanAndReduce(node.getElseStatement(), p, r);
 449         return r;
 450     }
 451 
 452     /**
 453      * {@inheritDoc} This implementation scans the children in left to right order.
 454      *
 455      * @param node  {@inheritDoc}
 456      * @param p  {@inheritDoc}
 457      * @return the result of scanning
 458      */
 459     @Override
 460     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 461         return scan(node.getExpression(), p);
 462     }
 463 
 464     /**
 465      * {@inheritDoc} This implementation returns {@code null}.
 466      *
 467      * @param node  {@inheritDoc}
 468      * @param p  {@inheritDoc}
 469      * @return the result of scanning
 470      */
 471     @Override
 472     public R visitBreak(BreakTree node, P p) {
 473         return null;
 474     }
 475 
 476     /**
 477      * {@inheritDoc} This implementation returns {@code null}.
 478      *
 479      * @param node  {@inheritDoc}
 480      * @param p  {@inheritDoc}
 481      * @return the result of scanning
 482      */
 483     @Override
 484     public R visitContinue(ContinueTree node, P p) {
 485         return null;
 486     }
 487 
 488     /**
 489      * {@inheritDoc} This implementation scans the children in left to right order.
 490      *
 491      * @param node  {@inheritDoc}
 492      * @param p  {@inheritDoc}
 493      * @return the result of scanning
 494      */
 495     @Override
 496     public R visitReturn(ReturnTree node, P p) {
 497         return scan(node.getExpression(), p);
 498     }
 499 
 500     /**
 501      * {@inheritDoc} This implementation scans the children in left to right order.
 502      *
 503      * @param node  {@inheritDoc}
 504      * @param p  {@inheritDoc}
 505      * @return the result of scanning
 506      */
 507     @Override
 508     public R visitThrow(ThrowTree node, P p) {
 509         return scan(node.getExpression(), p);
 510     }
 511 
 512     /**
 513      * {@inheritDoc} This implementation scans the children in left to right order.
 514      *
 515      * @param node  {@inheritDoc}
 516      * @param p  {@inheritDoc}
 517      * @return the result of scanning
 518      */
 519     @Override
 520     public R visitAssert(AssertTree node, P p) {
 521         R r = scan(node.getCondition(), p);
 522         r = scanAndReduce(node.getDetail(), p, r);
 523         return r;
 524     }
 525 
 526     /**
 527      * {@inheritDoc} This implementation scans the children in left to right order.
 528      *
 529      * @param node  {@inheritDoc}
 530      * @param p  {@inheritDoc}
 531      * @return the result of scanning
 532      */
 533     @Override
 534     public R visitMethodInvocation(MethodInvocationTree node, P p) {
 535         R r = scan(node.getTypeArguments(), p);
 536         r = scanAndReduce(node.getMethodSelect(), p, r);
 537         r = scanAndReduce(node.getArguments(), p, r);
 538         return r;
 539     }
 540 
 541     /**
 542      * {@inheritDoc} This implementation scans the children in left to right order.
 543      *
 544      * @param node  {@inheritDoc}
 545      * @param p  {@inheritDoc}
 546      * @return the result of scanning
 547      */
 548     @Override
 549     public R visitNewClass(NewClassTree node, P p) {
 550         R r = scan(node.getEnclosingExpression(), p);
 551         r = scanAndReduce(node.getIdentifier(), p, r);
 552         r = scanAndReduce(node.getTypeArguments(), p, r);
 553         r = scanAndReduce(node.getArguments(), p, r);
 554         r = scanAndReduce(node.getClassBody(), p, r);
 555         return r;
 556     }
 557 
 558     /**
 559      * {@inheritDoc} This implementation scans the children in left to right order.
 560      *
 561      * @param node  {@inheritDoc}
 562      * @param p  {@inheritDoc}
 563      * @return the result of scanning
 564      */
 565     @Override
 566     public R visitNewArray(NewArrayTree node, P p) {
 567         R r = scan(node.getType(), p);
 568         r = scanAndReduce(node.getDimensions(), p, r);
 569         r = scanAndReduce(node.getInitializers(), p, r);
 570         r = scanAndReduce(node.getAnnotations(), p, r);
 571         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 572             r = scanAndReduce(dimAnno, p, r);
 573         }
 574         return r;
 575     }
 576 
 577     /**
 578      * {@inheritDoc} This implementation scans the children in left to right order.
 579      *
 580      * @param node  {@inheritDoc}
 581      * @param p  {@inheritDoc}
 582      * @return the result of scanning
 583      */
 584     @Override
 585     public R visitLambdaExpression(LambdaExpressionTree node, P p) {
 586         R r = scan(node.getParameters(), p);
 587         r = scanAndReduce(node.getBody(), p, r);
 588         return r;
 589     }
 590 
 591     /**
 592      * {@inheritDoc} This implementation scans the children in left to right order.
 593      *
 594      * @param node  {@inheritDoc}
 595      * @param p  {@inheritDoc}
 596      * @return the result of scanning
 597      */
 598     @Override
 599     public R visitParenthesized(ParenthesizedTree node, P p) {
 600         return scan(node.getExpression(), p);
 601     }
 602 
 603     /**
 604      * {@inheritDoc} This implementation scans the children in left to right order.
 605      *
 606      * @param node  {@inheritDoc}
 607      * @param p  {@inheritDoc}
 608      * @return the result of scanning
 609      */
 610     @Override
 611     public R visitAssignment(AssignmentTree node, P p) {
 612         R r = scan(node.getVariable(), p);
 613         r = scanAndReduce(node.getExpression(), p, r);
 614         return r;
 615     }
 616 
 617     /**
 618      * {@inheritDoc} This implementation scans the children in left to right order.
 619      *
 620      * @param node  {@inheritDoc}
 621      * @param p  {@inheritDoc}
 622      * @return the result of scanning
 623      */
 624     @Override
 625     public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
 626         R r = scan(node.getVariable(), p);
 627         r = scanAndReduce(node.getExpression(), p, r);
 628         return r;
 629     }
 630 
 631     /**
 632      * {@inheritDoc} This implementation scans the children in left to right order.
 633      *
 634      * @param node  {@inheritDoc}
 635      * @param p  {@inheritDoc}
 636      * @return the result of scanning
 637      */
 638     @Override
 639     public R visitUnary(UnaryTree node, P p) {
 640         return scan(node.getExpression(), p);
 641     }
 642 
 643     /**
 644      * {@inheritDoc} This implementation scans the children in left to right order.
 645      *
 646      * @param node  {@inheritDoc}
 647      * @param p  {@inheritDoc}
 648      * @return the result of scanning
 649      */
 650     @Override
 651     public R visitBinary(BinaryTree node, P p) {
 652         R r = scan(node.getLeftOperand(), p);
 653         r = scanAndReduce(node.getRightOperand(), p, r);
 654         return r;
 655     }
 656 
 657     /**
 658      * {@inheritDoc} This implementation scans the children in left to right order.
 659      *
 660      * @param node  {@inheritDoc}
 661      * @param p  {@inheritDoc}
 662      * @return the result of scanning
 663      */
 664     @Override
 665     public R visitTypeCast(TypeCastTree node, P p) {
 666         R r = scan(node.getType(), p);
 667         r = scanAndReduce(node.getExpression(), p, r);
 668         return r;
 669     }
 670 
 671     /**
 672      * {@inheritDoc} This implementation scans the children in left to right order.
 673      *
 674      * @param node  {@inheritDoc}
 675      * @param p  {@inheritDoc}
 676      * @return the result of scanning
 677      */
 678     @Override
 679     public R visitInstanceOf(InstanceOfTree node, P p) {
 680         R r = scan(node.getExpression(), p);
 681         r = scanAndReduce(node.getType(), p, r);
 682         return r;
 683     }
 684 
 685     /**
 686      * {@inheritDoc} This implementation scans the children in left to right order.
 687      *
 688      * @param node  {@inheritDoc}
 689      * @param p  {@inheritDoc}
 690      * @return the result of scanning
 691      */
 692     @Override
 693     public R visitArrayAccess(ArrayAccessTree node, P p) {
 694         R r = scan(node.getExpression(), p);
 695         r = scanAndReduce(node.getIndex(), p, r);
 696         return r;
 697     }
 698 
 699     /**
 700      * {@inheritDoc} This implementation scans the children in left to right order.
 701      *
 702      * @param node  {@inheritDoc}
 703      * @param p  {@inheritDoc}
 704      * @return the result of scanning
 705      */
 706     @Override
 707     public R visitMemberSelect(MemberSelectTree node, P p) {
 708         return scan(node.getExpression(), p);
 709     }
 710 
 711     /**
 712      * {@inheritDoc} This implementation scans the children in left to right order.
 713      *
 714      * @param node  {@inheritDoc}
 715      * @param p  {@inheritDoc}
 716      * @return the result of scanning
 717      */
 718     @Override
 719     public R visitMemberReference(MemberReferenceTree node, P p) {
 720         R r = scan(node.getQualifierExpression(), p);
 721         r = scanAndReduce(node.getTypeArguments(), p, r);
 722         return r;
 723     }
 724 
 725     /**
 726      * {@inheritDoc} This implementation returns {@code null}.
 727      *
 728      * @param node  {@inheritDoc}
 729      * @param p  {@inheritDoc}
 730      * @return the result of scanning
 731      */
 732     @Override
 733     public R visitIdentifier(IdentifierTree node, P p) {
 734         return null;
 735     }
 736 
 737     /**
 738      * {@inheritDoc} This implementation returns {@code null}.
 739      *
 740      * @param node  {@inheritDoc}
 741      * @param p  {@inheritDoc}
 742      * @return the result of scanning
 743      */
 744     @Override
 745     public R visitLiteral(LiteralTree node, P p) {
 746         return null;
 747     }
 748 
 749     /**
 750      * {@inheritDoc} This implementation returns {@code null}.
 751      *
 752      * @param node  {@inheritDoc}
 753      * @param p  {@inheritDoc}
 754      * @return the result of scanning
 755      */
 756     @Override
 757     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 758         return null;
 759     }
 760 
 761     /**
 762      * {@inheritDoc} This implementation scans the children in left to right order.
 763      *
 764      * @param node  {@inheritDoc}
 765      * @param p  {@inheritDoc}
 766      * @return the result of scanning
 767      */
 768     @Override
 769     public R visitArrayType(ArrayTypeTree node, P p) {
 770         return scan(node.getType(), p);
 771     }
 772 
 773     /**
 774      * {@inheritDoc} This implementation scans the children in left to right order.
 775      *
 776      * @param node  {@inheritDoc}
 777      * @param p  {@inheritDoc}
 778      * @return the result of scanning
 779      */
 780     @Override
 781     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 782         R r = scan(node.getType(), p);
 783         r = scanAndReduce(node.getTypeArguments(), p, r);
 784         return r;
 785     }
 786 
 787     /**
 788      * {@inheritDoc} This implementation scans the children in left to right order.
 789      *
 790      * @param node  {@inheritDoc}
 791      * @param p  {@inheritDoc}
 792      * @return the result of scanning
 793      */
 794     @Override
 795     public R visitUnionType(UnionTypeTree node, P p) {
 796         return scan(node.getTypeAlternatives(), p);
 797     }
 798 
 799     /**
 800      * {@inheritDoc} This implementation scans the children in left to right order.
 801      *
 802      * @param node  {@inheritDoc}
 803      * @param p  {@inheritDoc}
 804      * @return the result of scanning
 805      */
 806     @Override
 807     public R visitIntersectionType(IntersectionTypeTree node, P p) {
 808         return scan(node.getBounds(), p);
 809     }
 810 
 811     /**
 812      * {@inheritDoc} This implementation scans the children in left to right order.
 813      *
 814      * @param node  {@inheritDoc}
 815      * @param p  {@inheritDoc}
 816      * @return the result of scanning
 817      */
 818     @Override
 819     public R visitTypeParameter(TypeParameterTree node, P p) {
 820         R r = scan(node.getAnnotations(), p);
 821         r = scanAndReduce(node.getBounds(), p, r);
 822         return r;
 823     }
 824 
 825     /**
 826      * {@inheritDoc} This implementation scans the children in left to right order.
 827      *
 828      * @param node  {@inheritDoc}
 829      * @param p  {@inheritDoc}
 830      * @return the result of scanning
 831      */
 832     @Override
 833     public R visitWildcard(WildcardTree node, P p) {
 834         return scan(node.getBound(), p);
 835     }
 836 
 837     /**
 838      * {@inheritDoc} This implementation scans the children in left to right order.
 839      *
 840      * @param node  {@inheritDoc}
 841      * @param p  {@inheritDoc}
 842      * @return the result of scanning
 843      */
 844     @Override
 845     public R visitModifiers(ModifiersTree node, P p) {
 846         return scan(node.getAnnotations(), p);
 847     }
 848 
 849     /**
 850      * {@inheritDoc} This implementation scans the children in left to right order.
 851      *
 852      * @param node  {@inheritDoc}
 853      * @param p  {@inheritDoc}
 854      * @return the result of scanning
 855      */
 856     @Override
 857     public R visitAnnotation(AnnotationTree node, P p) {
 858         R r = scan(node.getAnnotationType(), p);
 859         r = scanAndReduce(node.getArguments(), p, r);
 860         return r;
 861     }
 862 
 863     /**
 864      * {@inheritDoc} This implementation scans the children in left to right order.
 865      *
 866      * @param node  {@inheritDoc}
 867      * @param p  {@inheritDoc}
 868      * @return the result of scanning
 869      */
 870     @Override
 871     public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
 872         R r = scan(node.getAnnotations(), p);
 873         r = scanAndReduce(node.getUnderlyingType(), p, r);
 874         return r;
 875     }
 876 
 877     @Override
 878     public R visitModule(ModuleTree node, P p) {
 879         R r = scan(node.getAnnotations(), p);
 880         r = scanAndReduce(node.getName(), p, r);
 881         r = scanAndReduce(node.getDirectives(), p, r);
 882         return r;
 883     }
 884 
 885     @Override
 886     public R visitExports(ExportsTree node, P p) {
 887         R r = scan(node.getPackageName(), p);
 888         r = scanAndReduce(node.getModuleNames(), p, r);
 889         return r;
 890     }
 891 
 892     @Override
 893     public R visitOpens(OpensTree node, P p) {
 894         R r = scan(node.getPackageName(), p);
 895         r = scanAndReduce(node.getModuleNames(), p, r);
 896         return r;
 897     }
 898 
 899     @Override
 900     public R visitProvides(ProvidesTree node, P p) {
 901         R r = scan(node.getServiceName(), p);
 902         r = scanAndReduce(node.getImplementationNames(), p, r);
 903         return r;
 904     }
 905 
 906     @Override
 907     public R visitRequires(RequiresTree node, P p) {
 908         return scan(node.getModuleName(), p);
 909     }
 910 
 911     @Override
 912     public R visitUses(UsesTree node, P p) {
 913         return scan(node.getServiceName(), p);
 914     }
 915 
 916     /**
 917      * {@inheritDoc} This implementation returns {@code null}.
 918      *
 919      * @param node  {@inheritDoc}
 920      * @param p  {@inheritDoc}
 921      * @return the result of scanning
 922      */
 923     @Override
 924     public R visitOther(Tree node, P p) {
 925         return null;
 926     }
 927 
 928     /**
 929      * {@inheritDoc} This implementation returns {@code null}.
 930      *
 931      * @param node  {@inheritDoc}
 932      * @param p  {@inheritDoc}
 933      * @return the result of scanning
 934      */
 935     @Override
 936     public R visitErroneous(ErroneousTree node, P p) {
 937         return null;
 938     }
 939 
 940     /**
 941      * {@preview Associated with switch expressions, a preview feature of
 942      *           the Java language.
 943      *
 944      *           This method is associated with <i>switch expressions</i>, a preview
 945      *           feature of the Java language. Preview features
 946      *           may be removed in a future release, or upgraded to permanent
 947      *           features of the Java language.}
 948      *
 949      * {@inheritDoc} This implementation returns {@code null}.
 950      *
 951      * @param node  {@inheritDoc}
 952      * @param p  {@inheritDoc}
 953      * @return the result of scanning
 954      */
 955     @Override
 956     @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SWITCH_EXPRESSIONS)
 957     @SuppressWarnings("preview")
 958     public R visitYield(YieldTree node, P p) {
 959         return scan(node.getValue(), p);
 960     }
 961 }