author | jjg |
Tue, 08 Nov 2011 11:51:05 -0800 | |
changeset 10950 | e87b50888909 |
parent 10638 | c8e9604cf151 |
child 11708 | f83264d62af2 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, 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.File; |
|
29 |
import java.io.IOException; |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
30 |
import java.nio.CharBuffer; |
10 | 31 |
import java.util.*; |
32 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
33 |
||
34 |
import javax.annotation.processing.Processor; |
|
35 |
import javax.lang.model.element.Element; |
|
36 |
import javax.lang.model.element.TypeElement; |
|
37 |
import javax.lang.model.type.TypeMirror; |
|
38 |
import javax.tools.*; |
|
39 |
||
40 |
import com.sun.source.tree.*; |
|
41 |
import com.sun.source.util.*; |
|
42 |
import com.sun.tools.javac.code.*; |
|
43 |
import com.sun.tools.javac.code.Symbol.*; |
|
44 |
import com.sun.tools.javac.comp.*; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
45 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 46 |
import com.sun.tools.javac.main.*; |
47 |
import com.sun.tools.javac.model.*; |
|
48 |
import com.sun.tools.javac.parser.Parser; |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
49 |
import com.sun.tools.javac.parser.ParserFactory; |
10 | 50 |
import com.sun.tools.javac.tree.*; |
51 |
import com.sun.tools.javac.tree.JCTree.*; |
|
52 |
import com.sun.tools.javac.util.*; |
|
53 |
import com.sun.tools.javac.util.List; |
|
54 |
import com.sun.tools.javac.main.JavaCompiler; |
|
55 |
||
56 |
/** |
|
5848
c5a4ce47e780
6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5847
diff
changeset
|
57 |
* Provides access to functionality specific to the JDK Java Compiler, javac. |
10 | 58 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
59 |
* <p><b>This is NOT part of any supported API. |
10 | 60 |
* If you write code that depends on this, you do so at your own |
61 |
* risk. This code and its internal interfaces are subject to change |
|
62 |
* or deletion without notice.</b></p> |
|
63 |
* |
|
64 |
* @author Peter von der Ahé |
|
65 |
* @author Jonathan Gibbons |
|
66 |
*/ |
|
67 |
public class JavacTaskImpl extends JavacTask { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
68 |
private ClientCodeWrapper ccw; |
10 | 69 |
private Main compilerMain; |
70 |
private JavaCompiler compiler; |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1355
diff
changeset
|
71 |
private Locale locale; |
10 | 72 |
private String[] args; |
73 |
private Context context; |
|
74 |
private List<JavaFileObject> fileObjects; |
|
75 |
private Map<JavaFileObject, JCCompilationUnit> notYetEntered; |
|
76 |
private ListBuffer<Env<AttrContext>> genList; |
|
77 |
private TaskListener taskListener; |
|
78 |
private AtomicBoolean used = new AtomicBoolean(); |
|
79 |
private Iterable<? extends Processor> processors; |
|
80 |
||
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10631
diff
changeset
|
81 |
private Main.Result result = null; |
10 | 82 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
83 |
JavacTaskImpl(Main compilerMain, |
10 | 84 |
String[] args, |
85 |
Context context, |
|
86 |
List<JavaFileObject> fileObjects) { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
87 |
this.ccw = ClientCodeWrapper.instance(context); |
10 | 88 |
this.compilerMain = compilerMain; |
89 |
this.args = args; |
|
90 |
this.context = context; |
|
91 |
this.fileObjects = fileObjects; |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1355
diff
changeset
|
92 |
setLocale(Locale.getDefault()); |
10 | 93 |
// null checks |
94 |
compilerMain.getClass(); |
|
95 |
args.getClass(); |
|
96 |
fileObjects.getClass(); |
|
97 |
} |
|
98 |
||
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
99 |
JavacTaskImpl(Main compilerMain, |
10 | 100 |
Iterable<String> flags, |
101 |
Context context, |
|
102 |
Iterable<String> classes, |
|
103 |
Iterable<? extends JavaFileObject> fileObjects) { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
104 |
this(compilerMain, toArray(flags, classes), context, toList(fileObjects)); |
10 | 105 |
} |
106 |
||
107 |
static private String[] toArray(Iterable<String> flags, Iterable<String> classes) { |
|
108 |
ListBuffer<String> result = new ListBuffer<String>(); |
|
109 |
if (flags != null) |
|
110 |
for (String flag : flags) |
|
111 |
result.append(flag); |
|
112 |
if (classes != null) |
|
113 |
for (String cls : classes) |
|
114 |
result.append(cls); |
|
115 |
return result.toArray(new String[result.length()]); |
|
116 |
} |
|
117 |
||
118 |
static private List<JavaFileObject> toList(Iterable<? extends JavaFileObject> fileObjects) { |
|
119 |
if (fileObjects == null) |
|
120 |
return List.nil(); |
|
121 |
ListBuffer<JavaFileObject> result = new ListBuffer<JavaFileObject>(); |
|
122 |
for (JavaFileObject fo : fileObjects) |
|
123 |
result.append(fo); |
|
124 |
return result.toList(); |
|
125 |
} |
|
126 |
||
127 |
public Boolean call() { |
|
128 |
if (!used.getAndSet(true)) { |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
129 |
initContext(); |
3149 | 130 |
notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>(); |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
131 |
compilerMain.setAPIMode(true); |
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
132 |
result = compilerMain.compile(args, context, fileObjects, processors); |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
133 |
cleanup(); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10631
diff
changeset
|
134 |
return result.isOK(); |
10 | 135 |
} else { |
136 |
throw new IllegalStateException("multiple calls to method 'call'"); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
public void setProcessors(Iterable<? extends Processor> processors) { |
|
141 |
processors.getClass(); // null check |
|
142 |
// not mt-safe |
|
143 |
if (used.get()) |
|
144 |
throw new IllegalStateException(); |
|
145 |
this.processors = processors; |
|
146 |
} |
|
147 |
||
148 |
public void setLocale(Locale locale) { |
|
149 |
if (used.get()) |
|
150 |
throw new IllegalStateException(); |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1355
diff
changeset
|
151 |
this.locale = locale; |
10 | 152 |
} |
153 |
||
154 |
private void prepareCompiler() throws IOException { |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
155 |
if (used.getAndSet(true)) { |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
156 |
if (compiler == null) |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
157 |
throw new IllegalStateException(); |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
158 |
} else { |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
159 |
initContext(); |
10 | 160 |
compilerMain.setOptions(Options.instance(context)); |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
161 |
compilerMain.filenames = new LinkedHashSet<File>(); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
162 |
Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args)); |
10 | 163 |
if (!filenames.isEmpty()) |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
164 |
throw new IllegalArgumentException("Malformed arguments " + toString(filenames, " ")); |
10 | 165 |
compiler = JavaCompiler.instance(context); |
166 |
compiler.keepComments = true; |
|
167 |
compiler.genEndPos = true; |
|
168 |
// NOTE: this value will be updated after annotation processing |
|
169 |
compiler.initProcessAnnotations(processors); |
|
170 |
notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>(); |
|
171 |
for (JavaFileObject file: fileObjects) |
|
172 |
notYetEntered.put(file, null); |
|
173 |
genList = new ListBuffer<Env<AttrContext>>(); |
|
174 |
// endContext will be called when all classes have been generated |
|
175 |
// TODO: should handle the case after each phase if errors have occurred |
|
176 |
args = null; |
|
177 |
} |
|
178 |
} |
|
179 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
180 |
<T> String toString(Iterable<T> items, String sep) { |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
181 |
String currSep = ""; |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
182 |
StringBuilder sb = new StringBuilder(); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
183 |
for (T item: items) { |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
184 |
sb.append(currSep); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
185 |
sb.append(item.toString()); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
186 |
currSep = sep; |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
187 |
} |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
188 |
return sb.toString(); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
189 |
} |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
190 |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
191 |
private void initContext() { |
10 | 192 |
context.put(JavacTaskImpl.class, this); |
193 |
if (context.get(TaskListener.class) != null) |
|
194 |
context.put(TaskListener.class, (TaskListener)null); |
|
195 |
if (taskListener != null) |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
196 |
context.put(TaskListener.class, ccw.wrap(taskListener)); |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1355
diff
changeset
|
197 |
//initialize compiler's default locale |
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
198 |
context.put(Locale.class, locale); |
10 | 199 |
} |
200 |
||
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
201 |
void cleanup() { |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
202 |
if (compiler != null) |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
203 |
compiler.close(); |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
204 |
compiler = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
205 |
compilerMain = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
206 |
args = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
207 |
context = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
208 |
fileObjects = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
209 |
notYetEntered = null; |
10 | 210 |
} |
211 |
||
212 |
/** |
|
213 |
* Construct a JavaFileObject from the given file. |
|
214 |
* |
|
215 |
* <p><b>TODO: this method is useless here</b></p> |
|
216 |
* |
|
217 |
* @param file a file |
|
218 |
* @return a JavaFileObject from the standard file manager. |
|
219 |
*/ |
|
220 |
public JavaFileObject asJavaFileObject(File file) { |
|
221 |
JavacFileManager fm = (JavacFileManager)context.get(JavaFileManager.class); |
|
222 |
return fm.getRegularFile(file); |
|
223 |
} |
|
224 |
||
225 |
public void setTaskListener(TaskListener taskListener) { |
|
226 |
this.taskListener = taskListener; |
|
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* Parse the specified files returning a list of abstract syntax trees. |
|
231 |
* |
|
232 |
* @throws java.io.IOException TODO |
|
233 |
* @return a list of abstract syntax trees |
|
234 |
*/ |
|
235 |
public Iterable<? extends CompilationUnitTree> parse() throws IOException { |
|
236 |
try { |
|
237 |
prepareCompiler(); |
|
238 |
List<JCCompilationUnit> units = compiler.parseFiles(fileObjects); |
|
239 |
for (JCCompilationUnit unit: units) { |
|
240 |
JavaFileObject file = unit.getSourceFile(); |
|
241 |
if (notYetEntered.containsKey(file)) |
|
242 |
notYetEntered.put(file, unit); |
|
243 |
} |
|
244 |
return units; |
|
245 |
} |
|
246 |
finally { |
|
247 |
parsed = true; |
|
248 |
if (compiler != null && compiler.log != null) |
|
249 |
compiler.log.flush(); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
private boolean parsed = false; |
|
254 |
||
255 |
/** |
|
256 |
* Translate all the abstract syntax trees to elements. |
|
257 |
* |
|
258 |
* @throws IOException TODO |
|
259 |
* @return a list of elements corresponding to the top level |
|
260 |
* classes in the abstract syntax trees |
|
261 |
*/ |
|
262 |
public Iterable<? extends TypeElement> enter() throws IOException { |
|
263 |
return enter(null); |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Translate the given abstract syntax trees to elements. |
|
268 |
* |
|
269 |
* @param trees a list of abstract syntax trees. |
|
270 |
* @throws java.io.IOException TODO |
|
271 |
* @return a list of elements corresponding to the top level |
|
272 |
* classes in the abstract syntax trees |
|
273 |
*/ |
|
274 |
public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees) |
|
275 |
throws IOException |
|
276 |
{ |
|
10631
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
10193
diff
changeset
|
277 |
if (trees == null && notYetEntered != null && notYetEntered.isEmpty()) |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
10193
diff
changeset
|
278 |
return List.nil(); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
10193
diff
changeset
|
279 |
|
10 | 280 |
prepareCompiler(); |
281 |
||
282 |
ListBuffer<JCCompilationUnit> roots = null; |
|
283 |
||
284 |
if (trees == null) { |
|
285 |
// If there are still files which were specified to be compiled |
|
286 |
// (i.e. in fileObjects) but which have not yet been entered, |
|
287 |
// then we make sure they have been parsed and add them to the |
|
288 |
// list to be entered. |
|
289 |
if (notYetEntered.size() > 0) { |
|
290 |
if (!parsed) |
|
291 |
parse(); // TODO would be nice to specify files needed to be parsed |
|
292 |
for (JavaFileObject file: fileObjects) { |
|
293 |
JCCompilationUnit unit = notYetEntered.remove(file); |
|
294 |
if (unit != null) { |
|
295 |
if (roots == null) |
|
296 |
roots = new ListBuffer<JCCompilationUnit>(); |
|
297 |
roots.append(unit); |
|
298 |
} |
|
299 |
} |
|
300 |
notYetEntered.clear(); |
|
301 |
} |
|
302 |
} |
|
303 |
else { |
|
304 |
for (CompilationUnitTree cu : trees) { |
|
305 |
if (cu instanceof JCCompilationUnit) { |
|
306 |
if (roots == null) |
|
307 |
roots = new ListBuffer<JCCompilationUnit>(); |
|
308 |
roots.append((JCCompilationUnit)cu); |
|
309 |
notYetEntered.remove(cu.getSourceFile()); |
|
310 |
} |
|
311 |
else |
|
312 |
throw new IllegalArgumentException(cu.toString()); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
if (roots == null) |
|
317 |
return List.nil(); |
|
318 |
||
319 |
try { |
|
320 |
List<JCCompilationUnit> units = compiler.enterTrees(roots.toList()); |
|
321 |
||
322 |
if (notYetEntered.isEmpty()) |
|
323 |
compiler = compiler.processAnnotations(units); |
|
324 |
||
325 |
ListBuffer<TypeElement> elements = new ListBuffer<TypeElement>(); |
|
326 |
for (JCCompilationUnit unit : units) { |
|
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
327 |
for (JCTree node : unit.defs) { |
10950 | 328 |
if (node.hasTag(JCTree.Tag.CLASSDEF)) { |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
329 |
JCClassDecl cdef = (JCClassDecl) node; |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
330 |
if (cdef.sym != null) // maybe null if errors in anno processing |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
331 |
elements.append(cdef.sym); |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
332 |
} |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
333 |
} |
10 | 334 |
} |
335 |
return elements.toList(); |
|
336 |
} |
|
337 |
finally { |
|
338 |
compiler.log.flush(); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Complete all analysis. |
|
344 |
* @throws IOException TODO |
|
345 |
*/ |
|
346 |
@Override |
|
347 |
public Iterable<? extends Element> analyze() throws IOException { |
|
348 |
return analyze(null); |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Complete all analysis on the given classes. |
|
353 |
* This can be used to ensure that all compile time errors are reported. |
|
354 |
* The classes must have previously been returned from {@link #enter}. |
|
355 |
* If null is specified, all outstanding classes will be analyzed. |
|
356 |
* |
|
357 |
* @param classes a list of class elements |
|
358 |
*/ |
|
359 |
// This implementation requires that we open up privileges on JavaCompiler. |
|
360 |
// An alternative implementation would be to move this code to JavaCompiler and |
|
361 |
// wrap it here |
|
362 |
public Iterable<? extends Element> analyze(Iterable<? extends TypeElement> classes) throws IOException { |
|
363 |
enter(null); // ensure all classes have been entered |
|
364 |
||
365 |
final ListBuffer<Element> results = new ListBuffer<Element>(); |
|
366 |
try { |
|
367 |
if (classes == null) { |
|
368 |
handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results); |
|
369 |
} else { |
|
370 |
Filter f = new Filter() { |
|
371 |
public void process(Env<AttrContext> env) { |
|
372 |
handleFlowResults(compiler.flow(compiler.attribute(env)), results); |
|
373 |
} |
|
374 |
}; |
|
375 |
f.run(compiler.todo, classes); |
|
376 |
} |
|
377 |
} finally { |
|
378 |
compiler.log.flush(); |
|
379 |
} |
|
380 |
return results; |
|
381 |
} |
|
382 |
// where |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
383 |
private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
384 |
for (Env<AttrContext> env: queue) { |
10 | 385 |
switch (env.tree.getTag()) { |
10950 | 386 |
case CLASSDEF: |
10 | 387 |
JCClassDecl cdef = (JCClassDecl) env.tree; |
388 |
if (cdef.sym != null) |
|
389 |
elems.append(cdef.sym); |
|
390 |
break; |
|
10950 | 391 |
case TOPLEVEL: |
10 | 392 |
JCCompilationUnit unit = (JCCompilationUnit) env.tree; |
393 |
if (unit.packge != null) |
|
394 |
elems.append(unit.packge); |
|
395 |
break; |
|
396 |
} |
|
397 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
398 |
genList.addAll(queue); |
10 | 399 |
} |
400 |
||
401 |
||
402 |
/** |
|
403 |
* Generate code. |
|
404 |
* @throws IOException TODO |
|
405 |
*/ |
|
406 |
@Override |
|
407 |
public Iterable<? extends JavaFileObject> generate() throws IOException { |
|
408 |
return generate(null); |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* Generate code corresponding to the given classes. |
|
413 |
* The classes must have previously been returned from {@link #enter}. |
|
414 |
* If there are classes outstanding to be analyzed, that will be done before |
|
415 |
* any classes are generated. |
|
416 |
* If null is specified, code will be generated for all outstanding classes. |
|
417 |
* |
|
418 |
* @param classes a list of class elements |
|
419 |
*/ |
|
420 |
public Iterable<? extends JavaFileObject> generate(Iterable<? extends TypeElement> classes) throws IOException { |
|
421 |
final ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); |
|
422 |
try { |
|
423 |
analyze(null); // ensure all classes have been parsed, entered, and analyzed |
|
424 |
||
425 |
if (classes == null) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
426 |
compiler.generate(compiler.desugar(genList), results); |
10 | 427 |
genList.clear(); |
428 |
} |
|
429 |
else { |
|
430 |
Filter f = new Filter() { |
|
431 |
public void process(Env<AttrContext> env) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
432 |
compiler.generate(compiler.desugar(ListBuffer.of(env)), results); |
10 | 433 |
} |
434 |
}; |
|
435 |
f.run(genList, classes); |
|
436 |
} |
|
437 |
if (genList.isEmpty()) { |
|
438 |
compiler.reportDeferredDiagnostics(); |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
439 |
cleanup(); |
10 | 440 |
} |
441 |
} |
|
442 |
finally { |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
443 |
if (compiler != null) |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
444 |
compiler.log.flush(); |
10 | 445 |
} |
446 |
return results; |
|
447 |
} |
|
448 |
||
449 |
public TypeMirror getTypeMirror(Iterable<? extends Tree> path) { |
|
450 |
// TODO: Should complete attribution if necessary |
|
451 |
Tree last = null; |
|
452 |
for (Tree node : path) |
|
453 |
last = node; |
|
454 |
return ((JCTree)last).type; |
|
455 |
} |
|
456 |
||
457 |
public JavacElements getElements() { |
|
458 |
if (context == null) |
|
459 |
throw new IllegalStateException(); |
|
460 |
return JavacElements.instance(context); |
|
461 |
} |
|
462 |
||
463 |
public JavacTypes getTypes() { |
|
464 |
if (context == null) |
|
465 |
throw new IllegalStateException(); |
|
466 |
return JavacTypes.instance(context); |
|
467 |
} |
|
468 |
||
469 |
public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) { |
|
470 |
return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse(); |
|
471 |
} |
|
472 |
||
473 |
abstract class Filter { |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
474 |
void run(Queue<Env<AttrContext>> list, Iterable<? extends TypeElement> classes) { |
10 | 475 |
Set<TypeElement> set = new HashSet<TypeElement>(); |
476 |
for (TypeElement item: classes) |
|
477 |
set.add(item); |
|
478 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
479 |
ListBuffer<Env<AttrContext>> defer = ListBuffer.<Env<AttrContext>>lb(); |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
480 |
while (list.peek() != null) { |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
481 |
Env<AttrContext> env = list.remove(); |
10 | 482 |
ClassSymbol csym = env.enclClass.sym; |
483 |
if (csym != null && set.contains(csym.outermostClass())) |
|
484 |
process(env); |
|
485 |
else |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
486 |
defer = defer.append(env); |
10 | 487 |
} |
488 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
489 |
list.addAll(defer); |
10 | 490 |
} |
491 |
||
492 |
abstract void process(Env<AttrContext> env); |
|
493 |
} |
|
494 |
||
495 |
/** |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
496 |
* For internal use only. This method will be |
10 | 497 |
* removed without warning. |
498 |
*/ |
|
499 |
public Context getContext() { |
|
500 |
return context; |
|
501 |
} |
|
502 |
||
503 |
/** |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
504 |
* For internal use only. This method will be |
10 | 505 |
* removed without warning. |
506 |
*/ |
|
507 |
public void updateContext(Context newContext) { |
|
508 |
context = newContext; |
|
509 |
} |
|
510 |
||
511 |
/** |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
512 |
* For internal use only. This method will be |
10 | 513 |
* removed without warning. |
514 |
*/ |
|
515 |
public Type parseType(String expr, TypeElement scope) { |
|
516 |
if (expr == null || expr.equals("")) |
|
517 |
throw new IllegalArgumentException(); |
|
518 |
compiler = JavaCompiler.instance(context); |
|
519 |
JavaFileObject prev = compiler.log.useSource(null); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
520 |
ParserFactory parserFactory = ParserFactory.instance(context); |
10 | 521 |
Attr attr = Attr.instance(context); |
522 |
try { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
523 |
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
524 |
Parser parser = parserFactory.newParser(buf, false, false, false); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
525 |
JCTree tree = parser.parseType(); |
10 | 526 |
return attr.attribType(tree, (Symbol.TypeSymbol)scope); |
527 |
} finally { |
|
528 |
compiler.log.useSource(prev); |
|
529 |
} |
|
530 |
} |
|
531 |
||
532 |
} |