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 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.getLabels(), p);
 400         r = scanAndReduce(node.getGuard(), p, r);
 401         if (node.getCaseKind() == CaseTree.CaseKind.RULE)
 402             r = scanAndReduce(node.getBody(), p, r);
 403         else
 404             r = scanAndReduce(node.getStatements(), p, r);
 405         return r;
 406     }
 407 
 408     /**
 409      * {@inheritDoc}
 410      *
 411      * @implSpec This implementation scans the children in left to right order.
 412      *
 413      * @param node  {@inheritDoc}
 414      * @param p  {@inheritDoc}
 415      * @return the result of scanning
 416      */
 417     @Override
 418     public R visitSynchronized(SynchronizedTree node, P p) {
 419         R r = scan(node.getExpression(), p);
 420         r = scanAndReduce(node.getBlock(), p, r);
 421         return r;
 422     }
 423 
 424     /**
 425      * {@inheritDoc}
 426      *
 427      * @implSpec This implementation scans the children in left to right order.
 428      *
 429      * @param node  {@inheritDoc}
 430      * @param p  {@inheritDoc}
 431      * @return the result of scanning
 432      */
 433     @Override
 434     public R visitTry(TryTree node, P p) {
 435         R r = scan(node.getResources(), p);
 436         r = scanAndReduce(node.getBlock(), p, r);
 437         r = scanAndReduce(node.getCatches(), p, r);
 438         r = scanAndReduce(node.getFinallyBlock(), p, r);
 439         return r;
 440     }
 441 
 442     /**
 443      * {@inheritDoc}
 444      *
 445      * @implSpec This implementation scans the children in left to right order.
 446      *
 447      * @param node  {@inheritDoc}
 448      * @param p  {@inheritDoc}
 449      * @return the result of scanning
 450      */
 451     @Override
 452     public R visitCatch(CatchTree node, P p) {
 453         R r = scan(node.getParameter(), p);
 454         r = scanAndReduce(node.getBlock(), p, r);
 455         return r;
 456     }
 457 
 458     /**
 459      * {@inheritDoc}
 460      *
 461      * @implSpec This implementation scans the children in left to right order.
 462      *
 463      * @param node  {@inheritDoc}
 464      * @param p  {@inheritDoc}
 465      * @return the result of scanning
 466      */
 467     @Override
 468     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
 469         R r = scan(node.getCondition(), p);
 470         r = scanAndReduce(node.getTrueExpression(), p, r);
 471         r = scanAndReduce(node.getFalseExpression(), p, r);
 472         return r;
 473     }
 474 
 475     /**
 476      * {@inheritDoc}
 477      *
 478      * @implSpec This implementation scans the children in left to right order.
 479      *
 480      * @param node  {@inheritDoc}
 481      * @param p  {@inheritDoc}
 482      * @return the result of scanning
 483      */
 484     @Override
 485     public R visitIf(IfTree node, P p) {
 486         R r = scan(node.getCondition(), p);
 487         r = scanAndReduce(node.getThenStatement(), p, r);
 488         r = scanAndReduce(node.getElseStatement(), p, r);
 489         return r;
 490     }
 491 
 492     /**
 493      * {@inheritDoc}
 494      *
 495      * @implSpec This implementation scans the children in left to right order.
 496      *
 497      * @param node  {@inheritDoc}
 498      * @param p  {@inheritDoc}
 499      * @return the result of scanning
 500      */
 501     @Override
 502     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 503         return scan(node.getExpression(), p);
 504     }
 505 
 506     /**
 507      * {@inheritDoc}
 508      *
 509      * @implSpec This implementation returns {@code null}.
 510      *
 511      * @param node  {@inheritDoc}
 512      * @param p  {@inheritDoc}
 513      * @return the result of scanning
 514      */
 515     @Override
 516     public R visitBreak(BreakTree node, P p) {
 517         return null;
 518     }
 519 
 520     /**
 521      * {@inheritDoc}
 522      *
 523      * @implSpec This implementation returns {@code null}.
 524      *
 525      * @param node  {@inheritDoc}
 526      * @param p  {@inheritDoc}
 527      * @return the result of scanning
 528      */
 529     @Override
 530     public R visitContinue(ContinueTree node, P p) {
 531         return null;
 532     }
 533 
 534     /**
 535      * {@inheritDoc}
 536      *
 537      * @implSpec This implementation scans the children in left to right order.
 538      *
 539      * @param node  {@inheritDoc}
 540      * @param p  {@inheritDoc}
 541      * @return the result of scanning
 542      */
 543     @Override
 544     public R visitReturn(ReturnTree node, P p) {
 545         return scan(node.getExpression(), p);
 546     }
 547 
 548     /**
 549      * {@inheritDoc}
 550      *
 551      * @implSpec This implementation scans the children in left to right order.
 552      *
 553      * @param node  {@inheritDoc}
 554      * @param p  {@inheritDoc}
 555      * @return the result of scanning
 556      */
 557     @Override
 558     public R visitThrow(ThrowTree node, P p) {
 559         return scan(node.getExpression(), p);
 560     }
 561 
 562     /**
 563      * {@inheritDoc}
 564      *
 565      * @implSpec This implementation scans the children in left to right order.
 566      *
 567      * @param node  {@inheritDoc}
 568      * @param p  {@inheritDoc}
 569      * @return the result of scanning
 570      */
 571     @Override
 572     public R visitAssert(AssertTree node, P p) {
 573         R r = scan(node.getCondition(), p);
 574         r = scanAndReduce(node.getDetail(), p, r);
 575         return r;
 576     }
 577 
 578     /**
 579      * {@inheritDoc}
 580      *
 581      * @implSpec This implementation scans the children in left to right order.
 582      *
 583      * @param node  {@inheritDoc}
 584      * @param p  {@inheritDoc}
 585      * @return the result of scanning
 586      */
 587     @Override
 588     public R visitMethodInvocation(MethodInvocationTree node, P p) {
 589         R r = scan(node.getTypeArguments(), p);
 590         r = scanAndReduce(node.getMethodSelect(), p, r);
 591         r = scanAndReduce(node.getArguments(), p, r);
 592         return r;
 593     }
 594 
 595     /**
 596      * {@inheritDoc}
 597      *
 598      * @implSpec This implementation scans the children in left to right order.
 599      *
 600      * @param node  {@inheritDoc}
 601      * @param p  {@inheritDoc}
 602      * @return the result of scanning
 603      */
 604     @Override
 605     public R visitNewClass(NewClassTree node, P p) {
 606         R r = scan(node.getEnclosingExpression(), p);
 607         r = scanAndReduce(node.getIdentifier(), p, r);
 608         r = scanAndReduce(node.getTypeArguments(), p, r);
 609         r = scanAndReduce(node.getArguments(), p, r);
 610         r = scanAndReduce(node.getClassBody(), p, r);
 611         return r;
 612     }
 613 
 614     /**
 615      * {@inheritDoc}
 616      *
 617      * @implSpec This implementation scans the children in left to right order.
 618      *
 619      * @param node  {@inheritDoc}
 620      * @param p  {@inheritDoc}
 621      * @return the result of scanning
 622      */
 623     @Override
 624     public R visitNewArray(NewArrayTree node, P p) {
 625         R r = scan(node.getType(), p);
 626         r = scanAndReduce(node.getDimensions(), p, r);
 627         r = scanAndReduce(node.getInitializers(), p, r);
 628         r = scanAndReduce(node.getAnnotations(), p, r);
 629         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 630             r = scanAndReduce(dimAnno, p, r);
 631         }
 632         return r;
 633     }
 634 
 635     /**
 636      * {@inheritDoc}
 637      *
 638      * @implSpec This implementation scans the children in left to right order.
 639      *
 640      * @param node  {@inheritDoc}
 641      * @param p  {@inheritDoc}
 642      * @return the result of scanning
 643      */
 644     @Override
 645     public R visitLambdaExpression(LambdaExpressionTree node, P p) {
 646         R r = scan(node.getParameters(), p);
 647         r = scanAndReduce(node.getBody(), p, r);
 648         return r;
 649     }
 650 
 651     /**
 652      * {@inheritDoc}
 653      *
 654      * @implSpec This implementation scans the children in left to right order.
 655      *
 656      * @param node  {@inheritDoc}
 657      * @param p  {@inheritDoc}
 658      * @return the result of scanning
 659      */
 660     @Override
 661     public R visitParenthesized(ParenthesizedTree node, P p) {
 662         return scan(node.getExpression(), p);
 663     }
 664 
 665     /**
 666      * {@inheritDoc}
 667      *
 668      * @implSpec This implementation scans the children in left to right order.
 669      *
 670      * @param node  {@inheritDoc}
 671      * @param p  {@inheritDoc}
 672      * @return the result of scanning
 673      */
 674     @Override
 675     public R visitAssignment(AssignmentTree node, P p) {
 676         R r = scan(node.getVariable(), p);
 677         r = scanAndReduce(node.getExpression(), p, r);
 678         return r;
 679     }
 680 
 681     /**
 682      * {@inheritDoc}
 683      *
 684      * @implSpec This implementation scans the children in left to right order.
 685      *
 686      * @param node  {@inheritDoc}
 687      * @param p  {@inheritDoc}
 688      * @return the result of scanning
 689      */
 690     @Override
 691     public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
 692         R r = scan(node.getVariable(), p);
 693         r = scanAndReduce(node.getExpression(), p, r);
 694         return r;
 695     }
 696 
 697     /**
 698      * {@inheritDoc}
 699      *
 700      * @implSpec 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 visitUnary(UnaryTree node, P p) {
 708         return scan(node.getExpression(), p);
 709     }
 710 
 711     /**
 712      * {@inheritDoc}
 713      *
 714      * @implSpec This implementation scans the children in left to right order.
 715      *
 716      * @param node  {@inheritDoc}
 717      * @param p  {@inheritDoc}
 718      * @return the result of scanning
 719      */
 720     @Override
 721     public R visitBinary(BinaryTree node, P p) {
 722         R r = scan(node.getLeftOperand(), p);
 723         r = scanAndReduce(node.getRightOperand(), p, r);
 724         return r;
 725     }
 726 
 727     /**
 728      * {@inheritDoc}
 729      *
 730      * @implSpec This implementation scans the children in left to right order.
 731      *
 732      * @param node  {@inheritDoc}
 733      * @param p  {@inheritDoc}
 734      * @return the result of scanning
 735      */
 736     @Override
 737     public R visitTypeCast(TypeCastTree node, P p) {
 738         R r = scan(node.getType(), p);
 739         r = scanAndReduce(node.getExpression(), p, r);
 740         return r;
 741     }
 742 
 743     /**
 744      * {@inheritDoc}
 745      *
 746      * @implSpec This implementation scans the children in left to right order.
 747      *
 748      * @param node  {@inheritDoc}
 749      * @param p  {@inheritDoc}
 750      * @return the result of scanning
 751      */
 752     @Override
 753     public R visitInstanceOf(InstanceOfTree node, P p) {
 754         R r = scan(node.getExpression(), p);
 755         if (node.getPattern() != null) {
 756             r = scanAndReduce(node.getPattern(), p, r);
 757         } else {
 758             r = scanAndReduce(node.getType(), p, r);
 759         }
 760         return r;
 761     }
 762 
 763     /**
 764      * {@inheritDoc}
 765      *
 766      * @implSpec This implementation returns {@code null}.
 767      *
 768      * @param node  {@inheritDoc}
 769      * @param p  {@inheritDoc}
 770      * @return the result of scanning
 771      * @since 21
 772      */
 773     @Override
 774     public R visitAnyPattern(AnyPatternTree node, P p) {
 775         return null;
 776     }
 777 
 778     /**
 779      * {@inheritDoc}
 780      *
 781      * @implSpec This implementation scans the children in left to right order.
 782      *
 783      * @param node  {@inheritDoc}
 784      * @param p  {@inheritDoc}
 785      * @return the result of scanning
 786      * @since 21
 787      */
 788     @Override
 789     @PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES, reflective=true)
 790     public R visitStringTemplate(StringTemplateTree node, P p) {
 791         R r = scan(node.getProcessor(), p);
 792         r = scanAndReduce(node.getExpressions(), p, r);
 793         return r;
 794     }
 795 
 796     /**
 797      * {@inheritDoc}
 798      *
 799      * @implSpec This implementation scans the children in left to right order.
 800      *
 801      * @param node  {@inheritDoc}
 802      * @param p  {@inheritDoc}
 803      * @return the result of scanning
 804      * @since 14
 805      */
 806     @Override
 807     public R visitBindingPattern(BindingPatternTree node, P p) {
 808         return scan(node.getVariable(), p);
 809     }
 810 
 811     /**
 812      * {@inheritDoc}
 813      *
 814      * @implSpec This implementation returns {@code null}.
 815      *
 816      * @param node  {@inheritDoc}
 817      * @param p  {@inheritDoc}
 818      * @return the result of scanning
 819      * @since 21
 820      */
 821     @Override
 822     public R visitDefaultCaseLabel(DefaultCaseLabelTree node, P p) {
 823         return null;
 824     }
 825 
 826     /**
 827      * {@inheritDoc}
 828      *
 829      * @implSpec This implementation returns {@code null}.
 830      *
 831      * @param node  {@inheritDoc}
 832      * @param p  {@inheritDoc}
 833      * @return the result of scanning
 834      * @since 21
 835      */
 836     @Override
 837     public R visitConstantCaseLabel(ConstantCaseLabelTree node, P p) {
 838         return scan(node.getConstantExpression(), p);
 839     }
 840 
 841     /**
 842      * {@inheritDoc}
 843      *
 844      * @implSpec This implementation returns {@code null}.
 845      *
 846      * @param node  {@inheritDoc}
 847      * @param p  {@inheritDoc}
 848      * @return the result of scanning
 849      * @since 21
 850      */
 851     @Override
 852     public R visitPatternCaseLabel(PatternCaseLabelTree node, P p) {
 853         return scan(node.getPattern(), p);
 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      * @since 21
 865      */
 866     @Override
 867     public R visitDeconstructionPattern(DeconstructionPatternTree node, P p) {
 868         R r = scan(node.getDeconstructor(), p);
 869         r = scanAndReduce(node.getNestedPatterns(), p, r);
 870         return r;
 871     }
 872 
 873     /**
 874      * {@inheritDoc} This implementation scans the children in left to right order.
 875      *
 876      * @param node  {@inheritDoc}
 877      * @param p  {@inheritDoc}
 878      * @return the result of scanning
 879      */
 880     @Override
 881     public R visitArrayAccess(ArrayAccessTree node, P p) {
 882         R r = scan(node.getExpression(), p);
 883         r = scanAndReduce(node.getIndex(), p, r);
 884         return r;
 885     }
 886 
 887     /**
 888      * {@inheritDoc}
 889      *
 890      * @implSpec This implementation scans the children in left to right order.
 891      *
 892      * @param node  {@inheritDoc}
 893      * @param p  {@inheritDoc}
 894      * @return the result of scanning
 895      */
 896     @Override
 897     public R visitMemberSelect(MemberSelectTree node, P p) {
 898         return scan(node.getExpression(), p);
 899     }
 900 
 901     /**
 902      * {@inheritDoc}
 903      *
 904      * @implSpec This implementation scans the children in left to right order.
 905      *
 906      * @param node  {@inheritDoc}
 907      * @param p  {@inheritDoc}
 908      * @return the result of scanning
 909      */
 910     @Override
 911     public R visitMemberReference(MemberReferenceTree node, P p) {
 912         R r = scan(node.getQualifierExpression(), p);
 913         r = scanAndReduce(node.getTypeArguments(), p, r);
 914         return r;
 915     }
 916 
 917     /**
 918      * {@inheritDoc}
 919      *
 920      * @implSpec This implementation returns {@code null}.
 921      *
 922      * @param node  {@inheritDoc}
 923      * @param p  {@inheritDoc}
 924      * @return the result of scanning
 925      */
 926     @Override
 927     public R visitIdentifier(IdentifierTree node, P p) {
 928         return null;
 929     }
 930 
 931     /**
 932      * {@inheritDoc}
 933      *
 934      * @implSpec This implementation returns {@code null}.
 935      *
 936      * @param node  {@inheritDoc}
 937      * @param p  {@inheritDoc}
 938      * @return the result of scanning
 939      */
 940     @Override
 941     public R visitLiteral(LiteralTree node, P p) {
 942         return null;
 943     }
 944 
 945     /**
 946      * {@inheritDoc}
 947      *
 948      * @implSpec This implementation returns {@code null}.
 949      *
 950      * @param node  {@inheritDoc}
 951      * @param p  {@inheritDoc}
 952      * @return the result of scanning
 953      */
 954     @Override
 955     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 956         return null;
 957     }
 958 
 959     /**
 960      * {@inheritDoc}
 961      *
 962      * @implSpec This implementation scans the children in left to right order.
 963      *
 964      * @param node  {@inheritDoc}
 965      * @param p  {@inheritDoc}
 966      * @return the result of scanning
 967      */
 968     @Override
 969     public R visitArrayType(ArrayTypeTree node, P p) {
 970         return scan(node.getType(), p);
 971     }
 972 
 973     /**
 974      * {@inheritDoc}
 975      *
 976      * @implSpec This implementation scans the children in left to right order.
 977      *
 978      * @param node  {@inheritDoc}
 979      * @param p  {@inheritDoc}
 980      * @return the result of scanning
 981      */
 982     @Override
 983     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 984         R r = scan(node.getType(), p);
 985         r = scanAndReduce(node.getTypeArguments(), p, r);
 986         return r;
 987     }
 988 
 989     /**
 990      * {@inheritDoc}
 991      *
 992      * @implSpec This implementation scans the children in left to right order.
 993      *
 994      * @param node  {@inheritDoc}
 995      * @param p  {@inheritDoc}
 996      * @return the result of scanning
 997      */
 998     @Override
 999     public R visitUnionType(UnionTypeTree node, P p) {
1000         return scan(node.getTypeAlternatives(), p);
1001     }
1002 
1003     /**
1004      * {@inheritDoc}
1005      *
1006      * @implSpec This implementation scans the children in left to right order.
1007      *
1008      * @param node  {@inheritDoc}
1009      * @param p  {@inheritDoc}
1010      * @return the result of scanning
1011      */
1012     @Override
1013     public R visitIntersectionType(IntersectionTypeTree node, P p) {
1014         return scan(node.getBounds(), p);
1015     }
1016 
1017     /**
1018      * {@inheritDoc}
1019      *
1020      * @implSpec This implementation scans the children in left to right order.
1021      *
1022      * @param node  {@inheritDoc}
1023      * @param p  {@inheritDoc}
1024      * @return the result of scanning
1025      */
1026     @Override
1027     public R visitTypeParameter(TypeParameterTree node, P p) {
1028         R r = scan(node.getAnnotations(), p);
1029         r = scanAndReduce(node.getBounds(), p, r);
1030         return r;
1031     }
1032 
1033     /**
1034      * {@inheritDoc}
1035      *
1036      * @implSpec This implementation scans the children in left to right order.
1037      *
1038      * @param node  {@inheritDoc}
1039      * @param p  {@inheritDoc}
1040      * @return the result of scanning
1041      */
1042     @Override
1043     public R visitWildcard(WildcardTree node, P p) {
1044         return scan(node.getBound(), p);
1045     }
1046 
1047     /**
1048      * {@inheritDoc}
1049      *
1050      * @implSpec This implementation scans the children in left to right order.
1051      *
1052      * @param node  {@inheritDoc}
1053      * @param p  {@inheritDoc}
1054      * @return the result of scanning
1055      */
1056     @Override
1057     public R visitModifiers(ModifiersTree node, P p) {
1058         return scan(node.getAnnotations(), p);
1059     }
1060 
1061     /**
1062      * {@inheritDoc}
1063      *
1064      * @implSpec This implementation scans the children in left to right order.
1065      *
1066      * @param node  {@inheritDoc}
1067      * @param p  {@inheritDoc}
1068      * @return the result of scanning
1069      */
1070     @Override
1071     public R visitAnnotation(AnnotationTree node, P p) {
1072         R r = scan(node.getAnnotationType(), p);
1073         r = scanAndReduce(node.getArguments(), p, r);
1074         return r;
1075     }
1076 
1077     /**
1078      * {@inheritDoc}
1079      *
1080      * @implSpec This implementation scans the children in left to right order.
1081      *
1082      * @param node  {@inheritDoc}
1083      * @param p  {@inheritDoc}
1084      * @return the result of scanning
1085      */
1086     @Override
1087     public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
1088         R r = scan(node.getAnnotations(), p);
1089         r = scanAndReduce(node.getUnderlyingType(), p, r);
1090         return r;
1091     }
1092 
1093     /**
1094      * {@inheritDoc}
1095      *
1096      * @implSpec This implementation scans the children in left to right order.
1097      *
1098      * @param node  {@inheritDoc}
1099      * @param p  {@inheritDoc}
1100      * @return the result of scanning
1101      */
1102     @Override
1103     public R visitModule(ModuleTree node, P p) {
1104         R r = scan(node.getAnnotations(), p);
1105         r = scanAndReduce(node.getName(), p, r);
1106         r = scanAndReduce(node.getDirectives(), p, r);
1107         return r;
1108     }
1109 
1110     /**
1111      * {@inheritDoc}
1112      *
1113      * @implSpec This implementation scans the children in left to right order.
1114      *
1115      * @param node  {@inheritDoc}
1116      * @param p  {@inheritDoc}
1117      * @return the result of scanning
1118      */
1119     @Override
1120     public R visitExports(ExportsTree node, P p) {
1121         R r = scan(node.getPackageName(), p);
1122         r = scanAndReduce(node.getModuleNames(), p, r);
1123         return r;
1124     }
1125 
1126     /**
1127      * {@inheritDoc}
1128      *
1129      * @implSpec This implementation scans the children in left to right order.
1130      *
1131      * @param node  {@inheritDoc}
1132      * @param p  {@inheritDoc}
1133      * @return the result of scanning
1134      */
1135     @Override
1136     public R visitOpens(OpensTree node, P p) {
1137         R r = scan(node.getPackageName(), p);
1138         r = scanAndReduce(node.getModuleNames(), p, r);
1139         return r;
1140     }
1141 
1142     /**
1143      * {@inheritDoc}
1144      *
1145      * @implSpec This implementation scans the children in left to right order.
1146      *
1147      * @param node  {@inheritDoc}
1148      * @param p  {@inheritDoc}
1149      * @return the result of scanning
1150      */
1151     @Override
1152     public R visitProvides(ProvidesTree node, P p) {
1153         R r = scan(node.getServiceName(), p);
1154         r = scanAndReduce(node.getImplementationNames(), p, r);
1155         return r;
1156     }
1157 
1158     /**
1159      * {@inheritDoc}
1160      *
1161      * @implSpec This implementation scans the children in left to right order.
1162      *
1163      * @param node  {@inheritDoc}
1164      * @param p  {@inheritDoc}
1165      * @return the result of scanning
1166      */
1167     @Override
1168     public R visitRequires(RequiresTree node, P p) {
1169         return scan(node.getModuleName(), p);
1170     }
1171 
1172     /**
1173      * {@inheritDoc}
1174      *
1175      * @implSpec This implementation scans the children in left to right order.
1176      *
1177      * @param node  {@inheritDoc}
1178      * @param p  {@inheritDoc}
1179      * @return the result of scanning
1180      */
1181     @Override
1182     public R visitUses(UsesTree node, P p) {
1183         return scan(node.getServiceName(), p);
1184     }
1185 
1186     /**
1187      * {@inheritDoc}
1188      *
1189      * @implSpec This implementation returns {@code null}.
1190      *
1191      * @param node  {@inheritDoc}
1192      * @param p  {@inheritDoc}
1193      * @return the result of scanning
1194      */
1195     @Override
1196     public R visitOther(Tree node, P p) {
1197         return null;
1198     }
1199 
1200     /**
1201      * {@inheritDoc}
1202      *
1203      * @implSpec This implementation returns {@code null}.
1204      *
1205      * @param node  {@inheritDoc}
1206      * @param p  {@inheritDoc}
1207      * @return the result of scanning
1208      */
1209     @Override
1210     public R visitErroneous(ErroneousTree node, P p) {
1211         return null;
1212     }
1213 
1214     /**
1215      * {@inheritDoc}
1216      *
1217      * @implSpec This implementation scans the children in left to right order.
1218      *
1219      * @param node  {@inheritDoc}
1220      * @param p  {@inheritDoc}
1221      * @return the result of scanning
1222      */
1223     @Override
1224     public R visitYield(YieldTree node, P p) {
1225         return scan(node.getValue(), p);
1226     }
1227 }