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 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 * {@inheritDoc} This implementation calls {@code defaultAction}. 268 * 269 * @param node {@inheritDoc} 270 * @param p {@inheritDoc} 271 * @return the result of {@code defaultAction} 272 * 273 * @deprecated 274 * This method is modeling switch expressions, 275 * which are part of a preview feature and may be removed 276 * if the preview feature is removed. 277 */ 278 @Override 279 @Deprecated(forRemoval=true, since="12") 280 @SuppressWarnings("removal") 281 public R visitSwitchExpression(SwitchExpressionTree node, P p) { 282 return defaultAction(node, p); 283 } 284 285 /** 286 * {@inheritDoc} This implementation calls {@code defaultAction}. 287 * 288 * @param node {@inheritDoc} 289 * @param p {@inheritDoc} 290 * @return the result of {@code defaultAction} 291 */ 292 @Override 293 public R visitCase(CaseTree node, P p) { 294 return defaultAction(node, p); 295 } 296 297 /** 298 * {@inheritDoc} This implementation calls {@code defaultAction}. 299 * 300 * @param node {@inheritDoc} 301 * @param p {@inheritDoc} 302 * @return the result of {@code defaultAction} 303 */ 304 @Override 305 public R visitSynchronized(SynchronizedTree node, P p) { 306 return defaultAction(node, p); 307 } 308 309 /** 310 * {@inheritDoc} This implementation calls {@code defaultAction}. 311 * 312 * @param node {@inheritDoc} 313 * @param p {@inheritDoc} 314 * @return the result of {@code defaultAction} 315 */ 316 @Override 317 public R visitTry(TryTree node, P p) { 318 return defaultAction(node, p); 319 } 320 321 /** 322 * {@inheritDoc} This implementation calls {@code defaultAction}. 323 * 324 * @param node {@inheritDoc} 325 * @param p {@inheritDoc} 326 * @return the result of {@code defaultAction} 327 */ 328 @Override 329 public R visitCatch(CatchTree node, P p) { 330 return defaultAction(node, p); 331 } 332 333 /** 334 * {@inheritDoc} This implementation calls {@code defaultAction}. 335 * 336 * @param node {@inheritDoc} 337 * @param p {@inheritDoc} 338 * @return the result of {@code defaultAction} 339 */ 340 @Override 341 public R visitConditionalExpression(ConditionalExpressionTree node, P p) { 342 return defaultAction(node, p); 343 } 344 345 /** 346 * {@inheritDoc} This implementation calls {@code defaultAction}. 347 * 348 * @param node {@inheritDoc} 349 * @param p {@inheritDoc} 350 * @return the result of {@code defaultAction} 351 */ 352 @Override 353 public R visitIf(IfTree node, P p) { 354 return defaultAction(node, p); 355 } 356 357 /** 358 * {@inheritDoc} This implementation calls {@code defaultAction}. 359 * 360 * @param node {@inheritDoc} 361 * @param p {@inheritDoc} 362 * @return the result of {@code defaultAction} 363 */ 364 @Override 365 public R visitExpressionStatement(ExpressionStatementTree node, P p) { 366 return defaultAction(node, p); 367 } 368 369 /** 370 * {@inheritDoc} This implementation calls {@code defaultAction}. 371 * 372 * @param node {@inheritDoc} 373 * @param p {@inheritDoc} 374 * @return the result of {@code defaultAction} 375 */ 376 @Override 377 public R visitBreak(BreakTree node, P p) { 378 return defaultAction(node, p); 379 } 380 381 /** 382 * {@inheritDoc} This implementation calls {@code defaultAction}. 383 * 384 * @param node {@inheritDoc} 385 * @param p {@inheritDoc} 386 * @return the result of {@code defaultAction} 387 */ 388 @Override 389 public R visitContinue(ContinueTree node, P p) { 390 return defaultAction(node, p); 391 } 392 393 /** 394 * {@inheritDoc} This implementation calls {@code defaultAction}. 395 * 396 * @param node {@inheritDoc} 397 * @param p {@inheritDoc} 398 * @return the result of {@code defaultAction} 399 */ 400 @Override 401 public R visitReturn(ReturnTree node, P p) { 402 return defaultAction(node, p); 403 } 404 405 /** 406 * {@inheritDoc} This implementation calls {@code defaultAction}. 407 * 408 * @param node {@inheritDoc} 409 * @param p {@inheritDoc} 410 * @return the result of {@code defaultAction} 411 */ 412 @Override 413 public R visitThrow(ThrowTree node, P p) { 414 return defaultAction(node, p); 415 } 416 417 /** 418 * {@inheritDoc} This implementation calls {@code defaultAction}. 419 * 420 * @param node {@inheritDoc} 421 * @param p {@inheritDoc} 422 * @return the result of {@code defaultAction} 423 */ 424 @Override 425 public R visitAssert(AssertTree node, P p) { 426 return defaultAction(node, p); 427 } 428 429 /** 430 * {@inheritDoc} This implementation calls {@code defaultAction}. 431 * 432 * @param node {@inheritDoc} 433 * @param p {@inheritDoc} 434 * @return the result of {@code defaultAction} 435 */ 436 @Override 437 public R visitMethodInvocation(MethodInvocationTree node, P p) { 438 return defaultAction(node, p); 439 } 440 441 /** 442 * {@inheritDoc} This implementation calls {@code defaultAction}. 443 * 444 * @param node {@inheritDoc} 445 * @param p {@inheritDoc} 446 * @return the result of {@code defaultAction} 447 */ 448 @Override 449 public R visitNewClass(NewClassTree node, P p) { 450 return defaultAction(node, p); 451 } 452 453 /** 454 * {@inheritDoc} This implementation calls {@code defaultAction}. 455 * 456 * @param node {@inheritDoc} 457 * @param p {@inheritDoc} 458 * @return the result of {@code defaultAction} 459 */ 460 @Override 461 public R visitNewArray(NewArrayTree node, P p) { 462 return defaultAction(node, p); 463 } 464 465 /** 466 * {@inheritDoc} This implementation calls {@code defaultAction}. 467 * 468 * @param node {@inheritDoc} 469 * @param p {@inheritDoc} 470 * @return the result of {@code defaultAction} 471 */ 472 @Override 473 public R visitLambdaExpression(LambdaExpressionTree node, P p) { 474 return defaultAction(node, p); 475 } 476 477 /** 478 * {@inheritDoc} This implementation calls {@code defaultAction}. 479 * 480 * @param node {@inheritDoc} 481 * @param p {@inheritDoc} 482 * @return the result of {@code defaultAction} 483 */ 484 @Override 485 public R visitParenthesized(ParenthesizedTree node, P p) { 486 return defaultAction(node, p); 487 } 488 489 /** 490 * {@inheritDoc} This implementation calls {@code defaultAction}. 491 * 492 * @param node {@inheritDoc} 493 * @param p {@inheritDoc} 494 * @return the result of {@code defaultAction} 495 */ 496 @Override 497 public R visitAssignment(AssignmentTree node, P p) { 498 return defaultAction(node, p); 499 } 500 501 /** 502 * {@inheritDoc} This implementation calls {@code defaultAction}. 503 * 504 * @param node {@inheritDoc} 505 * @param p {@inheritDoc} 506 * @return the result of {@code defaultAction} 507 */ 508 @Override 509 public R visitCompoundAssignment(CompoundAssignmentTree node, P p) { 510 return defaultAction(node, p); 511 } 512 513 /** 514 * {@inheritDoc} This implementation calls {@code defaultAction}. 515 * 516 * @param node {@inheritDoc} 517 * @param p {@inheritDoc} 518 * @return the result of {@code defaultAction} 519 */ 520 @Override 521 public R visitUnary(UnaryTree node, P p) { 522 return defaultAction(node, p); 523 } 524 525 /** 526 * {@inheritDoc} This implementation calls {@code defaultAction}. 527 * 528 * @param node {@inheritDoc} 529 * @param p {@inheritDoc} 530 * @return the result of {@code defaultAction} 531 */ 532 @Override 533 public R visitBinary(BinaryTree node, P p) { 534 return defaultAction(node, p); 535 } 536 537 /** 538 * {@inheritDoc} This implementation calls {@code defaultAction}. 539 * 540 * @param node {@inheritDoc} 541 * @param p {@inheritDoc} 542 * @return the result of {@code defaultAction} 543 */ 544 @Override 545 public R visitTypeCast(TypeCastTree node, P p) { 546 return defaultAction(node, p); 547 } 548 549 /** 550 * {@inheritDoc} This implementation calls {@code defaultAction}. 551 * 552 * @param node {@inheritDoc} 553 * @param p {@inheritDoc} 554 * @return the result of {@code defaultAction} 555 */ 556 @Override 557 public R visitInstanceOf(InstanceOfTree node, P p) { 558 return defaultAction(node, p); 559 } 560 561 /** 562 * {@inheritDoc} This implementation calls {@code defaultAction}. 563 * 564 * @param node {@inheritDoc} 565 * @param p {@inheritDoc} 566 * @return the result of {@code defaultAction} 567 */ 568 @Override 569 public R visitArrayAccess(ArrayAccessTree node, P p) { 570 return defaultAction(node, p); 571 } 572 573 /** 574 * {@inheritDoc} This implementation calls {@code defaultAction}. 575 * 576 * @param node {@inheritDoc} 577 * @param p {@inheritDoc} 578 * @return the result of {@code defaultAction} 579 */ 580 @Override 581 public R visitMemberSelect(MemberSelectTree node, P p) { 582 return defaultAction(node, p); 583 } 584 585 /** 586 * {@inheritDoc} This implementation calls {@code defaultAction}. 587 * 588 * @param node {@inheritDoc} 589 * @param p {@inheritDoc} 590 * @return the result of {@code defaultAction} 591 */ 592 @Override 593 public R visitMemberReference(MemberReferenceTree node, P p) { 594 return defaultAction(node, p); 595 } 596 597 /** 598 * {@inheritDoc} This implementation calls {@code defaultAction}. 599 * 600 * @param node {@inheritDoc} 601 * @param p {@inheritDoc} 602 * @return the result of {@code defaultAction} 603 */ 604 @Override 605 public R visitIdentifier(IdentifierTree node, P p) { 606 return defaultAction(node, p); 607 } 608 609 /** 610 * {@inheritDoc} This implementation calls {@code defaultAction}. 611 * 612 * @param node {@inheritDoc} 613 * @param p {@inheritDoc} 614 * @return the result of {@code defaultAction} 615 */ 616 @Override 617 public R visitLiteral(LiteralTree node, P p) { 618 return defaultAction(node, p); 619 } 620 621 /** 622 * {@inheritDoc} This implementation calls {@code defaultAction}. 623 * 624 * @param node {@inheritDoc} 625 * @param p {@inheritDoc} 626 * @return the result of {@code defaultAction} 627 */ 628 @Override 629 public R visitPrimitiveType(PrimitiveTypeTree node, P p) { 630 return defaultAction(node, p); 631 } 632 633 /** 634 * {@inheritDoc} This implementation calls {@code defaultAction}. 635 * 636 * @param node {@inheritDoc} 637 * @param p {@inheritDoc} 638 * @return the result of {@code defaultAction} 639 */ 640 @Override 641 public R visitArrayType(ArrayTypeTree node, P p) { 642 return defaultAction(node, p); 643 } 644 645 /** 646 * {@inheritDoc} This implementation calls {@code defaultAction}. 647 * 648 * @param node {@inheritDoc} 649 * @param p {@inheritDoc} 650 * @return the result of {@code defaultAction} 651 */ 652 @Override 653 public R visitParameterizedType(ParameterizedTypeTree node, P p) { 654 return defaultAction(node, p); 655 } 656 657 /** 658 * {@inheritDoc} This implementation calls {@code defaultAction}. 659 * 660 * @param node {@inheritDoc} 661 * @param p {@inheritDoc} 662 * @return the result of {@code defaultAction} 663 */ 664 @Override 665 public R visitUnionType(UnionTypeTree node, P p) { 666 return defaultAction(node, p); 667 } 668 669 /** 670 * {@inheritDoc} This implementation calls {@code defaultAction}. 671 * 672 * @param node {@inheritDoc} 673 * @param p {@inheritDoc} 674 * @return the result of {@code defaultAction} 675 */ 676 @Override 677 public R visitIntersectionType(IntersectionTypeTree node, P p) { 678 return defaultAction(node, p); 679 } 680 681 /** 682 * {@inheritDoc} This implementation calls {@code defaultAction}. 683 * 684 * @param node {@inheritDoc} 685 * @param p {@inheritDoc} 686 * @return the result of {@code defaultAction} 687 */ 688 @Override 689 public R visitTypeParameter(TypeParameterTree node, P p) { 690 return defaultAction(node, p); 691 } 692 693 /** 694 * {@inheritDoc} This implementation calls {@code defaultAction}. 695 * 696 * @param node {@inheritDoc} 697 * @param p {@inheritDoc} 698 * @return the result of {@code defaultAction} 699 */ 700 @Override 701 public R visitWildcard(WildcardTree node, P p) { 702 return defaultAction(node, p); 703 } 704 705 /** 706 * {@inheritDoc} This implementation calls {@code defaultAction}. 707 * 708 * @param node {@inheritDoc} 709 * @param p {@inheritDoc} 710 * @return the result of {@code defaultAction} 711 */ 712 @Override 713 public R visitModifiers(ModifiersTree node, P p) { 714 return defaultAction(node, p); 715 } 716 717 /** 718 * {@inheritDoc} This implementation calls {@code defaultAction}. 719 * 720 * @param node {@inheritDoc} 721 * @param p {@inheritDoc} 722 * @return the result of {@code defaultAction} 723 */ 724 @Override 725 public R visitAnnotation(AnnotationTree node, P p) { 726 return defaultAction(node, p); 727 } 728 729 /** 730 * {@inheritDoc} This implementation calls {@code defaultAction}. 731 * 732 * @param node {@inheritDoc} 733 * @param p {@inheritDoc} 734 * @return the result of {@code defaultAction} 735 */ 736 @Override 737 public R visitAnnotatedType(AnnotatedTypeTree node, P p) { 738 return defaultAction(node, p); 739 } 740 741 public R visitModule(ModuleTree node, P p) { 742 return defaultAction(node, p); 743 } 744 745 @Override 746 public R visitExports(ExportsTree node, P p) { 747 return defaultAction(node, p); 748 } 749 750 @Override 751 public R visitOpens(OpensTree node, P p) { 752 return defaultAction(node, p); 753 } 754 755 @Override 756 public R visitProvides(ProvidesTree node, P p) { 757 return defaultAction(node, p); 758 } 759 760 @Override 761 public R visitRequires(RequiresTree node, P p) { 762 return defaultAction(node, p); 763 } 764 765 @Override 766 public R visitUses(UsesTree node, P p) { 767 return defaultAction(node, p); 768 } 769 770 public R visitErroneous(ErroneousTree node, P p) { 771 return defaultAction(node, p); 772 } 773 774 /** 775 * {@inheritDoc} This implementation calls {@code defaultAction}. 776 * 777 * @param node {@inheritDoc} 778 * @param p {@inheritDoc} 779 * @return the result of {@code defaultAction} 780 */ 781 @Override 782 public R visitOther(Tree node, P p) { 783 return defaultAction(node, p); 784 } 785 }