author | jjg |
Tue, 28 Feb 2012 10:33:49 -0800 | |
changeset 12016 | 1990493b64db |
parent 11055 | ec1418effa77 |
child 13077 | 16fb753bb5dc |
permissions | -rw-r--r-- |
10 | 1 |
/* |
12016 | 2 |
* Copyright (c) 2005, 2012, 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.api; |
|
27 |
||
28 |
import java.io.IOException; |
|
12016 | 29 |
|
10 | 30 |
import javax.annotation.processing.ProcessingEnvironment; |
31 |
import javax.lang.model.element.AnnotationMirror; |
|
32 |
import javax.lang.model.element.AnnotationValue; |
|
33 |
import javax.lang.model.element.Element; |
|
34 |
import javax.lang.model.element.ExecutableElement; |
|
35 |
import javax.lang.model.element.TypeElement; |
|
36 |
import javax.lang.model.type.DeclaredType; |
|
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
37 |
import javax.lang.model.type.TypeKind; |
10 | 38 |
import javax.lang.model.type.TypeMirror; |
3149 | 39 |
import javax.tools.Diagnostic; |
10 | 40 |
import javax.tools.JavaCompiler; |
41 |
import javax.tools.JavaFileObject; |
|
42 |
||
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
43 |
import com.sun.source.tree.CatchTree; |
10 | 44 |
import com.sun.source.tree.CompilationUnitTree; |
45 |
import com.sun.source.tree.Scope; |
|
46 |
import com.sun.source.tree.Tree; |
|
12016 | 47 |
import com.sun.source.util.JavacTask; |
10 | 48 |
import com.sun.source.util.SourcePositions; |
49 |
import com.sun.source.util.TreePath; |
|
50 |
import com.sun.source.util.Trees; |
|
6590
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
51 |
import com.sun.tools.javac.code.Flags; |
12016 | 52 |
import com.sun.tools.javac.code.Symbol; |
10 | 53 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
54 |
import com.sun.tools.javac.code.Symbol.TypeSymbol; |
|
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
55 |
import com.sun.tools.javac.code.Type.UnionClassType; |
10 | 56 |
import com.sun.tools.javac.comp.Attr; |
57 |
import com.sun.tools.javac.comp.AttrContext; |
|
58 |
import com.sun.tools.javac.comp.Enter; |
|
59 |
import com.sun.tools.javac.comp.Env; |
|
60 |
import com.sun.tools.javac.comp.MemberEnter; |
|
61 |
import com.sun.tools.javac.comp.Resolve; |
|
62 |
import com.sun.tools.javac.model.JavacElements; |
|
11055 | 63 |
import com.sun.tools.javac.parser.EndPosTable; |
10 | 64 |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
12016 | 65 |
import com.sun.tools.javac.tree.JCTree; |
10 | 66 |
import com.sun.tools.javac.tree.JCTree.*; |
67 |
import com.sun.tools.javac.tree.TreeCopier; |
|
68 |
import com.sun.tools.javac.tree.TreeInfo; |
|
69 |
import com.sun.tools.javac.tree.TreeMaker; |
|
10631
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
70 |
import com.sun.tools.javac.util.Assert; |
10 | 71 |
import com.sun.tools.javac.util.Context; |
3149 | 72 |
import com.sun.tools.javac.util.JCDiagnostic; |
10 | 73 |
import com.sun.tools.javac.util.List; |
74 |
import com.sun.tools.javac.util.Log; |
|
75 |
import com.sun.tools.javac.util.Pair; |
|
76 |
||
77 |
/** |
|
78 |
* Provides an implementation of Trees. |
|
79 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
80 |
* <p><b>This is NOT part of any supported API. |
10 | 81 |
* If you write code that depends on this, you do so at your own |
82 |
* risk. This code and its internal interfaces are subject to change |
|
83 |
* or deletion without notice.</b></p> |
|
84 |
* |
|
85 |
* @author Peter von der Ahé |
|
86 |
*/ |
|
87 |
public class JavacTrees extends Trees { |
|
88 |
||
6717 | 89 |
// in a world of a single context per compilation, these would all be final |
90 |
private Resolve resolve; |
|
91 |
private Enter enter; |
|
92 |
private Log log; |
|
93 |
private MemberEnter memberEnter; |
|
94 |
private Attr attr; |
|
95 |
private TreeMaker treeMaker; |
|
96 |
private JavacElements elements; |
|
97 |
private JavacTaskImpl javacTaskImpl; |
|
10 | 98 |
|
12016 | 99 |
// called reflectively from Trees.instance(CompilationTask task) |
10 | 100 |
public static JavacTrees instance(JavaCompiler.CompilationTask task) { |
101 |
if (!(task instanceof JavacTaskImpl)) |
|
102 |
throw new IllegalArgumentException(); |
|
103 |
return instance(((JavacTaskImpl)task).getContext()); |
|
104 |
} |
|
105 |
||
12016 | 106 |
// called reflectively from Trees.instance(ProcessingEnvironment env) |
10 | 107 |
public static JavacTrees instance(ProcessingEnvironment env) { |
108 |
if (!(env instanceof JavacProcessingEnvironment)) |
|
109 |
throw new IllegalArgumentException(); |
|
110 |
return instance(((JavacProcessingEnvironment)env).getContext()); |
|
111 |
} |
|
112 |
||
113 |
public static JavacTrees instance(Context context) { |
|
114 |
JavacTrees instance = context.get(JavacTrees.class); |
|
115 |
if (instance == null) |
|
116 |
instance = new JavacTrees(context); |
|
117 |
return instance; |
|
118 |
} |
|
119 |
||
120 |
private JavacTrees(Context context) { |
|
121 |
context.put(JavacTrees.class, this); |
|
6717 | 122 |
init(context); |
123 |
} |
|
124 |
||
125 |
public void updateContext(Context context) { |
|
126 |
init(context); |
|
127 |
} |
|
128 |
||
129 |
private void init(Context context) { |
|
10 | 130 |
attr = Attr.instance(context); |
131 |
enter = Enter.instance(context); |
|
132 |
elements = JavacElements.instance(context); |
|
133 |
log = Log.instance(context); |
|
134 |
resolve = Resolve.instance(context); |
|
135 |
treeMaker = TreeMaker.instance(context); |
|
136 |
memberEnter = MemberEnter.instance(context); |
|
12016 | 137 |
|
138 |
JavacTask t = context.get(JavacTask.class); |
|
139 |
if (t instanceof JavacTaskImpl) |
|
140 |
javacTaskImpl = (JavacTaskImpl) t; |
|
10 | 141 |
} |
142 |
||
143 |
public SourcePositions getSourcePositions() { |
|
144 |
return new SourcePositions() { |
|
145 |
public long getStartPosition(CompilationUnitTree file, Tree tree) { |
|
146 |
return TreeInfo.getStartPos((JCTree) tree); |
|
147 |
} |
|
148 |
||
149 |
public long getEndPosition(CompilationUnitTree file, Tree tree) { |
|
11055 | 150 |
EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions; |
151 |
return TreeInfo.getEndPos((JCTree) tree, endPosTable); |
|
10 | 152 |
} |
153 |
}; |
|
154 |
} |
|
155 |
||
156 |
public JCClassDecl getTree(TypeElement element) { |
|
157 |
return (JCClassDecl) getTree((Element) element); |
|
158 |
} |
|
159 |
||
160 |
public JCMethodDecl getTree(ExecutableElement method) { |
|
161 |
return (JCMethodDecl) getTree((Element) method); |
|
162 |
} |
|
163 |
||
164 |
public JCTree getTree(Element element) { |
|
165 |
Symbol symbol = (Symbol) element; |
|
166 |
TypeSymbol enclosing = symbol.enclClass(); |
|
167 |
Env<AttrContext> env = enter.getEnv(enclosing); |
|
168 |
if (env == null) |
|
169 |
return null; |
|
170 |
JCClassDecl classNode = env.enclClass; |
|
171 |
if (classNode != null) { |
|
172 |
if (TreeInfo.symbolFor(classNode) == element) |
|
173 |
return classNode; |
|
174 |
for (JCTree node : classNode.getMembers()) |
|
175 |
if (TreeInfo.symbolFor(node) == element) |
|
176 |
return node; |
|
177 |
} |
|
178 |
return null; |
|
179 |
} |
|
180 |
||
181 |
public JCTree getTree(Element e, AnnotationMirror a) { |
|
182 |
return getTree(e, a, null); |
|
183 |
} |
|
184 |
||
185 |
public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) { |
|
186 |
Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v); |
|
187 |
if (treeTopLevel == null) |
|
188 |
return null; |
|
189 |
return treeTopLevel.fst; |
|
190 |
} |
|
191 |
||
192 |
public TreePath getPath(CompilationUnitTree unit, Tree node) { |
|
193 |
return TreePath.getPath(unit, node); |
|
194 |
} |
|
195 |
||
196 |
public TreePath getPath(Element e) { |
|
197 |
return getPath(e, null, null); |
|
198 |
} |
|
199 |
||
200 |
public TreePath getPath(Element e, AnnotationMirror a) { |
|
201 |
return getPath(e, a, null); |
|
202 |
} |
|
203 |
||
204 |
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) { |
|
205 |
final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v); |
|
206 |
if (treeTopLevel == null) |
|
207 |
return null; |
|
208 |
return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst); |
|
209 |
} |
|
210 |
||
211 |
public Element getElement(TreePath path) { |
|
6590
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
212 |
JCTree tree = (JCTree) path.getLeaf(); |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
213 |
Symbol sym = TreeInfo.symbolFor(tree); |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
214 |
if (sym == null && TreeInfo.isDeclaration(tree)) { |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
215 |
for (TreePath p = path; p != null; p = p.getParentPath()) { |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
216 |
JCTree t = (JCTree) p.getLeaf(); |
10950 | 217 |
if (t.hasTag(JCTree.Tag.CLASSDEF)) { |
6590
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
218 |
JCClassDecl ct = (JCClassDecl) t; |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
219 |
if (ct.sym != null) { |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
220 |
if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) { |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
221 |
attr.attribClass(ct.pos(), ct.sym); |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
222 |
sym = TreeInfo.symbolFor(tree); |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
223 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
224 |
break; |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
225 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
226 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
227 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
228 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
5847
diff
changeset
|
229 |
return sym; |
10 | 230 |
} |
231 |
||
232 |
public TypeMirror getTypeMirror(TreePath path) { |
|
233 |
Tree t = path.getLeaf(); |
|
234 |
return ((JCTree)t).type; |
|
235 |
} |
|
236 |
||
237 |
public JavacScope getScope(TreePath path) { |
|
238 |
return new JavacScope(getAttrContext(path)); |
|
239 |
} |
|
240 |
||
7631 | 241 |
public String getDocComment(TreePath path) { |
242 |
CompilationUnitTree t = path.getCompilationUnit(); |
|
243 |
if (t instanceof JCTree.JCCompilationUnit) { |
|
244 |
JCCompilationUnit cu = (JCCompilationUnit) t; |
|
245 |
if (cu.docComments != null) { |
|
246 |
return cu.docComments.get(path.getLeaf()); |
|
247 |
} |
|
248 |
} |
|
249 |
return null; |
|
250 |
} |
|
251 |
||
10 | 252 |
public boolean isAccessible(Scope scope, TypeElement type) { |
253 |
if (scope instanceof JavacScope && type instanceof ClassSymbol) { |
|
254 |
Env<AttrContext> env = ((JavacScope) scope).env; |
|
7209
889e80aa1d2e
6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents:
7077
diff
changeset
|
255 |
return resolve.isAccessible(env, (ClassSymbol)type, true); |
10 | 256 |
} else |
257 |
return false; |
|
258 |
} |
|
259 |
||
260 |
public boolean isAccessible(Scope scope, Element member, DeclaredType type) { |
|
261 |
if (scope instanceof JavacScope |
|
262 |
&& member instanceof Symbol |
|
263 |
&& type instanceof com.sun.tools.javac.code.Type) { |
|
264 |
Env<AttrContext> env = ((JavacScope) scope).env; |
|
7209
889e80aa1d2e
6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents:
7077
diff
changeset
|
265 |
return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true); |
10 | 266 |
} else |
267 |
return false; |
|
268 |
} |
|
269 |
||
270 |
private Env<AttrContext> getAttrContext(TreePath path) { |
|
271 |
if (!(path.getLeaf() instanceof JCTree)) // implicit null-check |
|
272 |
throw new IllegalArgumentException(); |
|
273 |
||
10631
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
274 |
// if we're being invoked from a Tree API client via parse/enter/analyze, |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
275 |
// we need to make sure all the classes have been entered; |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
276 |
// if we're being invoked from JSR 199 or JSR 269, then the classes |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
277 |
// will already have been entered. |
10 | 278 |
if (javacTaskImpl != null) { |
279 |
try { |
|
280 |
javacTaskImpl.enter(null); |
|
281 |
} catch (IOException e) { |
|
282 |
throw new Error("unexpected error while entering symbols: " + e); |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
||
287 |
JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit(); |
|
288 |
Copier copier = new Copier(treeMaker.forToplevel(unit)); |
|
289 |
||
290 |
Env<AttrContext> env = null; |
|
291 |
JCMethodDecl method = null; |
|
292 |
JCVariableDecl field = null; |
|
293 |
||
294 |
List<Tree> l = List.nil(); |
|
295 |
TreePath p = path; |
|
296 |
while (p != null) { |
|
297 |
l = l.prepend(p.getLeaf()); |
|
298 |
p = p.getParentPath(); |
|
299 |
} |
|
300 |
||
301 |
for ( ; l.nonEmpty(); l = l.tail) { |
|
302 |
Tree tree = l.head; |
|
303 |
switch (tree.getKind()) { |
|
304 |
case COMPILATION_UNIT: |
|
305 |
// System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile); |
|
306 |
env = enter.getTopLevelEnv((JCCompilationUnit)tree); |
|
307 |
break; |
|
7077
55689b828dc5
6993304: JavacTrees.getAttrContext not updated to Tree.Kind.{ANNOTATION_TYPE,ENUM,INTERFACE}
jjg
parents:
6717
diff
changeset
|
308 |
case ANNOTATION_TYPE: |
10 | 309 |
case CLASS: |
7077
55689b828dc5
6993304: JavacTrees.getAttrContext not updated to Tree.Kind.{ANNOTATION_TYPE,ENUM,INTERFACE}
jjg
parents:
6717
diff
changeset
|
310 |
case ENUM: |
6600 | 311 |
case INTERFACE: |
10 | 312 |
// System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName()); |
313 |
env = enter.getClassEnv(((JCClassDecl)tree).sym); |
|
314 |
break; |
|
315 |
case METHOD: |
|
316 |
// System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName()); |
|
317 |
method = (JCMethodDecl)tree; |
|
318 |
break; |
|
319 |
case VARIABLE: |
|
320 |
// System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName()); |
|
321 |
field = (JCVariableDecl)tree; |
|
322 |
break; |
|
323 |
case BLOCK: { |
|
324 |
// System.err.println("BLOCK: "); |
|
10631
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
325 |
if (method != null) { |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
326 |
try { |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
327 |
Assert.check(method.body == tree); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
328 |
method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf()); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
329 |
env = memberEnter.getMethodEnv(method, env); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
330 |
env = attribStatToTree(method.body, env, copier.leafCopy); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
331 |
} finally { |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
332 |
method.body = (JCBlock) tree; |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
333 |
} |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
334 |
} else { |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
335 |
JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf()); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
336 |
env = attribStatToTree(body, env, copier.leafCopy); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
337 |
} |
10 | 338 |
return env; |
339 |
} |
|
340 |
default: |
|
341 |
// System.err.println("DEFAULT: " + tree.getKind()); |
|
342 |
if (field != null && field.getInitializer() == tree) { |
|
343 |
env = memberEnter.getInitEnv(field, env); |
|
344 |
JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf()); |
|
345 |
env = attribExprToTree(expr, env, copier.leafCopy); |
|
346 |
return env; |
|
347 |
} |
|
348 |
} |
|
349 |
} |
|
10631
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
9599
diff
changeset
|
350 |
return (field != null) ? memberEnter.getInitEnv(field, env) : env; |
10 | 351 |
} |
352 |
||
353 |
private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) { |
|
354 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
355 |
try { |
|
356 |
return attr.attribStatToTree(stat, env, tree); |
|
357 |
} finally { |
|
358 |
log.useSource(prev); |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) { |
|
363 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
364 |
try { |
|
365 |
return attr.attribExprToTree(expr, env, tree); |
|
366 |
} finally { |
|
367 |
log.useSource(prev); |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Makes a copy of a tree, noting the value resulting from copying a particular leaf. |
|
373 |
**/ |
|
374 |
static class Copier extends TreeCopier<JCTree> { |
|
375 |
JCTree leafCopy = null; |
|
376 |
||
377 |
Copier(TreeMaker M) { |
|
378 |
super(M); |
|
379 |
} |
|
380 |
||
6717 | 381 |
@Override |
10 | 382 |
public <T extends JCTree> T copy(T t, JCTree leaf) { |
383 |
T t2 = super.copy(t, leaf); |
|
384 |
if (t == leaf) |
|
385 |
leafCopy = t2; |
|
386 |
return t2; |
|
387 |
} |
|
388 |
} |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
389 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
390 |
/** |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
391 |
* Gets the original type from the ErrorType object. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
392 |
* @param errorType The errorType for which we want to get the original type. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
393 |
* @returns TypeMirror corresponding to the original type, replaced by the ErrorType. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
394 |
* noType (type.tag == NONE) is returned if there is no original type. |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
395 |
*/ |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
396 |
public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) { |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
397 |
if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) { |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
398 |
return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType(); |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
399 |
} |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
400 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
401 |
return com.sun.tools.javac.code.Type.noType; |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
10
diff
changeset
|
402 |
} |
3149 | 403 |
|
404 |
/** |
|
405 |
* Prints a message of the specified kind at the location of the |
|
406 |
* tree within the provided compilation unit |
|
407 |
* |
|
408 |
* @param kind the kind of message |
|
409 |
* @param msg the message, or an empty string if none |
|
410 |
* @param t the tree to use as a position hint |
|
411 |
* @param root the compilation unit that contains tree |
|
412 |
*/ |
|
413 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
|
414 |
com.sun.source.tree.Tree t, |
|
415 |
com.sun.source.tree.CompilationUnitTree root) { |
|
416 |
JavaFileObject oldSource = null; |
|
417 |
JavaFileObject newSource = null; |
|
418 |
JCDiagnostic.DiagnosticPosition pos = null; |
|
419 |
||
420 |
newSource = root.getSourceFile(); |
|
421 |
if (newSource != null) { |
|
422 |
oldSource = log.useSource(newSource); |
|
423 |
pos = ((JCTree) t).pos(); |
|
424 |
} |
|
425 |
||
426 |
try { |
|
427 |
switch (kind) { |
|
428 |
case ERROR: |
|
429 |
boolean prev = log.multipleErrors; |
|
430 |
try { |
|
431 |
log.error(pos, "proc.messager", msg.toString()); |
|
432 |
} finally { |
|
433 |
log.multipleErrors = prev; |
|
434 |
} |
|
435 |
break; |
|
436 |
||
437 |
case WARNING: |
|
438 |
log.warning(pos, "proc.messager", msg.toString()); |
|
439 |
break; |
|
440 |
||
441 |
case MANDATORY_WARNING: |
|
442 |
log.mandatoryWarning(pos, "proc.messager", msg.toString()); |
|
443 |
break; |
|
444 |
||
445 |
default: |
|
446 |
log.note(pos, "proc.messager", msg.toString()); |
|
447 |
} |
|
448 |
} finally { |
|
449 |
if (oldSource != null) |
|
450 |
log.useSource(oldSource); |
|
451 |
} |
|
452 |
} |
|
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
453 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
454 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
455 |
public TypeMirror getLub(CatchTree tree) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
456 |
JCCatch ct = (JCCatch) tree; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
457 |
JCVariableDecl v = ct.param; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
458 |
if (v.type != null && v.type.getKind() == TypeKind.UNION) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
459 |
UnionClassType ut = (UnionClassType) v.type; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
460 |
return ut.getLub(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
461 |
} else { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
462 |
return v.type; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
463 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
7631
diff
changeset
|
464 |
} |
10 | 465 |
} |