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 visitForLoop(ForLoopTree node, P p) {
 289         R r = scan(node.getInitializer(), p);
 290         r = scanAndReduce(node.getCondition(), p, r);
 291         r = scanAndReduce(node.getUpdate(), p, r);
 292         r = scanAndReduce(node.getStatement(), p, r);
 293         return r;
 294     }
 295 
 296     /**
 297      * {@inheritDoc} This implementation scans the children in left to right order.
 298      *
 299      * @param node  {@inheritDoc}
 300      * @param p  {@inheritDoc}
 301      * @return the result of scanning
 302      */
 303     @Override
 304     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
 305         R r = scan(node.getVariable(), p);
 306         r = scanAndReduce(node.getExpression(), p, r);
 307         r = scanAndReduce(node.getStatement(), p, r);
 308         return r;
 309     }
 310 
 311     /**
 312      * {@inheritDoc} This implementation scans the children in left to right order.
 313      *
 314      * @param node  {@inheritDoc}
 315      * @param p  {@inheritDoc}
 316      * @return the result of scanning
 317      */
 318     @Override
 319     public R visitLabeledStatement(LabeledStatementTree node, P p) {
 320         return scan(node.getStatement(), p);
 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 visitSwitch(SwitchTree node, P p) {
 332         R r = scan(node.getExpression(), p);
 333         r = scanAndReduce(node.getCases(), p, r);
 334         return r;
 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      * @deprecated
 345      * This method is modeling switch expressions,
 346      * which are part of a preview feature and may be removed
 347      * if the preview feature is removed.
 348      */
 349     @Override
 350     @Deprecated(forRemoval=true, since="12")
 351     @SuppressWarnings("removal")
 352     public R visitSwitchExpression(SwitchExpressionTree node, P p) {
 353         R r = scan(node.getExpression(), p);
 354         r = scanAndReduce(node.getCases(), p, r);
 355         return r;
 356     }
 357 
 358     /**
 359      * {@inheritDoc} 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     @SuppressWarnings("removal")
 367     public R visitCase(CaseTree node, P p) {
 368         R r = scan(node.getExpressions(), p);
 369         if (node.getCaseKind() == CaseKind.RULE)
 370             r = scanAndReduce(node.getBody(), p, r);
 371         else
 372             r = scanAndReduce(node.getStatements(), p, r);
 373         return r;
 374     }
 375 
 376     /**
 377      * {@inheritDoc} This implementation scans the children in left to right order.
 378      *
 379      * @param node  {@inheritDoc}
 380      * @param p  {@inheritDoc}
 381      * @return the result of scanning
 382      */
 383     @Override
 384     public R visitSynchronized(SynchronizedTree node, P p) {
 385         R r = scan(node.getExpression(), p);
 386         r = scanAndReduce(node.getBlock(), 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 visitTry(TryTree node, P p) {
 399         R r = scan(node.getResources(), p);
 400         r = scanAndReduce(node.getBlock(), p, r);
 401         r = scanAndReduce(node.getCatches(), p, r);
 402         r = scanAndReduce(node.getFinallyBlock(), p, r);
 403         return r;
 404     }
 405 
 406     /**
 407      * {@inheritDoc} This implementation scans the children in left to right order.
 408      *
 409      * @param node  {@inheritDoc}
 410      * @param p  {@inheritDoc}
 411      * @return the result of scanning
 412      */
 413     @Override
 414     public R visitCatch(CatchTree node, P p) {
 415         R r = scan(node.getParameter(), p);
 416         r = scanAndReduce(node.getBlock(), 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 visitConditionalExpression(ConditionalExpressionTree node, P p) {
 429         R r = scan(node.getCondition(), p);
 430         r = scanAndReduce(node.getTrueExpression(), p, r);
 431         r = scanAndReduce(node.getFalseExpression(), p, r);
 432         return r;
 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 visitIf(IfTree node, P p) {
 444         R r = scan(node.getCondition(), p);
 445         r = scanAndReduce(node.getThenStatement(), p, r);
 446         r = scanAndReduce(node.getElseStatement(), p, r);
 447         return r;
 448     }
 449 
 450     /**
 451      * {@inheritDoc} This implementation scans the children in left to right order.
 452      *
 453      * @param node  {@inheritDoc}
 454      * @param p  {@inheritDoc}
 455      * @return the result of scanning
 456      */
 457     @Override
 458     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 459         return scan(node.getExpression(), p);
 460     }
 461 
 462     /**
 463      * {@inheritDoc} This implementation returns {@code null}.
 464      *
 465      * @param node  {@inheritDoc}
 466      * @param p  {@inheritDoc}
 467      * @return the result of scanning
 468      */
 469     @Override
 470     public R visitBreak(BreakTree node, P p) {
 471         return null;
 472     }
 473 
 474     /**
 475      * {@inheritDoc} This implementation returns {@code null}.
 476      *
 477      * @param node  {@inheritDoc}
 478      * @param p  {@inheritDoc}
 479      * @return the result of scanning
 480      */
 481     @Override
 482     public R visitContinue(ContinueTree node, P p) {
 483         return null;
 484     }
 485 
 486     /**
 487      * {@inheritDoc} This implementation scans the children in left to right order.
 488      *
 489      * @param node  {@inheritDoc}
 490      * @param p  {@inheritDoc}
 491      * @return the result of scanning
 492      */
 493     @Override
 494     public R visitReturn(ReturnTree node, P p) {
 495         return scan(node.getExpression(), p);
 496     }
 497 
 498     /**
 499      * {@inheritDoc} This implementation scans the children in left to right order.
 500      *
 501      * @param node  {@inheritDoc}
 502      * @param p  {@inheritDoc}
 503      * @return the result of scanning
 504      */
 505     @Override
 506     public R visitThrow(ThrowTree node, P p) {
 507         return scan(node.getExpression(), p);
 508     }
 509 
 510     /**
 511      * {@inheritDoc} This implementation scans the children in left to right order.
 512      *
 513      * @param node  {@inheritDoc}
 514      * @param p  {@inheritDoc}
 515      * @return the result of scanning
 516      */
 517     @Override
 518     public R visitAssert(AssertTree node, P p) {
 519         R r = scan(node.getCondition(), p);
 520         r = scanAndReduce(node.getDetail(), p, r);
 521         return r;
 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 visitMethodInvocation(MethodInvocationTree node, P p) {
 533         R r = scan(node.getTypeArguments(), p);
 534         r = scanAndReduce(node.getMethodSelect(), p, r);
 535         r = scanAndReduce(node.getArguments(), p, r);
 536         return r;
 537     }
 538 
 539     /**
 540      * {@inheritDoc} This implementation scans the children in left to right order.
 541      *
 542      * @param node  {@inheritDoc}
 543      * @param p  {@inheritDoc}
 544      * @return the result of scanning
 545      */
 546     @Override
 547     public R visitNewClass(NewClassTree node, P p) {
 548         R r = scan(node.getEnclosingExpression(), p);
 549         r = scanAndReduce(node.getIdentifier(), p, r);
 550         r = scanAndReduce(node.getTypeArguments(), p, r);
 551         r = scanAndReduce(node.getArguments(), p, r);
 552         r = scanAndReduce(node.getClassBody(), p, r);
 553         return r;
 554     }
 555 
 556     /**
 557      * {@inheritDoc} This implementation scans the children in left to right order.
 558      *
 559      * @param node  {@inheritDoc}
 560      * @param p  {@inheritDoc}
 561      * @return the result of scanning
 562      */
 563     @Override
 564     public R visitNewArray(NewArrayTree node, P p) {
 565         R r = scan(node.getType(), p);
 566         r = scanAndReduce(node.getDimensions(), p, r);
 567         r = scanAndReduce(node.getInitializers(), p, r);
 568         r = scanAndReduce(node.getAnnotations(), p, r);
 569         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 570             r = scanAndReduce(dimAnno, p, r);
 571         }
 572         return r;
 573     }
 574 
 575     /**
 576      * {@inheritDoc} 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 visitLambdaExpression(LambdaExpressionTree node, P p) {
 584         R r = scan(node.getParameters(), p);
 585         r = scanAndReduce(node.getBody(), p, r);
 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 visitParenthesized(ParenthesizedTree node, P p) {
 598         return scan(node.getExpression(), p);
 599     }
 600 
 601     /**
 602      * {@inheritDoc} This implementation scans the children in left to right order.
 603      *
 604      * @param node  {@inheritDoc}
 605      * @param p  {@inheritDoc}
 606      * @return the result of scanning
 607      */
 608     @Override
 609     public R visitAssignment(AssignmentTree node, P p) {
 610         R r = scan(node.getVariable(), p);
 611         r = scanAndReduce(node.getExpression(), p, r);
 612         return r;
 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 visitCompoundAssignment(CompoundAssignmentTree 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 visitUnary(UnaryTree node, P p) {
 638         return scan(node.getExpression(), p);
 639     }
 640 
 641     /**
 642      * {@inheritDoc} 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 visitBinary(BinaryTree node, P p) {
 650         R r = scan(node.getLeftOperand(), p);
 651         r = scanAndReduce(node.getRightOperand(), p, r);
 652         return r;
 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 visitTypeCast(TypeCastTree node, P p) {
 664         R r = scan(node.getType(), p);
 665         r = scanAndReduce(node.getExpression(), 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 visitInstanceOf(InstanceOfTree node, P p) {
 678         R r = scan(node.getExpression(), p);
 679         r = scanAndReduce(node.getType(), 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 visitArrayAccess(ArrayAccessTree node, P p) {
 692         R r = scan(node.getExpression(), p);
 693         r = scanAndReduce(node.getIndex(), 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 visitMemberSelect(MemberSelectTree node, P p) {
 706         return scan(node.getExpression(), p);
 707     }
 708 
 709     /**
 710      * {@inheritDoc} This implementation scans the children in left to right order.
 711      *
 712      * @param node  {@inheritDoc}
 713      * @param p  {@inheritDoc}
 714      * @return the result of scanning
 715      */
 716     @Override
 717     public R visitMemberReference(MemberReferenceTree node, P p) {
 718         R r = scan(node.getQualifierExpression(), p);
 719         r = scanAndReduce(node.getTypeArguments(), p, r);
 720         return r;
 721     }
 722 
 723     /**
 724      * {@inheritDoc} This implementation returns {@code null}.
 725      *
 726      * @param node  {@inheritDoc}
 727      * @param p  {@inheritDoc}
 728      * @return the result of scanning
 729      */
 730     @Override
 731     public R visitIdentifier(IdentifierTree node, P p) {
 732         return null;
 733     }
 734 
 735     /**
 736      * {@inheritDoc} This implementation returns {@code null}.
 737      *
 738      * @param node  {@inheritDoc}
 739      * @param p  {@inheritDoc}
 740      * @return the result of scanning
 741      */
 742     @Override
 743     public R visitLiteral(LiteralTree node, P p) {
 744         return null;
 745     }
 746 
 747     /**
 748      * {@inheritDoc} This implementation returns {@code null}.
 749      *
 750      * @param node  {@inheritDoc}
 751      * @param p  {@inheritDoc}
 752      * @return the result of scanning
 753      */
 754     @Override
 755     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 756         return null;
 757     }
 758 
 759     /**
 760      * {@inheritDoc} This implementation scans the children in left to right order.
 761      *
 762      * @param node  {@inheritDoc}
 763      * @param p  {@inheritDoc}
 764      * @return the result of scanning
 765      */
 766     @Override
 767     public R visitArrayType(ArrayTypeTree node, P p) {
 768         return scan(node.getType(), p);
 769     }
 770 
 771     /**
 772      * {@inheritDoc} This implementation scans the children in left to right order.
 773      *
 774      * @param node  {@inheritDoc}
 775      * @param p  {@inheritDoc}
 776      * @return the result of scanning
 777      */
 778     @Override
 779     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 780         R r = scan(node.getType(), p);
 781         r = scanAndReduce(node.getTypeArguments(), p, r);
 782         return r;
 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 visitUnionType(UnionTypeTree node, P p) {
 794         return scan(node.getTypeAlternatives(), p);
 795     }
 796 
 797     /**
 798      * {@inheritDoc} This implementation scans the children in left to right order.
 799      *
 800      * @param node  {@inheritDoc}
 801      * @param p  {@inheritDoc}
 802      * @return the result of scanning
 803      */
 804     @Override
 805     public R visitIntersectionType(IntersectionTypeTree node, P p) {
 806         return scan(node.getBounds(), p);
 807     }
 808 
 809     /**
 810      * {@inheritDoc} This implementation scans the children in left to right order.
 811      *
 812      * @param node  {@inheritDoc}
 813      * @param p  {@inheritDoc}
 814      * @return the result of scanning
 815      */
 816     @Override
 817     public R visitTypeParameter(TypeParameterTree node, P p) {
 818         R r = scan(node.getAnnotations(), p);
 819         r = scanAndReduce(node.getBounds(), p, r);
 820         return r;
 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 visitWildcard(WildcardTree node, P p) {
 832         return scan(node.getBound(), p);
 833     }
 834 
 835     /**
 836      * {@inheritDoc} This implementation scans the children in left to right order.
 837      *
 838      * @param node  {@inheritDoc}
 839      * @param p  {@inheritDoc}
 840      * @return the result of scanning
 841      */
 842     @Override
 843     public R visitModifiers(ModifiersTree node, P p) {
 844         return scan(node.getAnnotations(), p);
 845     }
 846 
 847     /**
 848      * {@inheritDoc} This implementation scans the children in left to right order.
 849      *
 850      * @param node  {@inheritDoc}
 851      * @param p  {@inheritDoc}
 852      * @return the result of scanning
 853      */
 854     @Override
 855     public R visitAnnotation(AnnotationTree node, P p) {
 856         R r = scan(node.getAnnotationType(), p);
 857         r = scanAndReduce(node.getArguments(), p, r);
 858         return r;
 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 visitAnnotatedType(AnnotatedTypeTree node, P p) {
 870         R r = scan(node.getAnnotations(), p);
 871         r = scanAndReduce(node.getUnderlyingType(), p, r);
 872         return r;
 873     }
 874 
 875     @Override
 876     public R visitModule(ModuleTree node, P p) {
 877         R r = scan(node.getAnnotations(), p);
 878         r = scanAndReduce(node.getName(), p, r);
 879         r = scanAndReduce(node.getDirectives(), p, r);
 880         return r;
 881     }
 882 
 883     @Override
 884     public R visitExports(ExportsTree node, P p) {
 885         R r = scan(node.getPackageName(), p);
 886         r = scanAndReduce(node.getModuleNames(), p, r);
 887         return r;
 888     }
 889 
 890     @Override
 891     public R visitOpens(OpensTree node, P p) {
 892         R r = scan(node.getPackageName(), p);
 893         r = scanAndReduce(node.getModuleNames(), p, r);
 894         return r;
 895     }
 896 
 897     @Override
 898     public R visitProvides(ProvidesTree node, P p) {
 899         R r = scan(node.getServiceName(), p);
 900         r = scanAndReduce(node.getImplementationNames(), p, r);
 901         return r;
 902     }
 903 
 904     @Override
 905     public R visitRequires(RequiresTree node, P p) {
 906         return scan(node.getModuleName(), p);
 907     }
 908 
 909     @Override
 910     public R visitUses(UsesTree node, P p) {
 911         return scan(node.getServiceName(), p);
 912     }
 913 
 914     /**
 915      * {@inheritDoc} 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 visitOther(Tree node, P p) {
 923         return null;
 924     }
 925 
 926     /**
 927      * {@inheritDoc} This implementation returns {@code null}.
 928      *
 929      * @param node  {@inheritDoc}
 930      * @param p  {@inheritDoc}
 931      * @return the result of scanning
 932      */
 933     @Override
 934     public R visitErroneous(ErroneousTree node, P p) {
 935         return null;
 936     }
 937 
 938     /**
 939      * {@inheritDoc} This implementation returns {@code null}.
 940      *
 941      * @param node  {@inheritDoc}
 942      * @param p  {@inheritDoc}
 943      * @return the result of scanning
 944      *
 945      * @deprecated
 946      * This method is modeling switch expressions,
 947      * which are part of a preview feature and may be removed
 948      * if the preview feature is removed.
 949      */
 950     @Override
 951     @Deprecated(forRemoval=true, since="13")
 952     @SuppressWarnings("removal")
 953     public R visitYield(YieldTree node, P p) {
 954         return scan(node.getValue(), p);
 955     }
 956 }