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