Tue, 08 Nov 2011 11:51:05 -0800
6921494: provide way to print javac tree tag values
Reviewed-by: jjg, mcimadamore
Contributed-by: vicenterz@yahoo.es
1 /*
2 * Copyright (c) 1999, 2011, 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 */
26 package com.sun.tools.javac.tree;
28 import java.util.*;
30 import java.io.IOException;
31 import java.io.StringWriter;
32 import javax.lang.model.element.Modifier;
33 import javax.lang.model.type.TypeKind;
34 import javax.tools.JavaFileObject;
36 import com.sun.tools.javac.util.*;
37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
38 import com.sun.tools.javac.util.List;
39 import com.sun.tools.javac.code.*;
40 import com.sun.tools.javac.code.Scope.*;
41 import com.sun.tools.javac.code.Symbol.*;
42 import com.sun.source.tree.*;
44 import static com.sun.tools.javac.code.BoundKind.*;
45 import static com.sun.tools.javac.tree.JCTree.Tag.*;
47 /**
48 * Root class for abstract syntax tree nodes. It provides definitions
49 * for specific tree nodes as subclasses nested inside.
50 *
51 * <p>Each subclass is highly standardized. It generally contains
52 * only tree fields for the syntactic subcomponents of the node. Some
53 * classes that represent identifier uses or definitions also define a
54 * Symbol field that denotes the represented identifier. Classes for
55 * non-local jumps also carry the jump target as a field. The root
56 * class Tree itself defines fields for the tree's type and position.
57 * No other fields are kept in a tree node; instead parameters are
58 * passed to methods accessing the node.
59 *
60 * <p>Except for the methods defined by com.sun.source, the only
61 * method defined in subclasses is `visit' which applies a given
62 * visitor to the tree. The actual tree processing is done by visitor
63 * classes in other packages. The abstract class Visitor, as well as
64 * an Factory interface for trees, are defined as inner classes in
65 * Tree.
66 *
67 * <p>To avoid ambiguities with the Tree API in com.sun.source all sub
68 * classes should, by convention, start with JC (javac).
69 *
70 * <p><b>This is NOT part of any supported API.
71 * If you write code that depends on this, you do so at your own risk.
72 * This code and its internal interfaces are subject to change or
73 * deletion without notice.</b>
74 *
75 * @see TreeMaker
76 * @see TreeInfo
77 * @see TreeTranslator
78 * @see Pretty
79 */
80 public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
82 /* Tree tag values, identifying kinds of trees */
83 public enum Tag{
84 /** For methods that return an invalid tag if a given condition is not met
85 */
86 NO_TAG,
88 /** Toplevel nodes, of type TopLevel, representing entire source files.
89 */
90 TOPLEVEL,
92 /** Import clauses, of type Import.
93 */
94 IMPORT,
96 /** Class definitions, of type ClassDef.
97 */
98 CLASSDEF,
100 /** Method definitions, of type MethodDef.
101 */
102 METHODDEF,
104 /** Variable definitions, of type VarDef.
105 */
106 VARDEF,
108 /** The no-op statement ";", of type Skip
109 */
110 SKIP,
112 /** Blocks, of type Block.
113 */
114 BLOCK,
116 /** Do-while loops, of type DoLoop.
117 */
118 DOLOOP,
120 /** While-loops, of type WhileLoop.
121 */
122 WHILELOOP,
124 /** For-loops, of type ForLoop.
125 */
126 FORLOOP,
128 /** Foreach-loops, of type ForeachLoop.
129 */
130 FOREACHLOOP,
132 /** Labelled statements, of type Labelled.
133 */
134 LABELLED,
136 /** Switch statements, of type Switch.
137 */
138 SWITCH,
140 /** Case parts in switch statements, of type Case.
141 */
142 CASE,
144 /** Synchronized statements, of type Synchonized.
145 */
146 SYNCHRONIZED,
148 /** Try statements, of type Try.
149 */
150 TRY,
152 /** Catch clauses in try statements, of type Catch.
153 */
154 CATCH,
156 /** Conditional expressions, of type Conditional.
157 */
158 CONDEXPR,
160 /** Conditional statements, of type If.
161 */
162 IF,
164 /** Expression statements, of type Exec.
165 */
166 EXEC,
168 /** Break statements, of type Break.
169 */
170 BREAK,
172 /** Continue statements, of type Continue.
173 */
174 CONTINUE,
176 /** Return statements, of type Return.
177 */
178 RETURN,
180 /** Throw statements, of type Throw.
181 */
182 THROW,
184 /** Assert statements, of type Assert.
185 */
186 ASSERT,
188 /** Method invocation expressions, of type Apply.
189 */
190 APPLY,
192 /** Class instance creation expressions, of type NewClass.
193 */
194 NEWCLASS,
196 /** Array creation expressions, of type NewArray.
197 */
198 NEWARRAY,
200 /** Parenthesized subexpressions, of type Parens.
201 */
202 PARENS,
204 /** Assignment expressions, of type Assign.
205 */
206 ASSIGN,
208 /** Type cast expressions, of type TypeCast.
209 */
210 TYPECAST,
212 /** Type test expressions, of type TypeTest.
213 */
214 TYPETEST,
216 /** Indexed array expressions, of type Indexed.
217 */
218 INDEXED,
220 /** Selections, of type Select.
221 */
222 SELECT,
224 /** Simple identifiers, of type Ident.
225 */
226 IDENT,
228 /** Literals, of type Literal.
229 */
230 LITERAL,
232 /** Basic type identifiers, of type TypeIdent.
233 */
234 TYPEIDENT,
236 /** Array types, of type TypeArray.
237 */
238 TYPEARRAY,
240 /** Parameterized types, of type TypeApply.
241 */
242 TYPEAPPLY,
244 /** Union types, of type TypeUnion
245 */
246 TYPEUNION,
248 /** Formal type parameters, of type TypeParameter.
249 */
250 TYPEPARAMETER,
252 /** Type argument.
253 */
254 WILDCARD,
256 /** Bound kind: extends, super, exact, or unbound
257 */
258 TYPEBOUNDKIND,
260 /** metadata: Annotation.
261 */
262 ANNOTATION,
264 /** metadata: Modifiers
265 */
266 MODIFIERS,
268 ANNOTATED_TYPE,
270 /** Error trees, of type Erroneous.
271 */
272 ERRONEOUS,
274 /** Unary operators, of type Unary.
275 */
276 POS, // +
277 NEG, // -
278 NOT, // !
279 COMPL, // ~
280 PREINC, // ++ _
281 PREDEC, // -- _
282 POSTINC, // _ ++
283 POSTDEC, // _ --
285 /** unary operator for null reference checks, only used internally.
286 */
287 NULLCHK,
289 /** Binary operators, of type Binary.
290 */
291 OR, // ||
292 AND, // &&
293 BITOR, // |
294 BITXOR, // ^
295 BITAND, // &
296 EQ, // ==
297 NE, // !=
298 LT, // <
299 GT, // >
300 LE, // <=
301 GE, // >=
302 SL, // <<
303 SR, // >>
304 USR, // >>>
305 PLUS, // +
306 MINUS, // -
307 MUL, // *
308 DIV, // /
309 MOD, // %
311 /** Assignment operators, of type Assignop.
312 */
313 BITOR_ASG(BITOR), // |=
314 BITXOR_ASG(BITXOR), // ^=
315 BITAND_ASG(BITAND), // &=
317 SL_ASG(SL), // <<=
318 SR_ASG(SR), // >>=
319 USR_ASG(USR), // >>>=
320 PLUS_ASG(PLUS), // +=
321 MINUS_ASG(MINUS), // -=
322 MUL_ASG(MUL), // *=
323 DIV_ASG(DIV), // /=
324 MOD_ASG(MOD), // %=
326 /** A synthetic let expression, of type LetExpr.
327 */
328 LETEXPR; // ala scheme
330 private Tag noAssignTag;
332 private static int numberOfOperators = MOD.ordinal() - POS.ordinal() + 1;
334 private Tag(Tag noAssignTag) {
335 this.noAssignTag = noAssignTag;
336 }
338 private Tag() { }
340 public static int getNumberOfOperators() {
341 return numberOfOperators;
342 }
344 public Tag noAssignOp() {
345 if (noAssignTag != null)
346 return noAssignTag;
347 throw new AssertionError("noAssignOp() method is not available for non assignment tags");
348 }
350 public boolean isPostUnaryOp() {
351 return (this == POSTINC || this == POSTDEC);
352 }
354 public boolean isIncOrDecUnaryOp() {
355 return (this == PREINC || this == PREDEC || this == POSTINC || this == POSTDEC);
356 }
358 public boolean isAssignop() {
359 return noAssignTag != null;
360 }
362 public int operatorIndex() {
363 return (this.ordinal() - POS.ordinal());
364 }
365 }
367 /* The (encoded) position in the source file. @see util.Position.
368 */
369 public int pos;
371 /* The type of this node.
372 */
373 public Type type;
375 /* The tag of this node -- one of the constants declared above.
376 */
377 public abstract Tag getTag();
379 /* Returns true if the tag of this node is equals to tag.
380 */
381 public boolean hasTag(Tag tag) {
382 return tag == getTag();
383 }
385 /** Convert a tree to a pretty-printed string. */
386 @Override
387 public String toString() {
388 StringWriter s = new StringWriter();
389 try {
390 new Pretty(s, false).printExpr(this);
391 }
392 catch (IOException e) {
393 // should never happen, because StringWriter is defined
394 // never to throw any IOExceptions
395 throw new AssertionError(e);
396 }
397 return s.toString();
398 }
400 /** Set position field and return this tree.
401 */
402 public JCTree setPos(int pos) {
403 this.pos = pos;
404 return this;
405 }
407 /** Set type field and return this tree.
408 */
409 public JCTree setType(Type type) {
410 this.type = type;
411 return this;
412 }
414 /** Visit this tree with a given visitor.
415 */
416 public abstract void accept(Visitor v);
418 public abstract <R,D> R accept(TreeVisitor<R,D> v, D d);
420 /** Return a shallow copy of this tree.
421 */
422 @Override
423 public Object clone() {
424 try {
425 return super.clone();
426 } catch(CloneNotSupportedException e) {
427 throw new RuntimeException(e);
428 }
429 }
431 /** Get a default position for this tree node.
432 */
433 public DiagnosticPosition pos() {
434 return this;
435 }
437 // for default DiagnosticPosition
438 public JCTree getTree() {
439 return this;
440 }
442 // for default DiagnosticPosition
443 public int getStartPosition() {
444 return TreeInfo.getStartPos(this);
445 }
447 // for default DiagnosticPosition
448 public int getPreferredPosition() {
449 return pos;
450 }
452 // for default DiagnosticPosition
453 public int getEndPosition(Map<JCTree, Integer> endPosTable) {
454 return TreeInfo.getEndPos(this, endPosTable);
455 }
457 /**
458 * Everything in one source file is kept in a TopLevel structure.
459 * @param pid The tree representing the package clause.
460 * @param sourcefile The source file name.
461 * @param defs All definitions in this file (ClassDef, Import, and Skip)
462 * @param packge The package it belongs to.
463 * @param namedImportScope A scope for all named imports.
464 * @param starImportScope A scope for all import-on-demands.
465 * @param lineMap Line starting positions, defined only
466 * if option -g is set.
467 * @param docComments A hashtable that stores all documentation comments
468 * indexed by the tree nodes they refer to.
469 * defined only if option -s is set.
470 * @param endPositions A hashtable that stores ending positions of source
471 * ranges indexed by the tree nodes they belong to.
472 * Defined only if option -Xjcov is set.
473 */
474 public static class JCCompilationUnit extends JCTree implements CompilationUnitTree {
475 public List<JCAnnotation> packageAnnotations;
476 public JCExpression pid;
477 public List<JCTree> defs;
478 public JavaFileObject sourcefile;
479 public PackageSymbol packge;
480 public ImportScope namedImportScope;
481 public StarImportScope starImportScope;
482 public Position.LineMap lineMap = null;
483 public Map<JCTree, String> docComments = null;
484 public Map<JCTree, Integer> endPositions = null;
485 protected JCCompilationUnit(List<JCAnnotation> packageAnnotations,
486 JCExpression pid,
487 List<JCTree> defs,
488 JavaFileObject sourcefile,
489 PackageSymbol packge,
490 ImportScope namedImportScope,
491 StarImportScope starImportScope) {
492 this.packageAnnotations = packageAnnotations;
493 this.pid = pid;
494 this.defs = defs;
495 this.sourcefile = sourcefile;
496 this.packge = packge;
497 this.namedImportScope = namedImportScope;
498 this.starImportScope = starImportScope;
499 }
500 @Override
501 public void accept(Visitor v) { v.visitTopLevel(this); }
503 public Kind getKind() { return Kind.COMPILATION_UNIT; }
504 public List<JCAnnotation> getPackageAnnotations() {
505 return packageAnnotations;
506 }
507 public List<JCImport> getImports() {
508 ListBuffer<JCImport> imports = new ListBuffer<JCImport>();
509 for (JCTree tree : defs) {
510 if (tree.hasTag(IMPORT))
511 imports.append((JCImport)tree);
512 else if (!tree.hasTag(SKIP))
513 break;
514 }
515 return imports.toList();
516 }
517 public JCExpression getPackageName() { return pid; }
518 public JavaFileObject getSourceFile() {
519 return sourcefile;
520 }
521 public Position.LineMap getLineMap() {
522 return lineMap;
523 }
524 public List<JCTree> getTypeDecls() {
525 List<JCTree> typeDefs;
526 for (typeDefs = defs; !typeDefs.isEmpty(); typeDefs = typeDefs.tail)
527 if (!typeDefs.head.hasTag(IMPORT))
528 break;
529 return typeDefs;
530 }
531 @Override
532 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
533 return v.visitCompilationUnit(this, d);
534 }
536 @Override
537 public Tag getTag() {
538 return TOPLEVEL;
539 }
540 }
542 /**
543 * An import clause.
544 * @param qualid The imported class(es).
545 */
546 public static class JCImport extends JCTree implements ImportTree {
547 public boolean staticImport;
548 public JCTree qualid;
549 protected JCImport(JCTree qualid, boolean importStatic) {
550 this.qualid = qualid;
551 this.staticImport = importStatic;
552 }
553 @Override
554 public void accept(Visitor v) { v.visitImport(this); }
556 public boolean isStatic() { return staticImport; }
557 public JCTree getQualifiedIdentifier() { return qualid; }
559 public Kind getKind() { return Kind.IMPORT; }
560 @Override
561 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
562 return v.visitImport(this, d);
563 }
565 @Override
566 public Tag getTag() {
567 return IMPORT;
568 }
569 }
571 public static abstract class JCStatement extends JCTree implements StatementTree {
572 @Override
573 public JCStatement setType(Type type) {
574 super.setType(type);
575 return this;
576 }
577 @Override
578 public JCStatement setPos(int pos) {
579 super.setPos(pos);
580 return this;
581 }
582 }
584 public static abstract class JCExpression extends JCTree implements ExpressionTree {
585 @Override
586 public JCExpression setType(Type type) {
587 super.setType(type);
588 return this;
589 }
590 @Override
591 public JCExpression setPos(int pos) {
592 super.setPos(pos);
593 return this;
594 }
595 }
597 /**
598 * A class definition.
599 * @param modifiers the modifiers
600 * @param name the name of the class
601 * @param typarams formal class parameters
602 * @param extending the classes this class extends
603 * @param implementing the interfaces implemented by this class
604 * @param defs all variables and methods defined in this class
605 * @param sym the symbol
606 */
607 public static class JCClassDecl extends JCStatement implements ClassTree {
608 public JCModifiers mods;
609 public Name name;
610 public List<JCTypeParameter> typarams;
611 public JCExpression extending;
612 public List<JCExpression> implementing;
613 public List<JCTree> defs;
614 public ClassSymbol sym;
615 protected JCClassDecl(JCModifiers mods,
616 Name name,
617 List<JCTypeParameter> typarams,
618 JCExpression extending,
619 List<JCExpression> implementing,
620 List<JCTree> defs,
621 ClassSymbol sym)
622 {
623 this.mods = mods;
624 this.name = name;
625 this.typarams = typarams;
626 this.extending = extending;
627 this.implementing = implementing;
628 this.defs = defs;
629 this.sym = sym;
630 }
631 @Override
632 public void accept(Visitor v) { v.visitClassDef(this); }
634 public Kind getKind() {
635 if ((mods.flags & Flags.ANNOTATION) != 0)
636 return Kind.ANNOTATION_TYPE;
637 else if ((mods.flags & Flags.INTERFACE) != 0)
638 return Kind.INTERFACE;
639 else if ((mods.flags & Flags.ENUM) != 0)
640 return Kind.ENUM;
641 else
642 return Kind.CLASS;
643 }
645 public JCModifiers getModifiers() { return mods; }
646 public Name getSimpleName() { return name; }
647 public List<JCTypeParameter> getTypeParameters() {
648 return typarams;
649 }
650 public JCTree getExtendsClause() { return extending; }
651 public List<JCExpression> getImplementsClause() {
652 return implementing;
653 }
654 public List<JCTree> getMembers() {
655 return defs;
656 }
657 @Override
658 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
659 return v.visitClass(this, d);
660 }
662 @Override
663 public Tag getTag() {
664 return CLASSDEF;
665 }
666 }
668 /**
669 * A method definition.
670 * @param modifiers method modifiers
671 * @param name method name
672 * @param restype type of method return value
673 * @param typarams type parameters
674 * @param params value parameters
675 * @param thrown exceptions thrown by this method
676 * @param stats statements in the method
677 * @param sym method symbol
678 */
679 public static class JCMethodDecl extends JCTree implements MethodTree {
680 public JCModifiers mods;
681 public Name name;
682 public JCExpression restype;
683 public List<JCTypeParameter> typarams;
684 public List<JCVariableDecl> params;
685 public List<JCExpression> thrown;
686 public JCBlock body;
687 public JCExpression defaultValue; // for annotation types
688 public MethodSymbol sym;
689 protected JCMethodDecl(JCModifiers mods,
690 Name name,
691 JCExpression restype,
692 List<JCTypeParameter> typarams,
693 List<JCVariableDecl> params,
694 List<JCExpression> thrown,
695 JCBlock body,
696 JCExpression defaultValue,
697 MethodSymbol sym)
698 {
699 this.mods = mods;
700 this.name = name;
701 this.restype = restype;
702 this.typarams = typarams;
703 this.params = params;
704 this.thrown = thrown;
705 this.body = body;
706 this.defaultValue = defaultValue;
707 this.sym = sym;
708 }
709 @Override
710 public void accept(Visitor v) { v.visitMethodDef(this); }
712 public Kind getKind() { return Kind.METHOD; }
713 public JCModifiers getModifiers() { return mods; }
714 public Name getName() { return name; }
715 public JCTree getReturnType() { return restype; }
716 public List<JCTypeParameter> getTypeParameters() {
717 return typarams;
718 }
719 public List<JCVariableDecl> getParameters() {
720 return params;
721 }
722 public List<JCExpression> getThrows() {
723 return thrown;
724 }
725 public JCBlock getBody() { return body; }
726 public JCTree getDefaultValue() { // for annotation types
727 return defaultValue;
728 }
729 @Override
730 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
731 return v.visitMethod(this, d);
732 }
734 @Override
735 public Tag getTag() {
736 return METHODDEF;
737 }
738 }
740 /**
741 * A variable definition.
742 * @param modifiers variable modifiers
743 * @param name variable name
744 * @param vartype type of the variable
745 * @param init variables initial value
746 * @param sym symbol
747 */
748 public static class JCVariableDecl extends JCStatement implements VariableTree {
749 public JCModifiers mods;
750 public Name name;
751 public JCExpression vartype;
752 public JCExpression init;
753 public VarSymbol sym;
754 protected JCVariableDecl(JCModifiers mods,
755 Name name,
756 JCExpression vartype,
757 JCExpression init,
758 VarSymbol sym) {
759 this.mods = mods;
760 this.name = name;
761 this.vartype = vartype;
762 this.init = init;
763 this.sym = sym;
764 }
765 @Override
766 public void accept(Visitor v) { v.visitVarDef(this); }
768 public Kind getKind() { return Kind.VARIABLE; }
769 public JCModifiers getModifiers() { return mods; }
770 public Name getName() { return name; }
771 public JCTree getType() { return vartype; }
772 public JCExpression getInitializer() {
773 return init;
774 }
775 @Override
776 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
777 return v.visitVariable(this, d);
778 }
780 @Override
781 public Tag getTag() {
782 return VARDEF;
783 }
784 }
786 /**
787 * A no-op statement ";".
788 */
789 public static class JCSkip extends JCStatement implements EmptyStatementTree {
790 protected JCSkip() {
791 }
792 @Override
793 public void accept(Visitor v) { v.visitSkip(this); }
795 public Kind getKind() { return Kind.EMPTY_STATEMENT; }
796 @Override
797 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
798 return v.visitEmptyStatement(this, d);
799 }
801 @Override
802 public Tag getTag() {
803 return SKIP;
804 }
805 }
807 /**
808 * A statement block.
809 * @param stats statements
810 * @param flags flags
811 */
812 public static class JCBlock extends JCStatement implements BlockTree {
813 public long flags;
814 public List<JCStatement> stats;
815 /** Position of closing brace, optional. */
816 public int endpos = Position.NOPOS;
817 protected JCBlock(long flags, List<JCStatement> stats) {
818 this.stats = stats;
819 this.flags = flags;
820 }
821 @Override
822 public void accept(Visitor v) { v.visitBlock(this); }
824 public Kind getKind() { return Kind.BLOCK; }
825 public List<JCStatement> getStatements() {
826 return stats;
827 }
828 public boolean isStatic() { return (flags & Flags.STATIC) != 0; }
829 @Override
830 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
831 return v.visitBlock(this, d);
832 }
834 @Override
835 public Tag getTag() {
836 return BLOCK;
837 }
838 }
840 /**
841 * A do loop
842 */
843 public static class JCDoWhileLoop extends JCStatement implements DoWhileLoopTree {
844 public JCStatement body;
845 public JCExpression cond;
846 protected JCDoWhileLoop(JCStatement body, JCExpression cond) {
847 this.body = body;
848 this.cond = cond;
849 }
850 @Override
851 public void accept(Visitor v) { v.visitDoLoop(this); }
853 public Kind getKind() { return Kind.DO_WHILE_LOOP; }
854 public JCExpression getCondition() { return cond; }
855 public JCStatement getStatement() { return body; }
856 @Override
857 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
858 return v.visitDoWhileLoop(this, d);
859 }
861 @Override
862 public Tag getTag() {
863 return DOLOOP;
864 }
865 }
867 /**
868 * A while loop
869 */
870 public static class JCWhileLoop extends JCStatement implements WhileLoopTree {
871 public JCExpression cond;
872 public JCStatement body;
873 protected JCWhileLoop(JCExpression cond, JCStatement body) {
874 this.cond = cond;
875 this.body = body;
876 }
877 @Override
878 public void accept(Visitor v) { v.visitWhileLoop(this); }
880 public Kind getKind() { return Kind.WHILE_LOOP; }
881 public JCExpression getCondition() { return cond; }
882 public JCStatement getStatement() { return body; }
883 @Override
884 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
885 return v.visitWhileLoop(this, d);
886 }
888 @Override
889 public Tag getTag() {
890 return WHILELOOP;
891 }
892 }
894 /**
895 * A for loop.
896 */
897 public static class JCForLoop extends JCStatement implements ForLoopTree {
898 public List<JCStatement> init;
899 public JCExpression cond;
900 public List<JCExpressionStatement> step;
901 public JCStatement body;
902 protected JCForLoop(List<JCStatement> init,
903 JCExpression cond,
904 List<JCExpressionStatement> update,
905 JCStatement body)
906 {
907 this.init = init;
908 this.cond = cond;
909 this.step = update;
910 this.body = body;
911 }
912 @Override
913 public void accept(Visitor v) { v.visitForLoop(this); }
915 public Kind getKind() { return Kind.FOR_LOOP; }
916 public JCExpression getCondition() { return cond; }
917 public JCStatement getStatement() { return body; }
918 public List<JCStatement> getInitializer() {
919 return init;
920 }
921 public List<JCExpressionStatement> getUpdate() {
922 return step;
923 }
924 @Override
925 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
926 return v.visitForLoop(this, d);
927 }
929 @Override
930 public Tag getTag() {
931 return FORLOOP;
932 }
933 }
935 /**
936 * The enhanced for loop.
937 */
938 public static class JCEnhancedForLoop extends JCStatement implements EnhancedForLoopTree {
939 public JCVariableDecl var;
940 public JCExpression expr;
941 public JCStatement body;
942 protected JCEnhancedForLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {
943 this.var = var;
944 this.expr = expr;
945 this.body = body;
946 }
947 @Override
948 public void accept(Visitor v) { v.visitForeachLoop(this); }
950 public Kind getKind() { return Kind.ENHANCED_FOR_LOOP; }
951 public JCVariableDecl getVariable() { return var; }
952 public JCExpression getExpression() { return expr; }
953 public JCStatement getStatement() { return body; }
954 @Override
955 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
956 return v.visitEnhancedForLoop(this, d);
957 }
958 @Override
959 public Tag getTag() {
960 return FOREACHLOOP;
961 }
962 }
964 /**
965 * A labelled expression or statement.
966 */
967 public static class JCLabeledStatement extends JCStatement implements LabeledStatementTree {
968 public Name label;
969 public JCStatement body;
970 protected JCLabeledStatement(Name label, JCStatement body) {
971 this.label = label;
972 this.body = body;
973 }
974 @Override
975 public void accept(Visitor v) { v.visitLabelled(this); }
976 public Kind getKind() { return Kind.LABELED_STATEMENT; }
977 public Name getLabel() { return label; }
978 public JCStatement getStatement() { return body; }
979 @Override
980 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
981 return v.visitLabeledStatement(this, d);
982 }
983 @Override
984 public Tag getTag() {
985 return LABELLED;
986 }
987 }
989 /**
990 * A "switch ( ) { }" construction.
991 */
992 public static class JCSwitch extends JCStatement implements SwitchTree {
993 public JCExpression selector;
994 public List<JCCase> cases;
995 protected JCSwitch(JCExpression selector, List<JCCase> cases) {
996 this.selector = selector;
997 this.cases = cases;
998 }
999 @Override
1000 public void accept(Visitor v) { v.visitSwitch(this); }
1002 public Kind getKind() { return Kind.SWITCH; }
1003 public JCExpression getExpression() { return selector; }
1004 public List<JCCase> getCases() { return cases; }
1005 @Override
1006 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1007 return v.visitSwitch(this, d);
1008 }
1009 @Override
1010 public Tag getTag() {
1011 return SWITCH;
1012 }
1013 }
1015 /**
1016 * A "case :" of a switch.
1017 */
1018 public static class JCCase extends JCStatement implements CaseTree {
1019 public JCExpression pat;
1020 public List<JCStatement> stats;
1021 protected JCCase(JCExpression pat, List<JCStatement> stats) {
1022 this.pat = pat;
1023 this.stats = stats;
1024 }
1025 @Override
1026 public void accept(Visitor v) { v.visitCase(this); }
1028 public Kind getKind() { return Kind.CASE; }
1029 public JCExpression getExpression() { return pat; }
1030 public List<JCStatement> getStatements() { return stats; }
1031 @Override
1032 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1033 return v.visitCase(this, d);
1034 }
1035 @Override
1036 public Tag getTag() {
1037 return CASE;
1038 }
1039 }
1041 /**
1042 * A synchronized block.
1043 */
1044 public static class JCSynchronized extends JCStatement implements SynchronizedTree {
1045 public JCExpression lock;
1046 public JCBlock body;
1047 protected JCSynchronized(JCExpression lock, JCBlock body) {
1048 this.lock = lock;
1049 this.body = body;
1050 }
1051 @Override
1052 public void accept(Visitor v) { v.visitSynchronized(this); }
1054 public Kind getKind() { return Kind.SYNCHRONIZED; }
1055 public JCExpression getExpression() { return lock; }
1056 public JCBlock getBlock() { return body; }
1057 @Override
1058 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1059 return v.visitSynchronized(this, d);
1060 }
1061 @Override
1062 public Tag getTag() {
1063 return SYNCHRONIZED;
1064 }
1065 }
1067 /**
1068 * A "try { } catch ( ) { } finally { }" block.
1069 */
1070 public static class JCTry extends JCStatement implements TryTree {
1071 public JCBlock body;
1072 public List<JCCatch> catchers;
1073 public JCBlock finalizer;
1074 public List<JCTree> resources;
1075 protected JCTry(List<JCTree> resources,
1076 JCBlock body,
1077 List<JCCatch> catchers,
1078 JCBlock finalizer) {
1079 this.body = body;
1080 this.catchers = catchers;
1081 this.finalizer = finalizer;
1082 this.resources = resources;
1083 }
1084 @Override
1085 public void accept(Visitor v) { v.visitTry(this); }
1087 public Kind getKind() { return Kind.TRY; }
1088 public JCBlock getBlock() { return body; }
1089 public List<JCCatch> getCatches() {
1090 return catchers;
1091 }
1092 public JCBlock getFinallyBlock() { return finalizer; }
1093 @Override
1094 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1095 return v.visitTry(this, d);
1096 }
1097 @Override
1098 public List<? extends JCTree> getResources() {
1099 return resources;
1100 }
1101 @Override
1102 public Tag getTag() {
1103 return TRY;
1104 }
1105 }
1107 /**
1108 * A catch block.
1109 */
1110 public static class JCCatch extends JCTree implements CatchTree {
1111 public JCVariableDecl param;
1112 public JCBlock body;
1113 protected JCCatch(JCVariableDecl param, JCBlock body) {
1114 this.param = param;
1115 this.body = body;
1116 }
1117 @Override
1118 public void accept(Visitor v) { v.visitCatch(this); }
1120 public Kind getKind() { return Kind.CATCH; }
1121 public JCVariableDecl getParameter() { return param; }
1122 public JCBlock getBlock() { return body; }
1123 @Override
1124 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1125 return v.visitCatch(this, d);
1126 }
1127 @Override
1128 public Tag getTag() {
1129 return CATCH;
1130 }
1131 }
1133 /**
1134 * A ( ) ? ( ) : ( ) conditional expression
1135 */
1136 public static class JCConditional extends JCExpression implements ConditionalExpressionTree {
1137 public JCExpression cond;
1138 public JCExpression truepart;
1139 public JCExpression falsepart;
1140 protected JCConditional(JCExpression cond,
1141 JCExpression truepart,
1142 JCExpression falsepart)
1143 {
1144 this.cond = cond;
1145 this.truepart = truepart;
1146 this.falsepart = falsepart;
1147 }
1148 @Override
1149 public void accept(Visitor v) { v.visitConditional(this); }
1151 public Kind getKind() { return Kind.CONDITIONAL_EXPRESSION; }
1152 public JCExpression getCondition() { return cond; }
1153 public JCExpression getTrueExpression() { return truepart; }
1154 public JCExpression getFalseExpression() { return falsepart; }
1155 @Override
1156 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1157 return v.visitConditionalExpression(this, d);
1158 }
1159 @Override
1160 public Tag getTag() {
1161 return CONDEXPR;
1162 }
1163 }
1165 /**
1166 * An "if ( ) { } else { }" block
1167 */
1168 public static class JCIf extends JCStatement implements IfTree {
1169 public JCExpression cond;
1170 public JCStatement thenpart;
1171 public JCStatement elsepart;
1172 protected JCIf(JCExpression cond,
1173 JCStatement thenpart,
1174 JCStatement elsepart)
1175 {
1176 this.cond = cond;
1177 this.thenpart = thenpart;
1178 this.elsepart = elsepart;
1179 }
1180 @Override
1181 public void accept(Visitor v) { v.visitIf(this); }
1183 public Kind getKind() { return Kind.IF; }
1184 public JCExpression getCondition() { return cond; }
1185 public JCStatement getThenStatement() { return thenpart; }
1186 public JCStatement getElseStatement() { return elsepart; }
1187 @Override
1188 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1189 return v.visitIf(this, d);
1190 }
1191 @Override
1192 public Tag getTag() {
1193 return IF;
1194 }
1195 }
1197 /**
1198 * an expression statement
1199 * @param expr expression structure
1200 */
1201 public static class JCExpressionStatement extends JCStatement implements ExpressionStatementTree {
1202 public JCExpression expr;
1203 protected JCExpressionStatement(JCExpression expr)
1204 {
1205 this.expr = expr;
1206 }
1207 @Override
1208 public void accept(Visitor v) { v.visitExec(this); }
1210 public Kind getKind() { return Kind.EXPRESSION_STATEMENT; }
1211 public JCExpression getExpression() { return expr; }
1212 @Override
1213 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1214 return v.visitExpressionStatement(this, d);
1215 }
1216 @Override
1217 public Tag getTag() {
1218 return EXEC;
1219 }
1221 /** Convert a expression-statement tree to a pretty-printed string. */
1222 @Override
1223 public String toString() {
1224 StringWriter s = new StringWriter();
1225 try {
1226 new Pretty(s, false).printStat(this);
1227 }
1228 catch (IOException e) {
1229 // should never happen, because StringWriter is defined
1230 // never to throw any IOExceptions
1231 throw new AssertionError(e);
1232 }
1233 return s.toString();
1234 }
1235 }
1237 /**
1238 * A break from a loop or switch.
1239 */
1240 public static class JCBreak extends JCStatement implements BreakTree {
1241 public Name label;
1242 public JCTree target;
1243 protected JCBreak(Name label, JCTree target) {
1244 this.label = label;
1245 this.target = target;
1246 }
1247 @Override
1248 public void accept(Visitor v) { v.visitBreak(this); }
1250 public Kind getKind() { return Kind.BREAK; }
1251 public Name getLabel() { return label; }
1252 @Override
1253 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1254 return v.visitBreak(this, d);
1255 }
1256 @Override
1257 public Tag getTag() {
1258 return BREAK;
1259 }
1260 }
1262 /**
1263 * A continue of a loop.
1264 */
1265 public static class JCContinue extends JCStatement implements ContinueTree {
1266 public Name label;
1267 public JCTree target;
1268 protected JCContinue(Name label, JCTree target) {
1269 this.label = label;
1270 this.target = target;
1271 }
1272 @Override
1273 public void accept(Visitor v) { v.visitContinue(this); }
1275 public Kind getKind() { return Kind.CONTINUE; }
1276 public Name getLabel() { return label; }
1277 @Override
1278 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1279 return v.visitContinue(this, d);
1280 }
1281 @Override
1282 public Tag getTag() {
1283 return CONTINUE;
1284 }
1285 }
1287 /**
1288 * A return statement.
1289 */
1290 public static class JCReturn extends JCStatement implements ReturnTree {
1291 public JCExpression expr;
1292 protected JCReturn(JCExpression expr) {
1293 this.expr = expr;
1294 }
1295 @Override
1296 public void accept(Visitor v) { v.visitReturn(this); }
1298 public Kind getKind() { return Kind.RETURN; }
1299 public JCExpression getExpression() { return expr; }
1300 @Override
1301 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1302 return v.visitReturn(this, d);
1303 }
1304 @Override
1305 public Tag getTag() {
1306 return RETURN;
1307 }
1308 }
1310 /**
1311 * A throw statement.
1312 */
1313 public static class JCThrow extends JCStatement implements ThrowTree {
1314 public JCExpression expr;
1315 protected JCThrow(JCTree expr) {
1316 this.expr = (JCExpression)expr;
1317 }
1318 @Override
1319 public void accept(Visitor v) { v.visitThrow(this); }
1321 public Kind getKind() { return Kind.THROW; }
1322 public JCExpression getExpression() { return expr; }
1323 @Override
1324 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1325 return v.visitThrow(this, d);
1326 }
1327 @Override
1328 public Tag getTag() {
1329 return THROW;
1330 }
1331 }
1333 /**
1334 * An assert statement.
1335 */
1336 public static class JCAssert extends JCStatement implements AssertTree {
1337 public JCExpression cond;
1338 public JCExpression detail;
1339 protected JCAssert(JCExpression cond, JCExpression detail) {
1340 this.cond = cond;
1341 this.detail = detail;
1342 }
1343 @Override
1344 public void accept(Visitor v) { v.visitAssert(this); }
1346 public Kind getKind() { return Kind.ASSERT; }
1347 public JCExpression getCondition() { return cond; }
1348 public JCExpression getDetail() { return detail; }
1349 @Override
1350 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1351 return v.visitAssert(this, d);
1352 }
1353 @Override
1354 public Tag getTag() {
1355 return ASSERT;
1356 }
1357 }
1359 /**
1360 * A method invocation
1361 */
1362 public static class JCMethodInvocation extends JCExpression implements MethodInvocationTree {
1363 public List<JCExpression> typeargs;
1364 public JCExpression meth;
1365 public List<JCExpression> args;
1366 public Type varargsElement;
1367 protected JCMethodInvocation(List<JCExpression> typeargs,
1368 JCExpression meth,
1369 List<JCExpression> args)
1370 {
1371 this.typeargs = (typeargs == null) ? List.<JCExpression>nil()
1372 : typeargs;
1373 this.meth = meth;
1374 this.args = args;
1375 }
1376 @Override
1377 public void accept(Visitor v) { v.visitApply(this); }
1379 public Kind getKind() { return Kind.METHOD_INVOCATION; }
1380 public List<JCExpression> getTypeArguments() {
1381 return typeargs;
1382 }
1383 public JCExpression getMethodSelect() { return meth; }
1384 public List<JCExpression> getArguments() {
1385 return args;
1386 }
1387 @Override
1388 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1389 return v.visitMethodInvocation(this, d);
1390 }
1391 @Override
1392 public JCMethodInvocation setType(Type type) {
1393 super.setType(type);
1394 return this;
1395 }
1396 @Override
1397 public Tag getTag() {
1398 return(APPLY);
1399 }
1400 }
1402 /**
1403 * A new(...) operation.
1404 */
1405 public static class JCNewClass extends JCExpression implements NewClassTree {
1406 public JCExpression encl;
1407 public List<JCExpression> typeargs;
1408 public JCExpression clazz;
1409 public List<JCExpression> args;
1410 public JCClassDecl def;
1411 public Symbol constructor;
1412 public Type varargsElement;
1413 public Type constructorType;
1414 protected JCNewClass(JCExpression encl,
1415 List<JCExpression> typeargs,
1416 JCExpression clazz,
1417 List<JCExpression> args,
1418 JCClassDecl def)
1419 {
1420 this.encl = encl;
1421 this.typeargs = (typeargs == null) ? List.<JCExpression>nil()
1422 : typeargs;
1423 this.clazz = clazz;
1424 this.args = args;
1425 this.def = def;
1426 }
1427 @Override
1428 public void accept(Visitor v) { v.visitNewClass(this); }
1430 public Kind getKind() { return Kind.NEW_CLASS; }
1431 public JCExpression getEnclosingExpression() { // expr.new C< ... > ( ... )
1432 return encl;
1433 }
1434 public List<JCExpression> getTypeArguments() {
1435 return typeargs;
1436 }
1437 public JCExpression getIdentifier() { return clazz; }
1438 public List<JCExpression> getArguments() {
1439 return args;
1440 }
1441 public JCClassDecl getClassBody() { return def; }
1442 @Override
1443 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1444 return v.visitNewClass(this, d);
1445 }
1446 @Override
1447 public Tag getTag() {
1448 return NEWCLASS;
1449 }
1450 }
1452 /**
1453 * A new[...] operation.
1454 */
1455 public static class JCNewArray extends JCExpression implements NewArrayTree {
1456 public JCExpression elemtype;
1457 public List<JCExpression> dims;
1458 public List<JCExpression> elems;
1459 protected JCNewArray(JCExpression elemtype,
1460 List<JCExpression> dims,
1461 List<JCExpression> elems)
1462 {
1463 this.elemtype = elemtype;
1464 this.dims = dims;
1465 this.elems = elems;
1466 }
1467 @Override
1468 public void accept(Visitor v) { v.visitNewArray(this); }
1470 public Kind getKind() { return Kind.NEW_ARRAY; }
1471 public JCExpression getType() { return elemtype; }
1472 public List<JCExpression> getDimensions() {
1473 return dims;
1474 }
1475 public List<JCExpression> getInitializers() {
1476 return elems;
1477 }
1478 @Override
1479 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1480 return v.visitNewArray(this, d);
1481 }
1482 @Override
1483 public Tag getTag() {
1484 return NEWARRAY;
1485 }
1486 }
1488 /**
1489 * A parenthesized subexpression ( ... )
1490 */
1491 public static class JCParens extends JCExpression implements ParenthesizedTree {
1492 public JCExpression expr;
1493 protected JCParens(JCExpression expr) {
1494 this.expr = expr;
1495 }
1496 @Override
1497 public void accept(Visitor v) { v.visitParens(this); }
1499 public Kind getKind() { return Kind.PARENTHESIZED; }
1500 public JCExpression getExpression() { return expr; }
1501 @Override
1502 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1503 return v.visitParenthesized(this, d);
1504 }
1505 @Override
1506 public Tag getTag() {
1507 return PARENS;
1508 }
1509 }
1511 /**
1512 * A assignment with "=".
1513 */
1514 public static class JCAssign extends JCExpression implements AssignmentTree {
1515 public JCExpression lhs;
1516 public JCExpression rhs;
1517 protected JCAssign(JCExpression lhs, JCExpression rhs) {
1518 this.lhs = lhs;
1519 this.rhs = rhs;
1520 }
1521 @Override
1522 public void accept(Visitor v) { v.visitAssign(this); }
1524 public Kind getKind() { return Kind.ASSIGNMENT; }
1525 public JCExpression getVariable() { return lhs; }
1526 public JCExpression getExpression() { return rhs; }
1527 @Override
1528 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1529 return v.visitAssignment(this, d);
1530 }
1531 @Override
1532 public Tag getTag() {
1533 return ASSIGN;
1534 }
1535 }
1537 /**
1538 * An assignment with "+=", "|=" ...
1539 */
1540 public static class JCAssignOp extends JCExpression implements CompoundAssignmentTree {
1541 private Tag opcode;
1542 public JCExpression lhs;
1543 public JCExpression rhs;
1544 public Symbol operator;
1545 protected JCAssignOp(Tag opcode, JCTree lhs, JCTree rhs, Symbol operator) {
1546 this.opcode = opcode;
1547 this.lhs = (JCExpression)lhs;
1548 this.rhs = (JCExpression)rhs;
1549 this.operator = operator;
1550 }
1551 @Override
1552 public void accept(Visitor v) { v.visitAssignop(this); }
1554 public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
1555 public JCExpression getVariable() { return lhs; }
1556 public JCExpression getExpression() { return rhs; }
1557 public Symbol getOperator() {
1558 return operator;
1559 }
1560 @Override
1561 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1562 return v.visitCompoundAssignment(this, d);
1563 }
1564 @Override
1565 public Tag getTag() {
1566 return opcode;
1567 }
1568 }
1570 /**
1571 * A unary operation.
1572 */
1573 public static class JCUnary extends JCExpression implements UnaryTree {
1574 private Tag opcode;
1575 public JCExpression arg;
1576 public Symbol operator;
1577 protected JCUnary(Tag opcode, JCExpression arg) {
1578 this.opcode = opcode;
1579 this.arg = arg;
1580 }
1581 @Override
1582 public void accept(Visitor v) { v.visitUnary(this); }
1584 public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
1585 public JCExpression getExpression() { return arg; }
1586 public Symbol getOperator() {
1587 return operator;
1588 }
1589 @Override
1590 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1591 return v.visitUnary(this, d);
1592 }
1593 @Override
1594 public Tag getTag() {
1595 return opcode;
1596 }
1598 public void setTag(Tag tag) {
1599 opcode = tag;
1600 }
1601 }
1603 /**
1604 * A binary operation.
1605 */
1606 public static class JCBinary extends JCExpression implements BinaryTree {
1607 private Tag opcode;
1608 public JCExpression lhs;
1609 public JCExpression rhs;
1610 public Symbol operator;
1611 protected JCBinary(Tag opcode,
1612 JCExpression lhs,
1613 JCExpression rhs,
1614 Symbol operator) {
1615 this.opcode = opcode;
1616 this.lhs = lhs;
1617 this.rhs = rhs;
1618 this.operator = operator;
1619 }
1620 @Override
1621 public void accept(Visitor v) { v.visitBinary(this); }
1623 public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
1624 public JCExpression getLeftOperand() { return lhs; }
1625 public JCExpression getRightOperand() { return rhs; }
1626 public Symbol getOperator() {
1627 return operator;
1628 }
1629 @Override
1630 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1631 return v.visitBinary(this, d);
1632 }
1633 @Override
1634 public Tag getTag() {
1635 return opcode;
1636 }
1637 }
1639 /**
1640 * A type cast.
1641 */
1642 public static class JCTypeCast extends JCExpression implements TypeCastTree {
1643 public JCTree clazz;
1644 public JCExpression expr;
1645 protected JCTypeCast(JCTree clazz, JCExpression expr) {
1646 this.clazz = clazz;
1647 this.expr = expr;
1648 }
1649 @Override
1650 public void accept(Visitor v) { v.visitTypeCast(this); }
1652 public Kind getKind() { return Kind.TYPE_CAST; }
1653 public JCTree getType() { return clazz; }
1654 public JCExpression getExpression() { return expr; }
1655 @Override
1656 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1657 return v.visitTypeCast(this, d);
1658 }
1659 @Override
1660 public Tag getTag() {
1661 return TYPECAST;
1662 }
1663 }
1665 /**
1666 * A type test.
1667 */
1668 public static class JCInstanceOf extends JCExpression implements InstanceOfTree {
1669 public JCExpression expr;
1670 public JCTree clazz;
1671 protected JCInstanceOf(JCExpression expr, JCTree clazz) {
1672 this.expr = expr;
1673 this.clazz = clazz;
1674 }
1675 @Override
1676 public void accept(Visitor v) { v.visitTypeTest(this); }
1678 public Kind getKind() { return Kind.INSTANCE_OF; }
1679 public JCTree getType() { return clazz; }
1680 public JCExpression getExpression() { return expr; }
1681 @Override
1682 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1683 return v.visitInstanceOf(this, d);
1684 }
1685 @Override
1686 public Tag getTag() {
1687 return TYPETEST;
1688 }
1689 }
1691 /**
1692 * An array selection
1693 */
1694 public static class JCArrayAccess extends JCExpression implements ArrayAccessTree {
1695 public JCExpression indexed;
1696 public JCExpression index;
1697 protected JCArrayAccess(JCExpression indexed, JCExpression index) {
1698 this.indexed = indexed;
1699 this.index = index;
1700 }
1701 @Override
1702 public void accept(Visitor v) { v.visitIndexed(this); }
1704 public Kind getKind() { return Kind.ARRAY_ACCESS; }
1705 public JCExpression getExpression() { return indexed; }
1706 public JCExpression getIndex() { return index; }
1707 @Override
1708 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1709 return v.visitArrayAccess(this, d);
1710 }
1711 @Override
1712 public Tag getTag() {
1713 return INDEXED;
1714 }
1715 }
1717 /**
1718 * Selects through packages and classes
1719 * @param selected selected Tree hierarchie
1720 * @param selector name of field to select thru
1721 * @param sym symbol of the selected class
1722 */
1723 public static class JCFieldAccess extends JCExpression implements MemberSelectTree {
1724 public JCExpression selected;
1725 public Name name;
1726 public Symbol sym;
1727 protected JCFieldAccess(JCExpression selected, Name name, Symbol sym) {
1728 this.selected = selected;
1729 this.name = name;
1730 this.sym = sym;
1731 }
1732 @Override
1733 public void accept(Visitor v) { v.visitSelect(this); }
1735 public Kind getKind() { return Kind.MEMBER_SELECT; }
1736 public JCExpression getExpression() { return selected; }
1737 @Override
1738 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1739 return v.visitMemberSelect(this, d);
1740 }
1741 public Name getIdentifier() { return name; }
1742 @Override
1743 public Tag getTag() {
1744 return SELECT;
1745 }
1746 }
1748 /**
1749 * An identifier
1750 * @param idname the name
1751 * @param sym the symbol
1752 */
1753 public static class JCIdent extends JCExpression implements IdentifierTree {
1754 public Name name;
1755 public Symbol sym;
1756 protected JCIdent(Name name, Symbol sym) {
1757 this.name = name;
1758 this.sym = sym;
1759 }
1760 @Override
1761 public void accept(Visitor v) { v.visitIdent(this); }
1763 public Kind getKind() { return Kind.IDENTIFIER; }
1764 public Name getName() { return name; }
1765 @Override
1766 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1767 return v.visitIdentifier(this, d);
1768 }
1769 @Override
1770 public Tag getTag() {
1771 return IDENT;
1772 }
1773 }
1775 /**
1776 * A constant value given literally.
1777 * @param value value representation
1778 */
1779 public static class JCLiteral extends JCExpression implements LiteralTree {
1780 public int typetag;
1781 public Object value;
1782 protected JCLiteral(int typetag, Object value) {
1783 this.typetag = typetag;
1784 this.value = value;
1785 }
1786 @Override
1787 public void accept(Visitor v) { v.visitLiteral(this); }
1789 public Kind getKind() {
1790 switch (typetag) {
1791 case TypeTags.INT:
1792 return Kind.INT_LITERAL;
1793 case TypeTags.LONG:
1794 return Kind.LONG_LITERAL;
1795 case TypeTags.FLOAT:
1796 return Kind.FLOAT_LITERAL;
1797 case TypeTags.DOUBLE:
1798 return Kind.DOUBLE_LITERAL;
1799 case TypeTags.BOOLEAN:
1800 return Kind.BOOLEAN_LITERAL;
1801 case TypeTags.CHAR:
1802 return Kind.CHAR_LITERAL;
1803 case TypeTags.CLASS:
1804 return Kind.STRING_LITERAL;
1805 case TypeTags.BOT:
1806 return Kind.NULL_LITERAL;
1807 default:
1808 throw new AssertionError("unknown literal kind " + this);
1809 }
1810 }
1811 public Object getValue() {
1812 switch (typetag) {
1813 case TypeTags.BOOLEAN:
1814 int bi = (Integer) value;
1815 return (bi != 0);
1816 case TypeTags.CHAR:
1817 int ci = (Integer) value;
1818 char c = (char) ci;
1819 if (c != ci)
1820 throw new AssertionError("bad value for char literal");
1821 return c;
1822 default:
1823 return value;
1824 }
1825 }
1826 @Override
1827 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1828 return v.visitLiteral(this, d);
1829 }
1830 @Override
1831 public JCLiteral setType(Type type) {
1832 super.setType(type);
1833 return this;
1834 }
1835 @Override
1836 public Tag getTag() {
1837 return LITERAL;
1838 }
1839 }
1841 /**
1842 * Identifies a basic type.
1843 * @param tag the basic type id
1844 * @see TypeTags
1845 */
1846 public static class JCPrimitiveTypeTree extends JCExpression implements PrimitiveTypeTree {
1847 public int typetag;
1848 protected JCPrimitiveTypeTree(int typetag) {
1849 this.typetag = typetag;
1850 }
1851 @Override
1852 public void accept(Visitor v) { v.visitTypeIdent(this); }
1854 public Kind getKind() { return Kind.PRIMITIVE_TYPE; }
1855 public TypeKind getPrimitiveTypeKind() {
1856 switch (typetag) {
1857 case TypeTags.BOOLEAN:
1858 return TypeKind.BOOLEAN;
1859 case TypeTags.BYTE:
1860 return TypeKind.BYTE;
1861 case TypeTags.SHORT:
1862 return TypeKind.SHORT;
1863 case TypeTags.INT:
1864 return TypeKind.INT;
1865 case TypeTags.LONG:
1866 return TypeKind.LONG;
1867 case TypeTags.CHAR:
1868 return TypeKind.CHAR;
1869 case TypeTags.FLOAT:
1870 return TypeKind.FLOAT;
1871 case TypeTags.DOUBLE:
1872 return TypeKind.DOUBLE;
1873 case TypeTags.VOID:
1874 return TypeKind.VOID;
1875 default:
1876 throw new AssertionError("unknown primitive type " + this);
1877 }
1878 }
1879 @Override
1880 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1881 return v.visitPrimitiveType(this, d);
1882 }
1883 @Override
1884 public Tag getTag() {
1885 return TYPEIDENT;
1886 }
1887 }
1889 /**
1890 * An array type, A[]
1891 */
1892 public static class JCArrayTypeTree extends JCExpression implements ArrayTypeTree {
1893 public JCExpression elemtype;
1894 protected JCArrayTypeTree(JCExpression elemtype) {
1895 this.elemtype = elemtype;
1896 }
1897 @Override
1898 public void accept(Visitor v) { v.visitTypeArray(this); }
1900 public Kind getKind() { return Kind.ARRAY_TYPE; }
1901 public JCTree getType() { return elemtype; }
1902 @Override
1903 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1904 return v.visitArrayType(this, d);
1905 }
1906 @Override
1907 public Tag getTag() {
1908 return TYPEARRAY;
1909 }
1910 }
1912 /**
1913 * A parameterized type, T<...>
1914 */
1915 public static class JCTypeApply extends JCExpression implements ParameterizedTypeTree {
1916 public JCExpression clazz;
1917 public List<JCExpression> arguments;
1918 protected JCTypeApply(JCExpression clazz, List<JCExpression> arguments) {
1919 this.clazz = clazz;
1920 this.arguments = arguments;
1921 }
1922 @Override
1923 public void accept(Visitor v) { v.visitTypeApply(this); }
1925 public Kind getKind() { return Kind.PARAMETERIZED_TYPE; }
1926 public JCTree getType() { return clazz; }
1927 public List<JCExpression> getTypeArguments() {
1928 return arguments;
1929 }
1930 @Override
1931 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1932 return v.visitParameterizedType(this, d);
1933 }
1934 @Override
1935 public Tag getTag() {
1936 return TYPEAPPLY;
1937 }
1938 }
1940 /**
1941 * A union type, T1 | T2 | ... Tn (used in multicatch statements)
1942 */
1943 public static class JCTypeUnion extends JCExpression implements UnionTypeTree {
1945 public List<JCExpression> alternatives;
1947 protected JCTypeUnion(List<JCExpression> components) {
1948 this.alternatives = components;
1949 }
1950 @Override
1951 public void accept(Visitor v) { v.visitTypeUnion(this); }
1953 public Kind getKind() { return Kind.UNION_TYPE; }
1955 public List<JCExpression> getTypeAlternatives() {
1956 return alternatives;
1957 }
1958 @Override
1959 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1960 return v.visitUnionType(this, d);
1961 }
1962 @Override
1963 public Tag getTag() {
1964 return TYPEUNION;
1965 }
1966 }
1968 /**
1969 * A formal class parameter.
1970 * @param name name
1971 * @param bounds bounds
1972 */
1973 public static class JCTypeParameter extends JCTree implements TypeParameterTree {
1974 public Name name;
1975 public List<JCExpression> bounds;
1976 protected JCTypeParameter(Name name, List<JCExpression> bounds) {
1977 this.name = name;
1978 this.bounds = bounds;
1979 }
1980 @Override
1981 public void accept(Visitor v) { v.visitTypeParameter(this); }
1983 public Kind getKind() { return Kind.TYPE_PARAMETER; }
1984 public Name getName() { return name; }
1985 public List<JCExpression> getBounds() {
1986 return bounds;
1987 }
1988 @Override
1989 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1990 return v.visitTypeParameter(this, d);
1991 }
1992 @Override
1993 public Tag getTag() {
1994 return TYPEPARAMETER;
1995 }
1996 }
1998 public static class JCWildcard extends JCExpression implements WildcardTree {
1999 public TypeBoundKind kind;
2000 public JCTree inner;
2001 protected JCWildcard(TypeBoundKind kind, JCTree inner) {
2002 kind.getClass(); // null-check
2003 this.kind = kind;
2004 this.inner = inner;
2005 }
2006 @Override
2007 public void accept(Visitor v) { v.visitWildcard(this); }
2009 public Kind getKind() {
2010 switch (kind.kind) {
2011 case UNBOUND:
2012 return Kind.UNBOUNDED_WILDCARD;
2013 case EXTENDS:
2014 return Kind.EXTENDS_WILDCARD;
2015 case SUPER:
2016 return Kind.SUPER_WILDCARD;
2017 default:
2018 throw new AssertionError("Unknown wildcard bound " + kind);
2019 }
2020 }
2021 public JCTree getBound() { return inner; }
2022 @Override
2023 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2024 return v.visitWildcard(this, d);
2025 }
2026 @Override
2027 public Tag getTag() {
2028 return WILDCARD;
2029 }
2030 }
2032 public static class TypeBoundKind extends JCTree {
2033 public BoundKind kind;
2034 protected TypeBoundKind(BoundKind kind) {
2035 this.kind = kind;
2036 }
2037 @Override
2038 public void accept(Visitor v) { v.visitTypeBoundKind(this); }
2040 public Kind getKind() {
2041 throw new AssertionError("TypeBoundKind is not part of a public API");
2042 }
2043 @Override
2044 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2045 throw new AssertionError("TypeBoundKind is not part of a public API");
2046 }
2047 @Override
2048 public Tag getTag() {
2049 return TYPEBOUNDKIND;
2050 }
2051 }
2053 public static class JCAnnotation extends JCExpression implements AnnotationTree {
2054 public JCTree annotationType;
2055 public List<JCExpression> args;
2056 protected JCAnnotation(JCTree annotationType, List<JCExpression> args) {
2057 this.annotationType = annotationType;
2058 this.args = args;
2059 }
2060 @Override
2061 public void accept(Visitor v) { v.visitAnnotation(this); }
2063 public Kind getKind() { return Kind.ANNOTATION; }
2064 public JCTree getAnnotationType() { return annotationType; }
2065 public List<JCExpression> getArguments() {
2066 return args;
2067 }
2068 @Override
2069 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2070 return v.visitAnnotation(this, d);
2071 }
2072 @Override
2073 public Tag getTag() {
2074 return ANNOTATION;
2075 }
2076 }
2078 public static class JCModifiers extends JCTree implements com.sun.source.tree.ModifiersTree {
2079 public long flags;
2080 public List<JCAnnotation> annotations;
2081 protected JCModifiers(long flags, List<JCAnnotation> annotations) {
2082 this.flags = flags;
2083 this.annotations = annotations;
2084 }
2085 @Override
2086 public void accept(Visitor v) { v.visitModifiers(this); }
2088 public Kind getKind() { return Kind.MODIFIERS; }
2089 public Set<Modifier> getFlags() {
2090 return Flags.asModifierSet(flags);
2091 }
2092 public List<JCAnnotation> getAnnotations() {
2093 return annotations;
2094 }
2095 @Override
2096 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2097 return v.visitModifiers(this, d);
2098 }
2099 @Override
2100 public Tag getTag() {
2101 return MODIFIERS;
2102 }
2103 }
2105 public static class JCErroneous extends JCExpression
2106 implements com.sun.source.tree.ErroneousTree {
2107 public List<? extends JCTree> errs;
2108 protected JCErroneous(List<? extends JCTree> errs) {
2109 this.errs = errs;
2110 }
2111 @Override
2112 public void accept(Visitor v) { v.visitErroneous(this); }
2114 public Kind getKind() { return Kind.ERRONEOUS; }
2116 public List<? extends JCTree> getErrorTrees() {
2117 return errs;
2118 }
2120 @Override
2121 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2122 return v.visitErroneous(this, d);
2123 }
2124 @Override
2125 public Tag getTag() {
2126 return ERRONEOUS;
2127 }
2128 }
2130 /** (let int x = 3; in x+2) */
2131 public static class LetExpr extends JCExpression {
2132 public List<JCVariableDecl> defs;
2133 public JCTree expr;
2134 protected LetExpr(List<JCVariableDecl> defs, JCTree expr) {
2135 this.defs = defs;
2136 this.expr = expr;
2137 }
2138 @Override
2139 public void accept(Visitor v) { v.visitLetExpr(this); }
2141 public Kind getKind() {
2142 throw new AssertionError("LetExpr is not part of a public API");
2143 }
2144 @Override
2145 public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2146 throw new AssertionError("LetExpr is not part of a public API");
2147 }
2148 @Override
2149 public Tag getTag() {
2150 return LETEXPR;
2151 }
2152 }
2154 /** An interface for tree factories
2155 */
2156 public interface Factory {
2157 JCCompilationUnit TopLevel(List<JCAnnotation> packageAnnotations,
2158 JCExpression pid,
2159 List<JCTree> defs);
2160 JCImport Import(JCTree qualid, boolean staticImport);
2161 JCClassDecl ClassDef(JCModifiers mods,
2162 Name name,
2163 List<JCTypeParameter> typarams,
2164 JCExpression extending,
2165 List<JCExpression> implementing,
2166 List<JCTree> defs);
2167 JCMethodDecl MethodDef(JCModifiers mods,
2168 Name name,
2169 JCExpression restype,
2170 List<JCTypeParameter> typarams,
2171 List<JCVariableDecl> params,
2172 List<JCExpression> thrown,
2173 JCBlock body,
2174 JCExpression defaultValue);
2175 JCVariableDecl VarDef(JCModifiers mods,
2176 Name name,
2177 JCExpression vartype,
2178 JCExpression init);
2179 JCSkip Skip();
2180 JCBlock Block(long flags, List<JCStatement> stats);
2181 JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond);
2182 JCWhileLoop WhileLoop(JCExpression cond, JCStatement body);
2183 JCForLoop ForLoop(List<JCStatement> init,
2184 JCExpression cond,
2185 List<JCExpressionStatement> step,
2186 JCStatement body);
2187 JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body);
2188 JCLabeledStatement Labelled(Name label, JCStatement body);
2189 JCSwitch Switch(JCExpression selector, List<JCCase> cases);
2190 JCCase Case(JCExpression pat, List<JCStatement> stats);
2191 JCSynchronized Synchronized(JCExpression lock, JCBlock body);
2192 JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer);
2193 JCTry Try(List<JCTree> resources,
2194 JCBlock body,
2195 List<JCCatch> catchers,
2196 JCBlock finalizer);
2197 JCCatch Catch(JCVariableDecl param, JCBlock body);
2198 JCConditional Conditional(JCExpression cond,
2199 JCExpression thenpart,
2200 JCExpression elsepart);
2201 JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart);
2202 JCExpressionStatement Exec(JCExpression expr);
2203 JCBreak Break(Name label);
2204 JCContinue Continue(Name label);
2205 JCReturn Return(JCExpression expr);
2206 JCThrow Throw(JCTree expr);
2207 JCAssert Assert(JCExpression cond, JCExpression detail);
2208 JCMethodInvocation Apply(List<JCExpression> typeargs,
2209 JCExpression fn,
2210 List<JCExpression> args);
2211 JCNewClass NewClass(JCExpression encl,
2212 List<JCExpression> typeargs,
2213 JCExpression clazz,
2214 List<JCExpression> args,
2215 JCClassDecl def);
2216 JCNewArray NewArray(JCExpression elemtype,
2217 List<JCExpression> dims,
2218 List<JCExpression> elems);
2219 JCParens Parens(JCExpression expr);
2220 JCAssign Assign(JCExpression lhs, JCExpression rhs);
2221 JCAssignOp Assignop(Tag opcode, JCTree lhs, JCTree rhs);
2222 JCUnary Unary(Tag opcode, JCExpression arg);
2223 JCBinary Binary(Tag opcode, JCExpression lhs, JCExpression rhs);
2224 JCTypeCast TypeCast(JCTree expr, JCExpression type);
2225 JCInstanceOf TypeTest(JCExpression expr, JCTree clazz);
2226 JCArrayAccess Indexed(JCExpression indexed, JCExpression index);
2227 JCFieldAccess Select(JCExpression selected, Name selector);
2228 JCIdent Ident(Name idname);
2229 JCLiteral Literal(int tag, Object value);
2230 JCPrimitiveTypeTree TypeIdent(int typetag);
2231 JCArrayTypeTree TypeArray(JCExpression elemtype);
2232 JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments);
2233 JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds);
2234 JCWildcard Wildcard(TypeBoundKind kind, JCTree type);
2235 TypeBoundKind TypeBoundKind(BoundKind kind);
2236 JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args);
2237 JCModifiers Modifiers(long flags, List<JCAnnotation> annotations);
2238 JCErroneous Erroneous(List<? extends JCTree> errs);
2239 LetExpr LetExpr(List<JCVariableDecl> defs, JCTree expr);
2240 }
2242 /** A generic visitor class for trees.
2243 */
2244 public static abstract class Visitor {
2245 public void visitTopLevel(JCCompilationUnit that) { visitTree(that); }
2246 public void visitImport(JCImport that) { visitTree(that); }
2247 public void visitClassDef(JCClassDecl that) { visitTree(that); }
2248 public void visitMethodDef(JCMethodDecl that) { visitTree(that); }
2249 public void visitVarDef(JCVariableDecl that) { visitTree(that); }
2250 public void visitSkip(JCSkip that) { visitTree(that); }
2251 public void visitBlock(JCBlock that) { visitTree(that); }
2252 public void visitDoLoop(JCDoWhileLoop that) { visitTree(that); }
2253 public void visitWhileLoop(JCWhileLoop that) { visitTree(that); }
2254 public void visitForLoop(JCForLoop that) { visitTree(that); }
2255 public void visitForeachLoop(JCEnhancedForLoop that) { visitTree(that); }
2256 public void visitLabelled(JCLabeledStatement that) { visitTree(that); }
2257 public void visitSwitch(JCSwitch that) { visitTree(that); }
2258 public void visitCase(JCCase that) { visitTree(that); }
2259 public void visitSynchronized(JCSynchronized that) { visitTree(that); }
2260 public void visitTry(JCTry that) { visitTree(that); }
2261 public void visitCatch(JCCatch that) { visitTree(that); }
2262 public void visitConditional(JCConditional that) { visitTree(that); }
2263 public void visitIf(JCIf that) { visitTree(that); }
2264 public void visitExec(JCExpressionStatement that) { visitTree(that); }
2265 public void visitBreak(JCBreak that) { visitTree(that); }
2266 public void visitContinue(JCContinue that) { visitTree(that); }
2267 public void visitReturn(JCReturn that) { visitTree(that); }
2268 public void visitThrow(JCThrow that) { visitTree(that); }
2269 public void visitAssert(JCAssert that) { visitTree(that); }
2270 public void visitApply(JCMethodInvocation that) { visitTree(that); }
2271 public void visitNewClass(JCNewClass that) { visitTree(that); }
2272 public void visitNewArray(JCNewArray that) { visitTree(that); }
2273 public void visitParens(JCParens that) { visitTree(that); }
2274 public void visitAssign(JCAssign that) { visitTree(that); }
2275 public void visitAssignop(JCAssignOp that) { visitTree(that); }
2276 public void visitUnary(JCUnary that) { visitTree(that); }
2277 public void visitBinary(JCBinary that) { visitTree(that); }
2278 public void visitTypeCast(JCTypeCast that) { visitTree(that); }
2279 public void visitTypeTest(JCInstanceOf that) { visitTree(that); }
2280 public void visitIndexed(JCArrayAccess that) { visitTree(that); }
2281 public void visitSelect(JCFieldAccess that) { visitTree(that); }
2282 public void visitIdent(JCIdent that) { visitTree(that); }
2283 public void visitLiteral(JCLiteral that) { visitTree(that); }
2284 public void visitTypeIdent(JCPrimitiveTypeTree that) { visitTree(that); }
2285 public void visitTypeArray(JCArrayTypeTree that) { visitTree(that); }
2286 public void visitTypeApply(JCTypeApply that) { visitTree(that); }
2287 public void visitTypeUnion(JCTypeUnion that) { visitTree(that); }
2288 public void visitTypeParameter(JCTypeParameter that) { visitTree(that); }
2289 public void visitWildcard(JCWildcard that) { visitTree(that); }
2290 public void visitTypeBoundKind(TypeBoundKind that) { visitTree(that); }
2291 public void visitAnnotation(JCAnnotation that) { visitTree(that); }
2292 public void visitModifiers(JCModifiers that) { visitTree(that); }
2293 public void visitErroneous(JCErroneous that) { visitTree(that); }
2294 public void visitLetExpr(LetExpr that) { visitTree(that); }
2296 public void visitTree(JCTree that) { Assert.error(); }
2297 }
2299 }