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