1 /*
   2  * Copyright (c) 2005, 2019, 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 com.sun.source.tree.CaseTree.CaseKind;
  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>The default implementation of the visitXYZ methods will determine
  39  * a result as follows:
  40  * <ul>
  41  * <li>If the node being visited has no children, the result will be {@code null}.
  42  * <li>If the node being visited has one child, the result will be the
  43  * result of calling {@code scan} on that child. The child may be a simple node
  44  * or itself a list of nodes.
  45  * <li> If the node being visited has more than one child, the result will
  46  * be determined by calling {@code scan} each child in turn, and then combining the
  47  * result of each scan after the first with the cumulative result
  48  * so far, as determined by the {@link #reduce} method. Each child may be either
  49  * a simple node of a list of nodes. The default behavior of the {@code reduce}
  50  * method is such that the result of the visitXYZ method will be the result of
  51  * the last child scanned.
  52  * </ul>
  53  *
  54  * <p>Here is an example to count the number of identifier nodes in a tree:
  55  * <pre>
  56  *   class CountIdentifiers extends TreeScanner&lt;Integer,Void&gt; {
  57  *      {@literal @}Override
  58  *      public Integer visitIdentifier(IdentifierTree node, Void p) {
  59  *          return 1;
  60  *      }
  61  *      {@literal @}Override
  62  *      public Integer reduce(Integer r1, Integer r2) {
  63  *          return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
  64  *      }
  65  *   }
  66  * </pre>
  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     /**
  81      * Scans a single node.
  82      * @param tree the node to be scanned
  83      * @param p a parameter value passed to the visit method
  84      * @return the result value from the visit method
  85      */
  86     public R scan(Tree tree, P p) {
  87         return (tree == null) ? null : tree.accept(this, p);
  88     }
  89 
  90     private R scanAndReduce(Tree node, P p, R r) {
  91         return reduce(scan(node, p), r);
  92     }
  93 
  94     /**
  95      * Scans a sequence of nodes.
  96      * @param nodes the nodes to be scanned
  97      * @param p a parameter value to be passed to the visit method for each node
  98      * @return the combined return value from the visit methods.
  99      *      The values are combined using the {@link #reduce reduce} method.
 100      */
 101     public R scan(Iterable<? extends Tree> nodes, P p) {
 102         R r = null;
 103         if (nodes != null) {
 104             boolean first = true;
 105             for (Tree node : nodes) {
 106                 r = (first ? scan(node, p) : scanAndReduce(node, p, r));
 107                 first = false;
 108             }
 109         }
 110         return r;
 111     }
 112 
 113     private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {
 114         return reduce(scan(nodes, p), r);
 115     }
 116 
 117     /**
 118      * Reduces two results into a combined result.
 119      * The default implementation is to return the first parameter.
 120      * The general contract of the method is that it may take any action whatsoever.
 121      * @param r1 the first of the values to be combined
 122      * @param r2 the second of the values to be combined
 123      * @return the result of combining the two parameters
 124      */
 125     public R reduce(R r1, R r2) {
 126         return r1;
 127     }
 128 
 129 
 130 /* ***************************************************************************
 131  * Visitor methods
 132  ****************************************************************************/
 133 
 134     /**
 135      * {@inheritDoc} This implementation scans the children in left to right order.
 136      *
 137      * @param node  {@inheritDoc}
 138      * @param p  {@inheritDoc}
 139      * @return the result of scanning
 140      */
 141     @Override
 142     public R visitCompilationUnit(CompilationUnitTree node, P p) {
 143         R r = scan(node.getPackage(), p);
 144         r = scanAndReduce(node.getImports(), p, r);
 145         r = scanAndReduce(node.getTypeDecls(), p, r);
 146         return r;
 147     }
 148 
 149     /**
 150      * {@inheritDoc} This implementation scans the children in left to right order.
 151      *
 152      * @param node  {@inheritDoc}
 153      * @param p  {@inheritDoc}
 154      * @return the result of scanning
 155      */
 156     @Override
 157     public R visitPackage(PackageTree node, P p) {
 158         R r = scan(node.getAnnotations(), p);
 159         r = scanAndReduce(node.getPackageName(), p, r);
 160         return r;
 161     }
 162 
 163     /**
 164      * {@inheritDoc} This implementation scans the children in left to right order.
 165      *
 166      * @param node  {@inheritDoc}
 167      * @param p  {@inheritDoc}
 168      * @return the result of scanning
 169      */
 170     @Override
 171     public R visitImport(ImportTree node, P p) {
 172         return scan(node.getQualifiedIdentifier(), p);
 173     }
 174 
 175     /**
 176      * {@inheritDoc} 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 visitClass(ClassTree node, P p) {
 184         R r = scan(node.getModifiers(), p);
 185         r = scanAndReduce(node.getTypeParameters(), p, r);
 186         r = scanAndReduce(node.getExtendsClause(), p, r);
 187         r = scanAndReduce(node.getImplementsClause(), p, r);
 188         r = scanAndReduce(node.getMembers(), p, r);
 189         return r;
 190     }
 191 
 192     /**
 193      * {@inheritDoc} This implementation scans the children in left to right order.
 194      *
 195      * @param node  {@inheritDoc}
 196      * @param p  {@inheritDoc}
 197      * @return the result of scanning
 198      */
 199     @Override
 200     public R visitMethod(MethodTree node, P p) {
 201         R r = scan(node.getModifiers(), p);
 202         r = scanAndReduce(node.getReturnType(), p, r);
 203         r = scanAndReduce(node.getTypeParameters(), p, r);
 204         r = scanAndReduce(node.getParameters(), p, r);
 205         r = scanAndReduce(node.getReceiverParameter(), p, r);
 206         r = scanAndReduce(node.getThrows(), p, r);
 207         r = scanAndReduce(node.getBody(), p, r);
 208         r = scanAndReduce(node.getDefaultValue(), p, r);
 209         return r;
 210     }
 211 
 212     /**
 213      * {@inheritDoc} This implementation scans the children in left to right order.
 214      *
 215      * @param node  {@inheritDoc}
 216      * @param p  {@inheritDoc}
 217      * @return the result of scanning
 218      */
 219     @Override
 220     public R visitVariable(VariableTree node, P p) {
 221         R r = scan(node.getModifiers(), p);
 222         r = scanAndReduce(node.getType(), p, r);
 223         r = scanAndReduce(node.getNameExpression(), p, r);
 224         r = scanAndReduce(node.getInitializer(), p, r);
 225         return r;
 226     }
 227 
 228     /**
 229      * {@inheritDoc} This implementation returns {@code null}.
 230      *
 231      * @param node  {@inheritDoc}
 232      * @param p  {@inheritDoc}
 233      * @return the result of scanning
 234      */
 235     @Override
 236     public R visitEmptyStatement(EmptyStatementTree node, P p) {
 237         return null;
 238     }
 239 
 240     /**
 241      * {@inheritDoc} This implementation scans the children in left to right order.
 242      *
 243      * @param node  {@inheritDoc}
 244      * @param p  {@inheritDoc}
 245      * @return the result of scanning
 246      */
 247     @Override
 248     public R visitBlock(BlockTree node, P p) {
 249         return scan(node.getStatements(), p);
 250     }
 251 
 252     /**
 253      * {@inheritDoc} This implementation scans the children in left to right order.
 254      *
 255      * @param node  {@inheritDoc}
 256      * @param p  {@inheritDoc}
 257      * @return the result of scanning
 258      */
 259     @Override
 260     public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
 261         R r = scan(node.getStatement(), p);
 262         r = scanAndReduce(node.getCondition(), p, r);
 263         return r;
 264     }
 265 
 266     /**
 267      * {@inheritDoc} This implementation scans the children in left to right order.
 268      *
 269      * @param node  {@inheritDoc}
 270      * @param p  {@inheritDoc}
 271      * @return the result of scanning
 272      */
 273     @Override
 274     public R visitWhileLoop(WhileLoopTree node, P p) {
 275         R r = scan(node.getCondition(), p);
 276         r = scanAndReduce(node.getStatement(), p, r);
 277         return r;
 278     }
 279 
 280     /**
 281      * {@inheritDoc} This implementation scans the children in left to right order.
 282      *
 283      * @param node  {@inheritDoc}
 284      * @param p  {@inheritDoc}
 285      * @return the result of scanning
 286      */
 287     @Override
 288     public R visitWithField(WithFieldTree node, P p) {
 289         R r = scan(node.getField(), p);
 290         r = scanAndReduce(node.getValue(), p, r);
 291         return r;
 292     }
 293 
 294     /**
 295      * {@inheritDoc} This implementation scans the children in left to right order.
 296      *
 297      * @param node  {@inheritDoc}
 298      * @param p  {@inheritDoc}
 299      * @return the result of scanning
 300      */
 301     @Override
 302     public R visitForLoop(ForLoopTree node, P p) {
 303         R r = scan(node.getInitializer(), p);
 304         r = scanAndReduce(node.getCondition(), p, r);
 305         r = scanAndReduce(node.getUpdate(), p, r);
 306         r = scanAndReduce(node.getStatement(), p, r);
 307         return r;
 308     }
 309 
 310     /**
 311      * {@inheritDoc} This implementation scans the children in left to right order.
 312      *
 313      * @param node  {@inheritDoc}
 314      * @param p  {@inheritDoc}
 315      * @return the result of scanning
 316      */
 317     @Override
 318     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
 319         R r = scan(node.getVariable(), p);
 320         r = scanAndReduce(node.getExpression(), p, r);
 321         r = scanAndReduce(node.getStatement(), p, r);
 322         return r;
 323     }
 324 
 325     /**
 326      * {@inheritDoc} 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 visitLabeledStatement(LabeledStatementTree node, P p) {
 334         return scan(node.getStatement(), p);
 335     }
 336 
 337     /**
 338      * {@inheritDoc} This implementation scans the children in left to right order.
 339      *
 340      * @param node  {@inheritDoc}
 341      * @param p  {@inheritDoc}
 342      * @return the result of scanning
 343      */
 344     @Override
 345     public R visitSwitch(SwitchTree node, P p) {
 346         R r = scan(node.getExpression(), p);
 347         r = scanAndReduce(node.getCases(), p, r);
 348         return r;
 349     }
 350 
 351     /**
 352      * {@inheritDoc} This implementation scans the children in left to right order.
 353      *
 354      * @param node  {@inheritDoc}
 355      * @param p  {@inheritDoc}
 356      * @return the result of scanning
 357      *
 358      * @deprecated
 359      * This method is modeling switch expressions,
 360      * which are part of a preview feature and may be removed
 361      * if the preview feature is removed.
 362      */
 363     @Override
 364     @Deprecated(forRemoval=true, since="12")
 365     @SuppressWarnings("removal")
 366     public R visitSwitchExpression(SwitchExpressionTree node, P p) {
 367         R r = scan(node.getExpression(), p);
 368         r = scanAndReduce(node.getCases(), p, r);
 369         return r;
 370     }
 371 
 372     /**
 373      * {@inheritDoc} 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     @SuppressWarnings("removal")
 381     public R visitCase(CaseTree node, P p) {
 382         R r = scan(node.getExpressions(), p);
 383         if (node.getCaseKind() == CaseKind.RULE)
 384             r = scanAndReduce(node.getBody(), p, r);
 385         else
 386             r = scanAndReduce(node.getStatements(), p, r);
 387         return r;
 388     }
 389 
 390     /**
 391      * {@inheritDoc} This implementation scans the children in left to right order.
 392      *
 393      * @param node  {@inheritDoc}
 394      * @param p  {@inheritDoc}
 395      * @return the result of scanning
 396      */
 397     @Override
 398     public R visitSynchronized(SynchronizedTree node, P p) {
 399         R r = scan(node.getExpression(), p);
 400         r = scanAndReduce(node.getBlock(), p, r);
 401         return r;
 402     }
 403 
 404     /**
 405      * {@inheritDoc} 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 visitTry(TryTree node, P p) {
 413         R r = scan(node.getResources(), p);
 414         r = scanAndReduce(node.getBlock(), p, r);
 415         r = scanAndReduce(node.getCatches(), p, r);
 416         r = scanAndReduce(node.getFinallyBlock(), p, r);
 417         return r;
 418     }
 419 
 420     /**
 421      * {@inheritDoc} This implementation scans the children in left to right order.
 422      *
 423      * @param node  {@inheritDoc}
 424      * @param p  {@inheritDoc}
 425      * @return the result of scanning
 426      */
 427     @Override
 428     public R visitCatch(CatchTree node, P p) {
 429         R r = scan(node.getParameter(), p);
 430         r = scanAndReduce(node.getBlock(), p, r);
 431         return r;
 432     }
 433 
 434     /**
 435      * {@inheritDoc} This implementation scans the children in left to right order.
 436      *
 437      * @param node  {@inheritDoc}
 438      * @param p  {@inheritDoc}
 439      * @return the result of scanning
 440      */
 441     @Override
 442     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
 443         R r = scan(node.getCondition(), p);
 444         r = scanAndReduce(node.getTrueExpression(), p, r);
 445         r = scanAndReduce(node.getFalseExpression(), p, r);
 446         return r;
 447     }
 448 
 449     /**
 450      * {@inheritDoc} This implementation scans the children in left to right order.
 451      *
 452      * @param node  {@inheritDoc}
 453      * @param p  {@inheritDoc}
 454      * @return the result of scanning
 455      */
 456     @Override
 457     public R visitIf(IfTree node, P p) {
 458         R r = scan(node.getCondition(), p);
 459         r = scanAndReduce(node.getThenStatement(), p, r);
 460         r = scanAndReduce(node.getElseStatement(), p, r);
 461         return r;
 462     }
 463 
 464     /**
 465      * {@inheritDoc} This implementation scans the children in left to right order.
 466      *
 467      * @param node  {@inheritDoc}
 468      * @param p  {@inheritDoc}
 469      * @return the result of scanning
 470      */
 471     @Override
 472     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 473         return scan(node.getExpression(), p);
 474     }
 475 
 476     /**
 477      * {@inheritDoc} This implementation returns {@code null}.
 478      *
 479      * @param node  {@inheritDoc}
 480      * @param p  {@inheritDoc}
 481      * @return the result of scanning
 482      */
 483     @Override
 484     public R visitBreak(BreakTree node, P p) {
 485         return null;
 486     }
 487 
 488     /**
 489      * {@inheritDoc} This implementation returns {@code null}.
 490      *
 491      * @param node  {@inheritDoc}
 492      * @param p  {@inheritDoc}
 493      * @return the result of scanning
 494      */
 495     @Override
 496     public R visitContinue(ContinueTree node, P p) {
 497         return null;
 498     }
 499 
 500     /**
 501      * {@inheritDoc} This implementation scans the children in left to right order.
 502      *
 503      * @param node  {@inheritDoc}
 504      * @param p  {@inheritDoc}
 505      * @return the result of scanning
 506      */
 507     @Override
 508     public R visitReturn(ReturnTree node, P p) {
 509         return scan(node.getExpression(), p);
 510     }
 511 
 512     /**
 513      * {@inheritDoc} This implementation scans the children in left to right order.
 514      *
 515      * @param node  {@inheritDoc}
 516      * @param p  {@inheritDoc}
 517      * @return the result of scanning
 518      */
 519     @Override
 520     public R visitThrow(ThrowTree node, P p) {
 521         return scan(node.getExpression(), p);
 522     }
 523 
 524     /**
 525      * {@inheritDoc} This implementation scans the children in left to right order.
 526      *
 527      * @param node  {@inheritDoc}
 528      * @param p  {@inheritDoc}
 529      * @return the result of scanning
 530      */
 531     @Override
 532     public R visitAssert(AssertTree node, P p) {
 533         R r = scan(node.getCondition(), p);
 534         r = scanAndReduce(node.getDetail(), p, r);
 535         return r;
 536     }
 537 
 538     /**
 539      * {@inheritDoc} This implementation scans the children in left to right order.
 540      *
 541      * @param node  {@inheritDoc}
 542      * @param p  {@inheritDoc}
 543      * @return the result of scanning
 544      */
 545     @Override
 546     public R visitMethodInvocation(MethodInvocationTree node, P p) {
 547         R r = scan(node.getTypeArguments(), p);
 548         r = scanAndReduce(node.getMethodSelect(), p, r);
 549         r = scanAndReduce(node.getArguments(), p, r);
 550         return r;
 551     }
 552 
 553     /**
 554      * {@inheritDoc} This implementation scans the children in left to right order.
 555      *
 556      * @param node  {@inheritDoc}
 557      * @param p  {@inheritDoc}
 558      * @return the result of scanning
 559      */
 560     @Override
 561     public R visitNewClass(NewClassTree node, P p) {
 562         R r = scan(node.getEnclosingExpression(), p);
 563         r = scanAndReduce(node.getIdentifier(), p, r);
 564         r = scanAndReduce(node.getTypeArguments(), p, r);
 565         r = scanAndReduce(node.getArguments(), p, r);
 566         r = scanAndReduce(node.getClassBody(), p, r);
 567         return r;
 568     }
 569 
 570     /**
 571      * {@inheritDoc} This implementation scans the children in left to right order.
 572      *
 573      * @param node  {@inheritDoc}
 574      * @param p  {@inheritDoc}
 575      * @return the result of scanning
 576      */
 577     @Override
 578     public R visitNewArray(NewArrayTree node, P p) {
 579         R r = scan(node.getType(), p);
 580         r = scanAndReduce(node.getDimensions(), p, r);
 581         r = scanAndReduce(node.getInitializers(), p, r);
 582         r = scanAndReduce(node.getAnnotations(), p, r);
 583         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 584             r = scanAndReduce(dimAnno, p, r);
 585         }
 586         return r;
 587     }
 588 
 589     /**
 590      * {@inheritDoc} 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 visitLambdaExpression(LambdaExpressionTree node, P p) {
 598         R r = scan(node.getParameters(), p);
 599         r = scanAndReduce(node.getBody(), p, r);
 600         return r;
 601     }
 602 
 603     /**
 604      * {@inheritDoc} This implementation scans the children in left to right order.
 605      *
 606      * @param node  {@inheritDoc}
 607      * @param p  {@inheritDoc}
 608      * @return the result of scanning
 609      */
 610     @Override
 611     public R visitParenthesized(ParenthesizedTree node, P p) {
 612         return scan(node.getExpression(), p);
 613     }
 614 
 615     /**
 616      * {@inheritDoc} This implementation scans the children in left to right order.
 617      *
 618      * @param node  {@inheritDoc}
 619      * @param p  {@inheritDoc}
 620      * @return the result of scanning
 621      */
 622     @Override
 623     public R visitAssignment(AssignmentTree node, P p) {
 624         R r = scan(node.getVariable(), p);
 625         r = scanAndReduce(node.getExpression(), p, r);
 626         return r;
 627     }
 628 
 629     /**
 630      * {@inheritDoc} This implementation scans the children in left to right order.
 631      *
 632      * @param node  {@inheritDoc}
 633      * @param p  {@inheritDoc}
 634      * @return the result of scanning
 635      */
 636     @Override
 637     public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
 638         R r = scan(node.getVariable(), p);
 639         r = scanAndReduce(node.getExpression(), p, r);
 640         return r;
 641     }
 642 
 643     /**
 644      * {@inheritDoc} This implementation scans the children in left to right order.
 645      *
 646      * @param node  {@inheritDoc}
 647      * @param p  {@inheritDoc}
 648      * @return the result of scanning
 649      */
 650     @Override
 651     public R visitUnary(UnaryTree node, P p) {
 652         return scan(node.getExpression(), p);
 653     }
 654 
 655     /**
 656      * {@inheritDoc} This implementation scans the children in left to right order.
 657      *
 658      * @param node  {@inheritDoc}
 659      * @param p  {@inheritDoc}
 660      * @return the result of scanning
 661      */
 662     @Override
 663     public R visitBinary(BinaryTree node, P p) {
 664         R r = scan(node.getLeftOperand(), p);
 665         r = scanAndReduce(node.getRightOperand(), p, r);
 666         return r;
 667     }
 668 
 669     /**
 670      * {@inheritDoc} This implementation scans the children in left to right order.
 671      *
 672      * @param node  {@inheritDoc}
 673      * @param p  {@inheritDoc}
 674      * @return the result of scanning
 675      */
 676     @Override
 677     public R visitTypeCast(TypeCastTree node, P p) {
 678         R r = scan(node.getType(), p);
 679         r = scanAndReduce(node.getExpression(), p, r);
 680         return r;
 681     }
 682 
 683     /**
 684      * {@inheritDoc} This implementation scans the children in left to right order.
 685      *
 686      * @param node  {@inheritDoc}
 687      * @param p  {@inheritDoc}
 688      * @return the result of scanning
 689      */
 690     @Override
 691     public R visitInstanceOf(InstanceOfTree node, P p) {
 692         R r = scan(node.getExpression(), p);
 693         r = scanAndReduce(node.getType(), p, r);
 694         return r;
 695     }
 696 
 697     /**
 698      * {@inheritDoc} 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 visitArrayAccess(ArrayAccessTree node, P p) {
 706         R r = scan(node.getExpression(), p);
 707         r = scanAndReduce(node.getIndex(), p, r);
 708         return r;
 709     }
 710 
 711     /**
 712      * {@inheritDoc} 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 visitMemberSelect(MemberSelectTree node, P p) {
 720         return scan(node.getExpression(), p);
 721     }
 722 
 723     /**
 724      * {@inheritDoc} This implementation scans the children in left to right order.
 725      *
 726      * @param node  {@inheritDoc}
 727      * @param p  {@inheritDoc}
 728      * @return the result of scanning
 729      */
 730     @Override
 731     public R visitMemberReference(MemberReferenceTree node, P p) {
 732         R r = scan(node.getQualifierExpression(), p);
 733         r = scanAndReduce(node.getTypeArguments(), p, r);
 734         return r;
 735     }
 736 
 737     /**
 738      * {@inheritDoc} This implementation returns {@code null}.
 739      *
 740      * @param node  {@inheritDoc}
 741      * @param p  {@inheritDoc}
 742      * @return the result of scanning
 743      */
 744     @Override
 745     public R visitIdentifier(IdentifierTree node, P p) {
 746         return null;
 747     }
 748 
 749     /**
 750      * {@inheritDoc} This implementation returns {@code null}.
 751      *
 752      * @param node  {@inheritDoc}
 753      * @param p  {@inheritDoc}
 754      * @return the result of scanning
 755      */
 756     @Override
 757     public R visitLiteral(LiteralTree node, P p) {
 758         return null;
 759     }
 760 
 761     /**
 762      * {@inheritDoc} This implementation returns {@code null}.
 763      *
 764      * @param node  {@inheritDoc}
 765      * @param p  {@inheritDoc}
 766      * @return the result of scanning
 767      */
 768     @Override
 769     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 770         return null;
 771     }
 772 
 773     /**
 774      * {@inheritDoc} This implementation scans the children in left to right order.
 775      *
 776      * @param node  {@inheritDoc}
 777      * @param p  {@inheritDoc}
 778      * @return the result of scanning
 779      */
 780     @Override
 781     public R visitArrayType(ArrayTypeTree node, P p) {
 782         return scan(node.getType(), p);
 783     }
 784 
 785     /**
 786      * {@inheritDoc} This implementation scans the children in left to right order.
 787      *
 788      * @param node  {@inheritDoc}
 789      * @param p  {@inheritDoc}
 790      * @return the result of scanning
 791      */
 792     @Override
 793     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 794         R r = scan(node.getType(), p);
 795         r = scanAndReduce(node.getTypeArguments(), p, r);
 796         return r;
 797     }
 798 
 799     /**
 800      * {@inheritDoc} This implementation scans the children in left to right order.
 801      *
 802      * @param node  {@inheritDoc}
 803      * @param p  {@inheritDoc}
 804      * @return the result of scanning
 805      */
 806     @Override
 807     public R visitUnionType(UnionTypeTree node, P p) {
 808         return scan(node.getTypeAlternatives(), p);
 809     }
 810 
 811     /**
 812      * {@inheritDoc} This implementation scans the children in left to right order.
 813      *
 814      * @param node  {@inheritDoc}
 815      * @param p  {@inheritDoc}
 816      * @return the result of scanning
 817      */
 818     @Override
 819     public R visitIntersectionType(IntersectionTypeTree node, P p) {
 820         return scan(node.getBounds(), p);
 821     }
 822 
 823     /**
 824      * {@inheritDoc} This implementation scans the children in left to right order.
 825      *
 826      * @param node  {@inheritDoc}
 827      * @param p  {@inheritDoc}
 828      * @return the result of scanning
 829      */
 830     @Override
 831     public R visitTypeParameter(TypeParameterTree node, P p) {
 832         R r = scan(node.getAnnotations(), p);
 833         r = scanAndReduce(node.getBounds(), p, r);
 834         return r;
 835     }
 836 
 837     /**
 838      * {@inheritDoc} 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 visitWildcard(WildcardTree node, P p) {
 846         return scan(node.getBound(), p);
 847     }
 848 
 849     /**
 850      * {@inheritDoc} This implementation scans the children in left to right order.
 851      *
 852      * @param node  {@inheritDoc}
 853      * @param p  {@inheritDoc}
 854      * @return the result of scanning
 855      */
 856     @Override
 857     public R visitModifiers(ModifiersTree node, P p) {
 858         return scan(node.getAnnotations(), p);
 859     }
 860 
 861     /**
 862      * {@inheritDoc} This implementation scans the children in left to right order.
 863      *
 864      * @param node  {@inheritDoc}
 865      * @param p  {@inheritDoc}
 866      * @return the result of scanning
 867      */
 868     @Override
 869     public R visitAnnotation(AnnotationTree node, P p) {
 870         R r = scan(node.getAnnotationType(), p);
 871         r = scanAndReduce(node.getArguments(), p, r);
 872         return r;
 873     }
 874 
 875     /**
 876      * {@inheritDoc} This implementation scans the children in left to right order.
 877      *
 878      * @param node  {@inheritDoc}
 879      * @param p  {@inheritDoc}
 880      * @return the result of scanning
 881      */
 882     @Override
 883     public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
 884         R r = scan(node.getAnnotations(), p);
 885         r = scanAndReduce(node.getUnderlyingType(), p, r);
 886         return r;
 887     }
 888 
 889     @Override
 890     public R visitModule(ModuleTree node, P p) {
 891         R r = scan(node.getAnnotations(), p);
 892         r = scanAndReduce(node.getName(), p, r);
 893         r = scanAndReduce(node.getDirectives(), p, r);
 894         return r;
 895     }
 896 
 897     @Override
 898     public R visitExports(ExportsTree node, P p) {
 899         R r = scan(node.getPackageName(), p);
 900         r = scanAndReduce(node.getModuleNames(), p, r);
 901         return r;
 902     }
 903 
 904     @Override
 905     public R visitOpens(OpensTree node, P p) {
 906         R r = scan(node.getPackageName(), p);
 907         r = scanAndReduce(node.getModuleNames(), p, r);
 908         return r;
 909     }
 910 
 911     @Override
 912     public R visitProvides(ProvidesTree node, P p) {
 913         R r = scan(node.getServiceName(), p);
 914         r = scanAndReduce(node.getImplementationNames(), p, r);
 915         return r;
 916     }
 917 
 918     @Override
 919     public R visitRequires(RequiresTree node, P p) {
 920         return scan(node.getModuleName(), p);
 921     }
 922 
 923     @Override
 924     public R visitUses(UsesTree node, P p) {
 925         return scan(node.getServiceName(), p);
 926     }
 927 
 928     /**
 929      * {@inheritDoc} 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 visitOther(Tree node, P p) {
 937         return null;
 938     }
 939 
 940     /**
 941      * {@inheritDoc} This implementation returns {@code null}.
 942      *
 943      * @param node  {@inheritDoc}
 944      * @param p  {@inheritDoc}
 945      * @return the result of scanning
 946      */
 947     @Override
 948     public R visitErroneous(ErroneousTree node, P p) {
 949         return null;
 950     }
 951 
 952     /**
 953      * {@inheritDoc} This implementation returns {@code null}.
 954      *
 955      * @param node  {@inheritDoc}
 956      * @param p  {@inheritDoc}
 957      * @return the result of scanning
 958      *
 959      * @deprecated
 960      * This method is modeling switch expressions,
 961      * which are part of a preview feature and may be removed
 962      * if the preview feature is removed.
 963      */
 964     @Override
 965     @Deprecated(forRemoval=true, since="13")
 966     @SuppressWarnings("removal")
 967     public R visitYield(YieldTree node, P p) {
 968         return scan(node.getValue(), p);
 969     }
 970 }