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