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