Mon, 26 Jul 2010 14:25:56 -0700
6957438: improve code for generating warning messages containing option names
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.tree;
28 import com.sun.source.tree.Tree;
29 import com.sun.tools.javac.comp.AttrContext;
30 import com.sun.tools.javac.comp.Env;
31 import java.util.Map;
32 import com.sun.tools.javac.util.*;
33 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.tree.JCTree.*;
37 import static com.sun.tools.javac.code.Flags.*;
39 /** Utility class containing inspector methods for trees.
40 *
41 * <p><b>This is NOT part of any supported API.
42 * If you write code that depends on this, you do so at your own risk.
43 * This code and its internal interfaces are subject to change or
44 * deletion without notice.</b>
45 */
46 public class TreeInfo {
47 protected static final Context.Key<TreeInfo> treeInfoKey =
48 new Context.Key<TreeInfo>();
50 public static TreeInfo instance(Context context) {
51 TreeInfo instance = context.get(treeInfoKey);
52 if (instance == null)
53 instance = new TreeInfo(context);
54 return instance;
55 }
57 /** The names of all operators.
58 */
59 private Name[] opname = new Name[JCTree.MOD - JCTree.POS + 1];
61 private TreeInfo(Context context) {
62 context.put(treeInfoKey, this);
64 Names names = Names.instance(context);
65 opname[JCTree.POS - JCTree.POS] = names.fromString("+");
66 opname[JCTree.NEG - JCTree.POS] = names.hyphen;
67 opname[JCTree.NOT - JCTree.POS] = names.fromString("!");
68 opname[JCTree.COMPL - JCTree.POS] = names.fromString("~");
69 opname[JCTree.PREINC - JCTree.POS] = names.fromString("++");
70 opname[JCTree.PREDEC - JCTree.POS] = names.fromString("--");
71 opname[JCTree.POSTINC - JCTree.POS] = names.fromString("++");
72 opname[JCTree.POSTDEC - JCTree.POS] = names.fromString("--");
73 opname[JCTree.NULLCHK - JCTree.POS] = names.fromString("<*nullchk*>");
74 opname[JCTree.OR - JCTree.POS] = names.fromString("||");
75 opname[JCTree.AND - JCTree.POS] = names.fromString("&&");
76 opname[JCTree.EQ - JCTree.POS] = names.fromString("==");
77 opname[JCTree.NE - JCTree.POS] = names.fromString("!=");
78 opname[JCTree.LT - JCTree.POS] = names.fromString("<");
79 opname[JCTree.GT - JCTree.POS] = names.fromString(">");
80 opname[JCTree.LE - JCTree.POS] = names.fromString("<=");
81 opname[JCTree.GE - JCTree.POS] = names.fromString(">=");
82 opname[JCTree.BITOR - JCTree.POS] = names.fromString("|");
83 opname[JCTree.BITXOR - JCTree.POS] = names.fromString("^");
84 opname[JCTree.BITAND - JCTree.POS] = names.fromString("&");
85 opname[JCTree.SL - JCTree.POS] = names.fromString("<<");
86 opname[JCTree.SR - JCTree.POS] = names.fromString(">>");
87 opname[JCTree.USR - JCTree.POS] = names.fromString(">>>");
88 opname[JCTree.PLUS - JCTree.POS] = names.fromString("+");
89 opname[JCTree.MINUS - JCTree.POS] = names.hyphen;
90 opname[JCTree.MUL - JCTree.POS] = names.asterisk;
91 opname[JCTree.DIV - JCTree.POS] = names.slash;
92 opname[JCTree.MOD - JCTree.POS] = names.fromString("%");
93 }
96 /** Return name of operator with given tree tag.
97 */
98 public Name operatorName(int tag) {
99 return opname[tag - JCTree.POS];
100 }
102 /** Is tree a constructor declaration?
103 */
104 public static boolean isConstructor(JCTree tree) {
105 if (tree.getTag() == JCTree.METHODDEF) {
106 Name name = ((JCMethodDecl) tree).name;
107 return name == name.table.names.init;
108 } else {
109 return false;
110 }
111 }
113 /** Is there a constructor declaration in the given list of trees?
114 */
115 public static boolean hasConstructors(List<JCTree> trees) {
116 for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
117 if (isConstructor(l.head)) return true;
118 return false;
119 }
121 public static boolean isMultiCatch(JCCatch catchClause) {
122 return catchClause.param.vartype.getTag() == JCTree.TYPEDISJOINT;
123 }
125 /** Is statement an initializer for a synthetic field?
126 */
127 public static boolean isSyntheticInit(JCTree stat) {
128 if (stat.getTag() == JCTree.EXEC) {
129 JCExpressionStatement exec = (JCExpressionStatement)stat;
130 if (exec.expr.getTag() == JCTree.ASSIGN) {
131 JCAssign assign = (JCAssign)exec.expr;
132 if (assign.lhs.getTag() == JCTree.SELECT) {
133 JCFieldAccess select = (JCFieldAccess)assign.lhs;
134 if (select.sym != null &&
135 (select.sym.flags() & SYNTHETIC) != 0) {
136 Name selected = name(select.selected);
137 if (selected != null && selected == selected.table.names._this)
138 return true;
139 }
140 }
141 }
142 }
143 return false;
144 }
146 /** If the expression is a method call, return the method name, null
147 * otherwise. */
148 public static Name calledMethodName(JCTree tree) {
149 if (tree.getTag() == JCTree.EXEC) {
150 JCExpressionStatement exec = (JCExpressionStatement)tree;
151 if (exec.expr.getTag() == JCTree.APPLY) {
152 Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth);
153 return mname;
154 }
155 }
156 return null;
157 }
159 /** Is this a call to this or super?
160 */
161 public static boolean isSelfCall(JCTree tree) {
162 Name name = calledMethodName(tree);
163 if (name != null) {
164 Names names = name.table.names;
165 return name==names._this || name==names._super;
166 } else {
167 return false;
168 }
169 }
171 /** Is this a call to super?
172 */
173 public static boolean isSuperCall(JCTree tree) {
174 Name name = calledMethodName(tree);
175 if (name != null) {
176 Names names = name.table.names;
177 return name==names._super;
178 } else {
179 return false;
180 }
181 }
183 /** Is this a constructor whose first (non-synthetic) statement is not
184 * of the form this(...)?
185 */
186 public static boolean isInitialConstructor(JCTree tree) {
187 JCMethodInvocation app = firstConstructorCall(tree);
188 if (app == null) return false;
189 Name meth = name(app.meth);
190 return meth == null || meth != meth.table.names._this;
191 }
193 /** Return the first call in a constructor definition. */
194 public static JCMethodInvocation firstConstructorCall(JCTree tree) {
195 if (tree.getTag() != JCTree.METHODDEF) return null;
196 JCMethodDecl md = (JCMethodDecl) tree;
197 Names names = md.name.table.names;
198 if (md.name != names.init) return null;
199 if (md.body == null) return null;
200 List<JCStatement> stats = md.body.stats;
201 // Synthetic initializations can appear before the super call.
202 while (stats.nonEmpty() && isSyntheticInit(stats.head))
203 stats = stats.tail;
204 if (stats.isEmpty()) return null;
205 if (stats.head.getTag() != JCTree.EXEC) return null;
206 JCExpressionStatement exec = (JCExpressionStatement) stats.head;
207 if (exec.expr.getTag() != JCTree.APPLY) return null;
208 return (JCMethodInvocation)exec.expr;
209 }
211 /** Return true if a tree represents a diamond new expr. */
212 public static boolean isDiamond(JCTree tree) {
213 switch(tree.getTag()) {
214 case JCTree.TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty();
215 case JCTree.NEWCLASS: return isDiamond(((JCNewClass)tree).clazz);
216 default: return false;
217 }
218 }
220 /** Return true if a tree represents the null literal. */
221 public static boolean isNull(JCTree tree) {
222 if (tree.getTag() != JCTree.LITERAL)
223 return false;
224 JCLiteral lit = (JCLiteral) tree;
225 return (lit.typetag == TypeTags.BOT);
226 }
228 /** The position of the first statement in a block, or the position of
229 * the block itself if it is empty.
230 */
231 public static int firstStatPos(JCTree tree) {
232 if (tree.getTag() == JCTree.BLOCK && ((JCBlock) tree).stats.nonEmpty())
233 return ((JCBlock) tree).stats.head.pos;
234 else
235 return tree.pos;
236 }
238 /** The end position of given tree, if it is a block with
239 * defined endpos.
240 */
241 public static int endPos(JCTree tree) {
242 if (tree.getTag() == JCTree.BLOCK && ((JCBlock) tree).endpos != Position.NOPOS)
243 return ((JCBlock) tree).endpos;
244 else if (tree.getTag() == JCTree.SYNCHRONIZED)
245 return endPos(((JCSynchronized) tree).body);
246 else if (tree.getTag() == JCTree.TRY) {
247 JCTry t = (JCTry) tree;
248 return endPos((t.finalizer != null)
249 ? t.finalizer
250 : t.catchers.last().body);
251 } else
252 return tree.pos;
253 }
256 /** Get the start position for a tree node. The start position is
257 * defined to be the position of the first character of the first
258 * token of the node's source text.
259 * @param tree The tree node
260 */
261 public static int getStartPos(JCTree tree) {
262 if (tree == null)
263 return Position.NOPOS;
265 switch(tree.getTag()) {
266 case(JCTree.APPLY):
267 return getStartPos(((JCMethodInvocation) tree).meth);
268 case(JCTree.ASSIGN):
269 return getStartPos(((JCAssign) tree).lhs);
270 case(JCTree.BITOR_ASG): case(JCTree.BITXOR_ASG): case(JCTree.BITAND_ASG):
271 case(JCTree.SL_ASG): case(JCTree.SR_ASG): case(JCTree.USR_ASG):
272 case(JCTree.PLUS_ASG): case(JCTree.MINUS_ASG): case(JCTree.MUL_ASG):
273 case(JCTree.DIV_ASG): case(JCTree.MOD_ASG):
274 return getStartPos(((JCAssignOp) tree).lhs);
275 case(JCTree.OR): case(JCTree.AND): case(JCTree.BITOR):
276 case(JCTree.BITXOR): case(JCTree.BITAND): case(JCTree.EQ):
277 case(JCTree.NE): case(JCTree.LT): case(JCTree.GT):
278 case(JCTree.LE): case(JCTree.GE): case(JCTree.SL):
279 case(JCTree.SR): case(JCTree.USR): case(JCTree.PLUS):
280 case(JCTree.MINUS): case(JCTree.MUL): case(JCTree.DIV):
281 case(JCTree.MOD):
282 return getStartPos(((JCBinary) tree).lhs);
283 case(JCTree.CLASSDEF): {
284 JCClassDecl node = (JCClassDecl)tree;
285 if (node.mods.pos != Position.NOPOS)
286 return node.mods.pos;
287 break;
288 }
289 case(JCTree.CONDEXPR):
290 return getStartPos(((JCConditional) tree).cond);
291 case(JCTree.EXEC):
292 return getStartPos(((JCExpressionStatement) tree).expr);
293 case(JCTree.INDEXED):
294 return getStartPos(((JCArrayAccess) tree).indexed);
295 case(JCTree.METHODDEF): {
296 JCMethodDecl node = (JCMethodDecl)tree;
297 if (node.mods.pos != Position.NOPOS)
298 return node.mods.pos;
299 if (node.typarams.nonEmpty()) // List.nil() used for no typarams
300 return getStartPos(node.typarams.head);
301 return node.restype == null ? node.pos : getStartPos(node.restype);
302 }
303 case(JCTree.SELECT):
304 return getStartPos(((JCFieldAccess) tree).selected);
305 case(JCTree.TYPEAPPLY):
306 return getStartPos(((JCTypeApply) tree).clazz);
307 case(JCTree.TYPEARRAY):
308 return getStartPos(((JCArrayTypeTree) tree).elemtype);
309 case(JCTree.TYPETEST):
310 return getStartPos(((JCInstanceOf) tree).expr);
311 case(JCTree.POSTINC):
312 case(JCTree.POSTDEC):
313 return getStartPos(((JCUnary) tree).arg);
314 case(JCTree.ANNOTATED_TYPE): {
315 JCAnnotatedType node = (JCAnnotatedType) tree;
316 if (node.annotations.nonEmpty())
317 return getStartPos(node.annotations.head);
318 return getStartPos(node.underlyingType);
319 }
320 case(JCTree.NEWCLASS): {
321 JCNewClass node = (JCNewClass)tree;
322 if (node.encl != null)
323 return getStartPos(node.encl);
324 break;
325 }
326 case(JCTree.VARDEF): {
327 JCVariableDecl node = (JCVariableDecl)tree;
328 if (node.mods.pos != Position.NOPOS) {
329 return node.mods.pos;
330 } else {
331 return getStartPos(node.vartype);
332 }
333 }
334 case(JCTree.ERRONEOUS): {
335 JCErroneous node = (JCErroneous)tree;
336 if (node.errs != null && node.errs.nonEmpty())
337 return getStartPos(node.errs.head);
338 }
339 }
340 return tree.pos;
341 }
343 /** The end position of given tree, given a table of end positions generated by the parser
344 */
345 public static int getEndPos(JCTree tree, Map<JCTree, Integer> endPositions) {
346 if (tree == null)
347 return Position.NOPOS;
349 if (endPositions == null) {
350 // fall back on limited info in the tree
351 return endPos(tree);
352 }
354 Integer mapPos = endPositions.get(tree);
355 if (mapPos != null)
356 return mapPos;
358 switch(tree.getTag()) {
359 case(JCTree.BITOR_ASG): case(JCTree.BITXOR_ASG): case(JCTree.BITAND_ASG):
360 case(JCTree.SL_ASG): case(JCTree.SR_ASG): case(JCTree.USR_ASG):
361 case(JCTree.PLUS_ASG): case(JCTree.MINUS_ASG): case(JCTree.MUL_ASG):
362 case(JCTree.DIV_ASG): case(JCTree.MOD_ASG):
363 return getEndPos(((JCAssignOp) tree).rhs, endPositions);
364 case(JCTree.OR): case(JCTree.AND): case(JCTree.BITOR):
365 case(JCTree.BITXOR): case(JCTree.BITAND): case(JCTree.EQ):
366 case(JCTree.NE): case(JCTree.LT): case(JCTree.GT):
367 case(JCTree.LE): case(JCTree.GE): case(JCTree.SL):
368 case(JCTree.SR): case(JCTree.USR): case(JCTree.PLUS):
369 case(JCTree.MINUS): case(JCTree.MUL): case(JCTree.DIV):
370 case(JCTree.MOD):
371 return getEndPos(((JCBinary) tree).rhs, endPositions);
372 case(JCTree.CASE):
373 return getEndPos(((JCCase) tree).stats.last(), endPositions);
374 case(JCTree.CATCH):
375 return getEndPos(((JCCatch) tree).body, endPositions);
376 case(JCTree.CONDEXPR):
377 return getEndPos(((JCConditional) tree).falsepart, endPositions);
378 case(JCTree.FORLOOP):
379 return getEndPos(((JCForLoop) tree).body, endPositions);
380 case(JCTree.FOREACHLOOP):
381 return getEndPos(((JCEnhancedForLoop) tree).body, endPositions);
382 case(JCTree.IF): {
383 JCIf node = (JCIf)tree;
384 if (node.elsepart == null) {
385 return getEndPos(node.thenpart, endPositions);
386 } else {
387 return getEndPos(node.elsepart, endPositions);
388 }
389 }
390 case(JCTree.LABELLED):
391 return getEndPos(((JCLabeledStatement) tree).body, endPositions);
392 case(JCTree.MODIFIERS):
393 return getEndPos(((JCModifiers) tree).annotations.last(), endPositions);
394 case(JCTree.SYNCHRONIZED):
395 return getEndPos(((JCSynchronized) tree).body, endPositions);
396 case(JCTree.TOPLEVEL):
397 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPositions);
398 case(JCTree.TRY): {
399 JCTry node = (JCTry)tree;
400 if (node.finalizer != null) {
401 return getEndPos(node.finalizer, endPositions);
402 } else if (!node.catchers.isEmpty()) {
403 return getEndPos(node.catchers.last(), endPositions);
404 } else {
405 return getEndPos(node.body, endPositions);
406 }
407 }
408 case(JCTree.WILDCARD):
409 return getEndPos(((JCWildcard) tree).inner, endPositions);
410 case(JCTree.TYPECAST):
411 return getEndPos(((JCTypeCast) tree).expr, endPositions);
412 case(JCTree.TYPETEST):
413 return getEndPos(((JCInstanceOf) tree).clazz, endPositions);
414 case(JCTree.POS):
415 case(JCTree.NEG):
416 case(JCTree.NOT):
417 case(JCTree.COMPL):
418 case(JCTree.PREINC):
419 case(JCTree.PREDEC):
420 return getEndPos(((JCUnary) tree).arg, endPositions);
421 case(JCTree.WHILELOOP):
422 return getEndPos(((JCWhileLoop) tree).body, endPositions);
423 case(JCTree.ANNOTATED_TYPE):
424 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPositions);
425 case(JCTree.ERRONEOUS): {
426 JCErroneous node = (JCErroneous)tree;
427 if (node.errs != null && node.errs.nonEmpty())
428 return getEndPos(node.errs.last(), endPositions);
429 }
430 }
431 return Position.NOPOS;
432 }
435 /** A DiagnosticPosition with the preferred position set to the
436 * end position of given tree, if it is a block with
437 * defined endpos.
438 */
439 public static DiagnosticPosition diagEndPos(final JCTree tree) {
440 final int endPos = TreeInfo.endPos(tree);
441 return new DiagnosticPosition() {
442 public JCTree getTree() { return tree; }
443 public int getStartPosition() { return TreeInfo.getStartPos(tree); }
444 public int getPreferredPosition() { return endPos; }
445 public int getEndPosition(Map<JCTree, Integer> endPosTable) {
446 return TreeInfo.getEndPos(tree, endPosTable);
447 }
448 };
449 }
451 /** The position of the finalizer of given try/synchronized statement.
452 */
453 public static int finalizerPos(JCTree tree) {
454 if (tree.getTag() == JCTree.TRY) {
455 JCTry t = (JCTry) tree;
456 assert t.finalizer != null;
457 return firstStatPos(t.finalizer);
458 } else if (tree.getTag() == JCTree.SYNCHRONIZED) {
459 return endPos(((JCSynchronized) tree).body);
460 } else {
461 throw new AssertionError();
462 }
463 }
465 /** Find the position for reporting an error about a symbol, where
466 * that symbol is defined somewhere in the given tree. */
467 public static int positionFor(final Symbol sym, final JCTree tree) {
468 JCTree decl = declarationFor(sym, tree);
469 return ((decl != null) ? decl : tree).pos;
470 }
472 /** Find the position for reporting an error about a symbol, where
473 * that symbol is defined somewhere in the given tree. */
474 public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) {
475 JCTree decl = declarationFor(sym, tree);
476 return ((decl != null) ? decl : tree).pos();
477 }
479 /** Find the declaration for a symbol, where
480 * that symbol is defined somewhere in the given tree. */
481 public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
482 class DeclScanner extends TreeScanner {
483 JCTree result = null;
484 public void scan(JCTree tree) {
485 if (tree!=null && result==null)
486 tree.accept(this);
487 }
488 public void visitTopLevel(JCCompilationUnit that) {
489 if (that.packge == sym) result = that;
490 else super.visitTopLevel(that);
491 }
492 public void visitClassDef(JCClassDecl that) {
493 if (that.sym == sym) result = that;
494 else super.visitClassDef(that);
495 }
496 public void visitMethodDef(JCMethodDecl that) {
497 if (that.sym == sym) result = that;
498 else super.visitMethodDef(that);
499 }
500 public void visitVarDef(JCVariableDecl that) {
501 if (that.sym == sym) result = that;
502 else super.visitVarDef(that);
503 }
504 }
505 DeclScanner s = new DeclScanner();
506 tree.accept(s);
507 return s.result;
508 }
510 public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
511 return scopeFor(pathFor(node, unit));
512 }
514 public static Env<AttrContext> scopeFor(List<JCTree> path) {
515 // TODO: not implemented yet
516 throw new UnsupportedOperationException("not implemented yet");
517 }
519 public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) {
520 class Result extends Error {
521 static final long serialVersionUID = -5942088234594905625L;
522 List<JCTree> path;
523 Result(List<JCTree> path) {
524 this.path = path;
525 }
526 }
527 class PathFinder extends TreeScanner {
528 List<JCTree> path = List.nil();
529 public void scan(JCTree tree) {
530 if (tree != null) {
531 path = path.prepend(tree);
532 if (tree == node)
533 throw new Result(path);
534 super.scan(tree);
535 path = path.tail;
536 }
537 }
538 }
539 try {
540 new PathFinder().scan(unit);
541 } catch (Result result) {
542 return result.path;
543 }
544 return List.nil();
545 }
547 /** Return the statement referenced by a label.
548 * If the label refers to a loop or switch, return that switch
549 * otherwise return the labelled statement itself
550 */
551 public static JCTree referencedStatement(JCLabeledStatement tree) {
552 JCTree t = tree;
553 do t = ((JCLabeledStatement) t).body;
554 while (t.getTag() == JCTree.LABELLED);
555 switch (t.getTag()) {
556 case JCTree.DOLOOP: case JCTree.WHILELOOP: case JCTree.FORLOOP: case JCTree.FOREACHLOOP: case JCTree.SWITCH:
557 return t;
558 default:
559 return tree;
560 }
561 }
563 /** Skip parens and return the enclosed expression
564 */
565 public static JCExpression skipParens(JCExpression tree) {
566 while (tree.getTag() == JCTree.PARENS) {
567 tree = ((JCParens) tree).expr;
568 }
569 return tree;
570 }
572 /** Skip parens and return the enclosed expression
573 */
574 public static JCTree skipParens(JCTree tree) {
575 if (tree.getTag() == JCTree.PARENS)
576 return skipParens((JCParens)tree);
577 else
578 return tree;
579 }
581 /** Return the types of a list of trees.
582 */
583 public static List<Type> types(List<? extends JCTree> trees) {
584 ListBuffer<Type> ts = new ListBuffer<Type>();
585 for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
586 ts.append(l.head.type);
587 return ts.toList();
588 }
590 /** If this tree is an identifier or a field or a parameterized type,
591 * return its name, otherwise return null.
592 */
593 public static Name name(JCTree tree) {
594 switch (tree.getTag()) {
595 case JCTree.IDENT:
596 return ((JCIdent) tree).name;
597 case JCTree.SELECT:
598 return ((JCFieldAccess) tree).name;
599 case JCTree.TYPEAPPLY:
600 return name(((JCTypeApply) tree).clazz);
601 default:
602 return null;
603 }
604 }
606 /** If this tree is a qualified identifier, its return fully qualified name,
607 * otherwise return null.
608 */
609 public static Name fullName(JCTree tree) {
610 tree = skipParens(tree);
611 switch (tree.getTag()) {
612 case JCTree.IDENT:
613 return ((JCIdent) tree).name;
614 case JCTree.SELECT:
615 Name sname = fullName(((JCFieldAccess) tree).selected);
616 return sname == null ? null : sname.append('.', name(tree));
617 default:
618 return null;
619 }
620 }
622 public static Symbol symbolFor(JCTree node) {
623 node = skipParens(node);
624 switch (node.getTag()) {
625 case JCTree.CLASSDEF:
626 return ((JCClassDecl) node).sym;
627 case JCTree.METHODDEF:
628 return ((JCMethodDecl) node).sym;
629 case JCTree.VARDEF:
630 return ((JCVariableDecl) node).sym;
631 default:
632 return null;
633 }
634 }
636 /** If this tree is an identifier or a field, return its symbol,
637 * otherwise return null.
638 */
639 public static Symbol symbol(JCTree tree) {
640 tree = skipParens(tree);
641 switch (tree.getTag()) {
642 case JCTree.IDENT:
643 return ((JCIdent) tree).sym;
644 case JCTree.SELECT:
645 return ((JCFieldAccess) tree).sym;
646 case JCTree.TYPEAPPLY:
647 return symbol(((JCTypeApply) tree).clazz);
648 default:
649 return null;
650 }
651 }
653 /** Return true if this is a nonstatic selection. */
654 public static boolean nonstaticSelect(JCTree tree) {
655 tree = skipParens(tree);
656 if (tree.getTag() != JCTree.SELECT) return false;
657 JCFieldAccess s = (JCFieldAccess) tree;
658 Symbol e = symbol(s.selected);
659 return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
660 }
662 /** If this tree is an identifier or a field, set its symbol, otherwise skip.
663 */
664 public static void setSymbol(JCTree tree, Symbol sym) {
665 tree = skipParens(tree);
666 switch (tree.getTag()) {
667 case JCTree.IDENT:
668 ((JCIdent) tree).sym = sym; break;
669 case JCTree.SELECT:
670 ((JCFieldAccess) tree).sym = sym; break;
671 default:
672 }
673 }
675 /** If this tree is a declaration or a block, return its flags field,
676 * otherwise return 0.
677 */
678 public static long flags(JCTree tree) {
679 switch (tree.getTag()) {
680 case JCTree.VARDEF:
681 return ((JCVariableDecl) tree).mods.flags;
682 case JCTree.METHODDEF:
683 return ((JCMethodDecl) tree).mods.flags;
684 case JCTree.CLASSDEF:
685 return ((JCClassDecl) tree).mods.flags;
686 case JCTree.BLOCK:
687 return ((JCBlock) tree).flags;
688 default:
689 return 0;
690 }
691 }
693 /** Return first (smallest) flag in `flags':
694 * pre: flags != 0
695 */
696 public static long firstFlag(long flags) {
697 int flag = 1;
698 while ((flag & StandardFlags) != 0 && (flag & flags) == 0)
699 flag = flag << 1;
700 return flag;
701 }
703 /** Return flags as a string, separated by " ".
704 */
705 public static String flagNames(long flags) {
706 return Flags.toString(flags & StandardFlags).trim();
707 }
709 /** Operator precedences values.
710 */
711 public static final int
712 notExpression = -1, // not an expression
713 noPrec = 0, // no enclosing expression
714 assignPrec = 1,
715 assignopPrec = 2,
716 condPrec = 3,
717 orPrec = 4,
718 andPrec = 5,
719 bitorPrec = 6,
720 bitxorPrec = 7,
721 bitandPrec = 8,
722 eqPrec = 9,
723 ordPrec = 10,
724 shiftPrec = 11,
725 addPrec = 12,
726 mulPrec = 13,
727 prefixPrec = 14,
728 postfixPrec = 15,
729 precCount = 16;
732 /** Map operators to their precedence levels.
733 */
734 public static int opPrec(int op) {
735 switch(op) {
736 case JCTree.POS:
737 case JCTree.NEG:
738 case JCTree.NOT:
739 case JCTree.COMPL:
740 case JCTree.PREINC:
741 case JCTree.PREDEC: return prefixPrec;
742 case JCTree.POSTINC:
743 case JCTree.POSTDEC:
744 case JCTree.NULLCHK: return postfixPrec;
745 case JCTree.ASSIGN: return assignPrec;
746 case JCTree.BITOR_ASG:
747 case JCTree.BITXOR_ASG:
748 case JCTree.BITAND_ASG:
749 case JCTree.SL_ASG:
750 case JCTree.SR_ASG:
751 case JCTree.USR_ASG:
752 case JCTree.PLUS_ASG:
753 case JCTree.MINUS_ASG:
754 case JCTree.MUL_ASG:
755 case JCTree.DIV_ASG:
756 case JCTree.MOD_ASG: return assignopPrec;
757 case JCTree.OR: return orPrec;
758 case JCTree.AND: return andPrec;
759 case JCTree.EQ:
760 case JCTree.NE: return eqPrec;
761 case JCTree.LT:
762 case JCTree.GT:
763 case JCTree.LE:
764 case JCTree.GE: return ordPrec;
765 case JCTree.BITOR: return bitorPrec;
766 case JCTree.BITXOR: return bitxorPrec;
767 case JCTree.BITAND: return bitandPrec;
768 case JCTree.SL:
769 case JCTree.SR:
770 case JCTree.USR: return shiftPrec;
771 case JCTree.PLUS:
772 case JCTree.MINUS: return addPrec;
773 case JCTree.MUL:
774 case JCTree.DIV:
775 case JCTree.MOD: return mulPrec;
776 case JCTree.TYPETEST: return ordPrec;
777 default: throw new AssertionError();
778 }
779 }
781 static Tree.Kind tagToKind(int tag) {
782 switch (tag) {
783 // Postfix expressions
784 case JCTree.POSTINC: // _ ++
785 return Tree.Kind.POSTFIX_INCREMENT;
786 case JCTree.POSTDEC: // _ --
787 return Tree.Kind.POSTFIX_DECREMENT;
789 // Unary operators
790 case JCTree.PREINC: // ++ _
791 return Tree.Kind.PREFIX_INCREMENT;
792 case JCTree.PREDEC: // -- _
793 return Tree.Kind.PREFIX_DECREMENT;
794 case JCTree.POS: // +
795 return Tree.Kind.UNARY_PLUS;
796 case JCTree.NEG: // -
797 return Tree.Kind.UNARY_MINUS;
798 case JCTree.COMPL: // ~
799 return Tree.Kind.BITWISE_COMPLEMENT;
800 case JCTree.NOT: // !
801 return Tree.Kind.LOGICAL_COMPLEMENT;
803 // Binary operators
805 // Multiplicative operators
806 case JCTree.MUL: // *
807 return Tree.Kind.MULTIPLY;
808 case JCTree.DIV: // /
809 return Tree.Kind.DIVIDE;
810 case JCTree.MOD: // %
811 return Tree.Kind.REMAINDER;
813 // Additive operators
814 case JCTree.PLUS: // +
815 return Tree.Kind.PLUS;
816 case JCTree.MINUS: // -
817 return Tree.Kind.MINUS;
819 // Shift operators
820 case JCTree.SL: // <<
821 return Tree.Kind.LEFT_SHIFT;
822 case JCTree.SR: // >>
823 return Tree.Kind.RIGHT_SHIFT;
824 case JCTree.USR: // >>>
825 return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
827 // Relational operators
828 case JCTree.LT: // <
829 return Tree.Kind.LESS_THAN;
830 case JCTree.GT: // >
831 return Tree.Kind.GREATER_THAN;
832 case JCTree.LE: // <=
833 return Tree.Kind.LESS_THAN_EQUAL;
834 case JCTree.GE: // >=
835 return Tree.Kind.GREATER_THAN_EQUAL;
837 // Equality operators
838 case JCTree.EQ: // ==
839 return Tree.Kind.EQUAL_TO;
840 case JCTree.NE: // !=
841 return Tree.Kind.NOT_EQUAL_TO;
843 // Bitwise and logical operators
844 case JCTree.BITAND: // &
845 return Tree.Kind.AND;
846 case JCTree.BITXOR: // ^
847 return Tree.Kind.XOR;
848 case JCTree.BITOR: // |
849 return Tree.Kind.OR;
851 // Conditional operators
852 case JCTree.AND: // &&
853 return Tree.Kind.CONDITIONAL_AND;
854 case JCTree.OR: // ||
855 return Tree.Kind.CONDITIONAL_OR;
857 // Assignment operators
858 case JCTree.MUL_ASG: // *=
859 return Tree.Kind.MULTIPLY_ASSIGNMENT;
860 case JCTree.DIV_ASG: // /=
861 return Tree.Kind.DIVIDE_ASSIGNMENT;
862 case JCTree.MOD_ASG: // %=
863 return Tree.Kind.REMAINDER_ASSIGNMENT;
864 case JCTree.PLUS_ASG: // +=
865 return Tree.Kind.PLUS_ASSIGNMENT;
866 case JCTree.MINUS_ASG: // -=
867 return Tree.Kind.MINUS_ASSIGNMENT;
868 case JCTree.SL_ASG: // <<=
869 return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
870 case JCTree.SR_ASG: // >>=
871 return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
872 case JCTree.USR_ASG: // >>>=
873 return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
874 case JCTree.BITAND_ASG: // &=
875 return Tree.Kind.AND_ASSIGNMENT;
876 case JCTree.BITXOR_ASG: // ^=
877 return Tree.Kind.XOR_ASSIGNMENT;
878 case JCTree.BITOR_ASG: // |=
879 return Tree.Kind.OR_ASSIGNMENT;
881 // Null check (implementation detail), for example, __.getClass()
882 case JCTree.NULLCHK:
883 return Tree.Kind.OTHER;
885 default:
886 return null;
887 }
888 }
890 /**
891 * Returns the underlying type of the tree if it is annotated type,
892 * or the tree itself otherwise
893 */
894 public static JCExpression typeIn(JCExpression tree) {
895 switch (tree.getTag()) {
896 case JCTree.ANNOTATED_TYPE:
897 return ((JCAnnotatedType)tree).underlyingType;
898 case JCTree.IDENT: /* simple names */
899 case JCTree.TYPEIDENT: /* primitive name */
900 case JCTree.SELECT: /* qualified name */
901 case JCTree.TYPEARRAY: /* array types */
902 case JCTree.WILDCARD: /* wild cards */
903 case JCTree.TYPEPARAMETER: /* type parameters */
904 case JCTree.TYPEAPPLY: /* parameterized types */
905 return tree;
906 default:
907 throw new AssertionError("Unexpected type tree: " + tree);
908 }
909 }
911 public static JCTree innermostType(JCTree type) {
912 switch (type.getTag()) {
913 case JCTree.TYPEARRAY:
914 return innermostType(((JCArrayTypeTree)type).elemtype);
915 case JCTree.WILDCARD:
916 return innermostType(((JCWildcard)type).inner);
917 case JCTree.ANNOTATED_TYPE:
918 return innermostType(((JCAnnotatedType)type).underlyingType);
919 default:
920 return type;
921 }
922 }
923 }