Mon, 03 Jun 2013 17:09:26 -0700
8006615: [doclint] move remaining messages into resource bundle
Reviewed-by: mcimadamore, vromero
1 /*
2 * Copyright (c) 1999, 2013, 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;
30 import com.sun.source.tree.Tree;
31 import com.sun.tools.javac.code.*;
32 import com.sun.tools.javac.comp.AttrContext;
33 import com.sun.tools.javac.comp.Env;
34 import com.sun.tools.javac.tree.JCTree.*;
35 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
36 import com.sun.tools.javac.util.*;
37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
38 import static com.sun.tools.javac.code.Flags.*;
39 import static com.sun.tools.javac.code.TypeTag.BOT;
40 import static com.sun.tools.javac.tree.JCTree.Tag.*;
41 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
42 import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
44 /** Utility class containing inspector methods for trees.
45 *
46 * <p><b>This is NOT part of any supported API.
47 * If you write code that depends on this, you do so at your own risk.
48 * This code and its internal interfaces are subject to change or
49 * deletion without notice.</b>
50 */
51 public class TreeInfo {
52 protected static final Context.Key<TreeInfo> treeInfoKey =
53 new Context.Key<TreeInfo>();
55 public static TreeInfo instance(Context context) {
56 TreeInfo instance = context.get(treeInfoKey);
57 if (instance == null)
58 instance = new TreeInfo(context);
59 return instance;
60 }
62 /** The names of all operators.
63 */
64 private Name[] opname = new Name[Tag.getNumberOfOperators()];
66 private void setOpname(Tag tag, String name, Names names) {
67 setOpname(tag, names.fromString(name));
68 }
69 private void setOpname(Tag tag, Name name) {
70 opname[tag.operatorIndex()] = name;
71 }
73 private TreeInfo(Context context) {
74 context.put(treeInfoKey, this);
76 Names names = Names.instance(context);
77 setOpname(POS, "+", names);
78 setOpname(NEG, names.hyphen);
79 setOpname(NOT, "!", names);
80 setOpname(COMPL, "~", names);
81 setOpname(PREINC, "++", names);
82 setOpname(PREDEC, "--", names);
83 setOpname(POSTINC, "++", names);
84 setOpname(POSTDEC, "--", names);
85 setOpname(NULLCHK, "<*nullchk*>", names);
86 setOpname(OR, "||", names);
87 setOpname(AND, "&&", names);
88 setOpname(EQ, "==", names);
89 setOpname(NE, "!=", names);
90 setOpname(LT, "<", names);
91 setOpname(GT, ">", names);
92 setOpname(LE, "<=", names);
93 setOpname(GE, ">=", names);
94 setOpname(BITOR, "|", names);
95 setOpname(BITXOR, "^", names);
96 setOpname(BITAND, "&", names);
97 setOpname(SL, "<<", names);
98 setOpname(SR, ">>", names);
99 setOpname(USR, ">>>", names);
100 setOpname(PLUS, "+", names);
101 setOpname(MINUS, names.hyphen);
102 setOpname(MUL, names.asterisk);
103 setOpname(DIV, names.slash);
104 setOpname(MOD, "%", names);
105 }
107 public static List<JCExpression> args(JCTree t) {
108 switch (t.getTag()) {
109 case APPLY:
110 return ((JCMethodInvocation)t).args;
111 case NEWCLASS:
112 return ((JCNewClass)t).args;
113 default:
114 return null;
115 }
116 }
118 /** Return name of operator with given tree tag.
119 */
120 public Name operatorName(JCTree.Tag tag) {
121 return opname[tag.operatorIndex()];
122 }
124 /** Is tree a constructor declaration?
125 */
126 public static boolean isConstructor(JCTree tree) {
127 if (tree.hasTag(METHODDEF)) {
128 Name name = ((JCMethodDecl) tree).name;
129 return name == name.table.names.init;
130 } else {
131 return false;
132 }
133 }
135 /** Is there a constructor declaration in the given list of trees?
136 */
137 public static boolean hasConstructors(List<JCTree> trees) {
138 for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
139 if (isConstructor(l.head)) return true;
140 return false;
141 }
143 public static boolean isMultiCatch(JCCatch catchClause) {
144 return catchClause.param.vartype.hasTag(TYPEUNION);
145 }
147 /** Is statement an initializer for a synthetic field?
148 */
149 public static boolean isSyntheticInit(JCTree stat) {
150 if (stat.hasTag(EXEC)) {
151 JCExpressionStatement exec = (JCExpressionStatement)stat;
152 if (exec.expr.hasTag(ASSIGN)) {
153 JCAssign assign = (JCAssign)exec.expr;
154 if (assign.lhs.hasTag(SELECT)) {
155 JCFieldAccess select = (JCFieldAccess)assign.lhs;
156 if (select.sym != null &&
157 (select.sym.flags() & SYNTHETIC) != 0) {
158 Name selected = name(select.selected);
159 if (selected != null && selected == selected.table.names._this)
160 return true;
161 }
162 }
163 }
164 }
165 return false;
166 }
168 /** If the expression is a method call, return the method name, null
169 * otherwise. */
170 public static Name calledMethodName(JCTree tree) {
171 if (tree.hasTag(EXEC)) {
172 JCExpressionStatement exec = (JCExpressionStatement)tree;
173 if (exec.expr.hasTag(APPLY)) {
174 Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth);
175 return mname;
176 }
177 }
178 return null;
179 }
181 /** Is this a call to this or super?
182 */
183 public static boolean isSelfCall(JCTree tree) {
184 Name name = calledMethodName(tree);
185 if (name != null) {
186 Names names = name.table.names;
187 return name==names._this || name==names._super;
188 } else {
189 return false;
190 }
191 }
193 /** Is this a call to super?
194 */
195 public static boolean isSuperCall(JCTree tree) {
196 Name name = calledMethodName(tree);
197 if (name != null) {
198 Names names = name.table.names;
199 return name==names._super;
200 } else {
201 return false;
202 }
203 }
205 /** Is this a constructor whose first (non-synthetic) statement is not
206 * of the form this(...)?
207 */
208 public static boolean isInitialConstructor(JCTree tree) {
209 JCMethodInvocation app = firstConstructorCall(tree);
210 if (app == null) return false;
211 Name meth = name(app.meth);
212 return meth == null || meth != meth.table.names._this;
213 }
215 /** Return the first call in a constructor definition. */
216 public static JCMethodInvocation firstConstructorCall(JCTree tree) {
217 if (!tree.hasTag(METHODDEF)) return null;
218 JCMethodDecl md = (JCMethodDecl) tree;
219 Names names = md.name.table.names;
220 if (md.name != names.init) return null;
221 if (md.body == null) return null;
222 List<JCStatement> stats = md.body.stats;
223 // Synthetic initializations can appear before the super call.
224 while (stats.nonEmpty() && isSyntheticInit(stats.head))
225 stats = stats.tail;
226 if (stats.isEmpty()) return null;
227 if (!stats.head.hasTag(EXEC)) return null;
228 JCExpressionStatement exec = (JCExpressionStatement) stats.head;
229 if (!exec.expr.hasTag(APPLY)) return null;
230 return (JCMethodInvocation)exec.expr;
231 }
233 /** Return true if a tree represents a diamond new expr. */
234 public static boolean isDiamond(JCTree tree) {
235 switch(tree.getTag()) {
236 case TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty();
237 case NEWCLASS: return isDiamond(((JCNewClass)tree).clazz);
238 case ANNOTATED_TYPE: return isDiamond(((JCAnnotatedType)tree).underlyingType);
239 default: return false;
240 }
241 }
243 public static boolean isEnumInit(JCTree tree) {
244 switch (tree.getTag()) {
245 case VARDEF:
246 return (((JCVariableDecl)tree).mods.flags & ENUM) != 0;
247 default:
248 return false;
249 }
250 }
252 /** set 'polyKind' on given tree */
253 public static void setPolyKind(JCTree tree, PolyKind pkind) {
254 switch (tree.getTag()) {
255 case APPLY:
256 ((JCMethodInvocation)tree).polyKind = pkind;
257 break;
258 case NEWCLASS:
259 ((JCNewClass)tree).polyKind = pkind;
260 break;
261 case REFERENCE:
262 ((JCMemberReference)tree).refPolyKind = pkind;
263 break;
264 default:
265 throw new AssertionError("Unexpected tree: " + tree);
266 }
267 }
269 /** set 'varargsElement' on given tree */
270 public static void setVarargsElement(JCTree tree, Type varargsElement) {
271 switch (tree.getTag()) {
272 case APPLY:
273 ((JCMethodInvocation)tree).varargsElement = varargsElement;
274 break;
275 case NEWCLASS:
276 ((JCNewClass)tree).varargsElement = varargsElement;
277 break;
278 case REFERENCE:
279 ((JCMemberReference)tree).varargsElement = varargsElement;
280 break;
281 default:
282 throw new AssertionError("Unexpected tree: " + tree);
283 }
284 }
286 /** Return true if the tree corresponds to an expression statement */
287 public static boolean isExpressionStatement(JCExpression tree) {
288 switch(tree.getTag()) {
289 case PREINC: case PREDEC:
290 case POSTINC: case POSTDEC:
291 case ASSIGN:
292 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
293 case SL_ASG: case SR_ASG: case USR_ASG:
294 case PLUS_ASG: case MINUS_ASG:
295 case MUL_ASG: case DIV_ASG: case MOD_ASG:
296 case APPLY: case NEWCLASS:
297 case ERRONEOUS:
298 return true;
299 default:
300 return false;
301 }
302 }
304 /**
305 * Return true if the AST corresponds to a static select of the kind A.B
306 */
307 public static boolean isStaticSelector(JCTree base, Names names) {
308 if (base == null)
309 return false;
310 switch (base.getTag()) {
311 case IDENT:
312 JCIdent id = (JCIdent)base;
313 return id.name != names._this &&
314 id.name != names._super &&
315 isStaticSym(base);
316 case SELECT:
317 return isStaticSym(base) &&
318 isStaticSelector(((JCFieldAccess)base).selected, names);
319 case TYPEAPPLY:
320 case TYPEARRAY:
321 return true;
322 case ANNOTATED_TYPE:
323 return isStaticSelector(((JCAnnotatedType)base).underlyingType, names);
324 default:
325 return false;
326 }
327 }
328 //where
329 private static boolean isStaticSym(JCTree tree) {
330 Symbol sym = symbol(tree);
331 return (sym.kind == Kinds.TYP ||
332 sym.kind == Kinds.PCK);
333 }
335 /** Return true if a tree represents the null literal. */
336 public static boolean isNull(JCTree tree) {
337 if (!tree.hasTag(LITERAL))
338 return false;
339 JCLiteral lit = (JCLiteral) tree;
340 return (lit.typetag == BOT);
341 }
343 public static String getCommentText(Env<?> env, JCTree tree) {
344 DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
345 ? ((JCCompilationUnit) tree).docComments
346 : env.toplevel.docComments;
347 return (docComments == null) ? null : docComments.getCommentText(tree);
348 }
350 public static DCTree.DCDocComment getCommentTree(Env<?> env, JCTree tree) {
351 DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
352 ? ((JCCompilationUnit) tree).docComments
353 : env.toplevel.docComments;
354 return (docComments == null) ? null : docComments.getCommentTree(tree);
355 }
357 /** The position of the first statement in a block, or the position of
358 * the block itself if it is empty.
359 */
360 public static int firstStatPos(JCTree tree) {
361 if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty())
362 return ((JCBlock) tree).stats.head.pos;
363 else
364 return tree.pos;
365 }
367 /** The end position of given tree, if it is a block with
368 * defined endpos.
369 */
370 public static int endPos(JCTree tree) {
371 if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS)
372 return ((JCBlock) tree).endpos;
373 else if (tree.hasTag(SYNCHRONIZED))
374 return endPos(((JCSynchronized) tree).body);
375 else if (tree.hasTag(TRY)) {
376 JCTry t = (JCTry) tree;
377 return endPos((t.finalizer != null) ? t.finalizer
378 : (t.catchers.nonEmpty() ? t.catchers.last().body : t.body));
379 } else
380 return tree.pos;
381 }
384 /** Get the start position for a tree node. The start position is
385 * defined to be the position of the first character of the first
386 * token of the node's source text.
387 * @param tree The tree node
388 */
389 public static int getStartPos(JCTree tree) {
390 if (tree == null)
391 return Position.NOPOS;
393 switch(tree.getTag()) {
394 case APPLY:
395 return getStartPos(((JCMethodInvocation) tree).meth);
396 case ASSIGN:
397 return getStartPos(((JCAssign) tree).lhs);
398 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
399 case SL_ASG: case SR_ASG: case USR_ASG:
400 case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
401 case DIV_ASG: case MOD_ASG:
402 return getStartPos(((JCAssignOp) tree).lhs);
403 case OR: case AND: case BITOR:
404 case BITXOR: case BITAND: case EQ:
405 case NE: case LT: case GT:
406 case LE: case GE: case SL:
407 case SR: case USR: case PLUS:
408 case MINUS: case MUL: case DIV:
409 case MOD:
410 return getStartPos(((JCBinary) tree).lhs);
411 case CLASSDEF: {
412 JCClassDecl node = (JCClassDecl)tree;
413 if (node.mods.pos != Position.NOPOS)
414 return node.mods.pos;
415 break;
416 }
417 case CONDEXPR:
418 return getStartPos(((JCConditional) tree).cond);
419 case EXEC:
420 return getStartPos(((JCExpressionStatement) tree).expr);
421 case INDEXED:
422 return getStartPos(((JCArrayAccess) tree).indexed);
423 case METHODDEF: {
424 JCMethodDecl node = (JCMethodDecl)tree;
425 if (node.mods.pos != Position.NOPOS)
426 return node.mods.pos;
427 if (node.typarams.nonEmpty()) // List.nil() used for no typarams
428 return getStartPos(node.typarams.head);
429 return node.restype == null ? node.pos : getStartPos(node.restype);
430 }
431 case SELECT:
432 return getStartPos(((JCFieldAccess) tree).selected);
433 case TYPEAPPLY:
434 return getStartPos(((JCTypeApply) tree).clazz);
435 case TYPEARRAY:
436 return getStartPos(((JCArrayTypeTree) tree).elemtype);
437 case TYPETEST:
438 return getStartPos(((JCInstanceOf) tree).expr);
439 case POSTINC:
440 case POSTDEC:
441 return getStartPos(((JCUnary) tree).arg);
442 case ANNOTATED_TYPE: {
443 JCAnnotatedType node = (JCAnnotatedType) tree;
444 if (node.annotations.nonEmpty()) {
445 if (node.underlyingType.hasTag(TYPEARRAY) ||
446 node.underlyingType.hasTag(SELECT)) {
447 return getStartPos(node.underlyingType);
448 } else {
449 return getStartPos(node.annotations.head);
450 }
451 } else {
452 return getStartPos(node.underlyingType);
453 }
454 }
455 case NEWCLASS: {
456 JCNewClass node = (JCNewClass)tree;
457 if (node.encl != null)
458 return getStartPos(node.encl);
459 break;
460 }
461 case VARDEF: {
462 JCVariableDecl node = (JCVariableDecl)tree;
463 if (node.mods.pos != Position.NOPOS) {
464 return node.mods.pos;
465 } else if (node.vartype == null) {
466 //if there's no type (partially typed lambda parameter)
467 //simply return node position
468 return node.pos;
469 } else {
470 return getStartPos(node.vartype);
471 }
472 }
473 case ERRONEOUS: {
474 JCErroneous node = (JCErroneous)tree;
475 if (node.errs != null && node.errs.nonEmpty())
476 return getStartPos(node.errs.head);
477 }
478 }
479 return tree.pos;
480 }
482 /** The end position of given tree, given a table of end positions generated by the parser
483 */
484 public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
485 if (tree == null)
486 return Position.NOPOS;
488 if (endPosTable == null) {
489 // fall back on limited info in the tree
490 return endPos(tree);
491 }
493 int mapPos = endPosTable.getEndPos(tree);
494 if (mapPos != Position.NOPOS)
495 return mapPos;
497 switch(tree.getTag()) {
498 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
499 case SL_ASG: case SR_ASG: case USR_ASG:
500 case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
501 case DIV_ASG: case MOD_ASG:
502 return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
503 case OR: case AND: case BITOR:
504 case BITXOR: case BITAND: case EQ:
505 case NE: case LT: case GT:
506 case LE: case GE: case SL:
507 case SR: case USR: case PLUS:
508 case MINUS: case MUL: case DIV:
509 case MOD:
510 return getEndPos(((JCBinary) tree).rhs, endPosTable);
511 case CASE:
512 return getEndPos(((JCCase) tree).stats.last(), endPosTable);
513 case CATCH:
514 return getEndPos(((JCCatch) tree).body, endPosTable);
515 case CONDEXPR:
516 return getEndPos(((JCConditional) tree).falsepart, endPosTable);
517 case FORLOOP:
518 return getEndPos(((JCForLoop) tree).body, endPosTable);
519 case FOREACHLOOP:
520 return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable);
521 case IF: {
522 JCIf node = (JCIf)tree;
523 if (node.elsepart == null) {
524 return getEndPos(node.thenpart, endPosTable);
525 } else {
526 return getEndPos(node.elsepart, endPosTable);
527 }
528 }
529 case LABELLED:
530 return getEndPos(((JCLabeledStatement) tree).body, endPosTable);
531 case MODIFIERS:
532 return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
533 case SYNCHRONIZED:
534 return getEndPos(((JCSynchronized) tree).body, endPosTable);
535 case TOPLEVEL:
536 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
537 case TRY: {
538 JCTry node = (JCTry)tree;
539 if (node.finalizer != null) {
540 return getEndPos(node.finalizer, endPosTable);
541 } else if (!node.catchers.isEmpty()) {
542 return getEndPos(node.catchers.last(), endPosTable);
543 } else {
544 return getEndPos(node.body, endPosTable);
545 }
546 }
547 case WILDCARD:
548 return getEndPos(((JCWildcard) tree).inner, endPosTable);
549 case TYPECAST:
550 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
551 case TYPETEST:
552 return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
553 case POS:
554 case NEG:
555 case NOT:
556 case COMPL:
557 case PREINC:
558 case PREDEC:
559 return getEndPos(((JCUnary) tree).arg, endPosTable);
560 case WHILELOOP:
561 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
562 case ANNOTATED_TYPE:
563 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
564 case ERRONEOUS: {
565 JCErroneous node = (JCErroneous)tree;
566 if (node.errs != null && node.errs.nonEmpty())
567 return getEndPos(node.errs.last(), endPosTable);
568 }
569 }
570 return Position.NOPOS;
571 }
574 /** A DiagnosticPosition with the preferred position set to the
575 * end position of given tree, if it is a block with
576 * defined endpos.
577 */
578 public static DiagnosticPosition diagEndPos(final JCTree tree) {
579 final int endPos = TreeInfo.endPos(tree);
580 return new DiagnosticPosition() {
581 public JCTree getTree() { return tree; }
582 public int getStartPosition() { return TreeInfo.getStartPos(tree); }
583 public int getPreferredPosition() { return endPos; }
584 public int getEndPosition(EndPosTable endPosTable) {
585 return TreeInfo.getEndPos(tree, endPosTable);
586 }
587 };
588 }
590 /** The position of the finalizer of given try/synchronized statement.
591 */
592 public static int finalizerPos(JCTree tree) {
593 if (tree.hasTag(TRY)) {
594 JCTry t = (JCTry) tree;
595 Assert.checkNonNull(t.finalizer);
596 return firstStatPos(t.finalizer);
597 } else if (tree.hasTag(SYNCHRONIZED)) {
598 return endPos(((JCSynchronized) tree).body);
599 } else {
600 throw new AssertionError();
601 }
602 }
604 /** Find the position for reporting an error about a symbol, where
605 * that symbol is defined somewhere in the given tree. */
606 public static int positionFor(final Symbol sym, final JCTree tree) {
607 JCTree decl = declarationFor(sym, tree);
608 return ((decl != null) ? decl : tree).pos;
609 }
611 /** Find the position for reporting an error about a symbol, where
612 * that symbol is defined somewhere in the given tree. */
613 public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) {
614 JCTree decl = declarationFor(sym, tree);
615 return ((decl != null) ? decl : tree).pos();
616 }
618 /** Find the declaration for a symbol, where
619 * that symbol is defined somewhere in the given tree. */
620 public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
621 class DeclScanner extends TreeScanner {
622 JCTree result = null;
623 public void scan(JCTree tree) {
624 if (tree!=null && result==null)
625 tree.accept(this);
626 }
627 public void visitTopLevel(JCCompilationUnit that) {
628 if (that.packge == sym) result = that;
629 else super.visitTopLevel(that);
630 }
631 public void visitClassDef(JCClassDecl that) {
632 if (that.sym == sym) result = that;
633 else super.visitClassDef(that);
634 }
635 public void visitMethodDef(JCMethodDecl that) {
636 if (that.sym == sym) result = that;
637 else super.visitMethodDef(that);
638 }
639 public void visitVarDef(JCVariableDecl that) {
640 if (that.sym == sym) result = that;
641 else super.visitVarDef(that);
642 }
643 public void visitTypeParameter(JCTypeParameter that) {
644 if (that.type != null && that.type.tsym == sym) result = that;
645 else super.visitTypeParameter(that);
646 }
647 }
648 DeclScanner s = new DeclScanner();
649 tree.accept(s);
650 return s.result;
651 }
653 public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
654 return scopeFor(pathFor(node, unit));
655 }
657 public static Env<AttrContext> scopeFor(List<JCTree> path) {
658 // TODO: not implemented yet
659 throw new UnsupportedOperationException("not implemented yet");
660 }
662 public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) {
663 class Result extends Error {
664 static final long serialVersionUID = -5942088234594905625L;
665 List<JCTree> path;
666 Result(List<JCTree> path) {
667 this.path = path;
668 }
669 }
670 class PathFinder extends TreeScanner {
671 List<JCTree> path = List.nil();
672 public void scan(JCTree tree) {
673 if (tree != null) {
674 path = path.prepend(tree);
675 if (tree == node)
676 throw new Result(path);
677 super.scan(tree);
678 path = path.tail;
679 }
680 }
681 }
682 try {
683 new PathFinder().scan(unit);
684 } catch (Result result) {
685 return result.path;
686 }
687 return List.nil();
688 }
690 /** Return the statement referenced by a label.
691 * If the label refers to a loop or switch, return that switch
692 * otherwise return the labelled statement itself
693 */
694 public static JCTree referencedStatement(JCLabeledStatement tree) {
695 JCTree t = tree;
696 do t = ((JCLabeledStatement) t).body;
697 while (t.hasTag(LABELLED));
698 switch (t.getTag()) {
699 case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH:
700 return t;
701 default:
702 return tree;
703 }
704 }
706 /** Skip parens and return the enclosed expression
707 */
708 public static JCExpression skipParens(JCExpression tree) {
709 while (tree.hasTag(PARENS)) {
710 tree = ((JCParens) tree).expr;
711 }
712 return tree;
713 }
715 /** Skip parens and return the enclosed expression
716 */
717 public static JCTree skipParens(JCTree tree) {
718 if (tree.hasTag(PARENS))
719 return skipParens((JCParens)tree);
720 else
721 return tree;
722 }
724 /** Return the types of a list of trees.
725 */
726 public static List<Type> types(List<? extends JCTree> trees) {
727 ListBuffer<Type> ts = new ListBuffer<Type>();
728 for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
729 ts.append(l.head.type);
730 return ts.toList();
731 }
733 /** If this tree is an identifier or a field or a parameterized type,
734 * return its name, otherwise return null.
735 */
736 public static Name name(JCTree tree) {
737 switch (tree.getTag()) {
738 case IDENT:
739 return ((JCIdent) tree).name;
740 case SELECT:
741 return ((JCFieldAccess) tree).name;
742 case TYPEAPPLY:
743 return name(((JCTypeApply) tree).clazz);
744 default:
745 return null;
746 }
747 }
749 /** If this tree is a qualified identifier, its return fully qualified name,
750 * otherwise return null.
751 */
752 public static Name fullName(JCTree tree) {
753 tree = skipParens(tree);
754 switch (tree.getTag()) {
755 case IDENT:
756 return ((JCIdent) tree).name;
757 case SELECT:
758 Name sname = fullName(((JCFieldAccess) tree).selected);
759 return sname == null ? null : sname.append('.', name(tree));
760 default:
761 return null;
762 }
763 }
765 public static Symbol symbolFor(JCTree node) {
766 Symbol sym = symbolForImpl(node);
768 return sym != null ? sym.baseSymbol() : null;
769 }
771 private static Symbol symbolForImpl(JCTree node) {
772 node = skipParens(node);
773 switch (node.getTag()) {
774 case TOPLEVEL:
775 return ((JCCompilationUnit) node).packge;
776 case CLASSDEF:
777 return ((JCClassDecl) node).sym;
778 case METHODDEF:
779 return ((JCMethodDecl) node).sym;
780 case VARDEF:
781 return ((JCVariableDecl) node).sym;
782 case IDENT:
783 return ((JCIdent) node).sym;
784 case SELECT:
785 return ((JCFieldAccess) node).sym;
786 case REFERENCE:
787 return ((JCMemberReference) node).sym;
788 case NEWCLASS:
789 return ((JCNewClass) node).constructor;
790 case APPLY:
791 return symbolFor(((JCMethodInvocation) node).meth);
792 case TYPEAPPLY:
793 return symbolFor(((JCTypeApply) node).clazz);
794 case ANNOTATION:
795 case TYPE_ANNOTATION:
796 case TYPEPARAMETER:
797 if (node.type != null)
798 return node.type.tsym;
799 return null;
800 default:
801 return null;
802 }
803 }
805 public static boolean isDeclaration(JCTree node) {
806 node = skipParens(node);
807 switch (node.getTag()) {
808 case CLASSDEF:
809 case METHODDEF:
810 case VARDEF:
811 return true;
812 default:
813 return false;
814 }
815 }
817 /** If this tree is an identifier or a field, return its symbol,
818 * otherwise return null.
819 */
820 public static Symbol symbol(JCTree tree) {
821 tree = skipParens(tree);
822 switch (tree.getTag()) {
823 case IDENT:
824 return ((JCIdent) tree).sym;
825 case SELECT:
826 return ((JCFieldAccess) tree).sym;
827 case TYPEAPPLY:
828 return symbol(((JCTypeApply) tree).clazz);
829 case ANNOTATED_TYPE:
830 return symbol(((JCAnnotatedType) tree).underlyingType);
831 default:
832 return null;
833 }
834 }
836 /** Return true if this is a nonstatic selection. */
837 public static boolean nonstaticSelect(JCTree tree) {
838 tree = skipParens(tree);
839 if (!tree.hasTag(SELECT)) return false;
840 JCFieldAccess s = (JCFieldAccess) tree;
841 Symbol e = symbol(s.selected);
842 return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
843 }
845 /** If this tree is an identifier or a field, set its symbol, otherwise skip.
846 */
847 public static void setSymbol(JCTree tree, Symbol sym) {
848 tree = skipParens(tree);
849 switch (tree.getTag()) {
850 case IDENT:
851 ((JCIdent) tree).sym = sym; break;
852 case SELECT:
853 ((JCFieldAccess) tree).sym = sym; break;
854 default:
855 }
856 }
858 /** If this tree is a declaration or a block, return its flags field,
859 * otherwise return 0.
860 */
861 public static long flags(JCTree tree) {
862 switch (tree.getTag()) {
863 case VARDEF:
864 return ((JCVariableDecl) tree).mods.flags;
865 case METHODDEF:
866 return ((JCMethodDecl) tree).mods.flags;
867 case CLASSDEF:
868 return ((JCClassDecl) tree).mods.flags;
869 case BLOCK:
870 return ((JCBlock) tree).flags;
871 default:
872 return 0;
873 }
874 }
876 /** Return first (smallest) flag in `flags':
877 * pre: flags != 0
878 */
879 public static long firstFlag(long flags) {
880 long flag = 1;
881 while ((flag & flags & ExtendedStandardFlags) == 0)
882 flag = flag << 1;
883 return flag;
884 }
886 /** Return flags as a string, separated by " ".
887 */
888 public static String flagNames(long flags) {
889 return Flags.toString(flags & ExtendedStandardFlags).trim();
890 }
892 /** Operator precedences values.
893 */
894 public static final int
895 notExpression = -1, // not an expression
896 noPrec = 0, // no enclosing expression
897 assignPrec = 1,
898 assignopPrec = 2,
899 condPrec = 3,
900 orPrec = 4,
901 andPrec = 5,
902 bitorPrec = 6,
903 bitxorPrec = 7,
904 bitandPrec = 8,
905 eqPrec = 9,
906 ordPrec = 10,
907 shiftPrec = 11,
908 addPrec = 12,
909 mulPrec = 13,
910 prefixPrec = 14,
911 postfixPrec = 15,
912 precCount = 16;
915 /** Map operators to their precedence levels.
916 */
917 public static int opPrec(JCTree.Tag op) {
918 switch(op) {
919 case POS:
920 case NEG:
921 case NOT:
922 case COMPL:
923 case PREINC:
924 case PREDEC: return prefixPrec;
925 case POSTINC:
926 case POSTDEC:
927 case NULLCHK: return postfixPrec;
928 case ASSIGN: return assignPrec;
929 case BITOR_ASG:
930 case BITXOR_ASG:
931 case BITAND_ASG:
932 case SL_ASG:
933 case SR_ASG:
934 case USR_ASG:
935 case PLUS_ASG:
936 case MINUS_ASG:
937 case MUL_ASG:
938 case DIV_ASG:
939 case MOD_ASG: return assignopPrec;
940 case OR: return orPrec;
941 case AND: return andPrec;
942 case EQ:
943 case NE: return eqPrec;
944 case LT:
945 case GT:
946 case LE:
947 case GE: return ordPrec;
948 case BITOR: return bitorPrec;
949 case BITXOR: return bitxorPrec;
950 case BITAND: return bitandPrec;
951 case SL:
952 case SR:
953 case USR: return shiftPrec;
954 case PLUS:
955 case MINUS: return addPrec;
956 case MUL:
957 case DIV:
958 case MOD: return mulPrec;
959 case TYPETEST: return ordPrec;
960 default: throw new AssertionError();
961 }
962 }
964 static Tree.Kind tagToKind(JCTree.Tag tag) {
965 switch (tag) {
966 // Postfix expressions
967 case POSTINC: // _ ++
968 return Tree.Kind.POSTFIX_INCREMENT;
969 case POSTDEC: // _ --
970 return Tree.Kind.POSTFIX_DECREMENT;
972 // Unary operators
973 case PREINC: // ++ _
974 return Tree.Kind.PREFIX_INCREMENT;
975 case PREDEC: // -- _
976 return Tree.Kind.PREFIX_DECREMENT;
977 case POS: // +
978 return Tree.Kind.UNARY_PLUS;
979 case NEG: // -
980 return Tree.Kind.UNARY_MINUS;
981 case COMPL: // ~
982 return Tree.Kind.BITWISE_COMPLEMENT;
983 case NOT: // !
984 return Tree.Kind.LOGICAL_COMPLEMENT;
986 // Binary operators
988 // Multiplicative operators
989 case MUL: // *
990 return Tree.Kind.MULTIPLY;
991 case DIV: // /
992 return Tree.Kind.DIVIDE;
993 case MOD: // %
994 return Tree.Kind.REMAINDER;
996 // Additive operators
997 case PLUS: // +
998 return Tree.Kind.PLUS;
999 case MINUS: // -
1000 return Tree.Kind.MINUS;
1002 // Shift operators
1003 case SL: // <<
1004 return Tree.Kind.LEFT_SHIFT;
1005 case SR: // >>
1006 return Tree.Kind.RIGHT_SHIFT;
1007 case USR: // >>>
1008 return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
1010 // Relational operators
1011 case LT: // <
1012 return Tree.Kind.LESS_THAN;
1013 case GT: // >
1014 return Tree.Kind.GREATER_THAN;
1015 case LE: // <=
1016 return Tree.Kind.LESS_THAN_EQUAL;
1017 case GE: // >=
1018 return Tree.Kind.GREATER_THAN_EQUAL;
1020 // Equality operators
1021 case EQ: // ==
1022 return Tree.Kind.EQUAL_TO;
1023 case NE: // !=
1024 return Tree.Kind.NOT_EQUAL_TO;
1026 // Bitwise and logical operators
1027 case BITAND: // &
1028 return Tree.Kind.AND;
1029 case BITXOR: // ^
1030 return Tree.Kind.XOR;
1031 case BITOR: // |
1032 return Tree.Kind.OR;
1034 // Conditional operators
1035 case AND: // &&
1036 return Tree.Kind.CONDITIONAL_AND;
1037 case OR: // ||
1038 return Tree.Kind.CONDITIONAL_OR;
1040 // Assignment operators
1041 case MUL_ASG: // *=
1042 return Tree.Kind.MULTIPLY_ASSIGNMENT;
1043 case DIV_ASG: // /=
1044 return Tree.Kind.DIVIDE_ASSIGNMENT;
1045 case MOD_ASG: // %=
1046 return Tree.Kind.REMAINDER_ASSIGNMENT;
1047 case PLUS_ASG: // +=
1048 return Tree.Kind.PLUS_ASSIGNMENT;
1049 case MINUS_ASG: // -=
1050 return Tree.Kind.MINUS_ASSIGNMENT;
1051 case SL_ASG: // <<=
1052 return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
1053 case SR_ASG: // >>=
1054 return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
1055 case USR_ASG: // >>>=
1056 return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
1057 case BITAND_ASG: // &=
1058 return Tree.Kind.AND_ASSIGNMENT;
1059 case BITXOR_ASG: // ^=
1060 return Tree.Kind.XOR_ASSIGNMENT;
1061 case BITOR_ASG: // |=
1062 return Tree.Kind.OR_ASSIGNMENT;
1064 // Null check (implementation detail), for example, __.getClass()
1065 case NULLCHK:
1066 return Tree.Kind.OTHER;
1068 case ANNOTATION:
1069 return Tree.Kind.ANNOTATION;
1070 case TYPE_ANNOTATION:
1071 return Tree.Kind.TYPE_ANNOTATION;
1073 default:
1074 return null;
1075 }
1076 }
1078 /**
1079 * Returns the underlying type of the tree if it is an annotated type,
1080 * or the tree itself otherwise.
1081 */
1082 public static JCExpression typeIn(JCExpression tree) {
1083 switch (tree.getTag()) {
1084 case ANNOTATED_TYPE:
1085 return ((JCAnnotatedType)tree).underlyingType;
1086 case IDENT: /* simple names */
1087 case TYPEIDENT: /* primitive name */
1088 case SELECT: /* qualified name */
1089 case TYPEARRAY: /* array types */
1090 case WILDCARD: /* wild cards */
1091 case TYPEPARAMETER: /* type parameters */
1092 case TYPEAPPLY: /* parameterized types */
1093 case ERRONEOUS: /* error tree TODO: needed for BadCast JSR308 test case. Better way? */
1094 return tree;
1095 default:
1096 throw new AssertionError("Unexpected type tree: " + tree);
1097 }
1098 }
1100 /* Return the inner-most type of a type tree.
1101 * For an array that contains an annotated type, return that annotated type.
1102 * TODO: currently only used by Pretty. Describe behavior better.
1103 */
1104 public static JCTree innermostType(JCTree type) {
1105 JCTree lastAnnotatedType = null;
1106 JCTree cur = type;
1107 loop: while (true) {
1108 switch (cur.getTag()) {
1109 case TYPEARRAY:
1110 lastAnnotatedType = null;
1111 cur = ((JCArrayTypeTree)cur).elemtype;
1112 break;
1113 case WILDCARD:
1114 lastAnnotatedType = null;
1115 cur = ((JCWildcard)cur).inner;
1116 break;
1117 case ANNOTATED_TYPE:
1118 lastAnnotatedType = cur;
1119 cur = ((JCAnnotatedType)cur).underlyingType;
1120 break;
1121 default:
1122 break loop;
1123 }
1124 }
1125 if (lastAnnotatedType!=null) {
1126 return lastAnnotatedType;
1127 } else {
1128 return cur;
1129 }
1130 }
1132 private static class TypeAnnotationFinder extends TreeScanner {
1133 public boolean foundTypeAnno = false;
1134 public void visitAnnotation(JCAnnotation tree) {
1135 foundTypeAnno = foundTypeAnno || tree.hasTag(TYPE_ANNOTATION);
1136 }
1137 }
1139 public static boolean containsTypeAnnotation(JCTree e) {
1140 TypeAnnotationFinder finder = new TypeAnnotationFinder();
1141 finder.scan(e);
1142 return finder.foundTypeAnno;
1143 }
1144 }