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