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.tree;
  27 
  28 /**
  29  * Common interface for all nodes in an abstract syntax tree.
  30  *
  31  * <p><b>WARNING:</b> This interface and its sub-interfaces are
  32  * subject to change as the Java&trade; programming language evolves.
  33  * These interfaces are implemented by the JDK Java compiler (javac)
  34  * and should not be implemented either directly or indirectly by
  35  * other applications.
  36  *
  37  * @author Peter von der Ah&eacute;
  38  * @author Jonathan Gibbons
  39  *
  40  * @since 1.6
  41  */
  42 public interface Tree {
  43 
  44     /**
  45      * Enumerates all kinds of trees.
  46      */
  47     public enum Kind {
  48         /**
  49          * Used for instances of {@link AnnotatedTypeTree}
  50          * representing annotated types.
  51          */
  52         ANNOTATED_TYPE(AnnotatedTypeTree.class),
  53 
  54         /**
  55          * Used for instances of {@link AnnotationTree}
  56          * representing declaration annotations.
  57          */
  58         ANNOTATION(AnnotationTree.class),
  59 
  60         /**
  61          * Used for instances of {@link AnnotationTree}
  62          * representing type annotations.
  63          */
  64         TYPE_ANNOTATION(AnnotationTree.class),
  65 
  66         /**
  67          * Used for instances of {@link ArrayAccessTree}.
  68          */
  69         ARRAY_ACCESS(ArrayAccessTree.class),
  70 
  71         /**
  72          * Used for instances of {@link ArrayTypeTree}.
  73          */
  74         ARRAY_TYPE(ArrayTypeTree.class),
  75 
  76         /**
  77          * Used for instances of {@link AssertTree}.
  78          */
  79         ASSERT(AssertTree.class),
  80 
  81         /**
  82          * Used for instances of {@link AssignmentTree}.
  83          */
  84         ASSIGNMENT(AssignmentTree.class),
  85 
  86         /**
  87          * Used for instances of {@link BlockTree}.
  88          */
  89         BLOCK(BlockTree.class),
  90 
  91         /**
  92          * Used for instances of {@link BreakTree}.
  93          */
  94         BREAK(BreakTree.class),
  95 
  96         /**
  97          * Used for instances of {@link CaseTree}.
  98          */
  99         CASE(CaseTree.class),
 100 
 101         /**
 102          * Used for instances of {@link CatchTree}.
 103          */
 104         CATCH(CatchTree.class),
 105 
 106         /**
 107          * Used for instances of {@link ClassTree} representing classes.
 108          */
 109         CLASS(ClassTree.class),
 110 
 111         /**
 112          * Used for instances of {@link CompilationUnitTree}.
 113          */
 114         COMPILATION_UNIT(CompilationUnitTree.class),
 115 
 116         /**
 117          * Used for instances of {@link ConditionalExpressionTree}.
 118          */
 119         CONDITIONAL_EXPRESSION(ConditionalExpressionTree.class),
 120 
 121         /**
 122          * Used for instances of {@link ContinueTree}.
 123          */
 124         CONTINUE(ContinueTree.class),
 125 
 126         /**
 127          * Used for instances of {@link DoWhileLoopTree}.
 128          */
 129         DO_WHILE_LOOP(DoWhileLoopTree.class),
 130 
 131         /**
 132          * Used for instances of {@link EnhancedForLoopTree}.
 133          */
 134         ENHANCED_FOR_LOOP(EnhancedForLoopTree.class),
 135 
 136         /**
 137          * Used for instances of {@link ExpressionStatementTree}.
 138          */
 139         EXPRESSION_STATEMENT(ExpressionStatementTree.class),
 140 
 141         /**
 142          * Used for instances of {@link MemberSelectTree}.
 143          */
 144         MEMBER_SELECT(MemberSelectTree.class),
 145 
 146         /**
 147          * Used for instances of {@link MemberReferenceTree}.
 148          */
 149         MEMBER_REFERENCE(MemberReferenceTree.class),
 150 
 151         /**
 152          * Used for instances of {@link ForLoopTree}.
 153          */
 154         FOR_LOOP(ForLoopTree.class),
 155 
 156         /**
 157          * Used for instances of {@link IdentifierTree}.
 158          */
 159         IDENTIFIER(IdentifierTree.class),
 160 
 161         /**
 162          * Used for instances of {@link IfTree}.
 163          */
 164         IF(IfTree.class),
 165 
 166         /**
 167          * Used for instances of {@link ImportTree}.
 168          */
 169         IMPORT(ImportTree.class),
 170 
 171         /**
 172          * Used for instances of {@link InstanceOfTree}.
 173          */
 174         INSTANCE_OF(InstanceOfTree.class),
 175 
 176         /**
 177          * Used for instances of {@link LabeledStatementTree}.
 178          */
 179         LABELED_STATEMENT(LabeledStatementTree.class),
 180 
 181         /**
 182          * Used for instances of {@link MethodTree}.
 183          */
 184         METHOD(MethodTree.class),
 185 
 186         /**
 187          * Used for instances of {@link MethodInvocationTree}.
 188          */
 189         METHOD_INVOCATION(MethodInvocationTree.class),
 190 
 191         /**
 192          * Used for instances of {@link ModifiersTree}.
 193          */
 194         MODIFIERS(ModifiersTree.class),
 195 
 196         /**
 197          * Used for instances of {@link NewArrayTree}.
 198          */
 199         NEW_ARRAY(NewArrayTree.class),
 200 
 201         /**
 202          * Used for instances of {@link NewClassTree}.
 203          */
 204         NEW_CLASS(NewClassTree.class),
 205 
 206         /**
 207          * Used for instances of {@link LambdaExpressionTree}.
 208          */
 209         LAMBDA_EXPRESSION(LambdaExpressionTree.class),
 210 
 211         /**
 212          * Used for instances of {@link PackageTree}.
 213          * @since 9
 214          */
 215         PACKAGE(PackageTree.class),
 216 
 217         /**
 218          * Used for instances of {@link ParenthesizedTree}.
 219          */
 220         PARENTHESIZED(ParenthesizedTree.class),
 221 
 222         /**
 223          * Used for instances of {@link BindingPatternTree}.
 224          */
 225         BINDING_PATTERN(BindingPatternTree.class),
 226 
 227         /**
 228          * Used for instances of {@link PrimitiveTypeTree}.
 229          */
 230         PRIMITIVE_TYPE(PrimitiveTypeTree.class),
 231 
 232         /**
 233          * Used for instances of {@link ReturnTree}.
 234          */
 235         RETURN(ReturnTree.class),
 236 
 237         /**
 238          * Used for instances of {@link EmptyStatementTree}.
 239          */
 240         EMPTY_STATEMENT(EmptyStatementTree.class),
 241 
 242         /**
 243          * Used for instances of {@link SwitchTree}.
 244          */
 245         SWITCH(SwitchTree.class),
 246 
 247         /**
 248          * Used for instances of {@link SwitchExpressionTree}.
 249          *
 250          * @since 12
 251          *
 252          * @deprecated
 253          * This enum constant is modeling switch expressions,
 254          * which are part of a preview feature and may be removed
 255          * if the preview feature is removed.
 256          */
 257         @Deprecated(forRemoval=true, since="12")
 258         @SuppressWarnings("removal")
 259         SWITCH_EXPRESSION(SwitchExpressionTree.class),
 260 
 261         /**
 262          * Used for instances of {@link SynchronizedTree}.
 263          */
 264         SYNCHRONIZED(SynchronizedTree.class),
 265 
 266         /**
 267          * Used for instances of {@link ThrowTree}.
 268          */
 269         THROW(ThrowTree.class),
 270 
 271         /**
 272          * Used for instances of {@link TryTree}.
 273          */
 274         TRY(TryTree.class),
 275 
 276         /**
 277          * Used for instances of {@link ParameterizedTypeTree}.
 278          */
 279         PARAMETERIZED_TYPE(ParameterizedTypeTree.class),
 280 
 281         /**
 282          * Used for instances of {@link UnionTypeTree}.
 283          */
 284         UNION_TYPE(UnionTypeTree.class),
 285 
 286         /**
 287          * Used for instances of {@link IntersectionTypeTree}.
 288          */
 289         INTERSECTION_TYPE(IntersectionTypeTree.class),
 290 
 291         /**
 292          * Used for instances of {@link TypeCastTree}.
 293          */
 294         TYPE_CAST(TypeCastTree.class),
 295 
 296         /**
 297          * Used for instances of {@link TypeParameterTree}.
 298          */
 299         TYPE_PARAMETER(TypeParameterTree.class),
 300 
 301         /**
 302          * Used for instances of {@link VariableTree}.
 303          */
 304         VARIABLE(VariableTree.class),
 305 
 306         /**
 307          * Used for instances of {@link WhileLoopTree}.
 308          */
 309         WHILE_LOOP(WhileLoopTree.class),
 310 
 311         /**
 312          * Used for instances of {@link UnaryTree} representing postfix
 313          * increment operator {@code ++}.
 314          */
 315         POSTFIX_INCREMENT(UnaryTree.class),
 316 
 317         /**
 318          * Used for instances of {@link UnaryTree} representing postfix
 319          * decrement operator {@code --}.
 320          */
 321         POSTFIX_DECREMENT(UnaryTree.class),
 322 
 323         /**
 324          * Used for instances of {@link UnaryTree} representing prefix
 325          * increment operator {@code ++}.
 326          */
 327         PREFIX_INCREMENT(UnaryTree.class),
 328 
 329         /**
 330          * Used for instances of {@link UnaryTree} representing prefix
 331          * decrement operator {@code --}.
 332          */
 333         PREFIX_DECREMENT(UnaryTree.class),
 334 
 335         /**
 336          * Used for instances of {@link UnaryTree} representing unary plus
 337          * operator {@code +}.
 338          */
 339         UNARY_PLUS(UnaryTree.class),
 340 
 341         /**
 342          * Used for instances of {@link UnaryTree} representing unary minus
 343          * operator {@code -}.
 344          */
 345         UNARY_MINUS(UnaryTree.class),
 346 
 347         /**
 348          * Used for instances of {@link UnaryTree} representing bitwise
 349          * complement operator {@code ~}.
 350          */
 351         BITWISE_COMPLEMENT(UnaryTree.class),
 352 
 353         /**
 354          * Used for instances of {@link UnaryTree} representing logical
 355          * complement operator {@code !}.
 356          */
 357         LOGICAL_COMPLEMENT(UnaryTree.class),
 358 
 359         /**
 360          * Used for instances of {@link BinaryTree} representing
 361          * multiplication {@code *}.
 362          */
 363         MULTIPLY(BinaryTree.class),
 364 
 365         /**
 366          * Used for instances of {@link BinaryTree} representing
 367          * division {@code /}.
 368          */
 369         DIVIDE(BinaryTree.class),
 370 
 371         /**
 372          * Used for instances of {@link BinaryTree} representing
 373          * remainder {@code %}.
 374          */
 375         REMAINDER(BinaryTree.class),
 376 
 377         /**
 378          * Used for instances of {@link BinaryTree} representing
 379          * addition or string concatenation {@code +}.
 380          */
 381         PLUS(BinaryTree.class),
 382 
 383         /**
 384          * Used for instances of {@link BinaryTree} representing
 385          * subtraction {@code -}.
 386          */
 387         MINUS(BinaryTree.class),
 388 
 389         /**
 390          * Used for instances of {@link BinaryTree} representing
 391          * left shift {@code <<}.
 392          */
 393         LEFT_SHIFT(BinaryTree.class),
 394 
 395         /**
 396          * Used for instances of {@link BinaryTree} representing
 397          * right shift {@code >>}.
 398          */
 399         RIGHT_SHIFT(BinaryTree.class),
 400 
 401         /**
 402          * Used for instances of {@link BinaryTree} representing
 403          * unsigned right shift {@code >>>}.
 404          */
 405         UNSIGNED_RIGHT_SHIFT(BinaryTree.class),
 406 
 407         /**
 408          * Used for instances of {@link BinaryTree} representing
 409          * less-than {@code <}.
 410          */
 411         LESS_THAN(BinaryTree.class),
 412 
 413         /**
 414          * Used for instances of {@link BinaryTree} representing
 415          * greater-than {@code >}.
 416          */
 417         GREATER_THAN(BinaryTree.class),
 418 
 419         /**
 420          * Used for instances of {@link BinaryTree} representing
 421          * less-than-equal {@code <=}.
 422          */
 423         LESS_THAN_EQUAL(BinaryTree.class),
 424 
 425         /**
 426          * Used for instances of {@link BinaryTree} representing
 427          * greater-than-equal {@code >=}.
 428          */
 429         GREATER_THAN_EQUAL(BinaryTree.class),
 430 
 431         /**
 432          * Used for instances of {@link BinaryTree} representing
 433          * equal-to {@code ==}.
 434          */
 435         EQUAL_TO(BinaryTree.class),
 436 
 437         /**
 438          * Used for instances of {@link BinaryTree} representing
 439          * not-equal-to {@code !=}.
 440          */
 441         NOT_EQUAL_TO(BinaryTree.class),
 442 
 443         /**
 444          * Used for instances of {@link BinaryTree} representing
 445          * bitwise and logical "and" {@code &}.
 446          */
 447         AND(BinaryTree.class),
 448 
 449         /**
 450          * Used for instances of {@link BinaryTree} representing
 451          * bitwise and logical "xor" {@code ^}.
 452          */
 453         XOR(BinaryTree.class),
 454 
 455         /**
 456          * Used for instances of {@link BinaryTree} representing
 457          * bitwise and logical "or" {@code |}.
 458          */
 459         OR(BinaryTree.class),
 460 
 461         /**
 462          * Used for instances of {@link BinaryTree} representing
 463          * conditional-and {@code &&}.
 464          */
 465         CONDITIONAL_AND(BinaryTree.class),
 466 
 467         /**
 468          * Used for instances of {@link BinaryTree} representing
 469          * conditional-or {@code ||}.
 470          */
 471         CONDITIONAL_OR(BinaryTree.class),
 472 
 473         /**
 474          * Used for instances of {@link CompoundAssignmentTree} representing
 475          * multiplication assignment {@code *=}.
 476          */
 477         MULTIPLY_ASSIGNMENT(CompoundAssignmentTree.class),
 478 
 479         /**
 480          * Used for instances of {@link CompoundAssignmentTree} representing
 481          * division assignment {@code /=}.
 482          */
 483         DIVIDE_ASSIGNMENT(CompoundAssignmentTree.class),
 484 
 485         /**
 486          * Used for instances of {@link CompoundAssignmentTree} representing
 487          * remainder assignment {@code %=}.
 488          */
 489         REMAINDER_ASSIGNMENT(CompoundAssignmentTree.class),
 490 
 491         /**
 492          * Used for instances of {@link CompoundAssignmentTree} representing
 493          * addition or string concatenation assignment {@code +=}.
 494          */
 495         PLUS_ASSIGNMENT(CompoundAssignmentTree.class),
 496 
 497         /**
 498          * Used for instances of {@link CompoundAssignmentTree} representing
 499          * subtraction assignment {@code -=}.
 500          */
 501         MINUS_ASSIGNMENT(CompoundAssignmentTree.class),
 502 
 503         /**
 504          * Used for instances of {@link CompoundAssignmentTree} representing
 505          * left shift assignment {@code <<=}.
 506          */
 507         LEFT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
 508 
 509         /**
 510          * Used for instances of {@link CompoundAssignmentTree} representing
 511          * right shift assignment {@code >>=}.
 512          */
 513         RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
 514 
 515         /**
 516          * Used for instances of {@link CompoundAssignmentTree} representing
 517          * unsigned right shift assignment {@code >>>=}.
 518          */
 519         UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
 520 
 521         /**
 522          * Used for instances of {@link CompoundAssignmentTree} representing
 523          * bitwise and logical "and" assignment {@code &=}.
 524          */
 525         AND_ASSIGNMENT(CompoundAssignmentTree.class),
 526 
 527         /**
 528          * Used for instances of {@link CompoundAssignmentTree} representing
 529          * bitwise and logical "xor" assignment {@code ^=}.
 530          */
 531         XOR_ASSIGNMENT(CompoundAssignmentTree.class),
 532 
 533         /**
 534          * Used for instances of {@link CompoundAssignmentTree} representing
 535          * bitwise and logical "or" assignment {@code |=}.
 536          */
 537         OR_ASSIGNMENT(CompoundAssignmentTree.class),
 538 
 539         /**
 540          * Used for instances of {@link LiteralTree} representing
 541          * an integral literal expression of type {@code int}.
 542          */
 543         INT_LITERAL(LiteralTree.class),
 544 
 545         /**
 546          * Used for instances of {@link LiteralTree} representing
 547          * an integral literal expression of type {@code long}.
 548          */
 549         LONG_LITERAL(LiteralTree.class),
 550 
 551         /**
 552          * Used for instances of {@link LiteralTree} representing
 553          * a floating-point literal expression of type {@code float}.
 554          */
 555         FLOAT_LITERAL(LiteralTree.class),
 556 
 557         /**
 558          * Used for instances of {@link LiteralTree} representing
 559          * a floating-point literal expression of type {@code double}.
 560          */
 561         DOUBLE_LITERAL(LiteralTree.class),
 562 
 563         /**
 564          * Used for instances of {@link LiteralTree} representing
 565          * a boolean literal expression of type {@code boolean}.
 566          */
 567         BOOLEAN_LITERAL(LiteralTree.class),
 568 
 569         /**
 570          * Used for instances of {@link LiteralTree} representing
 571          * a character literal expression of type {@code char}.
 572          */
 573         CHAR_LITERAL(LiteralTree.class),
 574 
 575         /**
 576          * Used for instances of {@link LiteralTree} representing
 577          * a string literal expression of type {@link String}.
 578          */
 579         STRING_LITERAL(LiteralTree.class),
 580 
 581         /**
 582          * Used for instances of {@link LiteralTree} representing
 583          * the use of {@code null}.
 584          */
 585         NULL_LITERAL(LiteralTree.class),
 586 
 587         /**
 588          * Used for instances of {@link WildcardTree} representing
 589          * an unbounded wildcard type argument.
 590          */
 591         UNBOUNDED_WILDCARD(WildcardTree.class),
 592 
 593         /**
 594          * Used for instances of {@link WildcardTree} representing
 595          * an extends bounded wildcard type argument.
 596          */
 597         EXTENDS_WILDCARD(WildcardTree.class),
 598 
 599         /**
 600          * Used for instances of {@link WildcardTree} representing
 601          * a super bounded wildcard type argument.
 602          */
 603         SUPER_WILDCARD(WildcardTree.class),
 604 
 605         /**
 606          * Used for instances of {@link ErroneousTree}.
 607          */
 608         ERRONEOUS(ErroneousTree.class),
 609 
 610         /**
 611          * Used for instances of {@link ClassTree} representing interfaces.
 612          */
 613         INTERFACE(ClassTree.class),
 614 
 615         /**
 616          * Used for instances of {@link ClassTree} representing enums.
 617          */
 618         ENUM(ClassTree.class),
 619 
 620         /**
 621          * Used for instances of {@link ClassTree} representing annotation types.
 622          */
 623         ANNOTATION_TYPE(ClassTree.class),
 624 
 625         /**
 626          * Used for instances of {@link ModuleTree} representing module declarations.
 627          */
 628         MODULE(ModuleTree.class),
 629 
 630         /**
 631          * Used for instances of {@link ExportsTree} representing
 632          * exports directives in a module declaration.
 633          */
 634         EXPORTS(ExportsTree.class),
 635 
 636         /**
 637          * Used for instances of {@link ExportsTree} representing
 638          * opens directives in a module declaration.
 639          */
 640         OPENS(OpensTree.class),
 641 
 642         /**
 643          * Used for instances of {@link ProvidesTree} representing
 644          * provides directives in a module declaration.
 645          */
 646         PROVIDES(ProvidesTree.class),
 647 
 648         /**
 649          * Used for instances of {@link RequiresTree} representing
 650          * requires directives in a module declaration.
 651          */
 652         REQUIRES(RequiresTree.class),
 653 
 654         /**
 655          * Used for instances of {@link UsesTree} representing
 656          * uses directives in a module declaration.
 657          */
 658         USES(UsesTree.class),
 659 
 660         /**
 661          * An implementation-reserved node. This is the not the node
 662          * you are looking for.
 663          */
 664         OTHER(null),
 665 
 666         /**
 667          * Used for instances of {@link YieldTree}.
 668          *
 669          * @since 13
 670          *
 671          * @deprecated
 672          * This enum constant is modeling yield statement,
 673          * which are part of a preview feature and may be removed
 674          * if the preview feature is removed.
 675          */
 676         @Deprecated(forRemoval=true, since="13")
 677         @SuppressWarnings("removal")
 678         YIELD(YieldTree.class);
 679 
 680 
 681         Kind(Class<? extends Tree> intf) {
 682             associatedInterface = intf;
 683         }
 684 
 685         /**
 686          * Returns the associated interface type that uses this kind.
 687          * @return the associated interface
 688          */
 689         public Class<? extends Tree> asInterface() {
 690             return associatedInterface;
 691         }
 692 
 693         private final Class<? extends Tree> associatedInterface;
 694     }
 695 
 696     /**
 697      * Returns the kind of this tree.
 698      *
 699      * @return the kind of this tree.
 700      */
 701     Kind getKind();
 702 
 703     /**
 704      * Accept method used to implement the visitor pattern.  The
 705      * visitor pattern is used to implement operations on trees.
 706      *
 707      * @param <R> result type of this operation.
 708      * @param <D> type of additional data.
 709      * @param visitor the visitor to be called
 710      * @param data a value to be passed to the visitor
 711      * @return the result returned from calling the visitor
 712      */
 713     <R,D> R accept(TreeVisitor<R,D> visitor, D data);
 714 }