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 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     @Override
 345     @Deprecated(forRemoval=true, since="12")
 346     @SuppressWarnings("removal")
 347     public R visitSwitchExpression(SwitchExpressionTree node, P p) {
 348         R r = scan(node.getExpression(), p);
 349         r = scanAndReduce(node.getCases(), p, r);
 350         return r;
 351     }
 352 
 353     /**
 354      * {@inheritDoc} This implementation scans the children in left to right order.
 355      *
 356      * @param node  {@inheritDoc}
 357      * @param p  {@inheritDoc}
 358      * @return the result of scanning
 359      */
 360     @Override
 361     @SuppressWarnings("removal")
 362     public R visitCase(CaseTree node, P p) {
 363         R r = scan(node.getExpressions(), p);
 364         if (node.getCaseKind() == CaseKind.RULE)
 365             r = scanAndReduce(node.getBody(), p, r);
 366         else
 367             r = scanAndReduce(node.getStatements(), p, r);
 368         return r;
 369     }
 370 
 371     /**
 372      * {@inheritDoc} This implementation scans the children in left to right order.
 373      *
 374      * @param node  {@inheritDoc}
 375      * @param p  {@inheritDoc}
 376      * @return the result of scanning
 377      */
 378     @Override
 379     public R visitSynchronized(SynchronizedTree node, P p) {
 380         R r = scan(node.getExpression(), p);
 381         r = scanAndReduce(node.getBlock(), p, r);
 382         return r;
 383     }
 384 
 385     /**
 386      * {@inheritDoc} This implementation scans the children in left to right order.
 387      *
 388      * @param node  {@inheritDoc}
 389      * @param p  {@inheritDoc}
 390      * @return the result of scanning
 391      */
 392     @Override
 393     public R visitTry(TryTree node, P p) {
 394         R r = scan(node.getResources(), p);
 395         r = scanAndReduce(node.getBlock(), p, r);
 396         r = scanAndReduce(node.getCatches(), p, r);
 397         r = scanAndReduce(node.getFinallyBlock(), p, r);
 398         return r;
 399     }
 400 
 401     /**
 402      * {@inheritDoc} This implementation scans the children in left to right order.
 403      *
 404      * @param node  {@inheritDoc}
 405      * @param p  {@inheritDoc}
 406      * @return the result of scanning
 407      */
 408     @Override
 409     public R visitCatch(CatchTree node, P p) {
 410         R r = scan(node.getParameter(), p);
 411         r = scanAndReduce(node.getBlock(), p, r);
 412         return r;
 413     }
 414 
 415     /**
 416      * {@inheritDoc} This implementation scans the children in left to right order.
 417      *
 418      * @param node  {@inheritDoc}
 419      * @param p  {@inheritDoc}
 420      * @return the result of scanning
 421      */
 422     @Override
 423     public R visitConditionalExpression(ConditionalExpressionTree node, P p) {
 424         R r = scan(node.getCondition(), p);
 425         r = scanAndReduce(node.getTrueExpression(), p, r);
 426         r = scanAndReduce(node.getFalseExpression(), p, r);
 427         return r;
 428     }
 429 
 430     /**
 431      * {@inheritDoc} This implementation scans the children in left to right order.
 432      *
 433      * @param node  {@inheritDoc}
 434      * @param p  {@inheritDoc}
 435      * @return the result of scanning
 436      */
 437     @Override
 438     public R visitIf(IfTree node, P p) {
 439         R r = scan(node.getCondition(), p);
 440         r = scanAndReduce(node.getThenStatement(), p, r);
 441         r = scanAndReduce(node.getElseStatement(), p, r);
 442         return r;
 443     }
 444 
 445     /**
 446      * {@inheritDoc} This implementation scans the children in left to right order.
 447      *
 448      * @param node  {@inheritDoc}
 449      * @param p  {@inheritDoc}
 450      * @return the result of scanning
 451      */
 452     @Override
 453     public R visitExpressionStatement(ExpressionStatementTree node, P p) {
 454         return scan(node.getExpression(), p);
 455     }
 456 
 457     /**
 458      * {@inheritDoc} This implementation returns {@code null}.
 459      *
 460      * @param node  {@inheritDoc}
 461      * @param p  {@inheritDoc}
 462      * @return the result of scanning
 463      */
 464     @Override
 465     @SuppressWarnings("removal")
 466     public R visitBreak(BreakTree node, P p) {
 467         return scan(node.getValue(), p);
 468     }
 469 
 470     /**
 471      * {@inheritDoc} This implementation returns {@code null}.
 472      *
 473      * @param node  {@inheritDoc}
 474      * @param p  {@inheritDoc}
 475      * @return the result of scanning
 476      */
 477     @Override
 478     public R visitContinue(ContinueTree node, P p) {
 479         return null;
 480     }
 481 
 482     /**
 483      * {@inheritDoc} This implementation scans the children in left to right order.
 484      *
 485      * @param node  {@inheritDoc}
 486      * @param p  {@inheritDoc}
 487      * @return the result of scanning
 488      */
 489     @Override
 490     public R visitReturn(ReturnTree node, P p) {
 491         return scan(node.getExpression(), p);
 492     }
 493 
 494     /**
 495      * {@inheritDoc} This implementation scans the children in left to right order.
 496      *
 497      * @param node  {@inheritDoc}
 498      * @param p  {@inheritDoc}
 499      * @return the result of scanning
 500      */
 501     @Override
 502     public R visitThrow(ThrowTree node, P p) {
 503         return scan(node.getExpression(), p);
 504     }
 505 
 506     /**
 507      * {@inheritDoc} This implementation scans the children in left to right order.
 508      *
 509      * @param node  {@inheritDoc}
 510      * @param p  {@inheritDoc}
 511      * @return the result of scanning
 512      */
 513     @Override
 514     public R visitAssert(AssertTree node, P p) {
 515         R r = scan(node.getCondition(), p);
 516         r = scanAndReduce(node.getDetail(), p, r);
 517         return r;
 518     }
 519 
 520     /**
 521      * {@inheritDoc} This implementation scans the children in left to right order.
 522      *
 523      * @param node  {@inheritDoc}
 524      * @param p  {@inheritDoc}
 525      * @return the result of scanning
 526      */
 527     @Override
 528     public R visitMethodInvocation(MethodInvocationTree node, P p) {
 529         R r = scan(node.getTypeArguments(), p);
 530         r = scanAndReduce(node.getMethodSelect(), p, r);
 531         r = scanAndReduce(node.getArguments(), p, r);
 532         return r;
 533     }
 534 
 535     /**
 536      * {@inheritDoc} This implementation scans the children in left to right order.
 537      *
 538      * @param node  {@inheritDoc}
 539      * @param p  {@inheritDoc}
 540      * @return the result of scanning
 541      */
 542     @Override
 543     public R visitNewClass(NewClassTree node, P p) {
 544         R r = scan(node.getEnclosingExpression(), p);
 545         r = scanAndReduce(node.getIdentifier(), p, r);
 546         r = scanAndReduce(node.getTypeArguments(), p, r);
 547         r = scanAndReduce(node.getArguments(), p, r);
 548         r = scanAndReduce(node.getClassBody(), p, r);
 549         return r;
 550     }
 551 
 552     /**
 553      * {@inheritDoc} This implementation scans the children in left to right order.
 554      *
 555      * @param node  {@inheritDoc}
 556      * @param p  {@inheritDoc}
 557      * @return the result of scanning
 558      */
 559     @Override
 560     public R visitNewArray(NewArrayTree node, P p) {
 561         R r = scan(node.getType(), p);
 562         r = scanAndReduce(node.getDimensions(), p, r);
 563         r = scanAndReduce(node.getInitializers(), p, r);
 564         r = scanAndReduce(node.getAnnotations(), p, r);
 565         for (Iterable< ? extends Tree> dimAnno : node.getDimAnnotations()) {
 566             r = scanAndReduce(dimAnno, p, r);
 567         }
 568         return r;
 569     }
 570 
 571     /**
 572      * {@inheritDoc} This implementation scans the children in left to right order.
 573      *
 574      * @param node  {@inheritDoc}
 575      * @param p  {@inheritDoc}
 576      * @return the result of scanning
 577      */
 578     @Override
 579     public R visitLambdaExpression(LambdaExpressionTree node, P p) {
 580         R r = scan(node.getParameters(), p);
 581         r = scanAndReduce(node.getBody(), p, r);
 582         return r;
 583     }
 584 
 585     /**
 586      * {@inheritDoc} This implementation scans the children in left to right order.
 587      *
 588      * @param node  {@inheritDoc}
 589      * @param p  {@inheritDoc}
 590      * @return the result of scanning
 591      */
 592     @Override
 593     public R visitParenthesized(ParenthesizedTree node, P p) {
 594         return scan(node.getExpression(), p);
 595     }
 596 
 597     /**
 598      * {@inheritDoc} This implementation scans the children in left to right order.
 599      *
 600      * @param node  {@inheritDoc}
 601      * @param p  {@inheritDoc}
 602      * @return the result of scanning
 603      */
 604     @Override
 605     public R visitAssignment(AssignmentTree node, P p) {
 606         R r = scan(node.getVariable(), p);
 607         r = scanAndReduce(node.getExpression(), p, r);
 608         return r;
 609     }
 610 
 611     /**
 612      * {@inheritDoc} This implementation scans the children in left to right order.
 613      *
 614      * @param node  {@inheritDoc}
 615      * @param p  {@inheritDoc}
 616      * @return the result of scanning
 617      */
 618     @Override
 619     public R visitCompoundAssignment(CompoundAssignmentTree node, P p) {
 620         R r = scan(node.getVariable(), p);
 621         r = scanAndReduce(node.getExpression(), p, r);
 622         return r;
 623     }
 624 
 625     /**
 626      * {@inheritDoc} This implementation scans the children in left to right order.
 627      *
 628      * @param node  {@inheritDoc}
 629      * @param p  {@inheritDoc}
 630      * @return the result of scanning
 631      */
 632     @Override
 633     public R visitUnary(UnaryTree node, P p) {
 634         return scan(node.getExpression(), p);
 635     }
 636 
 637     /**
 638      * {@inheritDoc} This implementation scans the children in left to right order.
 639      *
 640      * @param node  {@inheritDoc}
 641      * @param p  {@inheritDoc}
 642      * @return the result of scanning
 643      */
 644     @Override
 645     public R visitBinary(BinaryTree node, P p) {
 646         R r = scan(node.getLeftOperand(), p);
 647         r = scanAndReduce(node.getRightOperand(), p, r);
 648         return r;
 649     }
 650 
 651     /**
 652      * {@inheritDoc} This implementation scans the children in left to right order.
 653      *
 654      * @param node  {@inheritDoc}
 655      * @param p  {@inheritDoc}
 656      * @return the result of scanning
 657      */
 658     @Override
 659     public R visitTypeCast(TypeCastTree node, P p) {
 660         R r = scan(node.getType(), p);
 661         r = scanAndReduce(node.getExpression(), p, r);
 662         return r;
 663     }
 664 
 665     /**
 666      * {@inheritDoc} This implementation scans the children in left to right order.
 667      *
 668      * @param node  {@inheritDoc}
 669      * @param p  {@inheritDoc}
 670      * @return the result of scanning
 671      */
 672     @Override
 673     public R visitInstanceOf(InstanceOfTree node, P p) {
 674         R r = scan(node.getExpression(), p);
 675         r = scanAndReduce(node.getType(), p, r);
 676         return r;
 677     }
 678 
 679     /**
 680      * {@inheritDoc} This implementation scans the children in left to right order.
 681      *
 682      * @param node  {@inheritDoc}
 683      * @param p  {@inheritDoc}
 684      * @return the result of scanning
 685      */
 686     @Override
 687     public R visitArrayAccess(ArrayAccessTree node, P p) {
 688         R r = scan(node.getExpression(), p);
 689         r = scanAndReduce(node.getIndex(), p, r);
 690         return r;
 691     }
 692 
 693     /**
 694      * {@inheritDoc} This implementation scans the children in left to right order.
 695      *
 696      * @param node  {@inheritDoc}
 697      * @param p  {@inheritDoc}
 698      * @return the result of scanning
 699      */
 700     @Override
 701     public R visitMemberSelect(MemberSelectTree node, P p) {
 702         return scan(node.getExpression(), p);
 703     }
 704 
 705     /**
 706      * {@inheritDoc} This implementation scans the children in left to right order.
 707      *
 708      * @param node  {@inheritDoc}
 709      * @param p  {@inheritDoc}
 710      * @return the result of scanning
 711      */
 712     @Override
 713     public R visitMemberReference(MemberReferenceTree node, P p) {
 714         R r = scan(node.getQualifierExpression(), p);
 715         r = scanAndReduce(node.getTypeArguments(), p, r);
 716         return r;
 717     }
 718 
 719     /**
 720      * {@inheritDoc} This implementation returns {@code null}.
 721      *
 722      * @param node  {@inheritDoc}
 723      * @param p  {@inheritDoc}
 724      * @return the result of scanning
 725      */
 726     @Override
 727     public R visitIdentifier(IdentifierTree node, P p) {
 728         return null;
 729     }
 730 
 731     /**
 732      * {@inheritDoc} This implementation returns {@code null}.
 733      *
 734      * @param node  {@inheritDoc}
 735      * @param p  {@inheritDoc}
 736      * @return the result of scanning
 737      */
 738     @Override
 739     public R visitLiteral(LiteralTree node, P p) {
 740         return null;
 741     }
 742 
 743     /**
 744      * {@inheritDoc} This implementation returns {@code null}.
 745      *
 746      * @param node  {@inheritDoc}
 747      * @param p  {@inheritDoc}
 748      * @return the result of scanning
 749      */
 750     @Override
 751     public R visitPrimitiveType(PrimitiveTypeTree node, P p) {
 752         return null;
 753     }
 754 
 755     /**
 756      * {@inheritDoc} This implementation scans the children in left to right order.
 757      *
 758      * @param node  {@inheritDoc}
 759      * @param p  {@inheritDoc}
 760      * @return the result of scanning
 761      */
 762     @Override
 763     public R visitArrayType(ArrayTypeTree node, P p) {
 764         return scan(node.getType(), p);
 765     }
 766 
 767     /**
 768      * {@inheritDoc} This implementation scans the children in left to right order.
 769      *
 770      * @param node  {@inheritDoc}
 771      * @param p  {@inheritDoc}
 772      * @return the result of scanning
 773      */
 774     @Override
 775     public R visitParameterizedType(ParameterizedTypeTree node, P p) {
 776         R r = scan(node.getType(), p);
 777         r = scanAndReduce(node.getTypeArguments(), p, r);
 778         return r;
 779     }
 780 
 781     /**
 782      * {@inheritDoc} This implementation scans the children in left to right order.
 783      *
 784      * @param node  {@inheritDoc}
 785      * @param p  {@inheritDoc}
 786      * @return the result of scanning
 787      */
 788     @Override
 789     public R visitUnionType(UnionTypeTree node, P p) {
 790         return scan(node.getTypeAlternatives(), p);
 791     }
 792 
 793     /**
 794      * {@inheritDoc} This implementation scans the children in left to right order.
 795      *
 796      * @param node  {@inheritDoc}
 797      * @param p  {@inheritDoc}
 798      * @return the result of scanning
 799      */
 800     @Override
 801     public R visitIntersectionType(IntersectionTypeTree node, P p) {
 802         return scan(node.getBounds(), p);
 803     }
 804 
 805     /**
 806      * {@inheritDoc} This implementation scans the children in left to right order.
 807      *
 808      * @param node  {@inheritDoc}
 809      * @param p  {@inheritDoc}
 810      * @return the result of scanning
 811      */
 812     @Override
 813     public R visitTypeParameter(TypeParameterTree node, P p) {
 814         R r = scan(node.getAnnotations(), p);
 815         r = scanAndReduce(node.getBounds(), p, r);
 816         return r;
 817     }
 818 
 819     /**
 820      * {@inheritDoc} This implementation scans the children in left to right order.
 821      *
 822      * @param node  {@inheritDoc}
 823      * @param p  {@inheritDoc}
 824      * @return the result of scanning
 825      */
 826     @Override
 827     public R visitWildcard(WildcardTree node, P p) {
 828         return scan(node.getBound(), p);
 829     }
 830 
 831     /**
 832      * {@inheritDoc} This implementation scans the children in left to right order.
 833      *
 834      * @param node  {@inheritDoc}
 835      * @param p  {@inheritDoc}
 836      * @return the result of scanning
 837      */
 838     @Override
 839     public R visitModifiers(ModifiersTree node, P p) {
 840         return scan(node.getAnnotations(), p);
 841     }
 842 
 843     /**
 844      * {@inheritDoc} This implementation scans the children in left to right order.
 845      *
 846      * @param node  {@inheritDoc}
 847      * @param p  {@inheritDoc}
 848      * @return the result of scanning
 849      */
 850     @Override
 851     public R visitAnnotation(AnnotationTree node, P p) {
 852         R r = scan(node.getAnnotationType(), p);
 853         r = scanAndReduce(node.getArguments(), p, r);
 854         return r;
 855     }
 856 
 857     /**
 858      * {@inheritDoc} This implementation scans the children in left to right order.
 859      *
 860      * @param node  {@inheritDoc}
 861      * @param p  {@inheritDoc}
 862      * @return the result of scanning
 863      */
 864     @Override
 865     public R visitAnnotatedType(AnnotatedTypeTree node, P p) {
 866         R r = scan(node.getAnnotations(), p);
 867         r = scanAndReduce(node.getUnderlyingType(), p, r);
 868         return r;
 869     }
 870 
 871     @Override
 872     public R visitModule(ModuleTree node, P p) {
 873         R r = scan(node.getAnnotations(), p);
 874         r = scanAndReduce(node.getName(), p, r);
 875         r = scanAndReduce(node.getDirectives(), p, r);
 876         return r;
 877     }
 878 
 879     @Override
 880     public R visitExports(ExportsTree node, P p) {
 881         R r = scan(node.getPackageName(), p);
 882         r = scanAndReduce(node.getModuleNames(), p, r);
 883         return r;
 884     }
 885 
 886     @Override
 887     public R visitOpens(OpensTree node, P p) {
 888         R r = scan(node.getPackageName(), p);
 889         r = scanAndReduce(node.getModuleNames(), p, r);
 890         return r;
 891     }
 892 
 893     @Override
 894     public R visitProvides(ProvidesTree node, P p) {
 895         R r = scan(node.getServiceName(), p);
 896         r = scanAndReduce(node.getImplementationNames(), p, r);
 897         return r;
 898     }
 899 
 900     @Override
 901     public R visitRequires(RequiresTree node, P p) {
 902         return scan(node.getModuleName(), p);
 903     }
 904 
 905     @Override
 906     public R visitUses(UsesTree node, P p) {
 907         return scan(node.getServiceName(), p);
 908     }
 909 
 910     /**
 911      * {@inheritDoc} This implementation returns {@code null}.
 912      *
 913      * @param node  {@inheritDoc}
 914      * @param p  {@inheritDoc}
 915      * @return the result of scanning
 916      */
 917     @Override
 918     public R visitOther(Tree node, P p) {
 919         return null;
 920     }
 921 
 922     /**
 923      * {@inheritDoc} This implementation returns {@code null}.
 924      *
 925      * @param node  {@inheritDoc}
 926      * @param p  {@inheritDoc}
 927      * @return the result of scanning
 928      */
 929     @Override
 930     public R visitErroneous(ErroneousTree node, P p) {
 931         return null;
 932     }
 933 }