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