1 /*
   2  * Copyright (c) 2005, 2021, 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 import jdk.internal.javac.PreviewFeature;
  30 
  31 /**
  32  * A TreeVisitor that visits all the child tree nodes.
  33  * To visit nodes of a particular type, just override the
  34  * corresponding visitXYZ method.
  35  * Inside your method, call super.visitXYZ to visit descendant
  36  * nodes.
  37  *
  38  * <p>Here is an example to count the number of identifier nodes in a tree:
  39  * <pre>
  40  *   class CountIdentifiers extends TreeScanner&lt;Integer,Void&gt; {
  41  *      {@literal @}Override
  42  *      public Integer visitIdentifier(IdentifierTree node, Void p) {
  43  *          return 1;
  44  *      }
  45  *      {@literal @}Override
  46  *      public Integer reduce(Integer r1, Integer r2) {
  47  *          return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
  48  *      }
  49  *   }
  50  * </pre>
  51  *
  52  * @implSpec
  53  * <p>The default implementation of the visitXYZ methods will determine
  54  * a result as follows:
  55  * <ul>
  56  * <li>If the node being visited has no children, the result will be {@code null}.
  57  * <li>If the node being visited has one child, the result will be the
  58  * result of calling {@code scan} with that child. The child may be a simple node
  59  * or itself a list of nodes.
  60  * <li>If the node being visited has more than one child, the result will
  61  * be determined by calling {@code scan} with each child in turn, and then combining the
  62  * result of each scan after the first with the cumulative result
  63  * so far, as determined by the {@link #reduce} method. Each child may be either
  64  * a simple node or a list of nodes. The default behavior of the {@code reduce}
  65  * method is such that the result of the visitXYZ method will be the result of
  66  * the last child scanned.
  67  * </ul>
  68  *
  69  * @param <R> the return type of this visitor's methods.  Use {@link
  70  *            Void} for visitors that do not need to return results.
  71  * @param <P> the type of the additional parameter to this visitor's
  72  *            methods.  Use {@code Void} for visitors that do not need an
  73  *            additional parameter.
  74  *
  75  * @author Peter von der Ah&eacute;
  76  * @author Jonathan Gibbons
  77  * @since 1.6
  78  */
  79 public class TreeScanner<R,P> implements TreeVisitor<R,P> {
  80     /**
  81      * Constructs a {@code TreeScanner}.
  82      */
  83     public TreeScanner() {}
  84 
  85     /**
  86      * Scans a single node.
  87      * @param tree the node to be scanned
  88      * @param p a parameter value passed to the visit method
  89      * @return the result value from the visit method
  90      */
  91     public R scan(Tree tree, P p) {
  92         return (tree == null) ? null : tree.accept(this, p);
  93     }
  94 
  95     private R scanAndReduce(Tree node, P p, R r) {
  96         return reduce(scan(node, p), r);
  97     }
  98 
  99     /**
 100      * Scans a sequence of nodes.
 101      * @param nodes the nodes to be scanned
 102      * @param p a parameter value to be passed to the visit method for each node
 103      * @return the combined return value from the visit methods.
 104      *      The values are combined using the {@link #reduce reduce} method.
 105      */
 106     public R scan(Iterable<? extends Tree> nodes, P p) {
 107         R r = null;
 108         if (nodes != null) {
 109             boolean first = true;
 110             for (Tree node : nodes) {
 111                 r = (first ? scan(node, p) : scanAndReduce(node, p, r));
 112                 first = false;
 113             }
 114         }
 115         return r;
 116     }
 117 
 118     private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {
 119         return reduce(scan(nodes, p), r);
 120     }
 121 
 122     /**
 123      * Reduces two results into a combined result.
 124      * The default implementation is to return the first parameter.
 125      * The general contract of the method is that it may take any action whatsoever.
 126      * @param r1 the first of the values to be combined
 127      * @param r2 the second of the values to be combined
 128      * @return the result of combining the two parameters
 129      */
 130     public R reduce(R r1, R r2) {
 131         return r1;
 132     }
 133 
 134 
 135 /* ***************************************************************************
 136  * Visitor methods
 137  ****************************************************************************/
 138 
 139     /**
 140      * {@inheritDoc}
 141      *
 142      * @implSpec This implementation scans the children in left to right order.
 143      *
 144      * @param node  {@inheritDoc}
 145      * @param p  {@inheritDoc}
 146      * @return the result of scanning
 147      */
 148     @Override
 149     public R visitCompilationUnit(CompilationUnitTree node, P p) {
 150         R r = scan(node.getPackage(), p);
 151         r = scanAndReduce(node.getImports(), p, r);
 152         r = scanAndReduce(node.getTypeDecls(), p, r);
 153         r = scanAndReduce(node.getModule(), p, r);
 154         return r;
 155     }
 156 
 157     /**
 158      * {@inheritDoc}
 159      *
 160      * @implSpec This implementation scans the children in left to right order.
 161      *
 162      * @param node  {@inheritDoc}
 163      * @param p  {@inheritDoc}
 164      * @return the result of scanning
 165      */
 166     @Override
 167     public R visitPackage(PackageTree node, P p) {
 168         R r = scan(node.getAnnotations(), p);
 169         r = scanAndReduce(node.getPackageName(), p, r);
 170         return r;
 171     }
 172 
 173     /**
 174      * {@inheritDoc}
 175      *
 176      * @implSpec This implementation scans the children in left to right order.
 177      *
 178      * @param node  {@inheritDoc}
 179      * @param p  {@inheritDoc}
 180      * @return the result of scanning
 181      */
 182     @Override
 183     public R visitImport(ImportTree node, P p) {
 184         return scan(node.getQualifiedIdentifier(), p);
 185     }
 186 
 187     /**
 188      * {@inheritDoc}
 189      *
 190      * @implSpec This implementation scans the children in left to right order.
 191      *
 192      * @param node  {@inheritDoc}
 193      * @param p  {@inheritDoc}
 194      * @return the result of scanning
 195      */
 196     @Override
 197     public R visitClass(ClassTree node, P p) {
 198         R r = scan(node.getModifiers(), p);
 199         r = scanAndReduce(node.getTypeParameters(), p, r);
 200         r = scanAndReduce(node.getExtendsClause(), p, r);
 201         r = scanAndReduce(node.getImplementsClause(), p, r);
 202         r = scanAndReduce(node.getPermitsClause(), p, r);
 203         r = scanAndReduce(node.getMembers(), p, r);
 204         return r;
 205     }
 206 
 207     /**
 208      * {@inheritDoc}
 209      *
 210      * @implSpec This implementation scans the children in left to right order.
 211      *
 212      * @param node  {@inheritDoc}
 213      * @param p  {@inheritDoc}
 214      * @return the result of scanning
 215      */
 216     @Override
 217     public R visitMethod(MethodTree node, P p) {
 218         R r = scan(node.getModifiers(), p);
 219         r = scanAndReduce(node.getReturnType(), p, r);
 220         r = scanAndReduce(node.getTypeParameters(), p, r);
 221         r = scanAndReduce(node.getParameters(), p, r);
 222         r = scanAndReduce(node.getReceiverParameter(), p, r);
 223         r = scanAndReduce(node.getThrows(), p, r);
 224         r = scanAndReduce(node.getBody(), p, r);
 225         r = scanAndReduce(node.getDefaultValue(), p, r);
 226         return r;
 227     }
 228 
 229     /**
 230      * {@inheritDoc}
 231      *
 232      * @implSpec This implementation scans the children in left to right order.
 233      *
 234      * @param node  {@inheritDoc}
 235      * @param p  {@inheritDoc}
 236      * @return the result of scanning
 237      */
 238     @Override
 239     public R visitVariable(VariableTree node, P p) {
 240         R r = scan(node.getModifiers(), p);
 241         r = scanAndReduce(node.getType(), p, r);
 242         r = scanAndReduce(node.getNameExpression(), p, r);
 243         r = scanAndReduce(node.getInitializer(), p, r);
 244         return r;
 245     }
 246 
 247     /**
 248      * {@inheritDoc}
 249      *
 250      * @implSpec This implementation returns {@code null}.
 251      *
 252      * @param node  {@inheritDoc}
 253      * @param p  {@inheritDoc}
 254      * @return the result of scanning
 255      */
 256     @Override
 257     public R visitEmptyStatement(EmptyStatementTree node, P p) {
 258         return null;
 259     }
 260 
 261     /**
 262      * {@inheritDoc}
 263      *
 264      * @implSpec This implementation scans the children in left to right order.
 265      *
 266      * @param node  {@inheritDoc}
 267      * @param p  {@inheritDoc}
 268      * @return the result of scanning
 269      */
 270     @Override
 271     public R visitBlock(BlockTree node, P p) {
 272         return scan(node.getStatements(), p);
 273     }
 274 
 275     /**
 276      * {@inheritDoc}
 277      *
 278      * @implSpec This implementation scans the children in left to right order.
 279      *
 280      * @param node  {@inheritDoc}
 281      * @param p  {@inheritDoc}
 282      * @return the result of scanning
 283      */
 284     @Override
 285     public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
 286         R r = scan(node.getStatement(), p);
 287         r = scanAndReduce(node.getCondition(), p, r);
 288         return r;
 289     }
 290 
 291     /**
 292      * {@inheritDoc}
 293      *
 294      * @implSpec This implementation scans the children in left to right order.
 295      *
 296      * @param node  {@inheritDoc}
 297      * @param p  {@inheritDoc}
 298      * @return the result of scanning
 299      */
 300     @Override
 301     public R visitWhileLoop(WhileLoopTree node, P p) {
 302         R r = scan(node.getCondition(), p);
 303         r = scanAndReduce(node.getStatement(), p, r);
 304         return r;
 305     }
 306 
 307     /**
 308      * {@inheritDoc}
 309      *
 310      * @implSpec This implementation scans the children in left to right order.
 311      *
 312      * @param node  {@inheritDoc}
 313      * @param p  {@inheritDoc}
 314      * @return the result of scanning
 315      */
 316     @Override
 317     public R visitForLoop(ForLoopTree node, P p) {
 318         R r = scan(node.getInitializer(), p);
 319         r = scanAndReduce(node.getCondition(), p, r);
 320         r = scanAndReduce(node.getUpdate(), p, r);
 321         r = scanAndReduce(node.getStatement(), p, r);
 322         return r;
 323     }
 324 
 325     /**
 326      * {@inheritDoc}
 327      *
 328      * @implSpec This implementation scans the children in left to right order.
 329      *
 330      * @param node  {@inheritDoc}
 331      * @param p  {@inheritDoc}
 332      * @return the result of scanning
 333      */
 334     @Override
 335     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
 336         R r = scan(node.getVariable(), p);
 337         r = scanAndReduce(node.getExpression(), p, r);
 338         r = scanAndReduce(node.getStatement(), p, r);
 339         return r;
 340     }
 341 
 342     /**
 343      * {@inheritDoc}
 344      *
 345      * @implSpec 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     public R visitLabeledStatement(LabeledStatementTree node, P p) {
 353         return scan(node.getStatement(), p);
 354     }
 355 
 356     /**
 357      * {@inheritDoc}
 358      *
 359      * @implSpec This implementation scans the children in left to right order.
 360      *
 361      * @param node  {@inheritDoc}
 362      * @param p  {@inheritDoc}
 363      * @return the result of scanning
 364      */
 365     @Override
 366     public R visitSwitch(SwitchTree node, P p) {
 367         R r = scan(node.getExpression(), p);
 368         r = scanAndReduce(node.getCases(), p, r);
 369         return r;
 370     }
 371 
 372     /**
 373      * {@inheritDoc}
 374      *
 375      * @implSpec This implementation scans the children in left to right order.
 376      *
 377      * @param node  {@inheritDoc}
 378      * @param p  {@inheritDoc}
 379      * @return the result of scanning
 380      */
 381     @Override
 382     public R visitSwitchExpression(SwitchExpressionTree node, P p) {
 383         R r = scan(node.getExpression(), p);
 384         r = scanAndReduce(node.getCases(), p, r);
 385         return r;
 386     }
 387 
 388     /**
 389      * {@inheritDoc}
 390      *
 391      * @implSpec This implementation scans the children in left to right order.
 392      *
 393      * @param node  {@inheritDoc}
 394      * @param p  {@inheritDoc}
 395      * @return the result of scanning
 396      */
 397     @Override
 398     public R visitCase(CaseTree node, P p) {
 399         R r = scan(node.getExpressions(), p);
 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 scans the children in left to right order.
 766      *
 767      * @param node  {@inheritDoc}
 768      * @param p  {@inheritDoc}
 769      * @return the result of scanning
 770      * @since 14
 771      */
 772     @Override
 773     public R visitBindingPattern(BindingPatternTree node, P p) {
 774         return scan(node.getVariable(), p);
 775     }
 776 
 777     /**
 778      * {@inheritDoc}
 779      *
 780      * @implSpec This implementation returns {@code null}.
 781      *
 782      * @param node  {@inheritDoc}
 783      * @param p  {@inheritDoc}
 784      * @return the result of scanning
 785      * @since 17
 786      */
 787     @Override
 788     @PreviewFeature(feature=PreviewFeature.Feature.SWITCH_PATTERN_MATCHING, reflective=true)
 789     public R visitDefaultCaseLabel(DefaultCaseLabelTree node, P p) {
 790         return null;
 791     }
 792 
 793     /**
 794      * {@inheritDoc}
 795      *
 796      * @implSpec This implementation scans the children in left to right order.
 797      *
 798      * @param node  {@inheritDoc}
 799      * @param p  {@inheritDoc}
 800      * @return the result of scanning
 801      */
 802     @Override
 803     public R visitArrayAccess(ArrayAccessTree node, P p) {
 804         R r = scan(node.getExpression(), p);
 805         r = scanAndReduce(node.getIndex(), p, r);
 806         return r;
 807     }
 808 
 809     /**
 810      * {@inheritDoc}
 811      *
 812      * @implSpec 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 visitMemberSelect(MemberSelectTree node, P p) {
 820         return scan(node.getExpression(), p);
 821     }
 822 
 823     /**
 824      * {@inheritDoc}
 825      *
 826      * @implSpec 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      * @since 17
 832      */
 833     @Override
 834     @PreviewFeature(feature=PreviewFeature.Feature.SWITCH_PATTERN_MATCHING, reflective=true)
 835     public R visitParenthesizedPattern(ParenthesizedPatternTree node, P p) {
 836         return scan(node.getPattern(), p);
 837     }
 838 
 839     /**
 840      * {@inheritDoc}
 841      *
 842      * @implSpec This implementation scans the children in left to right order.
 843      *
 844      * @param node  {@inheritDoc}
 845      * @param p  {@inheritDoc}
 846      * @return the result of scanning
 847      * @since 17
 848      */
 849     @Override
 850     @PreviewFeature(feature=PreviewFeature.Feature.SWITCH_PATTERN_MATCHING, reflective=true)
 851     public R visitGuardedPattern(GuardedPatternTree node, P p) {
 852         R r = scan(node.getPattern(), p);
 853         return scanAndReduce(node.getExpression(), p, r);
 854     }
 855 
 856     /**
 857      * {@inheritDoc}
 858      *
 859      * @implSpec This implementation scans the children in left to right order.
 860      *
 861      * @param node  {@inheritDoc}
 862      * @param p  {@inheritDoc}
 863      * @return the result of scanning
 864      */
 865     @Override
 866     public R visitMemberReference(MemberReferenceTree node, P p) {
 867         R r = scan(node.getQualifierExpression(), p);
 868         r = scanAndReduce(node.getTypeArguments(), p, r);
 869         return r;
 870     }
 871 
 872     /**
 873      * {@inheritDoc}
 874      *
 875      * @implSpec This implementation returns {@code null}.
 876      *
 877      * @param node  {@inheritDoc}
 878      * @param p  {@inheritDoc}
 879      * @return the result of scanning
 880      */
 881     @Override
 882     public R visitIdentifier(IdentifierTree node, P p) {
 883         return null;
 884     }
 885 
 886     /**
 887      * {@inheritDoc}
 888      *
 889      * @implSpec This implementation returns {@code null}.
 890      *
 891      * @param node  {@inheritDoc}
 892      * @param p  {@inheritDoc}
 893      * @return the result of scanning
 894      */
 895     @Override
 896     public R visitLiteral(LiteralTree node, P p) {
 897         return null;
 898     }
 899 
 900     /**
 901      * {@inheritDoc}
 902      *
 903      * @implSpec This implementation returns {@code null}.
 904      *
 905      * @param node  {@inheritDoc}
 906      * @param p  {@inheritDoc}
 907      * @return the result of scanning
 908      */
 909     @Override
 910     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 911         return null;
 912     }
 913 
 914     /**
 915      * {@inheritDoc}
 916      *
 917      * @implSpec This implementation scans the children in left to right order.
 918      *
 919      * @param node  {@inheritDoc}
 920      * @param p  {@inheritDoc}
 921      * @return the result of scanning
 922      */
 923     @Override
 924     public R visitArrayType(ArrayTypeTree node, P p) {
 925         return scan(node.getType(), p);
 926     }
 927 
 928     /**
 929      * {@inheritDoc}
 930      *
 931      * @implSpec This implementation scans the children in left to right order.
 932      *
 933      * @param node  {@inheritDoc}
 934      * @param p  {@inheritDoc}
 935      * @return the result of scanning
 936      */
 937     @Override
 938     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 939         R r = scan(node.getType(), p);
 940         r = scanAndReduce(node.getTypeArguments(), p, r);
 941         return r;
 942     }
 943 
 944     /**
 945      * {@inheritDoc}
 946      *
 947      * @implSpec This implementation scans the children in left to right order.
 948      *
 949      * @param node  {@inheritDoc}
 950      * @param p  {@inheritDoc}
 951      * @return the result of scanning
 952      */
 953     @Override
 954     public R visitUnionType(UnionTypeTree node, P p) {
 955         return scan(node.getTypeAlternatives(), p);
 956     }
 957 
 958     /**
 959      * {@inheritDoc}
 960      *
 961      * @implSpec This implementation scans the children in left to right order.
 962      *
 963      * @param node  {@inheritDoc}
 964      * @param p  {@inheritDoc}
 965      * @return the result of scanning
 966      */
 967     @Override
 968     public R visitIntersectionType(IntersectionTypeTree node, P p) {
 969         return scan(node.getBounds(), p);
 970     }
 971 
 972     /**
 973      * {@inheritDoc}
 974      *
 975      * @implSpec This implementation scans the children in left to right order.
 976      *
 977      * @param node  {@inheritDoc}
 978      * @param p  {@inheritDoc}
 979      * @return the result of scanning
 980      */
 981     @Override
 982     public R visitTypeParameter(TypeParameterTree node, P p) {
 983         R r = scan(node.getAnnotations(), p);
 984         r = scanAndReduce(node.getBounds(), p, r);
 985         return r;
 986     }
 987 
 988     /**
 989      * {@inheritDoc}
 990      *
 991      * @implSpec This implementation scans the children in left to right order.
 992      *
 993      * @param node  {@inheritDoc}
 994      * @param p  {@inheritDoc}
 995      * @return the result of scanning
 996      */
 997     @Override
 998     public R visitWildcard(WildcardTree node, P p) {
 999         return scan(node.getBound(), p);
1000     }
1001 
1002     /**
1003      * {@inheritDoc}
1004      *
1005      * @implSpec This implementation scans the children in left to right order.
1006      *
1007      * @param node  {@inheritDoc}
1008      * @param p  {@inheritDoc}
1009      * @return the result of scanning
1010      */
1011     @Override
1012     public R visitModifiers(ModifiersTree node, P p) {
1013         return scan(node.getAnnotations(), p);
1014     }
1015 
1016     /**
1017      * {@inheritDoc}
1018      *
1019      * @implSpec This implementation scans the children in left to right order.
1020      *
1021      * @param node  {@inheritDoc}
1022      * @param p  {@inheritDoc}
1023      * @return the result of scanning
1024      */
1025     @Override
1026     public R visitAnnotation(AnnotationTree node, P p) {
1027         R r = scan(node.getAnnotationType(), p);
1028         r = scanAndReduce(node.getArguments(), p, r);
1029         return r;
1030     }
1031 
1032     /**
1033      * {@inheritDoc}
1034      *
1035      * @implSpec This implementation scans the children in left to right order.
1036      *
1037      * @param node  {@inheritDoc}
1038      * @param p  {@inheritDoc}
1039      * @return the result of scanning
1040      */
1041     @Override
1042     public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
1043         R r = scan(node.getAnnotations(), p);
1044         r = scanAndReduce(node.getUnderlyingType(), p, r);
1045         return r;
1046     }
1047 
1048     /**
1049      * {@inheritDoc}
1050      *
1051      * @implSpec This implementation scans the children in left to right order.
1052      *
1053      * @param node  {@inheritDoc}
1054      * @param p  {@inheritDoc}
1055      * @return the result of scanning
1056      */
1057     @Override
1058     public R visitModule(ModuleTree node, P p) {
1059         R r = scan(node.getAnnotations(), p);
1060         r = scanAndReduce(node.getName(), p, r);
1061         r = scanAndReduce(node.getDirectives(), p, r);
1062         return r;
1063     }
1064 
1065     /**
1066      * {@inheritDoc}
1067      *
1068      * @implSpec This implementation scans the children in left to right order.
1069      *
1070      * @param node  {@inheritDoc}
1071      * @param p  {@inheritDoc}
1072      * @return the result of scanning
1073      */
1074     @Override
1075     public R visitExports(ExportsTree node, P p) {
1076         R r = scan(node.getPackageName(), p);
1077         r = scanAndReduce(node.getModuleNames(), p, r);
1078         return r;
1079     }
1080 
1081     /**
1082      * {@inheritDoc}
1083      *
1084      * @implSpec This implementation scans the children in left to right order.
1085      *
1086      * @param node  {@inheritDoc}
1087      * @param p  {@inheritDoc}
1088      * @return the result of scanning
1089      */
1090     @Override
1091     public R visitOpens(OpensTree node, P p) {
1092         R r = scan(node.getPackageName(), p);
1093         r = scanAndReduce(node.getModuleNames(), p, r);
1094         return r;
1095     }
1096 
1097     /**
1098      * {@inheritDoc}
1099      *
1100      * @implSpec This implementation scans the children in left to right order.
1101      *
1102      * @param node  {@inheritDoc}
1103      * @param p  {@inheritDoc}
1104      * @return the result of scanning
1105      */
1106     @Override
1107     public R visitProvides(ProvidesTree node, P p) {
1108         R r = scan(node.getServiceName(), p);
1109         r = scanAndReduce(node.getImplementationNames(), p, r);
1110         return r;
1111     }
1112 
1113     /**
1114      * {@inheritDoc}
1115      *
1116      * @implSpec This implementation scans the children in left to right order.
1117      *
1118      * @param node  {@inheritDoc}
1119      * @param p  {@inheritDoc}
1120      * @return the result of scanning
1121      */
1122     @Override
1123     public R visitRequires(RequiresTree node, P p) {
1124         return scan(node.getModuleName(), p);
1125     }
1126 
1127     /**
1128      * {@inheritDoc}
1129      *
1130      * @implSpec This implementation scans the children in left to right order.
1131      *
1132      * @param node  {@inheritDoc}
1133      * @param p  {@inheritDoc}
1134      * @return the result of scanning
1135      */
1136     @Override
1137     public R visitUses(UsesTree node, P p) {
1138         return scan(node.getServiceName(), p);
1139     }
1140 
1141     /**
1142      * {@inheritDoc}
1143      *
1144      * @implSpec This implementation returns {@code null}.
1145      *
1146      * @param node  {@inheritDoc}
1147      * @param p  {@inheritDoc}
1148      * @return the result of scanning
1149      */
1150     @Override
1151     public R visitOther(Tree node, P p) {
1152         return null;
1153     }
1154 
1155     /**
1156      * {@inheritDoc}
1157      *
1158      * @implSpec This implementation returns {@code null}.
1159      *
1160      * @param node  {@inheritDoc}
1161      * @param p  {@inheritDoc}
1162      * @return the result of scanning
1163      */
1164     @Override
1165     public R visitErroneous(ErroneousTree node, P p) {
1166         return null;
1167     }
1168 
1169     /**
1170      * {@inheritDoc}
1171      *
1172      * @implSpec This implementation scans the children in left to right order.
1173      *
1174      * @param node  {@inheritDoc}
1175      * @param p  {@inheritDoc}
1176      * @return the result of scanning
1177      */
1178     @Override
1179     public R visitYield(YieldTree node, P p) {
1180         return scan(node.getValue(), p);
1181     }
1182 }