duke@1: /*
jjg@1521: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.tree;
duke@1:
jjg@1280:
jjg@1455:
duke@1: import com.sun.source.tree.Tree;
jjg@1280: import com.sun.tools.javac.code.*;
duke@1: import com.sun.tools.javac.comp.AttrContext;
duke@1: import com.sun.tools.javac.comp.Env;
jjg@1280: import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1510: import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
duke@1: import com.sun.tools.javac.util.*;
duke@1: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1: import static com.sun.tools.javac.code.Flags.*;
jjg@1374: import static com.sun.tools.javac.code.TypeTag.BOT;
jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.*;
jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
duke@1:
duke@1: /** Utility class containing inspector methods for trees.
duke@1: *
jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class TreeInfo {
duke@1: protected static final Context.Key treeInfoKey =
duke@1: new Context.Key();
duke@1:
duke@1: public static TreeInfo instance(Context context) {
duke@1: TreeInfo instance = context.get(treeInfoKey);
duke@1: if (instance == null)
duke@1: instance = new TreeInfo(context);
duke@1: return instance;
duke@1: }
duke@1:
duke@1: /** The names of all operators.
duke@1: */
jjg@1127: private Name[] opname = new Name[Tag.getNumberOfOperators()];
jjg@1127:
jjg@1127: private void setOpname(Tag tag, String name, Names names) {
jjg@1127: setOpname(tag, names.fromString(name));
jjg@1127: }
jjg@1127: private void setOpname(Tag tag, Name name) {
jjg@1127: opname[tag.operatorIndex()] = name;
jjg@1127: }
duke@1:
duke@1: private TreeInfo(Context context) {
duke@1: context.put(treeInfoKey, this);
duke@1:
jjg@113: Names names = Names.instance(context);
jjg@1127: setOpname(POS, "+", names);
jjg@1127: setOpname(NEG, names.hyphen);
jjg@1127: setOpname(NOT, "!", names);
jjg@1127: setOpname(COMPL, "~", names);
jjg@1127: setOpname(PREINC, "++", names);
jjg@1127: setOpname(PREDEC, "--", names);
jjg@1127: setOpname(POSTINC, "++", names);
jjg@1127: setOpname(POSTDEC, "--", names);
jjg@1127: setOpname(NULLCHK, "<*nullchk*>", names);
jjg@1127: setOpname(OR, "||", names);
jjg@1127: setOpname(AND, "&&", names);
jjg@1127: setOpname(EQ, "==", names);
jjg@1127: setOpname(NE, "!=", names);
jjg@1127: setOpname(LT, "<", names);
jjg@1127: setOpname(GT, ">", names);
jjg@1127: setOpname(LE, "<=", names);
jjg@1127: setOpname(GE, ">=", names);
jjg@1127: setOpname(BITOR, "|", names);
jjg@1127: setOpname(BITXOR, "^", names);
jjg@1127: setOpname(BITAND, "&", names);
jjg@1127: setOpname(SL, "<<", names);
jjg@1127: setOpname(SR, ">>", names);
jjg@1127: setOpname(USR, ">>>", names);
jjg@1127: setOpname(PLUS, "+", names);
jjg@1127: setOpname(MINUS, names.hyphen);
jjg@1127: setOpname(MUL, names.asterisk);
jjg@1127: setOpname(DIV, names.slash);
jjg@1127: setOpname(MOD, "%", names);
duke@1: }
duke@1:
mcimadamore@1219: public static List args(JCTree t) {
mcimadamore@1219: switch (t.getTag()) {
mcimadamore@1219: case APPLY:
mcimadamore@1219: return ((JCMethodInvocation)t).args;
mcimadamore@1219: case NEWCLASS:
mcimadamore@1219: return ((JCNewClass)t).args;
mcimadamore@1219: default:
mcimadamore@1219: return null;
mcimadamore@1219: }
mcimadamore@1219: }
duke@1:
duke@1: /** Return name of operator with given tree tag.
duke@1: */
jjg@1127: public Name operatorName(JCTree.Tag tag) {
jjg@1127: return opname[tag.operatorIndex()];
duke@1: }
duke@1:
duke@1: /** Is tree a constructor declaration?
duke@1: */
duke@1: public static boolean isConstructor(JCTree tree) {
jjg@1127: if (tree.hasTag(METHODDEF)) {
duke@1: Name name = ((JCMethodDecl) tree).name;
jjg@113: return name == name.table.names.init;
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Is there a constructor declaration in the given list of trees?
duke@1: */
duke@1: public static boolean hasConstructors(List trees) {
duke@1: for (List l = trees; l.nonEmpty(); l = l.tail)
duke@1: if (isConstructor(l.head)) return true;
duke@1: return false;
duke@1: }
duke@1:
mcimadamore@550: public static boolean isMultiCatch(JCCatch catchClause) {
jjg@1127: return catchClause.param.vartype.hasTag(TYPEUNION);
mcimadamore@550: }
mcimadamore@550:
duke@1: /** Is statement an initializer for a synthetic field?
duke@1: */
duke@1: public static boolean isSyntheticInit(JCTree stat) {
jjg@1127: if (stat.hasTag(EXEC)) {
duke@1: JCExpressionStatement exec = (JCExpressionStatement)stat;
jjg@1127: if (exec.expr.hasTag(ASSIGN)) {
duke@1: JCAssign assign = (JCAssign)exec.expr;
jjg@1127: if (assign.lhs.hasTag(SELECT)) {
duke@1: JCFieldAccess select = (JCFieldAccess)assign.lhs;
duke@1: if (select.sym != null &&
duke@1: (select.sym.flags() & SYNTHETIC) != 0) {
duke@1: Name selected = name(select.selected);
jjg@113: if (selected != null && selected == selected.table.names._this)
duke@1: return true;
duke@1: }
duke@1: }
duke@1: }
duke@1: }
duke@1: return false;
duke@1: }
duke@1:
duke@1: /** If the expression is a method call, return the method name, null
duke@1: * otherwise. */
duke@1: public static Name calledMethodName(JCTree tree) {
jjg@1127: if (tree.hasTag(EXEC)) {
duke@1: JCExpressionStatement exec = (JCExpressionStatement)tree;
jjg@1127: if (exec.expr.hasTag(APPLY)) {
duke@1: Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth);
duke@1: return mname;
duke@1: }
duke@1: }
duke@1: return null;
duke@1: }
duke@1:
duke@1: /** Is this a call to this or super?
duke@1: */
duke@1: public static boolean isSelfCall(JCTree tree) {
duke@1: Name name = calledMethodName(tree);
duke@1: if (name != null) {
jjg@113: Names names = name.table.names;
duke@1: return name==names._this || name==names._super;
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Is this a call to super?
duke@1: */
duke@1: public static boolean isSuperCall(JCTree tree) {
duke@1: Name name = calledMethodName(tree);
duke@1: if (name != null) {
jjg@113: Names names = name.table.names;
duke@1: return name==names._super;
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Is this a constructor whose first (non-synthetic) statement is not
duke@1: * of the form this(...)?
duke@1: */
duke@1: public static boolean isInitialConstructor(JCTree tree) {
duke@1: JCMethodInvocation app = firstConstructorCall(tree);
duke@1: if (app == null) return false;
duke@1: Name meth = name(app.meth);
jjg@113: return meth == null || meth != meth.table.names._this;
duke@1: }
duke@1:
duke@1: /** Return the first call in a constructor definition. */
duke@1: public static JCMethodInvocation firstConstructorCall(JCTree tree) {
jjg@1127: if (!tree.hasTag(METHODDEF)) return null;
duke@1: JCMethodDecl md = (JCMethodDecl) tree;
jjg@113: Names names = md.name.table.names;
duke@1: if (md.name != names.init) return null;
duke@1: if (md.body == null) return null;
duke@1: List stats = md.body.stats;
duke@1: // Synthetic initializations can appear before the super call.
duke@1: while (stats.nonEmpty() && isSyntheticInit(stats.head))
duke@1: stats = stats.tail;
duke@1: if (stats.isEmpty()) return null;
jjg@1127: if (!stats.head.hasTag(EXEC)) return null;
duke@1: JCExpressionStatement exec = (JCExpressionStatement) stats.head;
jjg@1127: if (!exec.expr.hasTag(APPLY)) return null;
duke@1: return (JCMethodInvocation)exec.expr;
duke@1: }
duke@1:
mcimadamore@537: /** Return true if a tree represents a diamond new expr. */
mcimadamore@537: public static boolean isDiamond(JCTree tree) {
mcimadamore@537: switch(tree.getTag()) {
jjg@1127: case TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty();
jjg@1127: case NEWCLASS: return isDiamond(((JCNewClass)tree).clazz);
jjg@1563: case ANNOTATED_TYPE: return isDiamond(((JCAnnotatedType)tree).underlyingType);
mcimadamore@537: default: return false;
mcimadamore@537: }
mcimadamore@537: }
mcimadamore@537:
mcimadamore@1269: public static boolean isEnumInit(JCTree tree) {
mcimadamore@1269: switch (tree.getTag()) {
mcimadamore@1269: case VARDEF:
mcimadamore@1269: return (((JCVariableDecl)tree).mods.flags & ENUM) != 0;
mcimadamore@1269: default:
mcimadamore@1269: return false;
mcimadamore@1269: }
mcimadamore@1269: }
mcimadamore@1269:
mcimadamore@1347: /** Return true if a a tree corresponds to a poly expression. */
mcimadamore@1347: public static boolean isPoly(JCTree tree, JCTree origin) {
mcimadamore@1347: switch (tree.getTag()) {
mcimadamore@1347: case APPLY:
mcimadamore@1347: case NEWCLASS:
mcimadamore@1347: case CONDEXPR:
mcimadamore@1347: return !origin.hasTag(TYPECAST);
mcimadamore@1347: case LAMBDA:
mcimadamore@1347: case REFERENCE:
mcimadamore@1347: return true;
mcimadamore@1347: case PARENS:
mcimadamore@1347: return isPoly(((JCParens)tree).expr, origin);
mcimadamore@1347: default:
mcimadamore@1347: return false;
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1510: /** set 'polyKind' on given tree */
mcimadamore@1510: public static void setPolyKind(JCTree tree, PolyKind pkind) {
mcimadamore@1510: switch (tree.getTag()) {
mcimadamore@1510: case APPLY:
mcimadamore@1510: ((JCMethodInvocation)tree).polyKind = pkind;
mcimadamore@1510: break;
mcimadamore@1510: case NEWCLASS:
mcimadamore@1510: ((JCNewClass)tree).polyKind = pkind;
mcimadamore@1510: break;
mcimadamore@1510: case REFERENCE:
mcimadamore@1510: ((JCMemberReference)tree).refPolyKind = pkind;
mcimadamore@1510: break;
mcimadamore@1510: default:
mcimadamore@1510: throw new AssertionError("Unexpected tree: " + tree);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: /** set 'varargsElement' on given tree */
mcimadamore@1510: public static void setVarargsElement(JCTree tree, Type varargsElement) {
mcimadamore@1510: switch (tree.getTag()) {
mcimadamore@1510: case APPLY:
mcimadamore@1510: ((JCMethodInvocation)tree).varargsElement = varargsElement;
mcimadamore@1510: break;
mcimadamore@1510: case NEWCLASS:
mcimadamore@1510: ((JCNewClass)tree).varargsElement = varargsElement;
mcimadamore@1510: break;
mcimadamore@1510: case REFERENCE:
mcimadamore@1510: ((JCMemberReference)tree).varargsElement = varargsElement;
mcimadamore@1510: break;
mcimadamore@1510: default:
mcimadamore@1510: throw new AssertionError("Unexpected tree: " + tree);
mcimadamore@1510: }
mcimadamore@1348: }
mcimadamore@1433:
mcimadamore@1433: /** Return true if the tree corresponds to an expression statement */
mcimadamore@1433: public static boolean isExpressionStatement(JCExpression tree) {
mcimadamore@1433: switch(tree.getTag()) {
mcimadamore@1433: case PREINC: case PREDEC:
mcimadamore@1433: case POSTINC: case POSTDEC:
mcimadamore@1433: case ASSIGN:
mcimadamore@1433: case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
mcimadamore@1433: case SL_ASG: case SR_ASG: case USR_ASG:
mcimadamore@1433: case PLUS_ASG: case MINUS_ASG:
mcimadamore@1433: case MUL_ASG: case DIV_ASG: case MOD_ASG:
mcimadamore@1433: case APPLY: case NEWCLASS:
mcimadamore@1433: case ERRONEOUS:
mcimadamore@1433: return true;
mcimadamore@1433: default:
mcimadamore@1433: return false;
mcimadamore@1433: }
mcimadamore@1433: }
mcimadamore@1433:
mcimadamore@1143: /**
mcimadamore@1143: * Return true if the AST corresponds to a static select of the kind A.B
mcimadamore@1143: */
mcimadamore@1143: public static boolean isStaticSelector(JCTree base, Names names) {
mcimadamore@1143: if (base == null)
mcimadamore@1143: return false;
mcimadamore@1143: switch (base.getTag()) {
mcimadamore@1143: case IDENT:
mcimadamore@1143: JCIdent id = (JCIdent)base;
mcimadamore@1143: return id.name != names._this &&
mcimadamore@1143: id.name != names._super &&
mcimadamore@1143: isStaticSym(base);
mcimadamore@1143: case SELECT:
mcimadamore@1143: return isStaticSym(base) &&
mcimadamore@1143: isStaticSelector(((JCFieldAccess)base).selected, names);
mcimadamore@1143: case TYPEAPPLY:
mcimadamore@1352: case TYPEARRAY:
mcimadamore@1143: return true;
jjg@1563: case ANNOTATED_TYPE:
jjg@1563: return isStaticSelector(((JCAnnotatedType)base).underlyingType, names);
mcimadamore@1143: default:
mcimadamore@1143: return false;
mcimadamore@1143: }
mcimadamore@1143: }
mcimadamore@1143: //where
mcimadamore@1143: private static boolean isStaticSym(JCTree tree) {
mcimadamore@1143: Symbol sym = symbol(tree);
mcimadamore@1143: return (sym.kind == Kinds.TYP ||
mcimadamore@1143: sym.kind == Kinds.PCK);
mcimadamore@1143: }
mcimadamore@1143:
duke@1: /** Return true if a tree represents the null literal. */
duke@1: public static boolean isNull(JCTree tree) {
jjg@1127: if (!tree.hasTag(LITERAL))
duke@1: return false;
duke@1: JCLiteral lit = (JCLiteral) tree;
jjg@1374: return (lit.typetag == BOT);
duke@1: }
duke@1:
jjg@1280: public static String getCommentText(Env> env, JCTree tree) {
jjg@1280: DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
jjg@1280: ? ((JCCompilationUnit) tree).docComments
jjg@1280: : env.toplevel.docComments;
jjg@1281: return (docComments == null) ? null : docComments.getCommentText(tree);
jjg@1280: }
jjg@1280:
jjg@1455: public static DCTree.DCDocComment getCommentTree(Env> env, JCTree tree) {
jjg@1455: DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
jjg@1455: ? ((JCCompilationUnit) tree).docComments
jjg@1455: : env.toplevel.docComments;
jjg@1455: return (docComments == null) ? null : docComments.getCommentTree(tree);
jjg@1455: }
jjg@1455:
duke@1: /** The position of the first statement in a block, or the position of
duke@1: * the block itself if it is empty.
duke@1: */
duke@1: public static int firstStatPos(JCTree tree) {
jjg@1127: if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty())
duke@1: return ((JCBlock) tree).stats.head.pos;
duke@1: else
duke@1: return tree.pos;
duke@1: }
duke@1:
duke@1: /** The end position of given tree, if it is a block with
duke@1: * defined endpos.
duke@1: */
duke@1: public static int endPos(JCTree tree) {
jjg@1127: if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS)
duke@1: return ((JCBlock) tree).endpos;
jjg@1127: else if (tree.hasTag(SYNCHRONIZED))
duke@1: return endPos(((JCSynchronized) tree).body);
jjg@1127: else if (tree.hasTag(TRY)) {
duke@1: JCTry t = (JCTry) tree;
sundar@1300: return endPos((t.finalizer != null) ? t.finalizer
sundar@1300: : (t.catchers.nonEmpty() ? t.catchers.last().body : t.body));
duke@1: } else
duke@1: return tree.pos;
duke@1: }
duke@1:
duke@1:
duke@1: /** Get the start position for a tree node. The start position is
duke@1: * defined to be the position of the first character of the first
duke@1: * token of the node's source text.
duke@1: * @param tree The tree node
duke@1: */
duke@1: public static int getStartPos(JCTree tree) {
duke@1: if (tree == null)
duke@1: return Position.NOPOS;
duke@1:
duke@1: switch(tree.getTag()) {
jjg@1127: case APPLY:
jjg@1127: return getStartPos(((JCMethodInvocation) tree).meth);
jjg@1127: case ASSIGN:
jjg@1127: return getStartPos(((JCAssign) tree).lhs);
jjg@1127: case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
jjg@1127: case SL_ASG: case SR_ASG: case USR_ASG:
jjg@1127: case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
jjg@1127: case DIV_ASG: case MOD_ASG:
jjg@1127: return getStartPos(((JCAssignOp) tree).lhs);
jjg@1127: case OR: case AND: case BITOR:
jjg@1127: case BITXOR: case BITAND: case EQ:
jjg@1127: case NE: case LT: case GT:
jjg@1127: case LE: case GE: case SL:
jjg@1127: case SR: case USR: case PLUS:
jjg@1127: case MINUS: case MUL: case DIV:
jjg@1127: case MOD:
jjg@1127: return getStartPos(((JCBinary) tree).lhs);
jjg@1127: case CLASSDEF: {
jjg@1127: JCClassDecl node = (JCClassDecl)tree;
jjg@1127: if (node.mods.pos != Position.NOPOS)
jjg@1127: return node.mods.pos;
jjg@1127: break;
duke@1: }
jjg@1127: case CONDEXPR:
jjg@1127: return getStartPos(((JCConditional) tree).cond);
jjg@1127: case EXEC:
jjg@1127: return getStartPos(((JCExpressionStatement) tree).expr);
jjg@1127: case INDEXED:
jjg@1127: return getStartPos(((JCArrayAccess) tree).indexed);
jjg@1127: case METHODDEF: {
jjg@1127: JCMethodDecl node = (JCMethodDecl)tree;
jjg@1127: if (node.mods.pos != Position.NOPOS)
jjg@1127: return node.mods.pos;
jjg@1127: if (node.typarams.nonEmpty()) // List.nil() used for no typarams
jjg@1127: return getStartPos(node.typarams.head);
jjg@1127: return node.restype == null ? node.pos : getStartPos(node.restype);
jjg@1127: }
jjg@1127: case SELECT:
jjg@1127: return getStartPos(((JCFieldAccess) tree).selected);
jjg@1127: case TYPEAPPLY:
jjg@1127: return getStartPos(((JCTypeApply) tree).clazz);
jjg@1127: case TYPEARRAY:
jjg@1127: return getStartPos(((JCArrayTypeTree) tree).elemtype);
jjg@1127: case TYPETEST:
jjg@1127: return getStartPos(((JCInstanceOf) tree).expr);
jjg@1127: case POSTINC:
jjg@1127: case POSTDEC:
jjg@1127: return getStartPos(((JCUnary) tree).arg);
jjg@1521: case ANNOTATED_TYPE: {
jjg@1521: JCAnnotatedType node = (JCAnnotatedType) tree;
jjg@1521: if (node.annotations.nonEmpty()) {
jjg@1521: if (node.underlyingType.hasTag(TYPEARRAY) ||
jjg@1521: node.underlyingType.hasTag(SELECT)) {
jjg@1521: return getStartPos(node.underlyingType);
jjg@1521: } else {
jjg@1521: return getStartPos(node.annotations.head);
jjg@1521: }
jjg@1521: } else {
jjg@1521: return getStartPos(node.underlyingType);
jjg@1521: }
jjg@1521: }
jjg@1127: case NEWCLASS: {
jjg@1127: JCNewClass node = (JCNewClass)tree;
jjg@1127: if (node.encl != null)
jjg@1127: return getStartPos(node.encl);
jjg@1127: break;
jjg@1127: }
jjg@1127: case VARDEF: {
jjg@1127: JCVariableDecl node = (JCVariableDecl)tree;
jjg@1127: if (node.mods.pos != Position.NOPOS) {
jjg@1127: return node.mods.pos;
mcimadamore@1348: } else if (node.vartype == null) {
mcimadamore@1348: //if there's no type (partially typed lambda parameter)
mcimadamore@1348: //simply return node position
mcimadamore@1348: return node.pos;
jjg@1127: } else {
jjg@1127: return getStartPos(node.vartype);
jjg@1127: }
jjg@1127: }
jjg@1127: case ERRONEOUS: {
jjg@1127: JCErroneous node = (JCErroneous)tree;
jjg@1127: if (node.errs != null && node.errs.nonEmpty())
jjg@1127: return getStartPos(node.errs.head);
jjg@1127: }
duke@1: }
duke@1: return tree.pos;
duke@1: }
duke@1:
duke@1: /** The end position of given tree, given a table of end positions generated by the parser
duke@1: */
ksrini@1138: public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
duke@1: if (tree == null)
duke@1: return Position.NOPOS;
duke@1:
ksrini@1138: if (endPosTable == null) {
duke@1: // fall back on limited info in the tree
duke@1: return endPos(tree);
duke@1: }
duke@1:
ksrini@1138: int mapPos = endPosTable.getEndPos(tree);
ksrini@1138: if (mapPos != Position.NOPOS)
duke@1: return mapPos;
duke@1:
duke@1: switch(tree.getTag()) {
jjg@1127: case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
jjg@1127: case SL_ASG: case SR_ASG: case USR_ASG:
jjg@1127: case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
jjg@1127: case DIV_ASG: case MOD_ASG:
ksrini@1138: return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
jjg@1127: case OR: case AND: case BITOR:
jjg@1127: case BITXOR: case BITAND: case EQ:
jjg@1127: case NE: case LT: case GT:
jjg@1127: case LE: case GE: case SL:
jjg@1127: case SR: case USR: case PLUS:
jjg@1127: case MINUS: case MUL: case DIV:
jjg@1127: case MOD:
ksrini@1138: return getEndPos(((JCBinary) tree).rhs, endPosTable);
jjg@1127: case CASE:
ksrini@1138: return getEndPos(((JCCase) tree).stats.last(), endPosTable);
jjg@1127: case CATCH:
ksrini@1138: return getEndPos(((JCCatch) tree).body, endPosTable);
jjg@1127: case CONDEXPR:
ksrini@1138: return getEndPos(((JCConditional) tree).falsepart, endPosTable);
jjg@1127: case FORLOOP:
ksrini@1138: return getEndPos(((JCForLoop) tree).body, endPosTable);
jjg@1127: case FOREACHLOOP:
ksrini@1138: return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable);
jjg@1127: case IF: {
jjg@1127: JCIf node = (JCIf)tree;
jjg@1127: if (node.elsepart == null) {
ksrini@1138: return getEndPos(node.thenpart, endPosTable);
jjg@1127: } else {
ksrini@1138: return getEndPos(node.elsepart, endPosTable);
jjg@1127: }
duke@1: }
jjg@1127: case LABELLED:
ksrini@1138: return getEndPos(((JCLabeledStatement) tree).body, endPosTable);
jjg@1127: case MODIFIERS:
ksrini@1138: return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
jjg@1127: case SYNCHRONIZED:
ksrini@1138: return getEndPos(((JCSynchronized) tree).body, endPosTable);
jjg@1127: case TOPLEVEL:
ksrini@1138: return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
jjg@1127: case TRY: {
jjg@1127: JCTry node = (JCTry)tree;
jjg@1127: if (node.finalizer != null) {
ksrini@1138: return getEndPos(node.finalizer, endPosTable);
jjg@1127: } else if (!node.catchers.isEmpty()) {
ksrini@1138: return getEndPos(node.catchers.last(), endPosTable);
jjg@1127: } else {
ksrini@1138: return getEndPos(node.body, endPosTable);
jjg@1127: }
duke@1: }
jjg@1127: case WILDCARD:
ksrini@1138: return getEndPos(((JCWildcard) tree).inner, endPosTable);
jjg@1127: case TYPECAST:
ksrini@1138: return getEndPos(((JCTypeCast) tree).expr, endPosTable);
jjg@1127: case TYPETEST:
ksrini@1138: return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
jjg@1127: case POS:
jjg@1127: case NEG:
jjg@1127: case NOT:
jjg@1127: case COMPL:
jjg@1127: case PREINC:
jjg@1127: case PREDEC:
ksrini@1138: return getEndPos(((JCUnary) tree).arg, endPosTable);
jjg@1127: case WHILELOOP:
ksrini@1138: return getEndPos(((JCWhileLoop) tree).body, endPosTable);
jjg@1521: case ANNOTATED_TYPE:
jjg@1521: return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
jjg@1127: case ERRONEOUS: {
jjg@1127: JCErroneous node = (JCErroneous)tree;
jjg@1127: if (node.errs != null && node.errs.nonEmpty())
ksrini@1138: return getEndPos(node.errs.last(), endPosTable);
jjg@1127: }
duke@1: }
duke@1: return Position.NOPOS;
duke@1: }
duke@1:
duke@1:
duke@1: /** A DiagnosticPosition with the preferred position set to the
duke@1: * end position of given tree, if it is a block with
duke@1: * defined endpos.
duke@1: */
duke@1: public static DiagnosticPosition diagEndPos(final JCTree tree) {
duke@1: final int endPos = TreeInfo.endPos(tree);
duke@1: return new DiagnosticPosition() {
duke@1: public JCTree getTree() { return tree; }
duke@1: public int getStartPosition() { return TreeInfo.getStartPos(tree); }
duke@1: public int getPreferredPosition() { return endPos; }
ksrini@1138: public int getEndPosition(EndPosTable endPosTable) {
duke@1: return TreeInfo.getEndPos(tree, endPosTable);
duke@1: }
duke@1: };
duke@1: }
duke@1:
duke@1: /** The position of the finalizer of given try/synchronized statement.
duke@1: */
duke@1: public static int finalizerPos(JCTree tree) {
jjg@1127: if (tree.hasTag(TRY)) {
duke@1: JCTry t = (JCTry) tree;
jjg@816: Assert.checkNonNull(t.finalizer);
duke@1: return firstStatPos(t.finalizer);
jjg@1127: } else if (tree.hasTag(SYNCHRONIZED)) {
duke@1: return endPos(((JCSynchronized) tree).body);
duke@1: } else {
duke@1: throw new AssertionError();
duke@1: }
duke@1: }
duke@1:
duke@1: /** Find the position for reporting an error about a symbol, where
duke@1: * that symbol is defined somewhere in the given tree. */
duke@1: public static int positionFor(final Symbol sym, final JCTree tree) {
duke@1: JCTree decl = declarationFor(sym, tree);
duke@1: return ((decl != null) ? decl : tree).pos;
duke@1: }
duke@1:
duke@1: /** Find the position for reporting an error about a symbol, where
duke@1: * that symbol is defined somewhere in the given tree. */
duke@1: public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) {
duke@1: JCTree decl = declarationFor(sym, tree);
duke@1: return ((decl != null) ? decl : tree).pos();
duke@1: }
duke@1:
duke@1: /** Find the declaration for a symbol, where
duke@1: * that symbol is defined somewhere in the given tree. */
duke@1: public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
duke@1: class DeclScanner extends TreeScanner {
duke@1: JCTree result = null;
duke@1: public void scan(JCTree tree) {
duke@1: if (tree!=null && result==null)
duke@1: tree.accept(this);
duke@1: }
duke@1: public void visitTopLevel(JCCompilationUnit that) {
duke@1: if (that.packge == sym) result = that;
duke@1: else super.visitTopLevel(that);
duke@1: }
duke@1: public void visitClassDef(JCClassDecl that) {
duke@1: if (that.sym == sym) result = that;
duke@1: else super.visitClassDef(that);
duke@1: }
duke@1: public void visitMethodDef(JCMethodDecl that) {
duke@1: if (that.sym == sym) result = that;
duke@1: else super.visitMethodDef(that);
duke@1: }
duke@1: public void visitVarDef(JCVariableDecl that) {
duke@1: if (that.sym == sym) result = that;
duke@1: else super.visitVarDef(that);
duke@1: }
sundar@668: public void visitTypeParameter(JCTypeParameter that) {
jjh@771: if (that.type != null && that.type.tsym == sym) result = that;
sundar@668: else super.visitTypeParameter(that);
sundar@668: }
duke@1: }
duke@1: DeclScanner s = new DeclScanner();
duke@1: tree.accept(s);
duke@1: return s.result;
duke@1: }
duke@1:
duke@1: public static Env scopeFor(JCTree node, JCCompilationUnit unit) {
duke@1: return scopeFor(pathFor(node, unit));
duke@1: }
duke@1:
duke@1: public static Env scopeFor(List path) {
duke@1: // TODO: not implemented yet
duke@1: throw new UnsupportedOperationException("not implemented yet");
duke@1: }
duke@1:
duke@1: public static List pathFor(final JCTree node, final JCCompilationUnit unit) {
duke@1: class Result extends Error {
duke@1: static final long serialVersionUID = -5942088234594905625L;
duke@1: List path;
duke@1: Result(List path) {
duke@1: this.path = path;
duke@1: }
duke@1: }
duke@1: class PathFinder extends TreeScanner {
duke@1: List path = List.nil();
duke@1: public void scan(JCTree tree) {
duke@1: if (tree != null) {
duke@1: path = path.prepend(tree);
duke@1: if (tree == node)
duke@1: throw new Result(path);
duke@1: super.scan(tree);
duke@1: path = path.tail;
duke@1: }
duke@1: }
duke@1: }
duke@1: try {
duke@1: new PathFinder().scan(unit);
duke@1: } catch (Result result) {
duke@1: return result.path;
duke@1: }
duke@1: return List.nil();
duke@1: }
duke@1:
duke@1: /** Return the statement referenced by a label.
duke@1: * If the label refers to a loop or switch, return that switch
duke@1: * otherwise return the labelled statement itself
duke@1: */
duke@1: public static JCTree referencedStatement(JCLabeledStatement tree) {
duke@1: JCTree t = tree;
duke@1: do t = ((JCLabeledStatement) t).body;
jjg@1127: while (t.hasTag(LABELLED));
duke@1: switch (t.getTag()) {
jjg@1127: case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH:
duke@1: return t;
duke@1: default:
duke@1: return tree;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Skip parens and return the enclosed expression
duke@1: */
duke@1: public static JCExpression skipParens(JCExpression tree) {
jjg@1127: while (tree.hasTag(PARENS)) {
duke@1: tree = ((JCParens) tree).expr;
duke@1: }
duke@1: return tree;
duke@1: }
duke@1:
duke@1: /** Skip parens and return the enclosed expression
duke@1: */
duke@1: public static JCTree skipParens(JCTree tree) {
jjg@1127: if (tree.hasTag(PARENS))
duke@1: return skipParens((JCParens)tree);
duke@1: else
duke@1: return tree;
duke@1: }
duke@1:
duke@1: /** Return the types of a list of trees.
duke@1: */
duke@1: public static List types(List extends JCTree> trees) {
duke@1: ListBuffer ts = new ListBuffer();
duke@1: for (List extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
duke@1: ts.append(l.head.type);
duke@1: return ts.toList();
duke@1: }
duke@1:
duke@1: /** If this tree is an identifier or a field or a parameterized type,
duke@1: * return its name, otherwise return null.
duke@1: */
duke@1: public static Name name(JCTree tree) {
duke@1: switch (tree.getTag()) {
jjg@1127: case IDENT:
duke@1: return ((JCIdent) tree).name;
jjg@1127: case SELECT:
duke@1: return ((JCFieldAccess) tree).name;
jjg@1127: case TYPEAPPLY:
duke@1: return name(((JCTypeApply) tree).clazz);
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
duke@1: /** If this tree is a qualified identifier, its return fully qualified name,
duke@1: * otherwise return null.
duke@1: */
duke@1: public static Name fullName(JCTree tree) {
duke@1: tree = skipParens(tree);
duke@1: switch (tree.getTag()) {
jjg@1127: case IDENT:
duke@1: return ((JCIdent) tree).name;
jjg@1127: case SELECT:
duke@1: Name sname = fullName(((JCFieldAccess) tree).selected);
duke@1: return sname == null ? null : sname.append('.', name(tree));
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
duke@1: public static Symbol symbolFor(JCTree node) {
duke@1: node = skipParens(node);
duke@1: switch (node.getTag()) {
jjg@1127: case CLASSDEF:
duke@1: return ((JCClassDecl) node).sym;
jjg@1127: case METHODDEF:
duke@1: return ((JCMethodDecl) node).sym;
jjg@1127: case VARDEF:
duke@1: return ((JCVariableDecl) node).sym;
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
jjg@672: public static boolean isDeclaration(JCTree node) {
jjg@672: node = skipParens(node);
jjg@672: switch (node.getTag()) {
jjg@1127: case CLASSDEF:
jjg@1127: case METHODDEF:
jjg@1127: case VARDEF:
jjg@672: return true;
jjg@672: default:
jjg@672: return false;
jjg@672: }
jjg@672: }
jjg@672:
duke@1: /** If this tree is an identifier or a field, return its symbol,
duke@1: * otherwise return null.
duke@1: */
duke@1: public static Symbol symbol(JCTree tree) {
duke@1: tree = skipParens(tree);
duke@1: switch (tree.getTag()) {
jjg@1127: case IDENT:
duke@1: return ((JCIdent) tree).sym;
jjg@1127: case SELECT:
duke@1: return ((JCFieldAccess) tree).sym;
jjg@1127: case TYPEAPPLY:
duke@1: return symbol(((JCTypeApply) tree).clazz);
jjg@1521: case ANNOTATED_TYPE:
jjg@1521: return symbol(((JCAnnotatedType) tree).underlyingType);
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Return true if this is a nonstatic selection. */
duke@1: public static boolean nonstaticSelect(JCTree tree) {
duke@1: tree = skipParens(tree);
jjg@1127: if (!tree.hasTag(SELECT)) return false;
duke@1: JCFieldAccess s = (JCFieldAccess) tree;
duke@1: Symbol e = symbol(s.selected);
duke@1: return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
duke@1: }
duke@1:
duke@1: /** If this tree is an identifier or a field, set its symbol, otherwise skip.
duke@1: */
duke@1: public static void setSymbol(JCTree tree, Symbol sym) {
duke@1: tree = skipParens(tree);
duke@1: switch (tree.getTag()) {
jjg@1127: case IDENT:
duke@1: ((JCIdent) tree).sym = sym; break;
jjg@1127: case SELECT:
duke@1: ((JCFieldAccess) tree).sym = sym; break;
duke@1: default:
duke@1: }
duke@1: }
duke@1:
duke@1: /** If this tree is a declaration or a block, return its flags field,
duke@1: * otherwise return 0.
duke@1: */
duke@1: public static long flags(JCTree tree) {
duke@1: switch (tree.getTag()) {
jjg@1127: case VARDEF:
duke@1: return ((JCVariableDecl) tree).mods.flags;
jjg@1127: case METHODDEF:
duke@1: return ((JCMethodDecl) tree).mods.flags;
jjg@1127: case CLASSDEF:
duke@1: return ((JCClassDecl) tree).mods.flags;
jjg@1127: case BLOCK:
duke@1: return ((JCBlock) tree).flags;
duke@1: default:
duke@1: return 0;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Return first (smallest) flag in `flags':
duke@1: * pre: flags != 0
duke@1: */
duke@1: public static long firstFlag(long flags) {
mcimadamore@1366: long flag = 1;
mcimadamore@1366: while ((flag & flags & ExtendedStandardFlags) == 0)
duke@1: flag = flag << 1;
duke@1: return flag;
duke@1: }
duke@1:
duke@1: /** Return flags as a string, separated by " ".
duke@1: */
duke@1: public static String flagNames(long flags) {
mcimadamore@1366: return Flags.toString(flags & ExtendedStandardFlags).trim();
duke@1: }
duke@1:
duke@1: /** Operator precedences values.
duke@1: */
duke@1: public static final int
duke@1: notExpression = -1, // not an expression
duke@1: noPrec = 0, // no enclosing expression
duke@1: assignPrec = 1,
duke@1: assignopPrec = 2,
duke@1: condPrec = 3,
duke@1: orPrec = 4,
duke@1: andPrec = 5,
duke@1: bitorPrec = 6,
duke@1: bitxorPrec = 7,
duke@1: bitandPrec = 8,
duke@1: eqPrec = 9,
duke@1: ordPrec = 10,
duke@1: shiftPrec = 11,
duke@1: addPrec = 12,
duke@1: mulPrec = 13,
duke@1: prefixPrec = 14,
duke@1: postfixPrec = 15,
duke@1: precCount = 16;
duke@1:
duke@1:
duke@1: /** Map operators to their precedence levels.
duke@1: */
jjg@1127: public static int opPrec(JCTree.Tag op) {
duke@1: switch(op) {
jjg@1127: case POS:
jjg@1127: case NEG:
jjg@1127: case NOT:
jjg@1127: case COMPL:
jjg@1127: case PREINC:
jjg@1127: case PREDEC: return prefixPrec;
jjg@1127: case POSTINC:
jjg@1127: case POSTDEC:
jjg@1127: case NULLCHK: return postfixPrec;
jjg@1127: case ASSIGN: return assignPrec;
jjg@1127: case BITOR_ASG:
jjg@1127: case BITXOR_ASG:
jjg@1127: case BITAND_ASG:
jjg@1127: case SL_ASG:
jjg@1127: case SR_ASG:
jjg@1127: case USR_ASG:
jjg@1127: case PLUS_ASG:
jjg@1127: case MINUS_ASG:
jjg@1127: case MUL_ASG:
jjg@1127: case DIV_ASG:
jjg@1127: case MOD_ASG: return assignopPrec;
jjg@1127: case OR: return orPrec;
jjg@1127: case AND: return andPrec;
jjg@1127: case EQ:
jjg@1127: case NE: return eqPrec;
jjg@1127: case LT:
jjg@1127: case GT:
jjg@1127: case LE:
jjg@1127: case GE: return ordPrec;
jjg@1127: case BITOR: return bitorPrec;
jjg@1127: case BITXOR: return bitxorPrec;
jjg@1127: case BITAND: return bitandPrec;
jjg@1127: case SL:
jjg@1127: case SR:
jjg@1127: case USR: return shiftPrec;
jjg@1127: case PLUS:
jjg@1127: case MINUS: return addPrec;
jjg@1127: case MUL:
jjg@1127: case DIV:
jjg@1127: case MOD: return mulPrec;
jjg@1127: case TYPETEST: return ordPrec;
duke@1: default: throw new AssertionError();
duke@1: }
duke@1: }
duke@1:
jjg@1127: static Tree.Kind tagToKind(JCTree.Tag tag) {
duke@1: switch (tag) {
duke@1: // Postfix expressions
jjg@1127: case POSTINC: // _ ++
duke@1: return Tree.Kind.POSTFIX_INCREMENT;
jjg@1127: case POSTDEC: // _ --
duke@1: return Tree.Kind.POSTFIX_DECREMENT;
duke@1:
duke@1: // Unary operators
jjg@1127: case PREINC: // ++ _
duke@1: return Tree.Kind.PREFIX_INCREMENT;
jjg@1127: case PREDEC: // -- _
duke@1: return Tree.Kind.PREFIX_DECREMENT;
jjg@1127: case POS: // +
duke@1: return Tree.Kind.UNARY_PLUS;
jjg@1127: case NEG: // -
duke@1: return Tree.Kind.UNARY_MINUS;
jjg@1127: case COMPL: // ~
duke@1: return Tree.Kind.BITWISE_COMPLEMENT;
jjg@1127: case NOT: // !
duke@1: return Tree.Kind.LOGICAL_COMPLEMENT;
duke@1:
duke@1: // Binary operators
duke@1:
duke@1: // Multiplicative operators
jjg@1127: case MUL: // *
duke@1: return Tree.Kind.MULTIPLY;
jjg@1127: case DIV: // /
duke@1: return Tree.Kind.DIVIDE;
jjg@1127: case MOD: // %
duke@1: return Tree.Kind.REMAINDER;
duke@1:
duke@1: // Additive operators
jjg@1127: case PLUS: // +
duke@1: return Tree.Kind.PLUS;
jjg@1127: case MINUS: // -
duke@1: return Tree.Kind.MINUS;
duke@1:
duke@1: // Shift operators
jjg@1127: case SL: // <<
duke@1: return Tree.Kind.LEFT_SHIFT;
jjg@1127: case SR: // >>
duke@1: return Tree.Kind.RIGHT_SHIFT;
jjg@1127: case USR: // >>>
duke@1: return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
duke@1:
duke@1: // Relational operators
jjg@1127: case LT: // <
duke@1: return Tree.Kind.LESS_THAN;
jjg@1127: case GT: // >
duke@1: return Tree.Kind.GREATER_THAN;
jjg@1127: case LE: // <=
duke@1: return Tree.Kind.LESS_THAN_EQUAL;
jjg@1127: case GE: // >=
duke@1: return Tree.Kind.GREATER_THAN_EQUAL;
duke@1:
duke@1: // Equality operators
jjg@1127: case EQ: // ==
duke@1: return Tree.Kind.EQUAL_TO;
jjg@1127: case NE: // !=
duke@1: return Tree.Kind.NOT_EQUAL_TO;
duke@1:
duke@1: // Bitwise and logical operators
jjg@1127: case BITAND: // &
duke@1: return Tree.Kind.AND;
jjg@1127: case BITXOR: // ^
duke@1: return Tree.Kind.XOR;
jjg@1127: case BITOR: // |
duke@1: return Tree.Kind.OR;
duke@1:
duke@1: // Conditional operators
jjg@1127: case AND: // &&
duke@1: return Tree.Kind.CONDITIONAL_AND;
jjg@1127: case OR: // ||
duke@1: return Tree.Kind.CONDITIONAL_OR;
duke@1:
duke@1: // Assignment operators
jjg@1127: case MUL_ASG: // *=
duke@1: return Tree.Kind.MULTIPLY_ASSIGNMENT;
jjg@1127: case DIV_ASG: // /=
duke@1: return Tree.Kind.DIVIDE_ASSIGNMENT;
jjg@1127: case MOD_ASG: // %=
duke@1: return Tree.Kind.REMAINDER_ASSIGNMENT;
jjg@1127: case PLUS_ASG: // +=
duke@1: return Tree.Kind.PLUS_ASSIGNMENT;
jjg@1127: case MINUS_ASG: // -=
duke@1: return Tree.Kind.MINUS_ASSIGNMENT;
jjg@1127: case SL_ASG: // <<=
duke@1: return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
jjg@1127: case SR_ASG: // >>=
duke@1: return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
jjg@1127: case USR_ASG: // >>>=
duke@1: return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
jjg@1127: case BITAND_ASG: // &=
duke@1: return Tree.Kind.AND_ASSIGNMENT;
jjg@1127: case BITXOR_ASG: // ^=
duke@1: return Tree.Kind.XOR_ASSIGNMENT;
jjg@1127: case BITOR_ASG: // |=
duke@1: return Tree.Kind.OR_ASSIGNMENT;
duke@1:
duke@1: // Null check (implementation detail), for example, __.getClass()
jjg@1127: case NULLCHK:
duke@1: return Tree.Kind.OTHER;
duke@1:
jjg@1521: case ANNOTATION:
jjg@1521: return Tree.Kind.ANNOTATION;
jjg@1521: case TYPE_ANNOTATION:
jjg@1521: return Tree.Kind.TYPE_ANNOTATION;
jjg@1521:
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
jjg@308:
jjg@308: /**
jjg@1521: * Returns the underlying type of the tree if it is an annotated type,
jjg@1521: * or the tree itself otherwise.
jjg@308: */
jjg@308: public static JCExpression typeIn(JCExpression tree) {
jjg@308: switch (tree.getTag()) {
jjg@1521: case ANNOTATED_TYPE:
jjg@1521: return ((JCAnnotatedType)tree).underlyingType;
jjg@1127: case IDENT: /* simple names */
jjg@1127: case TYPEIDENT: /* primitive name */
jjg@1127: case SELECT: /* qualified name */
jjg@1127: case TYPEARRAY: /* array types */
jjg@1127: case WILDCARD: /* wild cards */
jjg@1127: case TYPEPARAMETER: /* type parameters */
jjg@1127: case TYPEAPPLY: /* parameterized types */
jjg@1521: case ERRONEOUS: /* error tree TODO: needed for BadCast JSR308 test case. Better way? */
jjg@308: return tree;
jjg@308: default:
jjg@308: throw new AssertionError("Unexpected type tree: " + tree);
jjg@308: }
jjg@308: }
jjg@470:
jjg@1521: /* Return the inner-most type of a type tree.
jjg@1521: * For an array that contains an annotated type, return that annotated type.
jjg@1521: * TODO: currently only used by Pretty. Describe behavior better.
jjg@1521: */
jjg@470: public static JCTree innermostType(JCTree type) {
jjg@1521: JCTree lastAnnotatedType = null;
jjg@1521: JCTree cur = type;
jjg@1521: loop: while (true) {
jjg@1521: switch (cur.getTag()) {
jjg@1521: case TYPEARRAY:
jjg@1521: lastAnnotatedType = null;
jjg@1521: cur = ((JCArrayTypeTree)cur).elemtype;
jjg@1521: break;
jjg@1521: case WILDCARD:
jjg@1521: lastAnnotatedType = null;
jjg@1521: cur = ((JCWildcard)cur).inner;
jjg@1521: break;
jjg@1521: case ANNOTATED_TYPE:
jjg@1521: lastAnnotatedType = cur;
jjg@1521: cur = ((JCAnnotatedType)cur).underlyingType;
jjg@1521: break;
jjg@1521: default:
jjg@1521: break loop;
jjg@1521: }
jjg@1521: }
jjg@1521: if (lastAnnotatedType!=null) {
jjg@1521: return lastAnnotatedType;
jjg@1521: } else {
jjg@1521: return cur;
jjg@470: }
jjg@470: }
jjg@1521:
jjg@1521: private static class TypeAnnotationFinder extends TreeScanner {
jjg@1521: public boolean foundTypeAnno = false;
jjg@1521: public void visitAnnotation(JCAnnotation tree) {
jjg@1521: foundTypeAnno = foundTypeAnno || tree.hasTag(TYPE_ANNOTATION);
jjg@1521: }
jjg@1521: }
jjg@1521:
jjg@1521: public static boolean containsTypeAnnotation(JCTree e) {
jjg@1521: TypeAnnotationFinder finder = new TypeAnnotationFinder();
jjg@1521: finder.scan(e);
jjg@1521: return finder.foundTypeAnno;
jjg@1521: }
duke@1: }