Mon, 06 Sep 2010 12:55:09 -0700
6930507: Symbols for anonymous and local classes made too late for use by java tree API
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 public void visitTypeParameter(JCTypeParameter that) {
505 if (that.type.tsym == sym) result = that;
506 else super.visitTypeParameter(that);
507 }
508 }
509 DeclScanner s = new DeclScanner();
510 tree.accept(s);
511 return s.result;
512 }
514 public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
515 return scopeFor(pathFor(node, unit));
516 }
518 public static Env<AttrContext> scopeFor(List<JCTree> path) {
519 // TODO: not implemented yet
520 throw new UnsupportedOperationException("not implemented yet");
521 }
523 public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) {
524 class Result extends Error {
525 static final long serialVersionUID = -5942088234594905625L;
526 List<JCTree> path;
527 Result(List<JCTree> path) {
528 this.path = path;
529 }
530 }
531 class PathFinder extends TreeScanner {
532 List<JCTree> path = List.nil();
533 public void scan(JCTree tree) {
534 if (tree != null) {
535 path = path.prepend(tree);
536 if (tree == node)
537 throw new Result(path);
538 super.scan(tree);
539 path = path.tail;
540 }
541 }
542 }
543 try {
544 new PathFinder().scan(unit);
545 } catch (Result result) {
546 return result.path;
547 }
548 return List.nil();
549 }
551 /** Return the statement referenced by a label.
552 * If the label refers to a loop or switch, return that switch
553 * otherwise return the labelled statement itself
554 */
555 public static JCTree referencedStatement(JCLabeledStatement tree) {
556 JCTree t = tree;
557 do t = ((JCLabeledStatement) t).body;
558 while (t.getTag() == JCTree.LABELLED);
559 switch (t.getTag()) {
560 case JCTree.DOLOOP: case JCTree.WHILELOOP: case JCTree.FORLOOP: case JCTree.FOREACHLOOP: case JCTree.SWITCH:
561 return t;
562 default:
563 return tree;
564 }
565 }
567 /** Skip parens and return the enclosed expression
568 */
569 public static JCExpression skipParens(JCExpression tree) {
570 while (tree.getTag() == JCTree.PARENS) {
571 tree = ((JCParens) tree).expr;
572 }
573 return tree;
574 }
576 /** Skip parens and return the enclosed expression
577 */
578 public static JCTree skipParens(JCTree tree) {
579 if (tree.getTag() == JCTree.PARENS)
580 return skipParens((JCParens)tree);
581 else
582 return tree;
583 }
585 /** Return the types of a list of trees.
586 */
587 public static List<Type> types(List<? extends JCTree> trees) {
588 ListBuffer<Type> ts = new ListBuffer<Type>();
589 for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
590 ts.append(l.head.type);
591 return ts.toList();
592 }
594 /** If this tree is an identifier or a field or a parameterized type,
595 * return its name, otherwise return null.
596 */
597 public static Name name(JCTree tree) {
598 switch (tree.getTag()) {
599 case JCTree.IDENT:
600 return ((JCIdent) tree).name;
601 case JCTree.SELECT:
602 return ((JCFieldAccess) tree).name;
603 case JCTree.TYPEAPPLY:
604 return name(((JCTypeApply) tree).clazz);
605 default:
606 return null;
607 }
608 }
610 /** If this tree is a qualified identifier, its return fully qualified name,
611 * otherwise return null.
612 */
613 public static Name fullName(JCTree tree) {
614 tree = skipParens(tree);
615 switch (tree.getTag()) {
616 case JCTree.IDENT:
617 return ((JCIdent) tree).name;
618 case JCTree.SELECT:
619 Name sname = fullName(((JCFieldAccess) tree).selected);
620 return sname == null ? null : sname.append('.', name(tree));
621 default:
622 return null;
623 }
624 }
626 public static Symbol symbolFor(JCTree node) {
627 node = skipParens(node);
628 switch (node.getTag()) {
629 case JCTree.CLASSDEF:
630 return ((JCClassDecl) node).sym;
631 case JCTree.METHODDEF:
632 return ((JCMethodDecl) node).sym;
633 case JCTree.VARDEF:
634 return ((JCVariableDecl) node).sym;
635 default:
636 return null;
637 }
638 }
640 public static boolean isDeclaration(JCTree node) {
641 node = skipParens(node);
642 switch (node.getTag()) {
643 case JCTree.CLASSDEF:
644 case JCTree.METHODDEF:
645 case JCTree.VARDEF:
646 return true;
647 default:
648 return false;
649 }
650 }
652 /** If this tree is an identifier or a field, return its symbol,
653 * otherwise return null.
654 */
655 public static Symbol symbol(JCTree tree) {
656 tree = skipParens(tree);
657 switch (tree.getTag()) {
658 case JCTree.IDENT:
659 return ((JCIdent) tree).sym;
660 case JCTree.SELECT:
661 return ((JCFieldAccess) tree).sym;
662 case JCTree.TYPEAPPLY:
663 return symbol(((JCTypeApply) tree).clazz);
664 default:
665 return null;
666 }
667 }
669 /** Return true if this is a nonstatic selection. */
670 public static boolean nonstaticSelect(JCTree tree) {
671 tree = skipParens(tree);
672 if (tree.getTag() != JCTree.SELECT) return false;
673 JCFieldAccess s = (JCFieldAccess) tree;
674 Symbol e = symbol(s.selected);
675 return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
676 }
678 /** If this tree is an identifier or a field, set its symbol, otherwise skip.
679 */
680 public static void setSymbol(JCTree tree, Symbol sym) {
681 tree = skipParens(tree);
682 switch (tree.getTag()) {
683 case JCTree.IDENT:
684 ((JCIdent) tree).sym = sym; break;
685 case JCTree.SELECT:
686 ((JCFieldAccess) tree).sym = sym; break;
687 default:
688 }
689 }
691 /** If this tree is a declaration or a block, return its flags field,
692 * otherwise return 0.
693 */
694 public static long flags(JCTree tree) {
695 switch (tree.getTag()) {
696 case JCTree.VARDEF:
697 return ((JCVariableDecl) tree).mods.flags;
698 case JCTree.METHODDEF:
699 return ((JCMethodDecl) tree).mods.flags;
700 case JCTree.CLASSDEF:
701 return ((JCClassDecl) tree).mods.flags;
702 case JCTree.BLOCK:
703 return ((JCBlock) tree).flags;
704 default:
705 return 0;
706 }
707 }
709 /** Return first (smallest) flag in `flags':
710 * pre: flags != 0
711 */
712 public static long firstFlag(long flags) {
713 int flag = 1;
714 while ((flag & StandardFlags) != 0 && (flag & flags) == 0)
715 flag = flag << 1;
716 return flag;
717 }
719 /** Return flags as a string, separated by " ".
720 */
721 public static String flagNames(long flags) {
722 return Flags.toString(flags & StandardFlags).trim();
723 }
725 /** Operator precedences values.
726 */
727 public static final int
728 notExpression = -1, // not an expression
729 noPrec = 0, // no enclosing expression
730 assignPrec = 1,
731 assignopPrec = 2,
732 condPrec = 3,
733 orPrec = 4,
734 andPrec = 5,
735 bitorPrec = 6,
736 bitxorPrec = 7,
737 bitandPrec = 8,
738 eqPrec = 9,
739 ordPrec = 10,
740 shiftPrec = 11,
741 addPrec = 12,
742 mulPrec = 13,
743 prefixPrec = 14,
744 postfixPrec = 15,
745 precCount = 16;
748 /** Map operators to their precedence levels.
749 */
750 public static int opPrec(int op) {
751 switch(op) {
752 case JCTree.POS:
753 case JCTree.NEG:
754 case JCTree.NOT:
755 case JCTree.COMPL:
756 case JCTree.PREINC:
757 case JCTree.PREDEC: return prefixPrec;
758 case JCTree.POSTINC:
759 case JCTree.POSTDEC:
760 case JCTree.NULLCHK: return postfixPrec;
761 case JCTree.ASSIGN: return assignPrec;
762 case JCTree.BITOR_ASG:
763 case JCTree.BITXOR_ASG:
764 case JCTree.BITAND_ASG:
765 case JCTree.SL_ASG:
766 case JCTree.SR_ASG:
767 case JCTree.USR_ASG:
768 case JCTree.PLUS_ASG:
769 case JCTree.MINUS_ASG:
770 case JCTree.MUL_ASG:
771 case JCTree.DIV_ASG:
772 case JCTree.MOD_ASG: return assignopPrec;
773 case JCTree.OR: return orPrec;
774 case JCTree.AND: return andPrec;
775 case JCTree.EQ:
776 case JCTree.NE: return eqPrec;
777 case JCTree.LT:
778 case JCTree.GT:
779 case JCTree.LE:
780 case JCTree.GE: return ordPrec;
781 case JCTree.BITOR: return bitorPrec;
782 case JCTree.BITXOR: return bitxorPrec;
783 case JCTree.BITAND: return bitandPrec;
784 case JCTree.SL:
785 case JCTree.SR:
786 case JCTree.USR: return shiftPrec;
787 case JCTree.PLUS:
788 case JCTree.MINUS: return addPrec;
789 case JCTree.MUL:
790 case JCTree.DIV:
791 case JCTree.MOD: return mulPrec;
792 case JCTree.TYPETEST: return ordPrec;
793 default: throw new AssertionError();
794 }
795 }
797 static Tree.Kind tagToKind(int tag) {
798 switch (tag) {
799 // Postfix expressions
800 case JCTree.POSTINC: // _ ++
801 return Tree.Kind.POSTFIX_INCREMENT;
802 case JCTree.POSTDEC: // _ --
803 return Tree.Kind.POSTFIX_DECREMENT;
805 // Unary operators
806 case JCTree.PREINC: // ++ _
807 return Tree.Kind.PREFIX_INCREMENT;
808 case JCTree.PREDEC: // -- _
809 return Tree.Kind.PREFIX_DECREMENT;
810 case JCTree.POS: // +
811 return Tree.Kind.UNARY_PLUS;
812 case JCTree.NEG: // -
813 return Tree.Kind.UNARY_MINUS;
814 case JCTree.COMPL: // ~
815 return Tree.Kind.BITWISE_COMPLEMENT;
816 case JCTree.NOT: // !
817 return Tree.Kind.LOGICAL_COMPLEMENT;
819 // Binary operators
821 // Multiplicative operators
822 case JCTree.MUL: // *
823 return Tree.Kind.MULTIPLY;
824 case JCTree.DIV: // /
825 return Tree.Kind.DIVIDE;
826 case JCTree.MOD: // %
827 return Tree.Kind.REMAINDER;
829 // Additive operators
830 case JCTree.PLUS: // +
831 return Tree.Kind.PLUS;
832 case JCTree.MINUS: // -
833 return Tree.Kind.MINUS;
835 // Shift operators
836 case JCTree.SL: // <<
837 return Tree.Kind.LEFT_SHIFT;
838 case JCTree.SR: // >>
839 return Tree.Kind.RIGHT_SHIFT;
840 case JCTree.USR: // >>>
841 return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
843 // Relational operators
844 case JCTree.LT: // <
845 return Tree.Kind.LESS_THAN;
846 case JCTree.GT: // >
847 return Tree.Kind.GREATER_THAN;
848 case JCTree.LE: // <=
849 return Tree.Kind.LESS_THAN_EQUAL;
850 case JCTree.GE: // >=
851 return Tree.Kind.GREATER_THAN_EQUAL;
853 // Equality operators
854 case JCTree.EQ: // ==
855 return Tree.Kind.EQUAL_TO;
856 case JCTree.NE: // !=
857 return Tree.Kind.NOT_EQUAL_TO;
859 // Bitwise and logical operators
860 case JCTree.BITAND: // &
861 return Tree.Kind.AND;
862 case JCTree.BITXOR: // ^
863 return Tree.Kind.XOR;
864 case JCTree.BITOR: // |
865 return Tree.Kind.OR;
867 // Conditional operators
868 case JCTree.AND: // &&
869 return Tree.Kind.CONDITIONAL_AND;
870 case JCTree.OR: // ||
871 return Tree.Kind.CONDITIONAL_OR;
873 // Assignment operators
874 case JCTree.MUL_ASG: // *=
875 return Tree.Kind.MULTIPLY_ASSIGNMENT;
876 case JCTree.DIV_ASG: // /=
877 return Tree.Kind.DIVIDE_ASSIGNMENT;
878 case JCTree.MOD_ASG: // %=
879 return Tree.Kind.REMAINDER_ASSIGNMENT;
880 case JCTree.PLUS_ASG: // +=
881 return Tree.Kind.PLUS_ASSIGNMENT;
882 case JCTree.MINUS_ASG: // -=
883 return Tree.Kind.MINUS_ASSIGNMENT;
884 case JCTree.SL_ASG: // <<=
885 return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
886 case JCTree.SR_ASG: // >>=
887 return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
888 case JCTree.USR_ASG: // >>>=
889 return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
890 case JCTree.BITAND_ASG: // &=
891 return Tree.Kind.AND_ASSIGNMENT;
892 case JCTree.BITXOR_ASG: // ^=
893 return Tree.Kind.XOR_ASSIGNMENT;
894 case JCTree.BITOR_ASG: // |=
895 return Tree.Kind.OR_ASSIGNMENT;
897 // Null check (implementation detail), for example, __.getClass()
898 case JCTree.NULLCHK:
899 return Tree.Kind.OTHER;
901 default:
902 return null;
903 }
904 }
906 /**
907 * Returns the underlying type of the tree if it is annotated type,
908 * or the tree itself otherwise
909 */
910 public static JCExpression typeIn(JCExpression tree) {
911 switch (tree.getTag()) {
912 case JCTree.ANNOTATED_TYPE:
913 return ((JCAnnotatedType)tree).underlyingType;
914 case JCTree.IDENT: /* simple names */
915 case JCTree.TYPEIDENT: /* primitive name */
916 case JCTree.SELECT: /* qualified name */
917 case JCTree.TYPEARRAY: /* array types */
918 case JCTree.WILDCARD: /* wild cards */
919 case JCTree.TYPEPARAMETER: /* type parameters */
920 case JCTree.TYPEAPPLY: /* parameterized types */
921 return tree;
922 default:
923 throw new AssertionError("Unexpected type tree: " + tree);
924 }
925 }
927 public static JCTree innermostType(JCTree type) {
928 switch (type.getTag()) {
929 case JCTree.TYPEARRAY:
930 return innermostType(((JCArrayTypeTree)type).elemtype);
931 case JCTree.WILDCARD:
932 return innermostType(((JCWildcard)type).inner);
933 case JCTree.ANNOTATED_TYPE:
934 return innermostType(((JCAnnotatedType)type).underlyingType);
935 default:
936 return type;
937 }
938 }
939 }