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 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          * {@preview Associated with pattern matching for instanceof, a preview feature of
 224          *           the Java language.
 225          *
 226          *           This enum constant is associated with <i>pattern matching for instanceof</i>, a preview
 227          *           feature of the Java language. Preview features
 228          *           may be removed in a future release, or upgraded to permanent
 229          *           features of the Java language.}
 230          *
 231          * Used for instances of {@link BindingPatternTree}.
 232          *
 233          * @since 14
 234          */
 235         BINDING_PATTERN(BindingPatternTree.class),
 236 
 237         /**
 238          * Used for instances of {@link PrimitiveTypeTree}.
 239          */
 240         PRIMITIVE_TYPE(PrimitiveTypeTree.class),
 241 
 242         /**
 243          * Used for instances of {@link ReturnTree}.
 244          */
 245         RETURN(ReturnTree.class),
 246 
 247         /**
 248          * Used for instances of {@link EmptyStatementTree}.
 249          */
 250         EMPTY_STATEMENT(EmptyStatementTree.class),
 251 
 252         /**
 253          * Used for instances of {@link SwitchTree}.
 254          */
 255         SWITCH(SwitchTree.class),
 256 
 257         /**
 258          * Used for instances of {@link SwitchExpressionTree}.
 259          *
 260          * @since 12
 261          */
 262         SWITCH_EXPRESSION(SwitchExpressionTree.class),
 263 
 264         /**
 265          * Used for instances of {@link SynchronizedTree}.
 266          */
 267         SYNCHRONIZED(SynchronizedTree.class),
 268 
 269         /**
 270          * Used for instances of {@link ThrowTree}.
 271          */
 272         THROW(ThrowTree.class),
 273 
 274         /**
 275          * Used for instances of {@link TryTree}.
 276          */
 277         TRY(TryTree.class),
 278 
 279         /**
 280          * Used for instances of {@link ParameterizedTypeTree}.
 281          */
 282         PARAMETERIZED_TYPE(ParameterizedTypeTree.class),
 283 
 284         /**
 285          * Used for instances of {@link UnionTypeTree}.
 286          */
 287         UNION_TYPE(UnionTypeTree.class),
 288 
 289         /**
 290          * Used for instances of {@link IntersectionTypeTree}.
 291          */
 292         INTERSECTION_TYPE(IntersectionTypeTree.class),
 293 
 294         /**
 295          * Used for instances of {@link TypeCastTree}.
 296          */
 297         TYPE_CAST(TypeCastTree.class),
 298 
 299         /**
 300          * Used for instances of {@link TypeParameterTree}.
 301          */
 302         TYPE_PARAMETER(TypeParameterTree.class),
 303 
 304         /**
 305          * Used for instances of {@link VariableTree}.
 306          */
 307         VARIABLE(VariableTree.class),
 308 
 309         /**
 310          * Used for instances of {@link WhileLoopTree}.
 311          */
 312         WHILE_LOOP(WhileLoopTree.class),
 313 
 314         /**
 315          * Used for instances of {@link UnaryTree} representing postfix
 316          * increment operator {@code ++}.
 317          */
 318         POSTFIX_INCREMENT(UnaryTree.class),
 319 
 320         /**
 321          * Used for instances of {@link UnaryTree} representing postfix
 322          * decrement operator {@code --}.
 323          */
 324         POSTFIX_DECREMENT(UnaryTree.class),
 325 
 326         /**
 327          * Used for instances of {@link UnaryTree} representing prefix
 328          * increment operator {@code ++}.
 329          */
 330         PREFIX_INCREMENT(UnaryTree.class),
 331 
 332         /**
 333          * Used for instances of {@link UnaryTree} representing prefix
 334          * decrement operator {@code --}.
 335          */
 336         PREFIX_DECREMENT(UnaryTree.class),
 337 
 338         /**
 339          * Used for instances of {@link UnaryTree} representing unary plus
 340          * operator {@code +}.
 341          */
 342         UNARY_PLUS(UnaryTree.class),
 343 
 344         /**
 345          * Used for instances of {@link UnaryTree} representing unary minus
 346          * operator {@code -}.
 347          */
 348         UNARY_MINUS(UnaryTree.class),
 349 
 350         /**
 351          * Used for instances of {@link UnaryTree} representing bitwise
 352          * complement operator {@code ~}.
 353          */
 354         BITWISE_COMPLEMENT(UnaryTree.class),
 355 
 356         /**
 357          * Used for instances of {@link UnaryTree} representing logical
 358          * complement operator {@code !}.
 359          */
 360         LOGICAL_COMPLEMENT(UnaryTree.class),
 361 
 362         /**
 363          * Used for instances of {@link BinaryTree} representing
 364          * multiplication {@code *}.
 365          */
 366         MULTIPLY(BinaryTree.class),
 367 
 368         /**
 369          * Used for instances of {@link BinaryTree} representing
 370          * division {@code /}.
 371          */
 372         DIVIDE(BinaryTree.class),
 373 
 374         /**
 375          * Used for instances of {@link BinaryTree} representing
 376          * remainder {@code %}.
 377          */
 378         REMAINDER(BinaryTree.class),
 379 
 380         /**
 381          * Used for instances of {@link BinaryTree} representing
 382          * addition or string concatenation {@code +}.
 383          */
 384         PLUS(BinaryTree.class),
 385 
 386         /**
 387          * Used for instances of {@link BinaryTree} representing
 388          * subtraction {@code -}.
 389          */
 390         MINUS(BinaryTree.class),
 391 
 392         /**
 393          * Used for instances of {@link BinaryTree} representing
 394          * left shift {@code <<}.
 395          */
 396         LEFT_SHIFT(BinaryTree.class),
 397 
 398         /**
 399          * Used for instances of {@link BinaryTree} representing
 400          * right shift {@code >>}.
 401          */
 402         RIGHT_SHIFT(BinaryTree.class),
 403 
 404         /**
 405          * Used for instances of {@link BinaryTree} representing
 406          * unsigned right shift {@code >>>}.
 407          */
 408         UNSIGNED_RIGHT_SHIFT(BinaryTree.class),
 409 
 410         /**
 411          * Used for instances of {@link BinaryTree} representing
 412          * less-than {@code <}.
 413          */
 414         LESS_THAN(BinaryTree.class),
 415 
 416         /**
 417          * Used for instances of {@link BinaryTree} representing
 418          * greater-than {@code >}.
 419          */
 420         GREATER_THAN(BinaryTree.class),
 421 
 422         /**
 423          * Used for instances of {@link BinaryTree} representing
 424          * less-than-equal {@code <=}.
 425          */
 426         LESS_THAN_EQUAL(BinaryTree.class),
 427 
 428         /**
 429          * Used for instances of {@link BinaryTree} representing
 430          * greater-than-equal {@code >=}.
 431          */
 432         GREATER_THAN_EQUAL(BinaryTree.class),
 433 
 434         /**
 435          * Used for instances of {@link BinaryTree} representing
 436          * equal-to {@code ==}.
 437          */
 438         EQUAL_TO(BinaryTree.class),
 439 
 440         /**
 441          * Used for instances of {@link BinaryTree} representing
 442          * not-equal-to {@code !=}.
 443          */
 444         NOT_EQUAL_TO(BinaryTree.class),
 445 
 446         /**
 447          * Used for instances of {@link BinaryTree} representing
 448          * bitwise and logical "and" {@code &}.
 449          */
 450         AND(BinaryTree.class),
 451 
 452         /**
 453          * Used for instances of {@link BinaryTree} representing
 454          * bitwise and logical "xor" {@code ^}.
 455          */
 456         XOR(BinaryTree.class),
 457 
 458         /**
 459          * Used for instances of {@link BinaryTree} representing
 460          * bitwise and logical "or" {@code |}.
 461          */
 462         OR(BinaryTree.class),
 463 
 464         /**
 465          * Used for instances of {@link BinaryTree} representing
 466          * conditional-and {@code &&}.
 467          */
 468         CONDITIONAL_AND(BinaryTree.class),
 469 
 470         /**
 471          * Used for instances of {@link BinaryTree} representing
 472          * conditional-or {@code ||}.
 473          */
 474         CONDITIONAL_OR(BinaryTree.class),
 475 
 476         /**
 477          * Used for instances of {@link CompoundAssignmentTree} representing
 478          * multiplication assignment {@code *=}.
 479          */
 480         MULTIPLY_ASSIGNMENT(CompoundAssignmentTree.class),
 481 
 482         /**
 483          * Used for instances of {@link CompoundAssignmentTree} representing
 484          * division assignment {@code /=}.
 485          */
 486         DIVIDE_ASSIGNMENT(CompoundAssignmentTree.class),
 487 
 488         /**
 489          * Used for instances of {@link CompoundAssignmentTree} representing
 490          * remainder assignment {@code %=}.
 491          */
 492         REMAINDER_ASSIGNMENT(CompoundAssignmentTree.class),
 493 
 494         /**
 495          * Used for instances of {@link CompoundAssignmentTree} representing
 496          * addition or string concatenation assignment {@code +=}.
 497          */
 498         PLUS_ASSIGNMENT(CompoundAssignmentTree.class),
 499 
 500         /**
 501          * Used for instances of {@link CompoundAssignmentTree} representing
 502          * subtraction assignment {@code -=}.
 503          */
 504         MINUS_ASSIGNMENT(CompoundAssignmentTree.class),
 505 
 506         /**
 507          * Used for instances of {@link CompoundAssignmentTree} representing
 508          * left shift assignment {@code <<=}.
 509          */
 510         LEFT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
 511 
 512         /**
 513          * Used for instances of {@link CompoundAssignmentTree} representing
 514          * right shift assignment {@code >>=}.
 515          */
 516         RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
 517 
 518         /**
 519          * Used for instances of {@link CompoundAssignmentTree} representing
 520          * unsigned right shift assignment {@code >>>=}.
 521          */
 522         UNSIGNED_RIGHT_SHIFT_ASSIGNMENT(CompoundAssignmentTree.class),
 523 
 524         /**
 525          * Used for instances of {@link CompoundAssignmentTree} representing
 526          * bitwise and logical "and" assignment {@code &=}.
 527          */
 528         AND_ASSIGNMENT(CompoundAssignmentTree.class),
 529 
 530         /**
 531          * Used for instances of {@link CompoundAssignmentTree} representing
 532          * bitwise and logical "xor" assignment {@code ^=}.
 533          */
 534         XOR_ASSIGNMENT(CompoundAssignmentTree.class),
 535 
 536         /**
 537          * Used for instances of {@link CompoundAssignmentTree} representing
 538          * bitwise and logical "or" assignment {@code |=}.
 539          */
 540         OR_ASSIGNMENT(CompoundAssignmentTree.class),
 541 
 542         /**
 543          * Used for instances of {@link LiteralTree} representing
 544          * an integral literal expression of type {@code int}.
 545          */
 546         INT_LITERAL(LiteralTree.class),
 547 
 548         /**
 549          * Used for instances of {@link LiteralTree} representing
 550          * an integral literal expression of type {@code long}.
 551          */
 552         LONG_LITERAL(LiteralTree.class),
 553 
 554         /**
 555          * Used for instances of {@link LiteralTree} representing
 556          * a floating-point literal expression of type {@code float}.
 557          */
 558         FLOAT_LITERAL(LiteralTree.class),
 559 
 560         /**
 561          * Used for instances of {@link LiteralTree} representing
 562          * a floating-point literal expression of type {@code double}.
 563          */
 564         DOUBLE_LITERAL(LiteralTree.class),
 565 
 566         /**
 567          * Used for instances of {@link LiteralTree} representing
 568          * a boolean literal expression of type {@code boolean}.
 569          */
 570         BOOLEAN_LITERAL(LiteralTree.class),
 571 
 572         /**
 573          * Used for instances of {@link LiteralTree} representing
 574          * a character literal expression of type {@code char}.
 575          */
 576         CHAR_LITERAL(LiteralTree.class),
 577 
 578         /**
 579          * Used for instances of {@link LiteralTree} representing
 580          * a string literal expression of type {@link String}.
 581          */
 582         STRING_LITERAL(LiteralTree.class),
 583 
 584         /**
 585          * Used for instances of {@link LiteralTree} representing
 586          * the use of {@code null}.
 587          */
 588         NULL_LITERAL(LiteralTree.class),
 589 
 590         /**
 591          * Used for instances of {@link WildcardTree} representing
 592          * an unbounded wildcard type argument.
 593          */
 594         UNBOUNDED_WILDCARD(WildcardTree.class),
 595 
 596         /**
 597          * Used for instances of {@link WildcardTree} representing
 598          * an extends bounded wildcard type argument.
 599          */
 600         EXTENDS_WILDCARD(WildcardTree.class),
 601 
 602         /**
 603          * Used for instances of {@link WildcardTree} representing
 604          * a super bounded wildcard type argument.
 605          */
 606         SUPER_WILDCARD(WildcardTree.class),
 607 
 608         /**
 609          * Used for instances of {@link ErroneousTree}.
 610          */
 611         ERRONEOUS(ErroneousTree.class),
 612 
 613         /**
 614          * Used for instances of {@link ClassTree} representing interfaces.
 615          */
 616         INTERFACE(ClassTree.class),
 617 
 618         /**
 619          * Used for instances of {@link ClassTree} representing enums.
 620          */
 621         ENUM(ClassTree.class),
 622 
 623         /**
 624          * Used for instances of {@link ClassTree} representing annotation types.
 625          */
 626         ANNOTATION_TYPE(ClassTree.class),
 627 
 628         /**
 629          * Used for instances of {@link ModuleTree} representing module declarations.
 630          */
 631         MODULE(ModuleTree.class),
 632 
 633         /**
 634          * Used for instances of {@link ExportsTree} representing
 635          * exports directives in a module declaration.
 636          */
 637         EXPORTS(ExportsTree.class),
 638 
 639         /**
 640          * Used for instances of {@link ExportsTree} representing
 641          * opens directives in a module declaration.
 642          */
 643         OPENS(OpensTree.class),
 644 
 645         /**
 646          * Used for instances of {@link ProvidesTree} representing
 647          * provides directives in a module declaration.
 648          */
 649         PROVIDES(ProvidesTree.class),
 650 
 651         /**
 652          * {@preview Associated with records, a preview feature of the Java language.
 653          *
 654          *           This enum constant is associated with <i>records</i>, a preview
 655          *           feature of the Java language. Preview features
 656          *           may be removed in a future release, or upgraded to permanent
 657          *           features of the Java language.}
 658          *
 659          * Used for instances of {@link ClassTree} representing records.
 660          *
 661          * @since 14
 662          */
 663         @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS,
 664                                      essentialAPI=false)
 665         RECORD(ClassTree.class),
 666 
 667         /**
 668          * Used for instances of {@link RequiresTree} representing
 669          * requires directives in a module declaration.
 670          */
 671         REQUIRES(RequiresTree.class),
 672 
 673         /**
 674          * Used for instances of {@link UsesTree} representing
 675          * uses directives in a module declaration.
 676          */
 677         USES(UsesTree.class),
 678 
 679         /**
 680          * An implementation-reserved node. This is the not the node
 681          * you are looking for.
 682          */
 683         OTHER(null),
 684 
 685         /**
 686          * Used for instances of {@link YieldTree}.
 687          *
 688          * @since 13
 689          */
 690         YIELD(YieldTree.class);
 691 
 692 
 693         Kind(Class<? extends Tree> intf) {
 694             associatedInterface = intf;
 695         }
 696 
 697         /**
 698          * Returns the associated interface type that uses this kind.
 699          * @return the associated interface
 700          */
 701         public Class<? extends Tree> asInterface() {
 702             return associatedInterface;
 703         }
 704 
 705         private final Class<? extends Tree> associatedInterface;
 706     }
 707 
 708     /**
 709      * Returns the kind of this tree.
 710      *
 711      * @return the kind of this tree.
 712      */
 713     Kind getKind();
 714 
 715     /**
 716      * Accept method used to implement the visitor pattern.  The
 717      * visitor pattern is used to implement operations on trees.
 718      *
 719      * @param <R> result type of this operation.
 720      * @param <D> type of additional data.
 721      * @param visitor the visitor to be called
 722      * @param data a value to be passed to the visitor
 723      * @return the result returned from calling the visitor
 724      */
 725     <R,D> R accept(TreeVisitor<R,D> visitor, D data);
 726 }