1 /*
   2  * Copyright (c) 2005, 2016, 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>The default implementation of the visitXYZ methods will determine
  38  * a result as follows:
  39  * <ul>
  40  * <li>If the node being visited has no children, the result will be {@code null}.
  41  * <li>If the node being visited has one child, the result will be the
  42  * result of calling {@code scan} on that child. The child may be a simple node
  43  * or itself a list of nodes.
  44  * <li> If the node being visited has more than one child, the result will
  45  * be determined by calling {@code scan} each child in turn, and then combining the
  46  * result of each scan after the first with the cumulative result
  47  * so far, as determined by the {@link #reduce} method. Each child may be either
  48  * a simple node of a list of nodes. The default behavior of the {@code reduce}
  49  * method is such that the result of the visitXYZ method will be the result of
  50  * the last child scanned.
  51  * </ul>
  52  *
  53  * <p>Here is an example to count the number of identifier nodes in a tree:
  54  * <pre>
  55  *   class CountIdentifiers extends TreeScanner&lt;Integer,Void&gt; {
  56  *      {@literal @}Override
  57  *      public Integer visitIdentifier(IdentifierTree node, Void p) {
  58  *          return 1;
  59  *      }
  60  *      {@literal @}Override
  61  *      public Integer reduce(Integer r1, Integer r2) {
  62  *          return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
  63  *      }
  64  *   }
  65  * </pre>
  66  *
  67  * @param <R> the return type of this visitor's methods.  Use {@link
  68  *            Void} for visitors that do not need to return results.
  69  * @param <P> the type of the additional parameter to this visitor's
  70  *            methods.  Use {@code Void} for visitors that do not need an
  71  *            additional parameter.
  72  *
  73  * @author Peter von der Ah&eacute;
  74  * @author Jonathan Gibbons
  75  * @since 1.6
  76  */
  77 public class TreeScanner<R,P> implements TreeVisitor<R,P> {
  78 
  79     /**
  80      * Scans a single node.
  81      * @param tree the node to be scanned
  82      * @param p a parameter value passed to the visit method
  83      * @return the result value from the visit method
  84      */
  85     public R scan(Tree tree, P p) {
  86         return (tree == null) ? null : tree.accept(this, p);
  87     }
  88 
  89     private R scanAndReduce(Tree node, P p, R r) {
  90         return reduce(scan(node, p), r);
  91     }
  92 
  93     /**
  94      * Scans a sequence of nodes.
  95      * @param nodes the nodes to be scanned
  96      * @param p a parameter value to be passed to the visit method for each node
  97      * @return the combined return value from the visit methods.
  98      *      The values are combined using the {@link #reduce reduce} method.
  99      */
 100     public R scan(Iterable<? extends Tree> nodes, P p) {
 101         R r = null;
 102         if (nodes != null) {
 103             boolean first = true;
 104             for (Tree node : nodes) {
 105                 r = (first ? scan(node, p) : scanAndReduce(node, p, r));
 106                 first = false;
 107             }
 108         }
 109         return r;
 110     }
 111 
 112     private R scanAndReduce(Iterable<? extends Tree> nodes, P p, R r) {
 113         return reduce(scan(nodes, p), r);
 114     }
 115 
 116     /**
 117      * Reduces two results into a combined result.
 118      * The default implementation is to return the first parameter.
 119      * The general contract of the method is that it may take any action whatsoever.
 120      * @param r1 the first of the values to be combined
 121      * @param r2 the second of the values to be combined
 122      * @return the result of combining the two parameters
 123      */
 124     public R reduce(R r1, R r2) {
 125         return r1;
 126     }
 127 
 128 
 129 /* ***************************************************************************
 130  * Visitor methods
 131  ****************************************************************************/
 132 
 133     /**
 134      * {@inheritDoc} This implementation scans the children in left to right order.
 135      *
 136      * @param node  {@inheritDoc}
 137      * @param p  {@inheritDoc}
 138      * @return the result of scanning
 139      */
 140     @Override
 141     public R visitCompilationUnit(CompilationUnitTree node, P p) {
 142         R r = scan(node.getPackage(), p);
 143         r = scanAndReduce(node.getImports(), p, r);
 144         r = scanAndReduce(node.getTypeDecls(), p, r);
 145         return r;
 146     }
 147 
 148     /**
 149      * {@inheritDoc} This implementation scans the children in left to right order.
 150      *
 151      * @param node  {@inheritDoc}
 152      * @param p  {@inheritDoc}
 153      * @return the result of scanning
 154      */
 155     @Override
 156     public R visitPackage(PackageTree node, P p) {
 157         R r = scan(node.getAnnotations(), p);
 158         r = scanAndReduce(node.getPackageName(), p, r);
 159         return r;
 160     }
 161 
 162     /**
 163      * {@inheritDoc} This implementation scans the children in left to right order.
 164      *
 165      * @param node  {@inheritDoc}
 166      * @param p  {@inheritDoc}
 167      * @return the result of scanning
 168      */
 169     @Override
 170     public R visitImport(ImportTree node, P p) {
 171         return scan(node.getQualifiedIdentifier(), p);
 172     }
 173 
 174     /**
 175      * {@inheritDoc} 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 visitClass(ClassTree node, P p) {
 183         R r = scan(node.getModifiers(), p);
 184         r = scanAndReduce(node.getTypeParameters(), p, r);
 185         r = scanAndReduce(node.getExtendsClause(), p, r);
 186         r = scanAndReduce(node.getImplementsClause(), p, r);
 187         r = scanAndReduce(node.getMembers(), p, r);
 188         return r;
 189     }
 190 
 191     /**
 192      * {@inheritDoc} This implementation scans the children in left to right order.
 193      *
 194      * @param node  {@inheritDoc}
 195      * @param p  {@inheritDoc}
 196      * @return the result of scanning
 197      */
 198     @Override
 199     public R visitMethod(MethodTree node, P p) {
 200         R r = scan(node.getModifiers(), p);
 201         r = scanAndReduce(node.getReturnType(), p, r);
 202         r = scanAndReduce(node.getTypeParameters(), p, r);
 203         r = scanAndReduce(node.getParameters(), p, r);
 204         r = scanAndReduce(node.getReceiverParameter(), p, r);
 205         r = scanAndReduce(node.getThrows(), p, r);
 206         r = scanAndReduce(node.getBody(), p, r);
 207         r = scanAndReduce(node.getDefaultValue(), p, r);
 208         return r;
 209     }
 210 
 211     /**
 212      * {@inheritDoc} This implementation scans the children in left to right order.
 213      *
 214      * @param node  {@inheritDoc}
 215      * @param p  {@inheritDoc}
 216      * @return the result of scanning
 217      */
 218     @Override
 219     public R visitVariable(VariableTree node, P p) {
 220         R r = scan(node.getModifiers(), p);
 221         r = scanAndReduce(node.getType(), p, r);
 222         r = scanAndReduce(node.getNameExpression(), p, r);
 223         r = scanAndReduce(node.getInitializer(), p, r);
 224         return r;
 225     }
 226 
 227     /**
 228      * {@inheritDoc} This implementation returns {@code null}.
 229      *
 230      * @param node  {@inheritDoc}
 231      * @param p  {@inheritDoc}
 232      * @return the result of scanning
 233      */
 234     @Override
 235     public R visitEmptyStatement(EmptyStatementTree node, P p) {
 236         return null;
 237     }
 238 
 239     /**
 240      * {@inheritDoc} This implementation scans the children in left to right order.
 241      *
 242      * @param node  {@inheritDoc}
 243      * @param p  {@inheritDoc}
 244      * @return the result of scanning
 245      */
 246     @Override
 247     public R visitBlock(BlockTree node, P p) {
 248         return scan(node.getStatements(), p);
 249     }
 250 
 251     /**
 252      * {@inheritDoc} This implementation scans the children in left to right order.
 253      *
 254      * @param node  {@inheritDoc}
 255      * @param p  {@inheritDoc}
 256      * @return the result of scanning
 257      */
 258     @Override
 259     public R visitDoWhileLoop(DoWhileLoopTree node, P p) {
 260         R r = scan(node.getStatement(), p);
 261         r = scanAndReduce(node.getCondition(), p, r);
 262         return r;
 263     }
 264 
 265     /**
 266      * {@inheritDoc} This implementation scans the children in left to right order.
 267      *
 268      * @param node  {@inheritDoc}
 269      * @param p  {@inheritDoc}
 270      * @return the result of scanning
 271      */
 272     @Override
 273     public R visitWhileLoop(WhileLoopTree node, P p) {
 274         R r = scan(node.getCondition(), p);
 275         r = scanAndReduce(node.getStatement(), p, r);
 276         return r;
 277     }
 278 
 279     /**
 280      * {@inheritDoc} This implementation scans the children in left to right order.
 281      *
 282      * @param node  {@inheritDoc}
 283      * @param p  {@inheritDoc}
 284      * @return the result of scanning
 285      */
 286     @Override
 287     public R visitForLoop(ForLoopTree node, P p) {
 288         R r = scan(node.getInitializer(), p);
 289         r = scanAndReduce(node.getCondition(), p, r);
 290         r = scanAndReduce(node.getUpdate(), p, r);
 291         r = scanAndReduce(node.getStatement(), p, r);
 292         return r;
 293     }
 294 
 295     /**
 296      * {@inheritDoc} This implementation scans the children in left to right order.
 297      *
 298      * @param node  {@inheritDoc}
 299      * @param p  {@inheritDoc}
 300      * @return the result of scanning
 301      */
 302     @Override
 303     public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
 304         R r = scan(node.getVariable(), p);
 305         r = scanAndReduce(node.getExpression(), 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 visitLabeledStatement(LabeledStatementTree node, P p) {
 319         return scan(node.getStatement(), p);
 320     }
 321 
 322     /**
 323      * {@inheritDoc} This implementation scans the children in left to right order.
 324      *
 325      * @param node  {@inheritDoc}
 326      * @param p  {@inheritDoc}
 327      * @return the result of scanning
 328      */
 329     @Override
 330     public R visitSwitch(SwitchTree node, P p) {
 331         R r = scan(node.getExpression(), p);
 332         r = scanAndReduce(node.getCases(), p, r);
 333         return r;
 334     }
 335 
 336     /**
 337      * {@inheritDoc} This implementation scans the children in left to right order.
 338      *
 339      * @param node  {@inheritDoc}
 340      * @param p  {@inheritDoc}
 341      * @return the result of scanning
 342      */
 343     @Override
 344     public R visitCase(CaseTree node, P p) {
 345         R r = scan(node.getExpression(), p);
 346         r = scanAndReduce(node.getStatements(), p, r);
 347         return r;
 348     }
 349 
 350     /**
 351      * {@inheritDoc} This implementation scans the children in left to right order.
 352      *
 353      * @param node  {@inheritDoc}
 354      * @param p  {@inheritDoc}
 355      * @return the result of scanning
 356      */
 357     @Override
 358     public R visitSynchronized(SynchronizedTree node, P p) {
 359         R r = scan(node.getExpression(), p);
 360         r = scanAndReduce(node.getBlock(), p, r);
 361         return r;
 362     }
 363 
 364     /**
 365      * {@inheritDoc} This implementation scans the children in left to right order.
 366      *
 367      * @param node  {@inheritDoc}
 368      * @param p  {@inheritDoc}
 369      * @return the result of scanning
 370      */
 371     @Override
 372     public R visitTry(TryTree node, P p) {
 373         R r = scan(node.getResources(), p);
 374         r = scanAndReduce(node.getBlock(), p, r);
 375         r = scanAndReduce(node.getCatches(), p, r);
 376         r = scanAndReduce(node.getFinallyBlock(), p, r);
 377         return r;
 378     }
 379 
 380     /**
 381      * {@inheritDoc} This implementation scans the children in left to right order.
 382      *
 383      * @param node  {@inheritDoc}
 384      * @param p  {@inheritDoc}
 385      * @return the result of scanning
 386      */
 387     @Override
 388     public R visitCatch(CatchTree node, P p) {
 389         R r = scan(node.getParameter(), p);
 390         r = scanAndReduce(node.getBlock(), p, r);
 391         return r;
 392     }
 393 
 394     /**
 395      * {@inheritDoc} This implementation scans the children in left to right order.
 396      *
 397      * @param node  {@inheritDoc}
 398      * @param p  {@inheritDoc}
 399      * @return the result of scanning
 400      */
 401     @Override
 402     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
 403         R r = scan(node.getCondition(), p);
 404         r = scanAndReduce(node.getTrueExpression(), p, r);
 405         r = scanAndReduce(node.getFalseExpression(), p, r);
 406         return r;
 407     }
 408 
 409     /**
 410      * {@inheritDoc} This implementation scans the children in left to right order.
 411      *
 412      * @param node  {@inheritDoc}
 413      * @param p  {@inheritDoc}
 414      * @return the result of scanning
 415      */
 416     @Override
 417     public R visitIf(IfTree node, P p) {
 418         R r = scan(node.getCondition(), p);
 419         r = scanAndReduce(node.getThenStatement(), p, r);
 420         r = scanAndReduce(node.getElseStatement(), p, r);
 421         return r;
 422     }
 423 
 424     /**
 425      * {@inheritDoc} This implementation scans the children in left to right order.
 426      *
 427      * @param node  {@inheritDoc}
 428      * @param p  {@inheritDoc}
 429      * @return the result of scanning
 430      */
 431     @Override
 432     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 433         return scan(node.getExpression(), p);
 434     }
 435 
 436     /**
 437      * {@inheritDoc} This implementation returns {@code null}.
 438      *
 439      * @param node  {@inheritDoc}
 440      * @param p  {@inheritDoc}
 441      * @return the result of scanning
 442      */
 443     @Override
 444     public R visitBreak(BreakTree node, P p) {
 445         return null;
 446     }
 447 
 448     /**
 449      * {@inheritDoc} This implementation returns {@code null}.
 450      *
 451      * @param node  {@inheritDoc}
 452      * @param p  {@inheritDoc}
 453      * @return the result of scanning
 454      */
 455     @Override
 456     public R visitContinue(ContinueTree node, P p) {
 457         return null;
 458     }
 459 
 460     /**
 461      * {@inheritDoc} This implementation scans the children in left to right order.
 462      *
 463      * @param node  {@inheritDoc}
 464      * @param p  {@inheritDoc}
 465      * @return the result of scanning
 466      */
 467     @Override
 468     public R visitReturn(ReturnTree node, P p) {
 469         return scan(node.getExpression(), p);
 470     }
 471 
 472     /**
 473      * {@inheritDoc} This implementation scans the children in left to right order.
 474      *
 475      * @param node  {@inheritDoc}
 476      * @param p  {@inheritDoc}
 477      * @return the result of scanning
 478      */
 479     @Override
 480     public R visitThrow(ThrowTree node, P p) {
 481         return scan(node.getExpression(), p);
 482     }
 483 
 484     /**
 485      * {@inheritDoc} This implementation scans the children in left to right order.
 486      *
 487      * @param node  {@inheritDoc}
 488      * @param p  {@inheritDoc}
 489      * @return the result of scanning
 490      */
 491     @Override
 492     public R visitAssert(AssertTree node, P p) {
 493         R r = scan(node.getCondition(), p);
 494         r = scanAndReduce(node.getDetail(), p, r);
 495         return r;
 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 visitMethodInvocation(MethodInvocationTree node, P p) {
 507         R r = scan(node.getTypeArguments(), p);
 508         r = scanAndReduce(node.getMethodSelect(), p, r);
 509         r = scanAndReduce(node.getArguments(), p, r);
 510         return r;
 511     }
 512 
 513     /**
 514      * {@inheritDoc} This implementation scans the children in left to right order.
 515      *
 516      * @param node  {@inheritDoc}
 517      * @param p  {@inheritDoc}
 518      * @return the result of scanning
 519      */
 520     @Override
 521     public R visitNewClass(NewClassTree node, P p) {
 522         R r = scan(node.getEnclosingExpression(), p);
 523         r = scanAndReduce(node.getIdentifier(), p, r);
 524         r = scanAndReduce(node.getTypeArguments(), p, r);
 525         r = scanAndReduce(node.getArguments(), p, r);
 526         r = scanAndReduce(node.getClassBody(), p, r);
 527         return r;
 528     }
 529 
 530     /**
 531      * {@inheritDoc} This implementation scans the children in left to right order.
 532      *
 533      * @param node  {@inheritDoc}
 534      * @param p  {@inheritDoc}
 535      * @return the result of scanning
 536      */
 537     @Override
 538     public R visitNewArray(NewArrayTree node, P p) {
 539         R r = scan(node.getType(), p);
 540         r = scanAndReduce(node.getDimensions(), p, r);
 541         r = scanAndReduce(node.getInitializers(), p, r);
 542         r = scanAndReduce(node.getAnnotations(), p, r);
 543         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 544             r = scanAndReduce(dimAnno, p, r);
 545         }
 546         return r;
 547     }
 548 
 549     /**
 550      * {@inheritDoc} This implementation scans the children in left to right order.
 551      *
 552      * @param node  {@inheritDoc}
 553      * @param p  {@inheritDoc}
 554      * @return the result of scanning
 555      */
 556     @Override
 557     public R visitLambdaExpression(LambdaExpressionTree node, P p) {
 558         R r = scan(node.getParameters(), p);
 559         r = scanAndReduce(node.getBody(), p, r);
 560         return r;
 561     }
 562 
 563     /**
 564      * {@inheritDoc} This implementation scans the children in left to right order.
 565      *
 566      * @param node  {@inheritDoc}
 567      * @param p  {@inheritDoc}
 568      * @return the result of scanning
 569      */
 570     @Override
 571     public R visitParenthesized(ParenthesizedTree node, P p) {
 572         return scan(node.getExpression(), p);
 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 visitAssignment(AssignmentTree node, P p) {
 584         R r = scan(node.getVariable(), p);
 585         r = scanAndReduce(node.getExpression(), 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 visitCompoundAssignment(CompoundAssignmentTree node, P p) {
 598         R r = scan(node.getVariable(), p);
 599         r = scanAndReduce(node.getExpression(), 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 visitUnary(UnaryTree 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 visitBinary(BinaryTree node, P p) {
 624         R r = scan(node.getLeftOperand(), p);
 625         r = scanAndReduce(node.getRightOperand(), 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 visitTypeCast(TypeCastTree node, P p) {
 638         R r = scan(node.getType(), 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 visitInstanceOf(InstanceOfTree node, P p) {
 652         R r = scan(node.getExpression(), p);
 653         r = scanAndReduce(node.getType(), p, r);
 654         return r;
 655     }
 656 
 657     /**
 658      * {@inheritDoc} This implementation scans the children in left to right order.
 659      *
 660      * @param node  {@inheritDoc}
 661      * @param p  {@inheritDoc}
 662      * @return the result of scanning
 663      */
 664     @Override
 665     public R visitArrayAccess(ArrayAccessTree node, P p) {
 666         R r = scan(node.getExpression(), p);
 667         r = scanAndReduce(node.getIndex(), p, r);
 668         return r;
 669     }
 670 
 671     /**
 672      * {@inheritDoc} This implementation scans the children in left to right order.
 673      *
 674      * @param node  {@inheritDoc}
 675      * @param p  {@inheritDoc}
 676      * @return the result of scanning
 677      */
 678     @Override
 679     public R visitMemberSelect(MemberSelectTree node, P p) {
 680         return scan(node.getExpression(), p);
 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 visitMemberReference(MemberReferenceTree node, P p) {
 692         R r = scan(node.getQualifierExpression(), p);
 693         r = scanAndReduce(node.getTypeArguments(), p, r);
 694         return r;
 695     }
 696 
 697     /**
 698      * {@inheritDoc} This implementation returns {@code null}.
 699      *
 700      * @param node  {@inheritDoc}
 701      * @param p  {@inheritDoc}
 702      * @return the result of scanning
 703      */
 704     @Override
 705     public R visitIdentifier(IdentifierTree node, P p) {
 706         return null;
 707     }
 708 
 709     /**
 710      * {@inheritDoc} This implementation returns {@code null}.
 711      *
 712      * @param node  {@inheritDoc}
 713      * @param p  {@inheritDoc}
 714      * @return the result of scanning
 715      */
 716     @Override
 717     public R visitLiteral(LiteralTree node, P p) {
 718         return null;
 719     }
 720 
 721     /**
 722      * {@inheritDoc} This implementation returns {@code null}.
 723      *
 724      * @param node  {@inheritDoc}
 725      * @param p  {@inheritDoc}
 726      * @return the result of scanning
 727      */
 728     @Override
 729     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 730         return null;
 731     }
 732 
 733     /**
 734      * {@inheritDoc} This implementation scans the children in left to right order.
 735      *
 736      * @param node  {@inheritDoc}
 737      * @param p  {@inheritDoc}
 738      * @return the result of scanning
 739      */
 740     @Override
 741     public R visitArrayType(ArrayTypeTree node, P p) {
 742         return scan(node.getType(), p);
 743     }
 744 
 745     /**
 746      * {@inheritDoc} This implementation scans the children in left to right order.
 747      *
 748      * @param node  {@inheritDoc}
 749      * @param p  {@inheritDoc}
 750      * @return the result of scanning
 751      */
 752     @Override
 753     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 754         R r = scan(node.getType(), p);
 755         r = scanAndReduce(node.getTypeArguments(), p, r);
 756         return r;
 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 visitUnionType(UnionTypeTree node, P p) {
 768         return scan(node.getTypeAlternatives(), 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 visitIntersectionType(IntersectionTypeTree node, P p) {
 780         return scan(node.getBounds(), p);
 781     }
 782 
 783     /**
 784      * {@inheritDoc} This implementation scans the children in left to right order.
 785      *
 786      * @param node  {@inheritDoc}
 787      * @param p  {@inheritDoc}
 788      * @return the result of scanning
 789      */
 790     @Override
 791     public R visitTypeParameter(TypeParameterTree node, P p) {
 792         R r = scan(node.getAnnotations(), p);
 793         r = scanAndReduce(node.getBounds(), p, r);
 794         return r;
 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 visitWildcard(WildcardTree node, P p) {
 806         return scan(node.getBound(), 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 visitModifiers(ModifiersTree node, P p) {
 818         return scan(node.getAnnotations(), p);
 819     }
 820 
 821     /**
 822      * {@inheritDoc} This implementation scans the children in left to right order.
 823      *
 824      * @param node  {@inheritDoc}
 825      * @param p  {@inheritDoc}
 826      * @return the result of scanning
 827      */
 828     @Override
 829     public R visitAnnotation(AnnotationTree node, P p) {
 830         R r = scan(node.getAnnotationType(), p);
 831         r = scanAndReduce(node.getArguments(), p, r);
 832         return r;
 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 visitAnnotatedType(AnnotatedTypeTree node, P p) {
 844         R r = scan(node.getAnnotations(), p);
 845         r = scanAndReduce(node.getUnderlyingType(), p, r);
 846         return r;
 847     }
 848 
 849     @Override
 850     public R visitModule(ModuleTree node, P p) {
 851         R r = scan(node.getAnnotations(), p);
 852         r = scanAndReduce(node.getName(), p, r);
 853         r = scanAndReduce(node.getDirectives(), p, r);
 854         return r;
 855     }
 856 
 857     @Override
 858     public R visitExports(ExportsTree node, P p) {
 859         R r = scan(node.getPackageName(), p);
 860         r = scanAndReduce(node.getModuleNames(), p, r);
 861         return r;
 862     }
 863 
 864     @Override
 865     public R visitOpens(OpensTree node, P p) {
 866         R r = scan(node.getPackageName(), p);
 867         r = scanAndReduce(node.getModuleNames(), p, r);
 868         return r;
 869     }
 870 
 871     @Override
 872     public R visitProvides(ProvidesTree node, P p) {
 873         R r = scan(node.getServiceName(), p);
 874         r = scanAndReduce(node.getImplementationNames(), p, r);
 875         return r;
 876     }
 877 
 878     @Override
 879     public R visitRequires(RequiresTree node, P p) {
 880         return scan(node.getModuleName(), p);
 881     }
 882 
 883     @Override
 884     public R visitUses(UsesTree node, P p) {
 885         return scan(node.getServiceName(), p);
 886     }
 887 
 888     /**
 889      * {@inheritDoc} This implementation returns {@code null}.
 890      *
 891      * @param node  {@inheritDoc}
 892      * @param p  {@inheritDoc}
 893      * @return the result of scanning
 894      */
 895     @Override
 896     public R visitOther(Tree node, P p) {
 897         return null;
 898     }
 899 
 900     /**
 901      * {@inheritDoc} This implementation returns {@code null}.
 902      *
 903      * @param node  {@inheritDoc}
 904      * @param p  {@inheritDoc}
 905      * @return the result of scanning
 906      */
 907     @Override
 908     public R visitErroneous(ErroneousTree node, P p) {
 909         return null;
 910     }
 911 }