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