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