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 30 /** 31 * A simple visitor for tree nodes. 32 * 33 * @param <R> the return type of this visitor's methods. Use {@link 34 * Void} for visitors that do not need to return results. 35 * @param <P> the type of the additional parameter to this visitor's 36 * methods. Use {@code Void} for visitors that do not need an 37 * additional parameter. 38 * 39 * @author Peter von der Ahé 40 * @since 1.6 41 */ 42 public class SimpleTreeVisitor <R,P> implements TreeVisitor<R,P> { 43 /** 44 * The default value, returned by the {@link #defaultAction default action}. 45 */ 46 protected final R DEFAULT_VALUE; 47 48 /** 49 * Creates a visitor, with a DEFAULT_VALUE of {@code null}. 50 */ 51 protected SimpleTreeVisitor() { 52 DEFAULT_VALUE = null; 53 } 54 55 /** 56 * Creates a visitor, with a specified DEFAULT_VALUE. 57 * @param defaultValue the default value to be returned by the default action. 58 */ 59 protected SimpleTreeVisitor(R defaultValue) { 60 DEFAULT_VALUE = defaultValue; 61 } 62 63 /** 64 * The default action, used by all visit methods that are not overridden. 65 * @param node the node being visited 66 * @param p the parameter value passed to the visit method 67 * @return the result value to be returned from the visit method 68 */ 69 protected R defaultAction(Tree node, P p) { 70 return DEFAULT_VALUE; 71 } 72 73 /** 74 * Invokes the appropriate visit method specific to the type of the node. 75 * @param node the node on which to dispatch 76 * @param p a parameter to be passed to the appropriate visit method 77 * @return the value returns from the appropriate visit method 78 */ 79 public final R visit(Tree node, P p) { 80 return (node == null) ? null : node.accept(this, p); 81 } 82 83 /** 84 * Invokes the appropriate visit method on each of a sequence of nodes. 85 * @param nodes the nodes on which to dispatch 86 * @param p a parameter value to be passed to each appropriate visit method 87 * @return the value return from the last of the visit methods, or null 88 * if none were called. 89 */ 90 public final R visit(Iterable<? extends Tree> nodes, P p) { 91 R r = null; 92 if (nodes != null) 93 for (Tree node : nodes) 94 r = visit(node, p); 95 return r; 96 } 97 98 /** 99 * {@inheritDoc} This implementation calls {@code defaultAction}. 100 * 101 * @param node {@inheritDoc} 102 * @param p {@inheritDoc} 103 * @return the result of {@code defaultAction} 104 */ 105 @Override 106 public R visitCompilationUnit(CompilationUnitTree node, P p) { 107 return defaultAction(node, p); 108 } 109 110 /** 111 * {@inheritDoc} This implementation calls {@code defaultAction}. 112 * 113 * @param node {@inheritDoc} 114 * @param p {@inheritDoc} 115 * @return the result of {@code defaultAction} 116 */ 117 @Override 118 public R visitPackage(PackageTree node, P p) { 119 return defaultAction(node, p); 120 } 121 122 /** 123 * {@inheritDoc} This implementation calls {@code defaultAction}. 124 * 125 * @param node {@inheritDoc} 126 * @param p {@inheritDoc} 127 * @return the result of {@code defaultAction} 128 */ 129 @Override 130 public R visitImport(ImportTree node, P p) { 131 return defaultAction(node, p); 132 } 133 134 /** 135 * {@inheritDoc} This implementation calls {@code defaultAction}. 136 * 137 * @param node {@inheritDoc} 138 * @param p {@inheritDoc} 139 * @return the result of {@code defaultAction} 140 */ 141 @Override 142 public R visitClass(ClassTree node, P p) { 143 return defaultAction(node, p); 144 } 145 146 /** 147 * {@inheritDoc} This implementation calls {@code defaultAction}. 148 * 149 * @param node {@inheritDoc} 150 * @param p {@inheritDoc} 151 * @return the result of {@code defaultAction} 152 */ 153 @Override 154 public R visitMethod(MethodTree node, P p) { 155 return defaultAction(node, p); 156 } 157 158 /** 159 * {@inheritDoc} This implementation calls {@code defaultAction}. 160 * 161 * @param node {@inheritDoc} 162 * @param p {@inheritDoc} 163 * @return the result of {@code defaultAction} 164 */ 165 @Override 166 public R visitVariable(VariableTree node, P p) { 167 return defaultAction(node, p); 168 } 169 170 /** 171 * {@inheritDoc} This implementation calls {@code defaultAction}. 172 * 173 * @param node {@inheritDoc} 174 * @param p {@inheritDoc} 175 * @return the result of {@code defaultAction} 176 */ 177 @Override 178 public R visitEmptyStatement(EmptyStatementTree node, P p) { 179 return defaultAction(node, p); 180 } 181 182 /** 183 * {@inheritDoc} This implementation calls {@code defaultAction}. 184 * 185 * @param node {@inheritDoc} 186 * @param p {@inheritDoc} 187 * @return the result of {@code defaultAction} 188 */ 189 @Override 190 public R visitBlock(BlockTree node, P p) { 191 return defaultAction(node, p); 192 } 193 194 /** 195 * {@inheritDoc} This implementation calls {@code defaultAction}. 196 * 197 * @param node {@inheritDoc} 198 * @param p {@inheritDoc} 199 * @return the result of {@code defaultAction} 200 */ 201 @Override 202 public R visitDoWhileLoop(DoWhileLoopTree node, P p) { 203 return defaultAction(node, p); 204 } 205 206 /** 207 * {@inheritDoc} This implementation calls {@code defaultAction}. 208 * 209 * @param node {@inheritDoc} 210 * @param p {@inheritDoc} 211 * @return the result of {@code defaultAction} 212 */ 213 @Override 214 public R visitWhileLoop(WhileLoopTree node, P p) { 215 return defaultAction(node, p); 216 } 217 218 /** 219 * {@inheritDoc} This implementation calls {@code defaultAction}. 220 * 221 * @param node {@inheritDoc} 222 * @param p {@inheritDoc} 223 * @return the result of {@code defaultAction} 224 */ 225 @Override 226 public R visitForLoop(ForLoopTree node, P p) { 227 return defaultAction(node, p); 228 } 229 230 /** 231 * {@inheritDoc} This implementation calls {@code defaultAction}. 232 * 233 * @param node {@inheritDoc} 234 * @param p {@inheritDoc} 235 * @return the result of {@code defaultAction} 236 */ 237 @Override 238 public R visitEnhancedForLoop(EnhancedForLoopTree node, P p) { 239 return defaultAction(node, p); 240 } 241 242 /** 243 * {@inheritDoc} This implementation calls {@code defaultAction}. 244 * 245 * @param node {@inheritDoc} 246 * @param p {@inheritDoc} 247 * @return the result of {@code defaultAction} 248 */ 249 @Override 250 public R visitLabeledStatement(LabeledStatementTree node, P p) { 251 return defaultAction(node, p); 252 } 253 254 /** 255 * {@inheritDoc} This implementation calls {@code defaultAction}. 256 * 257 * @param node {@inheritDoc} 258 * @param p {@inheritDoc} 259 * @return the result of {@code defaultAction} 260 */ 261 @Override 262 public R visitSwitch(SwitchTree node, P p) { 263 return defaultAction(node, p); 264 } 265 266 /** 267 * {@preview Associated with switch expressions, a preview feature of 268 * the Java language. 269 * 270 * This method is associated with <i>switch expressions</i>, a preview 271 * feature of the Java language. Preview features 272 * may be removed in a future release, or upgraded to permanent 273 * features of the Java language.} 274 * 275 * {@inheritDoc} This implementation calls {@code defaultAction}. 276 * 277 * @param node {@inheritDoc} 278 * @param p {@inheritDoc} 279 * @return the result of {@code defaultAction} 280 */ 281 @Override 282 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SWITCH_EXPRESSIONS) 283 @SuppressWarnings("preview") 284 public R visitSwitchExpression(SwitchExpressionTree node, P p) { 285 return defaultAction(node, p); 286 } 287 288 /** 289 * {@inheritDoc} This implementation calls {@code defaultAction}. 290 * 291 * @param node {@inheritDoc} 292 * @param p {@inheritDoc} 293 * @return the result of {@code defaultAction} 294 */ 295 @Override 296 public R visitCase(CaseTree node, P p) { 297 return defaultAction(node, p); 298 } 299 300 /** 301 * {@inheritDoc} This implementation calls {@code defaultAction}. 302 * 303 * @param node {@inheritDoc} 304 * @param p {@inheritDoc} 305 * @return the result of {@code defaultAction} 306 */ 307 @Override 308 public R visitSynchronized(SynchronizedTree node, P p) { 309 return defaultAction(node, p); 310 } 311 312 /** 313 * {@inheritDoc} This implementation calls {@code defaultAction}. 314 * 315 * @param node {@inheritDoc} 316 * @param p {@inheritDoc} 317 * @return the result of {@code defaultAction} 318 */ 319 @Override 320 public R visitTry(TryTree node, P p) { 321 return defaultAction(node, p); 322 } 323 324 /** 325 * {@inheritDoc} This implementation calls {@code defaultAction}. 326 * 327 * @param node {@inheritDoc} 328 * @param p {@inheritDoc} 329 * @return the result of {@code defaultAction} 330 */ 331 @Override 332 public R visitCatch(CatchTree node, P p) { 333 return defaultAction(node, p); 334 } 335 336 /** 337 * {@inheritDoc} This implementation calls {@code defaultAction}. 338 * 339 * @param node {@inheritDoc} 340 * @param p {@inheritDoc} 341 * @return the result of {@code defaultAction} 342 */ 343 @Override 344 public R visitConditionalExpression(ConditionalExpressionTree node, P p) { 345 return defaultAction(node, p); 346 } 347 348 /** 349 * {@inheritDoc} This implementation calls {@code defaultAction}. 350 * 351 * @param node {@inheritDoc} 352 * @param p {@inheritDoc} 353 * @return the result of {@code defaultAction} 354 */ 355 @Override 356 public R visitIf(IfTree node, P p) { 357 return defaultAction(node, p); 358 } 359 360 /** 361 * {@inheritDoc} This implementation calls {@code defaultAction}. 362 * 363 * @param node {@inheritDoc} 364 * @param p {@inheritDoc} 365 * @return the result of {@code defaultAction} 366 */ 367 @Override 368 public R visitExpressionStatement(ExpressionStatementTree node, P p) { 369 return defaultAction(node, p); 370 } 371 372 /** 373 * {@inheritDoc} This implementation calls {@code defaultAction}. 374 * 375 * @param node {@inheritDoc} 376 * @param p {@inheritDoc} 377 * @return the result of {@code defaultAction} 378 */ 379 @Override 380 public R visitBreak(BreakTree node, P p) { 381 return defaultAction(node, p); 382 } 383 384 /** 385 * {@inheritDoc} This implementation calls {@code defaultAction}. 386 * 387 * @param node {@inheritDoc} 388 * @param p {@inheritDoc} 389 * @return the result of {@code defaultAction} 390 */ 391 @Override 392 public R visitContinue(ContinueTree node, P p) { 393 return defaultAction(node, p); 394 } 395 396 /** 397 * {@inheritDoc} This implementation calls {@code defaultAction}. 398 * 399 * @param node {@inheritDoc} 400 * @param p {@inheritDoc} 401 * @return the result of {@code defaultAction} 402 */ 403 @Override 404 public R visitReturn(ReturnTree node, P p) { 405 return defaultAction(node, p); 406 } 407 408 /** 409 * {@inheritDoc} This implementation calls {@code defaultAction}. 410 * 411 * @param node {@inheritDoc} 412 * @param p {@inheritDoc} 413 * @return the result of {@code defaultAction} 414 */ 415 @Override 416 public R visitThrow(ThrowTree node, P p) { 417 return defaultAction(node, p); 418 } 419 420 /** 421 * {@inheritDoc} This implementation calls {@code defaultAction}. 422 * 423 * @param node {@inheritDoc} 424 * @param p {@inheritDoc} 425 * @return the result of {@code defaultAction} 426 */ 427 @Override 428 public R visitAssert(AssertTree node, P p) { 429 return defaultAction(node, p); 430 } 431 432 /** 433 * {@inheritDoc} This implementation calls {@code defaultAction}. 434 * 435 * @param node {@inheritDoc} 436 * @param p {@inheritDoc} 437 * @return the result of {@code defaultAction} 438 */ 439 @Override 440 public R visitMethodInvocation(MethodInvocationTree node, P p) { 441 return defaultAction(node, p); 442 } 443 444 /** 445 * {@inheritDoc} This implementation calls {@code defaultAction}. 446 * 447 * @param node {@inheritDoc} 448 * @param p {@inheritDoc} 449 * @return the result of {@code defaultAction} 450 */ 451 @Override 452 public R visitNewClass(NewClassTree node, P p) { 453 return defaultAction(node, p); 454 } 455 456 /** 457 * {@inheritDoc} This implementation calls {@code defaultAction}. 458 * 459 * @param node {@inheritDoc} 460 * @param p {@inheritDoc} 461 * @return the result of {@code defaultAction} 462 */ 463 @Override 464 public R visitNewArray(NewArrayTree node, P p) { 465 return defaultAction(node, p); 466 } 467 468 /** 469 * {@inheritDoc} This implementation calls {@code defaultAction}. 470 * 471 * @param node {@inheritDoc} 472 * @param p {@inheritDoc} 473 * @return the result of {@code defaultAction} 474 */ 475 @Override 476 public R visitLambdaExpression(LambdaExpressionTree node, P p) { 477 return defaultAction(node, p); 478 } 479 480 /** 481 * {@inheritDoc} This implementation calls {@code defaultAction}. 482 * 483 * @param node {@inheritDoc} 484 * @param p {@inheritDoc} 485 * @return the result of {@code defaultAction} 486 */ 487 @Override 488 public R visitParenthesized(ParenthesizedTree node, P p) { 489 return defaultAction(node, p); 490 } 491 492 /** 493 * {@inheritDoc} This implementation calls {@code defaultAction}. 494 * 495 * @param node {@inheritDoc} 496 * @param p {@inheritDoc} 497 * @return the result of {@code defaultAction} 498 */ 499 @Override 500 public R visitAssignment(AssignmentTree node, P p) { 501 return defaultAction(node, p); 502 } 503 504 /** 505 * {@inheritDoc} This implementation calls {@code defaultAction}. 506 * 507 * @param node {@inheritDoc} 508 * @param p {@inheritDoc} 509 * @return the result of {@code defaultAction} 510 */ 511 @Override 512 public R visitCompoundAssignment(CompoundAssignmentTree node, P p) { 513 return defaultAction(node, p); 514 } 515 516 /** 517 * {@inheritDoc} This implementation calls {@code defaultAction}. 518 * 519 * @param node {@inheritDoc} 520 * @param p {@inheritDoc} 521 * @return the result of {@code defaultAction} 522 */ 523 @Override 524 public R visitUnary(UnaryTree node, P p) { 525 return defaultAction(node, p); 526 } 527 528 /** 529 * {@inheritDoc} This implementation calls {@code defaultAction}. 530 * 531 * @param node {@inheritDoc} 532 * @param p {@inheritDoc} 533 * @return the result of {@code defaultAction} 534 */ 535 @Override 536 public R visitBinary(BinaryTree node, P p) { 537 return defaultAction(node, p); 538 } 539 540 /** 541 * {@inheritDoc} This implementation calls {@code defaultAction}. 542 * 543 * @param node {@inheritDoc} 544 * @param p {@inheritDoc} 545 * @return the result of {@code defaultAction} 546 */ 547 @Override 548 public R visitTypeCast(TypeCastTree node, P p) { 549 return defaultAction(node, p); 550 } 551 552 /** 553 * {@inheritDoc} This implementation calls {@code defaultAction}. 554 * 555 * @param node {@inheritDoc} 556 * @param p {@inheritDoc} 557 * @return the result of {@code defaultAction} 558 */ 559 @Override 560 public R visitInstanceOf(InstanceOfTree node, P p) { 561 return defaultAction(node, p); 562 } 563 564 /** 565 * {@inheritDoc} This implementation calls {@code defaultAction}. 566 * 567 * @param node {@inheritDoc} 568 * @param p {@inheritDoc} 569 * @return the result of {@code defaultAction} 570 */ 571 @Override 572 public R visitArrayAccess(ArrayAccessTree node, P p) { 573 return defaultAction(node, p); 574 } 575 576 /** 577 * {@inheritDoc} This implementation calls {@code defaultAction}. 578 * 579 * @param node {@inheritDoc} 580 * @param p {@inheritDoc} 581 * @return the result of {@code defaultAction} 582 */ 583 @Override 584 public R visitMemberSelect(MemberSelectTree node, P p) { 585 return defaultAction(node, p); 586 } 587 588 /** 589 * {@inheritDoc} This implementation calls {@code defaultAction}. 590 * 591 * @param node {@inheritDoc} 592 * @param p {@inheritDoc} 593 * @return the result of {@code defaultAction} 594 */ 595 @Override 596 public R visitMemberReference(MemberReferenceTree node, P p) { 597 return defaultAction(node, p); 598 } 599 600 /** 601 * {@inheritDoc} This implementation calls {@code defaultAction}. 602 * 603 * @param node {@inheritDoc} 604 * @param p {@inheritDoc} 605 * @return the result of {@code defaultAction} 606 */ 607 @Override 608 public R visitIdentifier(IdentifierTree node, P p) { 609 return defaultAction(node, p); 610 } 611 612 /** 613 * {@inheritDoc} This implementation calls {@code defaultAction}. 614 * 615 * @param node {@inheritDoc} 616 * @param p {@inheritDoc} 617 * @return the result of {@code defaultAction} 618 */ 619 @Override 620 public R visitLiteral(LiteralTree node, P p) { 621 return defaultAction(node, p); 622 } 623 624 /** 625 * {@inheritDoc} This implementation calls {@code defaultAction}. 626 * 627 * @param node {@inheritDoc} 628 * @param p {@inheritDoc} 629 * @return the result of {@code defaultAction} 630 */ 631 @Override 632 public R visitPrimitiveType(PrimitiveTypeTree node, P p) { 633 return defaultAction(node, p); 634 } 635 636 /** 637 * {@inheritDoc} This implementation calls {@code defaultAction}. 638 * 639 * @param node {@inheritDoc} 640 * @param p {@inheritDoc} 641 * @return the result of {@code defaultAction} 642 */ 643 @Override 644 public R visitArrayType(ArrayTypeTree node, P p) { 645 return defaultAction(node, p); 646 } 647 648 /** 649 * {@inheritDoc} This implementation calls {@code defaultAction}. 650 * 651 * @param node {@inheritDoc} 652 * @param p {@inheritDoc} 653 * @return the result of {@code defaultAction} 654 */ 655 @Override 656 public R visitParameterizedType(ParameterizedTypeTree node, P p) { 657 return defaultAction(node, p); 658 } 659 660 /** 661 * {@inheritDoc} This implementation calls {@code defaultAction}. 662 * 663 * @param node {@inheritDoc} 664 * @param p {@inheritDoc} 665 * @return the result of {@code defaultAction} 666 */ 667 @Override 668 public R visitUnionType(UnionTypeTree node, P p) { 669 return defaultAction(node, p); 670 } 671 672 /** 673 * {@inheritDoc} This implementation calls {@code defaultAction}. 674 * 675 * @param node {@inheritDoc} 676 * @param p {@inheritDoc} 677 * @return the result of {@code defaultAction} 678 */ 679 @Override 680 public R visitIntersectionType(IntersectionTypeTree node, P p) { 681 return defaultAction(node, p); 682 } 683 684 /** 685 * {@inheritDoc} This implementation calls {@code defaultAction}. 686 * 687 * @param node {@inheritDoc} 688 * @param p {@inheritDoc} 689 * @return the result of {@code defaultAction} 690 */ 691 @Override 692 public R visitTypeParameter(TypeParameterTree node, P p) { 693 return defaultAction(node, p); 694 } 695 696 /** 697 * {@inheritDoc} This implementation calls {@code defaultAction}. 698 * 699 * @param node {@inheritDoc} 700 * @param p {@inheritDoc} 701 * @return the result of {@code defaultAction} 702 */ 703 @Override 704 public R visitWildcard(WildcardTree node, P p) { 705 return defaultAction(node, p); 706 } 707 708 /** 709 * {@inheritDoc} This implementation calls {@code defaultAction}. 710 * 711 * @param node {@inheritDoc} 712 * @param p {@inheritDoc} 713 * @return the result of {@code defaultAction} 714 */ 715 @Override 716 public R visitModifiers(ModifiersTree node, P p) { 717 return defaultAction(node, p); 718 } 719 720 /** 721 * {@inheritDoc} This implementation calls {@code defaultAction}. 722 * 723 * @param node {@inheritDoc} 724 * @param p {@inheritDoc} 725 * @return the result of {@code defaultAction} 726 */ 727 @Override 728 public R visitAnnotation(AnnotationTree node, P p) { 729 return defaultAction(node, p); 730 } 731 732 /** 733 * {@inheritDoc} This implementation calls {@code defaultAction}. 734 * 735 * @param node {@inheritDoc} 736 * @param p {@inheritDoc} 737 * @return the result of {@code defaultAction} 738 */ 739 @Override 740 public R visitAnnotatedType(AnnotatedTypeTree node, P p) { 741 return defaultAction(node, p); 742 } 743 744 public R visitModule(ModuleTree node, P p) { 745 return defaultAction(node, p); 746 } 747 748 @Override 749 public R visitExports(ExportsTree node, P p) { 750 return defaultAction(node, p); 751 } 752 753 @Override 754 public R visitOpens(OpensTree node, P p) { 755 return defaultAction(node, p); 756 } 757 758 @Override 759 public R visitProvides(ProvidesTree node, P p) { 760 return defaultAction(node, p); 761 } 762 763 @Override 764 public R visitRequires(RequiresTree node, P p) { 765 return defaultAction(node, p); 766 } 767 768 @Override 769 public R visitUses(UsesTree node, P p) { 770 return defaultAction(node, p); 771 } 772 773 public R visitErroneous(ErroneousTree node, P p) { 774 return defaultAction(node, p); 775 } 776 777 /** 778 * {@inheritDoc} This implementation calls {@code defaultAction}. 779 * 780 * @param node {@inheritDoc} 781 * @param p {@inheritDoc} 782 * @return the result of {@code defaultAction} 783 */ 784 @Override 785 public R visitOther(Tree node, P p) { 786 return defaultAction(node, p); 787 } 788 789 /** 790 * {@inheritDoc} This implementation calls {@code defaultAction}. 791 * 792 * @param node {@inheritDoc} 793 * @param p {@inheritDoc} 794 * @return the result of {@code defaultAction} 795 */ 796 @Override 797 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SWITCH_EXPRESSIONS) 798 @SuppressWarnings("preview") 799 public R visitYield(YieldTree node, P p) { 800 return defaultAction(node, p); 801 } 802 }