1 /*
   2  * Copyright (c) 2005, 2022, 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>Here is an example to count the number of identifier nodes in a tree:
  38  * <pre>
  39  *   class CountIdentifiers extends TreeScanner&lt;Integer,Void&gt; {
  40  *      {@literal @}Override
  41  *      public Integer visitIdentifier(IdentifierTree node, Void p) {
  42  *          return 1;
  43  *      }
  44  *      {@literal @}Override
  45  *      public Integer reduce(Integer r1, Integer r2) {
  46  *          return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
  47  *      }
  48  *   }
  49  * </pre>
  50  *
  51  * @implSpec
  52  * <p>The default implementation of the visitXYZ methods will determine
  53  * a result as follows:
  54  * <ul>
  55  * <li>If the node being visited has no children, the result will be {@code null}.
  56  * <li>If the node being visited has one child, the result will be the
  57  * result of calling {@code scan} with that child. The child may be a simple node
  58  * or itself a list of nodes.
  59  * <li>If the node being visited has more than one child, the result will
  60  * be determined by calling {@code scan} with each child in turn, and then combining the
  61  * result of each scan after the first with the cumulative result
  62  * so far, as determined by the {@link #reduce} method. Each child may be either
  63  * a simple node or a list of nodes. The default behavior of the {@code reduce}
  64  * method is such that the result of the visitXYZ method will be the result of
  65  * the last child scanned.
  66  * </ul>
  67  *
  68  * @param <R> the return type of this visitor's methods.  Use {@link
  69  *            Void} for visitors that do not need to return results.
  70  * @param <P> the type of the additional parameter to this visitor's
  71  *            methods.  Use {@code Void} for visitors that do not need an
  72  *            additional parameter.
  73  *
  74  * @author Peter von der Ah&eacute;
  75  * @author Jonathan Gibbons
  76  * @since 1.6
  77  */
  78 public class TreeScanner<R,P> implements TreeVisitor<R,P> {
  79     /**
  80      * Constructs a {@code TreeScanner}.
  81      */
  82     public TreeScanner() {}
  83 
  84     /**
  85      * Scans a single node.
  86      * @param tree the node to be scanned
  87      * @param p a parameter value passed to the visit method
  88      * @return the result value from the visit method
  89      */
  90     public R scan(Tree tree, P p) {
  91         return (tree == null) ? null : tree.accept(this, p);
  92     }
  93 
  94     private R scanAndReduce(Tree node, P p, R r) {
  95         return reduce(scan(node, p), r);
  96     }
  97 
  98     /**
  99      * Scans a sequence of nodes.
 100      * @param nodes the nodes to be scanned
 101      * @param p a parameter value to be passed to the visit method for each node
 102      * @return the combined return value from the visit methods.
 103      *      The values are combined using the {@link #reduce reduce} method.
 104      */
 105     public R scan(Iterable<? extends Tree> nodes, P p) {
 106         R r = null;
 107         if (nodes != null) {
 108             boolean first = true;
 109             for (Tree node : nodes) {
 110                 r = (first ? scan(node, p) : scanAndReduce(node, p, r));
 111                 first = false;
 112             }
 113         }
 114         return r;
 115     }
 116 
 117     private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {
 118         return reduce(scan(nodes, p), r);
 119     }
 120 
 121     /**
 122      * Reduces two results into a combined result.
 123      * The default implementation is to return the first parameter.
 124      * The general contract of the method is that it may take any action whatsoever.
 125      * @param r1 the first of the values to be combined
 126      * @param r2 the second of the values to be combined
 127      * @return the result of combining the two parameters
 128      */
 129     public R reduce(R r1, R r2) {
 130         return r1;
 131     }
 132 
 133 
 134 /* ***************************************************************************
 135  * Visitor methods
 136  ****************************************************************************/
 137 
 138     /**
 139      * {@inheritDoc}
 140      *
 141      * @implSpec This implementation scans the children in left to right order.
 142      *
 143      * @param node  {@inheritDoc}
 144      * @param p  {@inheritDoc}
 145      * @return the result of scanning
 146      */
 147     @Override
 148     public R visitCompilationUnit(CompilationUnitTree node, P p) {
 149         R r = scan(node.getPackage(), p);
 150         r = scanAndReduce(node.getImports(), p, r);
 151         r = scanAndReduce(node.getTypeDecls(), p, r);
 152         r = scanAndReduce(node.getModule(), p, r);
 153         return r;
 154     }
 155 
 156     /**
 157      * {@inheritDoc}
 158      *
 159      * @implSpec This implementation scans the children in left to right order.
 160      *
 161      * @param node  {@inheritDoc}
 162      * @param p  {@inheritDoc}
 163      * @return the result of scanning
 164      */
 165     @Override
 166     public R visitPackage(PackageTree node, P p) {
 167         R r = scan(node.getAnnotations(), p);
 168         r = scanAndReduce(node.getPackageName(), p, r);
 169         return r;
 170     }
 171 
 172     /**
 173      * {@inheritDoc}
 174      *
 175      * @implSpec 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 visitImport(ImportTree node, P p) {
 183         return scan(node.getQualifiedIdentifier(), p);
 184     }
 185 
 186     /**
 187      * {@inheritDoc}
 188      *
 189      * @implSpec This implementation scans the children in left to right order.
 190      *
 191      * @param node  {@inheritDoc}
 192      * @param p  {@inheritDoc}
 193      * @return the result of scanning
 194      */
 195     @Override
 196     public R visitClass(ClassTree node, P p) {
 197         R r = scan(node.getModifiers(), p);
 198         r = scanAndReduce(node.getTypeParameters(), p, r);
 199         r = scanAndReduce(node.getExtendsClause(), p, r);
 200         r = scanAndReduce(node.getImplementsClause(), p, r);
 201         r = scanAndReduce(node.getPermitsClause(), p, r);
 202         r = scanAndReduce(node.getMembers(), p, r);
 203         return r;
 204     }
 205 
 206     /**
 207      * {@inheritDoc}
 208      *
 209      * @implSpec This implementation scans the children in left to right order.
 210      *
 211      * @param node  {@inheritDoc}
 212      * @param p  {@inheritDoc}
 213      * @return the result of scanning
 214      */
 215     @Override
 216     public R visitMethod(MethodTree node, P p) {
 217         R r = scan(node.getModifiers(), p);
 218         r = scanAndReduce(node.getReturnType(), p, r);
 219         r = scanAndReduce(node.getTypeParameters(), p, r);
 220         r = scanAndReduce(node.getParameters(), p, r);
 221         r = scanAndReduce(node.getReceiverParameter(), p, r);
 222         r = scanAndReduce(node.getThrows(), p, r);
 223         r = scanAndReduce(node.getBody(), p, r);
 224         r = scanAndReduce(node.getDefaultValue(), p, r);
 225         return r;
 226     }
 227 
 228     /**
 229      * {@inheritDoc}
 230      *
 231      * @implSpec This implementation scans the children in left to right order.
 232      *
 233      * @param node  {@inheritDoc}
 234      * @param p  {@inheritDoc}
 235      * @return the result of scanning
 236      */
 237     @Override
 238     public R visitVariable(VariableTree node, P p) {
 239         R r = scan(node.getModifiers(), p);
 240         r = scanAndReduce(node.getType(), p, r);
 241         r = scanAndReduce(node.getNameExpression(), p, r);
 242         r = scanAndReduce(node.getInitializer(), p, r);
 243         return r;
 244     }
 245 
 246     /**
 247      * {@inheritDoc}
 248      *
 249      * @implSpec This implementation returns {@code null}.
 250      *
 251      * @param node  {@inheritDoc}
 252      * @param p  {@inheritDoc}
 253      * @return the result of scanning
 254      */
 255     @Override
 256     public R visitEmptyStatement(EmptyStatementTree node, P p) {
 257         return null;
 258     }
 259 
 260     /**
 261      * {@inheritDoc}
 262      *
 263      * @implSpec This implementation scans the children in left to right order.
 264      *
 265      * @param node  {@inheritDoc}
 266      * @param p  {@inheritDoc}
 267      * @return the result of scanning
 268      */
 269     @Override
 270     public R visitBlock(BlockTree node, P p) {
 271         return scan(node.getStatements(), p);
 272     }
 273 
 274     /**
 275      * {@inheritDoc}
 276      *
 277      * @implSpec This implementation scans the children in left to right order.
 278      *
 279      * @param node  {@inheritDoc}
 280      * @param p  {@inheritDoc}
 281      * @return the result of scanning
 282      */
 283     @Override
 284     public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
 285         R r = scan(node.getStatement(), p);
 286         r = scanAndReduce(node.getCondition(), p, r);
 287         return r;
 288     }
 289 
 290     /**
 291      * {@inheritDoc}
 292      *
 293      * @implSpec This implementation scans the children in left to right order.
 294      *
 295      * @param node  {@inheritDoc}
 296      * @param p  {@inheritDoc}
 297      * @return the result of scanning
 298      */
 299     @Override
 300     public R visitWhileLoop(WhileLoopTree node, P p) {
 301         R r = scan(node.getCondition(), p);
 302         r = scanAndReduce(node.getStatement(), p, r);
 303         return r;
 304     }
 305 
 306     /**
 307      * {@inheritDoc} This implementation scans the children in left to right order.
 308      *
 309      * @param node  {@inheritDoc}
 310      * @param p  {@inheritDoc}
 311      * @return the result of scanning
 312      */
 313     @Override
 314     public R visitForLoop(ForLoopTree node, P p) {
 315         R r = scan(node.getInitializer(), p);
 316         r = scanAndReduce(node.getCondition(), p, r);
 317         r = scanAndReduce(node.getUpdate(), p, r);
 318         r = scanAndReduce(node.getStatement(), p, r);
 319         return r;
 320     }
 321 
 322     /**
 323      * {@inheritDoc}
 324      *
 325      * @implSpec This implementation scans the children in left to right order.
 326      *
 327      * @param node  {@inheritDoc}
 328      * @param p  {@inheritDoc}
 329      * @return the result of scanning
 330      */
 331     @Override
 332     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
 333         R r = scan(node.getVariable(), p);
 334         r = scanAndReduce(node.getExpression(), p, r);
 335         r = scanAndReduce(node.getStatement(), p, r);
 336         return r;
 337     }
 338 
 339     /**
 340      * {@inheritDoc}
 341      *
 342      * @implSpec This implementation scans the children in left to right order.
 343      *
 344      * @param node  {@inheritDoc}
 345      * @param p  {@inheritDoc}
 346      * @return the result of scanning
 347      */
 348     @Override
 349     public R visitLabeledStatement(LabeledStatementTree node, P p) {
 350         return scan(node.getStatement(), p);
 351     }
 352 
 353     /**
 354      * {@inheritDoc}
 355      *
 356      * @implSpec This implementation scans the children in left to right order.
 357      *
 358      * @param node  {@inheritDoc}
 359      * @param p  {@inheritDoc}
 360      * @return the result of scanning
 361      */
 362     @Override
 363     public R visitSwitch(SwitchTree node, P p) {
 364         R r = scan(node.getExpression(), p);
 365         r = scanAndReduce(node.getCases(), p, r);
 366         return r;
 367     }
 368 
 369     /**
 370      * {@inheritDoc}
 371      *
 372      * @implSpec This implementation scans the children in left to right order.
 373      *
 374      * @param node  {@inheritDoc}
 375      * @param p  {@inheritDoc}
 376      * @return the result of scanning
 377      *
 378      * @since 14
 379      */
 380     @Override
 381     public R visitSwitchExpression(SwitchExpressionTree node, P p) {
 382         R r = scan(node.getExpression(), p);
 383         r = scanAndReduce(node.getCases(), p, r);
 384         return r;
 385     }
 386 
 387     /**
 388      * {@inheritDoc}
 389      *
 390      * @implSpec This implementation scans the children in left to right order.
 391      *
 392      * @param node  {@inheritDoc}
 393      * @param p  {@inheritDoc}
 394      * @return the result of scanning
 395      */
 396     @Override
 397     public R visitCase(CaseTree node, P p) {
 398         R r = scan(node.getLabels(), p);
 399         r = scanAndReduce(node.getGuard(), p, r);
 400         if (node.getCaseKind() == CaseTree.CaseKind.RULE)
 401             r = scanAndReduce(node.getBody(), p, r);
 402         else
 403             r = scanAndReduce(node.getStatements(), p, r);
 404         return r;
 405     }
 406 
 407     /**
 408      * {@inheritDoc}
 409      *
 410      * @implSpec This implementation scans the children in left to right order.
 411      *
 412      * @param node  {@inheritDoc}
 413      * @param p  {@inheritDoc}
 414      * @return the result of scanning
 415      */
 416     @Override
 417     public R visitSynchronized(SynchronizedTree node, P p) {
 418         R r = scan(node.getExpression(), p);
 419         r = scanAndReduce(node.getBlock(), p, r);
 420         return r;
 421     }
 422 
 423     /**
 424      * {@inheritDoc}
 425      *
 426      * @implSpec This implementation scans the children in left to right order.
 427      *
 428      * @param node  {@inheritDoc}
 429      * @param p  {@inheritDoc}
 430      * @return the result of scanning
 431      */
 432     @Override
 433     public R visitTry(TryTree node, P p) {
 434         R r = scan(node.getResources(), p);
 435         r = scanAndReduce(node.getBlock(), p, r);
 436         r = scanAndReduce(node.getCatches(), p, r);
 437         r = scanAndReduce(node.getFinallyBlock(), p, r);
 438         return r;
 439     }
 440 
 441     /**
 442      * {@inheritDoc}
 443      *
 444      * @implSpec This implementation scans the children in left to right order.
 445      *
 446      * @param node  {@inheritDoc}
 447      * @param p  {@inheritDoc}
 448      * @return the result of scanning
 449      */
 450     @Override
 451     public R visitCatch(CatchTree node, P p) {
 452         R r = scan(node.getParameter(), p);
 453         r = scanAndReduce(node.getBlock(), p, r);
 454         return r;
 455     }
 456 
 457     /**
 458      * {@inheritDoc}
 459      *
 460      * @implSpec This implementation scans the children in left to right order.
 461      *
 462      * @param node  {@inheritDoc}
 463      * @param p  {@inheritDoc}
 464      * @return the result of scanning
 465      */
 466     @Override
 467     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
 468         R r = scan(node.getCondition(), p);
 469         r = scanAndReduce(node.getTrueExpression(), p, r);
 470         r = scanAndReduce(node.getFalseExpression(), p, r);
 471         return r;
 472     }
 473 
 474     /**
 475      * {@inheritDoc}
 476      *
 477      * @implSpec This implementation scans the children in left to right order.
 478      *
 479      * @param node  {@inheritDoc}
 480      * @param p  {@inheritDoc}
 481      * @return the result of scanning
 482      */
 483     @Override
 484     public R visitIf(IfTree node, P p) {
 485         R r = scan(node.getCondition(), p);
 486         r = scanAndReduce(node.getThenStatement(), p, r);
 487         r = scanAndReduce(node.getElseStatement(), p, r);
 488         return r;
 489     }
 490 
 491     /**
 492      * {@inheritDoc}
 493      *
 494      * @implSpec This implementation scans the children in left to right order.
 495      *
 496      * @param node  {@inheritDoc}
 497      * @param p  {@inheritDoc}
 498      * @return the result of scanning
 499      */
 500     @Override
 501     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 502         return scan(node.getExpression(), p);
 503     }
 504 
 505     /**
 506      * {@inheritDoc}
 507      *
 508      * @implSpec This implementation returns {@code null}.
 509      *
 510      * @param node  {@inheritDoc}
 511      * @param p  {@inheritDoc}
 512      * @return the result of scanning
 513      */
 514     @Override
 515     public R visitBreak(BreakTree node, P p) {
 516         return null;
 517     }
 518 
 519     /**
 520      * {@inheritDoc}
 521      *
 522      * @implSpec This implementation returns {@code null}.
 523      *
 524      * @param node  {@inheritDoc}
 525      * @param p  {@inheritDoc}
 526      * @return the result of scanning
 527      */
 528     @Override
 529     public R visitContinue(ContinueTree node, P p) {
 530         return null;
 531     }
 532 
 533     /**
 534      * {@inheritDoc}
 535      *
 536      * @implSpec This implementation scans the children in left to right order.
 537      *
 538      * @param node  {@inheritDoc}
 539      * @param p  {@inheritDoc}
 540      * @return the result of scanning
 541      */
 542     @Override
 543     public R visitReturn(ReturnTree node, P p) {
 544         return scan(node.getExpression(), p);
 545     }
 546 
 547     /**
 548      * {@inheritDoc}
 549      *
 550      * @implSpec This implementation scans the children in left to right order.
 551      *
 552      * @param node  {@inheritDoc}
 553      * @param p  {@inheritDoc}
 554      * @return the result of scanning
 555      */
 556     @Override
 557     public R visitThrow(ThrowTree node, P p) {
 558         return scan(node.getExpression(), p);
 559     }
 560 
 561     /**
 562      * {@inheritDoc}
 563      *
 564      * @implSpec This implementation scans the children in left to right order.
 565      *
 566      * @param node  {@inheritDoc}
 567      * @param p  {@inheritDoc}
 568      * @return the result of scanning
 569      */
 570     @Override
 571     public R visitAssert(AssertTree node, P p) {
 572         R r = scan(node.getCondition(), p);
 573         r = scanAndReduce(node.getDetail(), p, r);
 574         return r;
 575     }
 576 
 577     /**
 578      * {@inheritDoc}
 579      *
 580      * @implSpec This implementation scans the children in left to right order.
 581      *
 582      * @param node  {@inheritDoc}
 583      * @param p  {@inheritDoc}
 584      * @return the result of scanning
 585      */
 586     @Override
 587     public R visitMethodInvocation(MethodInvocationTree node, P p) {
 588         R r = scan(node.getTypeArguments(), p);
 589         r = scanAndReduce(node.getMethodSelect(), p, r);
 590         r = scanAndReduce(node.getArguments(), p, r);
 591         return r;
 592     }
 593 
 594     /**
 595      * {@inheritDoc}
 596      *
 597      * @implSpec This implementation scans the children in left to right order.
 598      *
 599      * @param node  {@inheritDoc}
 600      * @param p  {@inheritDoc}
 601      * @return the result of scanning
 602      */
 603     @Override
 604     public R visitNewClass(NewClassTree node, P p) {
 605         R r = scan(node.getEnclosingExpression(), p);
 606         r = scanAndReduce(node.getIdentifier(), p, r);
 607         r = scanAndReduce(node.getTypeArguments(), p, r);
 608         r = scanAndReduce(node.getArguments(), p, r);
 609         r = scanAndReduce(node.getClassBody(), p, r);
 610         return r;
 611     }
 612 
 613     /**
 614      * {@inheritDoc}
 615      *
 616      * @implSpec This implementation scans the children in left to right order.
 617      *
 618      * @param node  {@inheritDoc}
 619      * @param p  {@inheritDoc}
 620      * @return the result of scanning
 621      */
 622     @Override
 623     public R visitNewArray(NewArrayTree node, P p) {
 624         R r = scan(node.getType(), p);
 625         r = scanAndReduce(node.getDimensions(), p, r);
 626         r = scanAndReduce(node.getInitializers(), p, r);
 627         r = scanAndReduce(node.getAnnotations(), p, r);
 628         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 629             r = scanAndReduce(dimAnno, p, r);
 630         }
 631         return r;
 632     }
 633 
 634     /**
 635      * {@inheritDoc}
 636      *
 637      * @implSpec This implementation scans the children in left to right order.
 638      *
 639      * @param node  {@inheritDoc}
 640      * @param p  {@inheritDoc}
 641      * @return the result of scanning
 642      */
 643     @Override
 644     public R visitLambdaExpression(LambdaExpressionTree node, P p) {
 645         R r = scan(node.getParameters(), p);
 646         r = scanAndReduce(node.getBody(), p, r);
 647         return r;
 648     }
 649 
 650     /**
 651      * {@inheritDoc}
 652      *
 653      * @implSpec This implementation scans the children in left to right order.
 654      *
 655      * @param node  {@inheritDoc}
 656      * @param p  {@inheritDoc}
 657      * @return the result of scanning
 658      */
 659     @Override
 660     public R visitParenthesized(ParenthesizedTree node, P p) {
 661         return scan(node.getExpression(), p);
 662     }
 663 
 664     /**
 665      * {@inheritDoc}
 666      *
 667      * @implSpec This implementation scans the children in left to right order.
 668      *
 669      * @param node  {@inheritDoc}
 670      * @param p  {@inheritDoc}
 671      * @return the result of scanning
 672      */
 673     @Override
 674     public R visitAssignment(AssignmentTree node, P p) {
 675         R r = scan(node.getVariable(), p);
 676         r = scanAndReduce(node.getExpression(), p, r);
 677         return r;
 678     }
 679 
 680     /**
 681      * {@inheritDoc}
 682      *
 683      * @implSpec This implementation scans the children in left to right order.
 684      *
 685      * @param node  {@inheritDoc}
 686      * @param p  {@inheritDoc}
 687      * @return the result of scanning
 688      */
 689     @Override
 690     public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
 691         R r = scan(node.getVariable(), p);
 692         r = scanAndReduce(node.getExpression(), p, r);
 693         return r;
 694     }
 695 
 696     /**
 697      * {@inheritDoc}
 698      *
 699      * @implSpec This implementation scans the children in left to right order.
 700      *
 701      * @param node  {@inheritDoc}
 702      * @param p  {@inheritDoc}
 703      * @return the result of scanning
 704      */
 705     @Override
 706     public R visitUnary(UnaryTree node, P p) {
 707         return scan(node.getExpression(), p);
 708     }
 709 
 710     /**
 711      * {@inheritDoc}
 712      *
 713      * @implSpec This implementation scans the children in left to right order.
 714      *
 715      * @param node  {@inheritDoc}
 716      * @param p  {@inheritDoc}
 717      * @return the result of scanning
 718      */
 719     @Override
 720     public R visitBinary(BinaryTree node, P p) {
 721         R r = scan(node.getLeftOperand(), p);
 722         r = scanAndReduce(node.getRightOperand(), p, r);
 723         return r;
 724     }
 725 
 726     /**
 727      * {@inheritDoc}
 728      *
 729      * @implSpec This implementation scans the children in left to right order.
 730      *
 731      * @param node  {@inheritDoc}
 732      * @param p  {@inheritDoc}
 733      * @return the result of scanning
 734      */
 735     @Override
 736     public R visitTypeCast(TypeCastTree node, P p) {
 737         R r = scan(node.getType(), p);
 738         r = scanAndReduce(node.getExpression(), p, r);
 739         return r;
 740     }
 741 
 742     /**
 743      * {@inheritDoc}
 744      *
 745      * @implSpec This implementation scans the children in left to right order.
 746      *
 747      * @param node  {@inheritDoc}
 748      * @param p  {@inheritDoc}
 749      * @return the result of scanning
 750      */
 751     @Override
 752     public R visitInstanceOf(InstanceOfTree node, P p) {
 753         R r = scan(node.getExpression(), p);
 754         if (node.getPattern() != null) {
 755             r = scanAndReduce(node.getPattern(), p, r);
 756         } else {
 757             r = scanAndReduce(node.getType(), p, r);
 758         }
 759         return r;
 760     }
 761 
 762     /**
 763      * {@inheritDoc}
 764      *
 765      * @implSpec This implementation returns {@code null}.
 766      *
 767      * @param node  {@inheritDoc}
 768      * @param p  {@inheritDoc}
 769      * @return the result of scanning
 770      * @since 21
 771      */
 772     @Override
 773     public R visitAnyPattern(AnyPatternTree node, P p) {
 774         return null;
 775     }
 776 
 777     /**
 778      * {@inheritDoc}
 779      *
 780      * @implSpec This implementation scans the children in left to right order.
 781      *
 782      * @param node  {@inheritDoc}
 783      * @param p  {@inheritDoc}
 784      * @return the result of scanning
 785      * @since 14
 786      */
 787     @Override
 788     public R visitBindingPattern(BindingPatternTree node, P p) {
 789         return scan(node.getVariable(), p);
 790     }
 791 
 792     /**
 793      * {@inheritDoc}
 794      *
 795      * @implSpec This implementation returns {@code null}.
 796      *
 797      * @param node  {@inheritDoc}
 798      * @param p  {@inheritDoc}
 799      * @return the result of scanning
 800      * @since 21
 801      */
 802     @Override
 803     public R visitDefaultCaseLabel(DefaultCaseLabelTree node, P p) {
 804         return null;
 805     }
 806 
 807     /**
 808      * {@inheritDoc}
 809      *
 810      * @implSpec This implementation returns {@code null}.
 811      *
 812      * @param node  {@inheritDoc}
 813      * @param p  {@inheritDoc}
 814      * @return the result of scanning
 815      * @since 21
 816      */
 817     @Override
 818     public R visitConstantCaseLabel(ConstantCaseLabelTree node, P p) {
 819         return scan(node.getConstantExpression(), p);
 820     }
 821 
 822     /**
 823      * {@inheritDoc}
 824      *
 825      * @implSpec This implementation returns {@code null}.
 826      *
 827      * @param node  {@inheritDoc}
 828      * @param p  {@inheritDoc}
 829      * @return the result of scanning
 830      * @since 21
 831      */
 832     @Override
 833     public R visitPatternCaseLabel(PatternCaseLabelTree node, P p) {
 834         return scan(node.getPattern(), p);
 835     }
 836 
 837     /**
 838      * {@inheritDoc}
 839      *
 840      * @implSpec This implementation scans the children in left to right order.
 841      *
 842      * @param node  {@inheritDoc}
 843      * @param p  {@inheritDoc}
 844      * @return the result of scanning
 845      * @since 21
 846      */
 847     @Override
 848     public R visitDeconstructionPattern(DeconstructionPatternTree node, P p) {
 849         R r = scan(node.getDeconstructor(), p);
 850         r = scanAndReduce(node.getNestedPatterns(), p, r);
 851         return r;
 852     }
 853 
 854     /**
 855      * {@inheritDoc} This implementation scans the children in left to right order.
 856      *
 857      * @param node  {@inheritDoc}
 858      * @param p  {@inheritDoc}
 859      * @return the result of scanning
 860      */
 861     @Override
 862     public R visitArrayAccess(ArrayAccessTree node, P p) {
 863         R r = scan(node.getExpression(), p);
 864         r = scanAndReduce(node.getIndex(), p, r);
 865         return r;
 866     }
 867 
 868     /**
 869      * {@inheritDoc}
 870      *
 871      * @implSpec This implementation scans the children in left to right order.
 872      *
 873      * @param node  {@inheritDoc}
 874      * @param p  {@inheritDoc}
 875      * @return the result of scanning
 876      */
 877     @Override
 878     public R visitMemberSelect(MemberSelectTree node, P p) {
 879         return scan(node.getExpression(), p);
 880     }
 881 
 882     /**
 883      * {@inheritDoc}
 884      *
 885      * @implSpec This implementation scans the children in left to right order.
 886      *
 887      * @param node  {@inheritDoc}
 888      * @param p  {@inheritDoc}
 889      * @return the result of scanning
 890      */
 891     @Override
 892     public R visitMemberReference(MemberReferenceTree node, P p) {
 893         R r = scan(node.getQualifierExpression(), p);
 894         r = scanAndReduce(node.getTypeArguments(), p, r);
 895         return r;
 896     }
 897 
 898     /**
 899      * {@inheritDoc}
 900      *
 901      * @implSpec This implementation returns {@code null}.
 902      *
 903      * @param node  {@inheritDoc}
 904      * @param p  {@inheritDoc}
 905      * @return the result of scanning
 906      */
 907     @Override
 908     public R visitIdentifier(IdentifierTree node, P p) {
 909         return null;
 910     }
 911 
 912     /**
 913      * {@inheritDoc}
 914      *
 915      * @implSpec This implementation returns {@code null}.
 916      *
 917      * @param node  {@inheritDoc}
 918      * @param p  {@inheritDoc}
 919      * @return the result of scanning
 920      */
 921     @Override
 922     public R visitLiteral(LiteralTree node, P p) {
 923         return null;
 924     }
 925 
 926     /**
 927      * {@inheritDoc}
 928      *
 929      * @implSpec 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 visitPrimitiveType(PrimitiveTypeTree node, P p) {
 937         return null;
 938     }
 939 
 940     /**
 941      * {@inheritDoc}
 942      *
 943      * @implSpec This implementation scans the children in left to right order.
 944      *
 945      * @param node  {@inheritDoc}
 946      * @param p  {@inheritDoc}
 947      * @return the result of scanning
 948      */
 949     @Override
 950     public R visitArrayType(ArrayTypeTree node, P p) {
 951         return scan(node.getType(), p);
 952     }
 953 
 954     /**
 955      * {@inheritDoc}
 956      *
 957      * @implSpec This implementation scans the children in left to right order.
 958      *
 959      * @param node  {@inheritDoc}
 960      * @param p  {@inheritDoc}
 961      * @return the result of scanning
 962      */
 963     @Override
 964     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 965         R r = scan(node.getType(), p);
 966         r = scanAndReduce(node.getTypeArguments(), p, r);
 967         return r;
 968     }
 969 
 970     /**
 971      * {@inheritDoc}
 972      *
 973      * @implSpec This implementation scans the children in left to right order.
 974      *
 975      * @param node  {@inheritDoc}
 976      * @param p  {@inheritDoc}
 977      * @return the result of scanning
 978      */
 979     @Override
 980     public R visitUnionType(UnionTypeTree node, P p) {
 981         return scan(node.getTypeAlternatives(), p);
 982     }
 983 
 984     /**
 985      * {@inheritDoc}
 986      *
 987      * @implSpec This implementation scans the children in left to right order.
 988      *
 989      * @param node  {@inheritDoc}
 990      * @param p  {@inheritDoc}
 991      * @return the result of scanning
 992      */
 993     @Override
 994     public R visitIntersectionType(IntersectionTypeTree node, P p) {
 995         return scan(node.getBounds(), p);
 996     }
 997 
 998     /**
 999      * {@inheritDoc}
1000      *
1001      * @implSpec This implementation scans the children in left to right order.
1002      *
1003      * @param node  {@inheritDoc}
1004      * @param p  {@inheritDoc}
1005      * @return the result of scanning
1006      */
1007     @Override
1008     public R visitTypeParameter(TypeParameterTree node, P p) {
1009         R r = scan(node.getAnnotations(), p);
1010         r = scanAndReduce(node.getBounds(), p, r);
1011         return r;
1012     }
1013 
1014     /**
1015      * {@inheritDoc}
1016      *
1017      * @implSpec This implementation scans the children in left to right order.
1018      *
1019      * @param node  {@inheritDoc}
1020      * @param p  {@inheritDoc}
1021      * @return the result of scanning
1022      */
1023     @Override
1024     public R visitWildcard(WildcardTree node, P p) {
1025         return scan(node.getBound(), p);
1026     }
1027 
1028     /**
1029      * {@inheritDoc}
1030      *
1031      * @implSpec This implementation scans the children in left to right order.
1032      *
1033      * @param node  {@inheritDoc}
1034      * @param p  {@inheritDoc}
1035      * @return the result of scanning
1036      */
1037     @Override
1038     public R visitModifiers(ModifiersTree node, P p) {
1039         return scan(node.getAnnotations(), p);
1040     }
1041 
1042     /**
1043      * {@inheritDoc}
1044      *
1045      * @implSpec This implementation scans the children in left to right order.
1046      *
1047      * @param node  {@inheritDoc}
1048      * @param p  {@inheritDoc}
1049      * @return the result of scanning
1050      */
1051     @Override
1052     public R visitAnnotation(AnnotationTree node, P p) {
1053         R r = scan(node.getAnnotationType(), p);
1054         r = scanAndReduce(node.getArguments(), p, r);
1055         return r;
1056     }
1057 
1058     /**
1059      * {@inheritDoc}
1060      *
1061      * @implSpec This implementation scans the children in left to right order.
1062      *
1063      * @param node  {@inheritDoc}
1064      * @param p  {@inheritDoc}
1065      * @return the result of scanning
1066      */
1067     @Override
1068     public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
1069         R r = scan(node.getAnnotations(), p);
1070         r = scanAndReduce(node.getUnderlyingType(), p, r);
1071         return r;
1072     }
1073 
1074     /**
1075      * {@inheritDoc}
1076      *
1077      * @implSpec This implementation scans the children in left to right order.
1078      *
1079      * @param node  {@inheritDoc}
1080      * @param p  {@inheritDoc}
1081      * @return the result of scanning
1082      */
1083     @Override
1084     public R visitModule(ModuleTree node, P p) {
1085         R r = scan(node.getAnnotations(), p);
1086         r = scanAndReduce(node.getName(), p, r);
1087         r = scanAndReduce(node.getDirectives(), p, r);
1088         return r;
1089     }
1090 
1091     /**
1092      * {@inheritDoc}
1093      *
1094      * @implSpec This implementation scans the children in left to right order.
1095      *
1096      * @param node  {@inheritDoc}
1097      * @param p  {@inheritDoc}
1098      * @return the result of scanning
1099      */
1100     @Override
1101     public R visitExports(ExportsTree node, P p) {
1102         R r = scan(node.getPackageName(), p);
1103         r = scanAndReduce(node.getModuleNames(), p, r);
1104         return r;
1105     }
1106 
1107     /**
1108      * {@inheritDoc}
1109      *
1110      * @implSpec This implementation scans the children in left to right order.
1111      *
1112      * @param node  {@inheritDoc}
1113      * @param p  {@inheritDoc}
1114      * @return the result of scanning
1115      */
1116     @Override
1117     public R visitOpens(OpensTree node, P p) {
1118         R r = scan(node.getPackageName(), p);
1119         r = scanAndReduce(node.getModuleNames(), p, r);
1120         return r;
1121     }
1122 
1123     /**
1124      * {@inheritDoc}
1125      *
1126      * @implSpec This implementation scans the children in left to right order.
1127      *
1128      * @param node  {@inheritDoc}
1129      * @param p  {@inheritDoc}
1130      * @return the result of scanning
1131      */
1132     @Override
1133     public R visitProvides(ProvidesTree node, P p) {
1134         R r = scan(node.getServiceName(), p);
1135         r = scanAndReduce(node.getImplementationNames(), p, r);
1136         return r;
1137     }
1138 
1139     /**
1140      * {@inheritDoc}
1141      *
1142      * @implSpec This implementation scans the children in left to right order.
1143      *
1144      * @param node  {@inheritDoc}
1145      * @param p  {@inheritDoc}
1146      * @return the result of scanning
1147      */
1148     @Override
1149     public R visitRequires(RequiresTree node, P p) {
1150         return scan(node.getModuleName(), p);
1151     }
1152 
1153     /**
1154      * {@inheritDoc}
1155      *
1156      * @implSpec This implementation scans the children in left to right order.
1157      *
1158      * @param node  {@inheritDoc}
1159      * @param p  {@inheritDoc}
1160      * @return the result of scanning
1161      */
1162     @Override
1163     public R visitUses(UsesTree node, P p) {
1164         return scan(node.getServiceName(), p);
1165     }
1166 
1167     /**
1168      * {@inheritDoc}
1169      *
1170      * @implSpec This implementation returns {@code null}.
1171      *
1172      * @param node  {@inheritDoc}
1173      * @param p  {@inheritDoc}
1174      * @return the result of scanning
1175      */
1176     @Override
1177     public R visitOther(Tree node, P p) {
1178         return null;
1179     }
1180 
1181     /**
1182      * {@inheritDoc}
1183      *
1184      * @implSpec This implementation returns {@code null}.
1185      *
1186      * @param node  {@inheritDoc}
1187      * @param p  {@inheritDoc}
1188      * @return the result of scanning
1189      */
1190     @Override
1191     public R visitErroneous(ErroneousTree node, P p) {
1192         return null;
1193     }
1194 
1195     /**
1196      * {@inheritDoc}
1197      *
1198      * @implSpec This implementation scans the children in left to right order.
1199      *
1200      * @param node  {@inheritDoc}
1201      * @param p  {@inheritDoc}
1202      * @return the result of scanning
1203      *
1204      * @since 14
1205      */
1206     @Override
1207     public R visitYield(YieldTree node, P p) {
1208         return scan(node.getValue(), p);
1209     }
1210 }