author | jjg |
Tue, 23 Sep 2008 10:44:51 -0700 | |
changeset 1355 | 74cc97efef51 |
parent 1260 | a772ba9ba43d |
child 1870 | 57a1138dffc8 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
735 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.main; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.HashSet; |
|
1046
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
30 |
import java.util.LinkedHashSet; |
10 | 31 |
import java.util.LinkedHashMap; |
32 |
import java.util.Map; |
|
33 |
import java.util.MissingResourceException; |
|
34 |
import java.util.ResourceBundle; |
|
35 |
import java.util.Set; |
|
36 |
import java.util.logging.Handler; |
|
37 |
import java.util.logging.Level; |
|
38 |
import java.util.logging.Logger; |
|
39 |
||
40 |
import javax.tools.JavaFileManager; |
|
41 |
import javax.tools.JavaFileObject; |
|
42 |
import javax.tools.DiagnosticListener; |
|
43 |
||
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
44 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 45 |
import com.sun.source.util.TaskEvent; |
46 |
import com.sun.source.util.TaskListener; |
|
47 |
||
48 |
import com.sun.tools.javac.util.*; |
|
49 |
import com.sun.tools.javac.code.*; |
|
50 |
import com.sun.tools.javac.tree.*; |
|
51 |
import com.sun.tools.javac.parser.*; |
|
52 |
import com.sun.tools.javac.comp.*; |
|
53 |
import com.sun.tools.javac.jvm.*; |
|
54 |
||
55 |
import com.sun.tools.javac.code.Symbol.*; |
|
56 |
import com.sun.tools.javac.tree.JCTree.*; |
|
57 |
||
58 |
import com.sun.tools.javac.processing.*; |
|
59 |
import javax.annotation.processing.Processor; |
|
60 |
||
61 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
|
62 |
import static com.sun.tools.javac.util.ListBuffer.lb; |
|
63 |
||
64 |
// TEMP, until we have a more efficient way to save doc comment info |
|
65 |
import com.sun.tools.javac.parser.DocCommentScanner; |
|
66 |
||
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
67 |
import java.util.HashMap; |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
68 |
import java.util.Queue; |
10 | 69 |
import javax.lang.model.SourceVersion; |
70 |
||
71 |
/** This class could be the main entry point for GJC when GJC is used as a |
|
72 |
* component in a larger software system. It provides operations to |
|
73 |
* construct a new compiler, and to run a new compiler on a set of source |
|
74 |
* files. |
|
75 |
* |
|
76 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
77 |
* you write code that depends on this, you do so at your own risk. |
|
78 |
* This code and its internal interfaces are subject to change or |
|
79 |
* deletion without notice.</b> |
|
80 |
*/ |
|
81 |
public class JavaCompiler implements ClassReader.SourceCompleter { |
|
82 |
/** The context key for the compiler. */ |
|
83 |
protected static final Context.Key<JavaCompiler> compilerKey = |
|
84 |
new Context.Key<JavaCompiler>(); |
|
85 |
||
86 |
/** Get the JavaCompiler instance for this context. */ |
|
87 |
public static JavaCompiler instance(Context context) { |
|
88 |
JavaCompiler instance = context.get(compilerKey); |
|
89 |
if (instance == null) |
|
90 |
instance = new JavaCompiler(context); |
|
91 |
return instance; |
|
92 |
} |
|
93 |
||
94 |
/** The current version number as a string. |
|
95 |
*/ |
|
96 |
public static String version() { |
|
97 |
return version("release"); // mm.nn.oo[-milestone] |
|
98 |
} |
|
99 |
||
100 |
/** The current full version number as a string. |
|
101 |
*/ |
|
102 |
public static String fullVersion() { |
|
103 |
return version("full"); // mm.mm.oo[-milestone]-build |
|
104 |
} |
|
105 |
||
106 |
private static final String versionRBName = "com.sun.tools.javac.resources.version"; |
|
107 |
private static ResourceBundle versionRB; |
|
108 |
||
109 |
private static String version(String key) { |
|
110 |
if (versionRB == null) { |
|
111 |
try { |
|
112 |
versionRB = ResourceBundle.getBundle(versionRBName); |
|
113 |
} catch (MissingResourceException e) { |
|
114 |
return Log.getLocalizedString("version.resource.missing", System.getProperty("java.version")); |
|
115 |
} |
|
116 |
} |
|
117 |
try { |
|
118 |
return versionRB.getString(key); |
|
119 |
} |
|
120 |
catch (MissingResourceException e) { |
|
121 |
return Log.getLocalizedString("version.unknown", System.getProperty("java.version")); |
|
122 |
} |
|
123 |
} |
|
124 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
125 |
/** |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
126 |
* Control how the compiler's latter phases (attr, flow, desugar, generate) |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
127 |
* are connected. Each individual file is processed by each phase in turn, |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
128 |
* but with different compile policies, you can control the order in which |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
129 |
* each class is processed through its next phase. |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
130 |
* |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
131 |
* <p>Generally speaking, the compiler will "fail fast" in the face of |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
132 |
* errors, although not aggressively so. flow, desugar, etc become no-ops |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
133 |
* once any errors have occurred. No attempt is currently made to determine |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
134 |
* if it might be safe to process a class through its next phase because |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
135 |
* it does not depend on any unrelated errors that might have occurred. |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
136 |
*/ |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
137 |
protected static enum CompilePolicy { |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
138 |
/** |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
139 |
* Just attribute the parse trees. |
10 | 140 |
*/ |
141 |
ATTR_ONLY, |
|
142 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
143 |
/** |
10 | 144 |
* Just attribute and do flow analysis on the parse trees. |
145 |
* This should catch most user errors. |
|
146 |
*/ |
|
147 |
CHECK_ONLY, |
|
148 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
149 |
/** |
10 | 150 |
* Attribute everything, then do flow analysis for everything, |
151 |
* then desugar everything, and only then generate output. |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
152 |
* This means no output will be generated if there are any |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
153 |
* errors in any classes. |
10 | 154 |
*/ |
155 |
SIMPLE, |
|
156 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
157 |
/** |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
158 |
* Groups the classes for each source file together, then process |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
159 |
* each group in a manner equivalent to the {@code SIMPLE} policy. |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
160 |
* This means no output will be generated if there are any |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
161 |
* errors in any of the classes in a source file. |
10 | 162 |
*/ |
163 |
BY_FILE, |
|
164 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
165 |
/** |
10 | 166 |
* Completely process each entry on the todo list in turn. |
167 |
* -- this is the same for 1.5. |
|
168 |
* Means output might be generated for some classes in a compilation unit |
|
169 |
* and not others. |
|
170 |
*/ |
|
171 |
BY_TODO; |
|
172 |
||
173 |
static CompilePolicy decode(String option) { |
|
174 |
if (option == null) |
|
175 |
return DEFAULT_COMPILE_POLICY; |
|
176 |
else if (option.equals("attr")) |
|
177 |
return ATTR_ONLY; |
|
178 |
else if (option.equals("check")) |
|
179 |
return CHECK_ONLY; |
|
180 |
else if (option.equals("simple")) |
|
181 |
return SIMPLE; |
|
182 |
else if (option.equals("byfile")) |
|
183 |
return BY_FILE; |
|
184 |
else if (option.equals("bytodo")) |
|
185 |
return BY_TODO; |
|
186 |
else |
|
187 |
return DEFAULT_COMPILE_POLICY; |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
private static CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO; |
|
192 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
193 |
protected static enum ImplicitSourcePolicy { |
10 | 194 |
/** Don't generate or process implicitly read source files. */ |
195 |
NONE, |
|
196 |
/** Generate classes for implicitly read source files. */ |
|
197 |
CLASS, |
|
198 |
/** Like CLASS, but generate warnings if annotation processing occurs */ |
|
199 |
UNSET; |
|
200 |
||
201 |
static ImplicitSourcePolicy decode(String option) { |
|
202 |
if (option == null) |
|
203 |
return UNSET; |
|
204 |
else if (option.equals("none")) |
|
205 |
return NONE; |
|
206 |
else if (option.equals("class")) |
|
207 |
return CLASS; |
|
208 |
else |
|
209 |
return UNSET; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
/** The log to be used for error reporting. |
|
214 |
*/ |
|
215 |
public Log log; |
|
216 |
||
169 | 217 |
/** Factory for creating diagnostic objects |
218 |
*/ |
|
219 |
JCDiagnostic.Factory diagFactory; |
|
220 |
||
10 | 221 |
/** The tree factory module. |
222 |
*/ |
|
223 |
protected TreeMaker make; |
|
224 |
||
225 |
/** The class reader. |
|
226 |
*/ |
|
227 |
protected ClassReader reader; |
|
228 |
||
229 |
/** The class writer. |
|
230 |
*/ |
|
231 |
protected ClassWriter writer; |
|
232 |
||
233 |
/** The module for the symbol table entry phases. |
|
234 |
*/ |
|
235 |
protected Enter enter; |
|
236 |
||
237 |
/** The symbol table. |
|
238 |
*/ |
|
239 |
protected Symtab syms; |
|
240 |
||
241 |
/** The language version. |
|
242 |
*/ |
|
243 |
protected Source source; |
|
244 |
||
245 |
/** The module for code generation. |
|
246 |
*/ |
|
247 |
protected Gen gen; |
|
248 |
||
249 |
/** The name table. |
|
250 |
*/ |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
251 |
protected Names names; |
10 | 252 |
|
253 |
/** The attributor. |
|
254 |
*/ |
|
255 |
protected Attr attr; |
|
256 |
||
257 |
/** The attributor. |
|
258 |
*/ |
|
259 |
protected Check chk; |
|
260 |
||
261 |
/** The flow analyzer. |
|
262 |
*/ |
|
263 |
protected Flow flow; |
|
264 |
||
265 |
/** The type eraser. |
|
266 |
*/ |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
267 |
protected TransTypes transTypes; |
10 | 268 |
|
269 |
/** The syntactic sugar desweetener. |
|
270 |
*/ |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
271 |
protected Lower lower; |
10 | 272 |
|
273 |
/** The annotation annotator. |
|
274 |
*/ |
|
275 |
protected Annotate annotate; |
|
276 |
||
277 |
/** Force a completion failure on this name |
|
278 |
*/ |
|
279 |
protected final Name completionFailureName; |
|
280 |
||
281 |
/** Type utilities. |
|
282 |
*/ |
|
283 |
protected Types types; |
|
284 |
||
285 |
/** Access to file objects. |
|
286 |
*/ |
|
287 |
protected JavaFileManager fileManager; |
|
288 |
||
289 |
/** Factory for parsers. |
|
290 |
*/ |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
1046
diff
changeset
|
291 |
protected ParserFactory parserFactory; |
10 | 292 |
|
293 |
/** Optional listener for progress events |
|
294 |
*/ |
|
295 |
protected TaskListener taskListener; |
|
296 |
||
297 |
/** |
|
298 |
* Annotation processing may require and provide a new instance |
|
299 |
* of the compiler to be used for the analyze and generate phases. |
|
300 |
*/ |
|
301 |
protected JavaCompiler delegateCompiler; |
|
302 |
||
303 |
/** |
|
304 |
* Flag set if any annotation processing occurred. |
|
305 |
**/ |
|
306 |
protected boolean annotationProcessingOccurred; |
|
307 |
||
308 |
/** |
|
309 |
* Flag set if any implicit source files read. |
|
310 |
**/ |
|
311 |
protected boolean implicitSourceFilesRead; |
|
312 |
||
313 |
protected Context context; |
|
314 |
||
315 |
/** Construct a new compiler using a shared context. |
|
316 |
*/ |
|
317 |
public JavaCompiler(final Context context) { |
|
318 |
this.context = context; |
|
319 |
context.put(compilerKey, this); |
|
320 |
||
321 |
// if fileManager not already set, register the JavacFileManager to be used |
|
322 |
if (context.get(JavaFileManager.class) == null) |
|
323 |
JavacFileManager.preRegister(context); |
|
324 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
325 |
names = Names.instance(context); |
10 | 326 |
log = Log.instance(context); |
169 | 327 |
diagFactory = JCDiagnostic.Factory.instance(context); |
10 | 328 |
reader = ClassReader.instance(context); |
329 |
make = TreeMaker.instance(context); |
|
330 |
writer = ClassWriter.instance(context); |
|
331 |
enter = Enter.instance(context); |
|
332 |
todo = Todo.instance(context); |
|
333 |
||
334 |
fileManager = context.get(JavaFileManager.class); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
1046
diff
changeset
|
335 |
parserFactory = ParserFactory.instance(context); |
10 | 336 |
|
337 |
try { |
|
338 |
// catch completion problems with predefineds |
|
339 |
syms = Symtab.instance(context); |
|
340 |
} catch (CompletionFailure ex) { |
|
341 |
// inlined Check.completionError as it is not initialized yet |
|
169 | 342 |
log.error("cant.access", ex.sym, ex.getDetailValue()); |
10 | 343 |
if (ex instanceof ClassReader.BadClassFile) |
344 |
throw new Abort(); |
|
345 |
} |
|
346 |
source = Source.instance(context); |
|
347 |
attr = Attr.instance(context); |
|
348 |
chk = Check.instance(context); |
|
349 |
gen = Gen.instance(context); |
|
350 |
flow = Flow.instance(context); |
|
351 |
transTypes = TransTypes.instance(context); |
|
352 |
lower = Lower.instance(context); |
|
353 |
annotate = Annotate.instance(context); |
|
354 |
types = Types.instance(context); |
|
355 |
taskListener = context.get(TaskListener.class); |
|
356 |
||
357 |
reader.sourceCompleter = this; |
|
358 |
||
359 |
Options options = Options.instance(context); |
|
360 |
||
361 |
verbose = options.get("-verbose") != null; |
|
362 |
sourceOutput = options.get("-printsource") != null; // used to be -s |
|
363 |
stubOutput = options.get("-stubs") != null; |
|
364 |
relax = options.get("-relax") != null; |
|
365 |
printFlat = options.get("-printflat") != null; |
|
366 |
attrParseOnly = options.get("-attrparseonly") != null; |
|
367 |
encoding = options.get("-encoding"); |
|
368 |
lineDebugInfo = options.get("-g:") == null || |
|
369 |
options.get("-g:lines") != null; |
|
370 |
genEndPos = options.get("-Xjcov") != null || |
|
371 |
context.get(DiagnosticListener.class) != null; |
|
372 |
devVerbose = options.get("dev") != null; |
|
373 |
processPcks = options.get("process.packages") != null; |
|
374 |
||
375 |
verboseCompilePolicy = options.get("verboseCompilePolicy") != null; |
|
376 |
||
377 |
if (attrParseOnly) |
|
378 |
compilePolicy = CompilePolicy.ATTR_ONLY; |
|
379 |
else |
|
380 |
compilePolicy = CompilePolicy.decode(options.get("compilePolicy")); |
|
381 |
||
382 |
implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit")); |
|
383 |
||
384 |
completionFailureName = |
|
385 |
(options.get("failcomplete") != null) |
|
386 |
? names.fromString(options.get("failcomplete")) |
|
387 |
: null; |
|
388 |
} |
|
389 |
||
390 |
/* Switches: |
|
391 |
*/ |
|
392 |
||
393 |
/** Verbose output. |
|
394 |
*/ |
|
395 |
public boolean verbose; |
|
396 |
||
397 |
/** Emit plain Java source files rather than class files. |
|
398 |
*/ |
|
399 |
public boolean sourceOutput; |
|
400 |
||
401 |
/** Emit stub source files rather than class files. |
|
402 |
*/ |
|
403 |
public boolean stubOutput; |
|
404 |
||
405 |
/** Generate attributed parse tree only. |
|
406 |
*/ |
|
407 |
public boolean attrParseOnly; |
|
408 |
||
409 |
/** Switch: relax some constraints for producing the jsr14 prototype. |
|
410 |
*/ |
|
411 |
boolean relax; |
|
412 |
||
413 |
/** Debug switch: Emit Java sources after inner class flattening. |
|
414 |
*/ |
|
415 |
public boolean printFlat; |
|
416 |
||
417 |
/** The encoding to be used for source input. |
|
418 |
*/ |
|
419 |
public String encoding; |
|
420 |
||
421 |
/** Generate code with the LineNumberTable attribute for debugging |
|
422 |
*/ |
|
423 |
public boolean lineDebugInfo; |
|
424 |
||
425 |
/** Switch: should we store the ending positions? |
|
426 |
*/ |
|
427 |
public boolean genEndPos; |
|
428 |
||
429 |
/** Switch: should we debug ignored exceptions |
|
430 |
*/ |
|
431 |
protected boolean devVerbose; |
|
432 |
||
433 |
/** Switch: should we (annotation) process packages as well |
|
434 |
*/ |
|
435 |
protected boolean processPcks; |
|
436 |
||
437 |
/** Switch: is annotation processing requested explitly via |
|
438 |
* CompilationTask.setProcessors? |
|
439 |
*/ |
|
440 |
protected boolean explicitAnnotationProcessingRequested = false; |
|
441 |
||
442 |
/** |
|
443 |
* The policy for the order in which to perform the compilation |
|
444 |
*/ |
|
445 |
protected CompilePolicy compilePolicy; |
|
446 |
||
447 |
/** |
|
448 |
* The policy for what to do with implicitly read source files |
|
449 |
*/ |
|
450 |
protected ImplicitSourcePolicy implicitSourcePolicy; |
|
451 |
||
452 |
/** |
|
453 |
* Report activity related to compilePolicy |
|
454 |
*/ |
|
455 |
public boolean verboseCompilePolicy; |
|
456 |
||
457 |
/** A queue of all as yet unattributed classes. |
|
458 |
*/ |
|
459 |
public Todo todo; |
|
460 |
||
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
461 |
protected enum CompileState { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
462 |
TODO(0), |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
463 |
ATTR(1), |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
464 |
FLOW(2); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
465 |
CompileState(int value) { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
466 |
this.value = value; |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
467 |
} |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
468 |
boolean isDone(CompileState other) { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
469 |
return value >= other.value; |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
470 |
} |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
471 |
private int value; |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
472 |
}; |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
473 |
protected class CompileStates extends HashMap<Env<AttrContext>,CompileState> { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
474 |
boolean isDone(Env<AttrContext> env, CompileState cs) { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
475 |
CompileState ecs = get(env); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
476 |
return ecs != null && ecs.isDone(cs); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
477 |
} |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
478 |
} |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
479 |
private CompileStates compileStates = new CompileStates(); |
10 | 480 |
|
481 |
/** The set of currently compiled inputfiles, needed to ensure |
|
482 |
* we don't accidentally overwrite an input file when -s is set. |
|
483 |
* initialized by `compile'. |
|
484 |
*/ |
|
485 |
protected Set<JavaFileObject> inputFiles = new HashSet<JavaFileObject>(); |
|
486 |
||
487 |
/** The number of errors reported so far. |
|
488 |
*/ |
|
489 |
public int errorCount() { |
|
490 |
if (delegateCompiler != null && delegateCompiler != this) |
|
491 |
return delegateCompiler.errorCount(); |
|
492 |
else |
|
493 |
return log.nerrors; |
|
494 |
} |
|
495 |
||
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
496 |
protected final <T> Queue<T> stopIfError(Queue<T> queue) { |
10 | 497 |
if (errorCount() == 0) |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
498 |
return queue; |
10 | 499 |
else |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
500 |
return ListBuffer.lb(); |
10 | 501 |
} |
502 |
||
503 |
protected final <T> List<T> stopIfError(List<T> list) { |
|
504 |
if (errorCount() == 0) |
|
505 |
return list; |
|
506 |
else |
|
507 |
return List.nil(); |
|
508 |
} |
|
509 |
||
510 |
/** The number of warnings reported so far. |
|
511 |
*/ |
|
512 |
public int warningCount() { |
|
513 |
if (delegateCompiler != null && delegateCompiler != this) |
|
514 |
return delegateCompiler.warningCount(); |
|
515 |
else |
|
516 |
return log.nwarnings; |
|
517 |
} |
|
518 |
||
519 |
/** Whether or not any parse errors have occurred. |
|
520 |
*/ |
|
521 |
public boolean parseErrors() { |
|
522 |
return parseErrors; |
|
523 |
} |
|
524 |
||
525 |
/** Try to open input stream with given name. |
|
526 |
* Report an error if this fails. |
|
527 |
* @param filename The file name of the input stream to be opened. |
|
528 |
*/ |
|
529 |
public CharSequence readSource(JavaFileObject filename) { |
|
530 |
try { |
|
531 |
inputFiles.add(filename); |
|
532 |
return filename.getCharContent(false); |
|
533 |
} catch (IOException e) { |
|
534 |
log.error("error.reading.file", filename, e.getLocalizedMessage()); |
|
535 |
return null; |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
/** Parse contents of input stream. |
|
540 |
* @param filename The name of the file from which input stream comes. |
|
541 |
* @param input The input stream to be parsed. |
|
542 |
*/ |
|
543 |
protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { |
|
544 |
long msec = now(); |
|
545 |
JCCompilationUnit tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), |
|
546 |
null, List.<JCTree>nil()); |
|
547 |
if (content != null) { |
|
548 |
if (verbose) { |
|
549 |
printVerbose("parsing.started", filename); |
|
550 |
} |
|
551 |
if (taskListener != null) { |
|
552 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename); |
|
553 |
taskListener.started(e); |
|
554 |
} |
|
555 |
int initialErrorCount = log.nerrors; |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
1046
diff
changeset
|
556 |
Parser parser = parserFactory.newParser(content, keepComments(), genEndPos, lineDebugInfo); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
1046
diff
changeset
|
557 |
tree = parser.parseCompilationUnit(); |
10 | 558 |
parseErrors |= (log.nerrors > initialErrorCount); |
559 |
if (verbose) { |
|
560 |
printVerbose("parsing.done", Long.toString(elapsed(msec))); |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
tree.sourcefile = filename; |
|
565 |
||
566 |
if (content != null && taskListener != null) { |
|
567 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree); |
|
568 |
taskListener.finished(e); |
|
569 |
} |
|
570 |
||
571 |
return tree; |
|
572 |
} |
|
573 |
// where |
|
574 |
public boolean keepComments = false; |
|
575 |
protected boolean keepComments() { |
|
576 |
return keepComments || sourceOutput || stubOutput; |
|
577 |
} |
|
578 |
||
579 |
||
580 |
/** Parse contents of file. |
|
581 |
* @param filename The name of the file to be parsed. |
|
582 |
*/ |
|
583 |
@Deprecated |
|
584 |
public JCTree.JCCompilationUnit parse(String filename) throws IOException { |
|
585 |
JavacFileManager fm = (JavacFileManager)fileManager; |
|
586 |
return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next()); |
|
587 |
} |
|
588 |
||
589 |
/** Parse contents of file. |
|
590 |
* @param filename The name of the file to be parsed. |
|
591 |
*/ |
|
592 |
public JCTree.JCCompilationUnit parse(JavaFileObject filename) { |
|
593 |
JavaFileObject prev = log.useSource(filename); |
|
594 |
try { |
|
595 |
JCTree.JCCompilationUnit t = parse(filename, readSource(filename)); |
|
596 |
if (t.endPositions != null) |
|
597 |
log.setEndPosTable(filename, t.endPositions); |
|
598 |
return t; |
|
599 |
} finally { |
|
600 |
log.useSource(prev); |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
/** Resolve an identifier. |
|
605 |
* @param name The identifier to resolve |
|
606 |
*/ |
|
607 |
public Symbol resolveIdent(String name) { |
|
608 |
if (name.equals("")) |
|
609 |
return syms.errSymbol; |
|
610 |
JavaFileObject prev = log.useSource(null); |
|
611 |
try { |
|
612 |
JCExpression tree = null; |
|
613 |
for (String s : name.split("\\.", -1)) { |
|
614 |
if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords |
|
615 |
return syms.errSymbol; |
|
616 |
tree = (tree == null) ? make.Ident(names.fromString(s)) |
|
617 |
: make.Select(tree, names.fromString(s)); |
|
618 |
} |
|
619 |
JCCompilationUnit toplevel = |
|
620 |
make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil()); |
|
621 |
toplevel.packge = syms.unnamedPackage; |
|
622 |
return attr.attribIdent(tree, toplevel); |
|
623 |
} finally { |
|
624 |
log.useSource(prev); |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
/** Emit plain Java source for a class. |
|
629 |
* @param env The attribution environment of the outermost class |
|
630 |
* containing this class. |
|
631 |
* @param cdef The class definition to be printed. |
|
632 |
*/ |
|
633 |
JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { |
|
634 |
JavaFileObject outFile |
|
635 |
= fileManager.getJavaFileForOutput(CLASS_OUTPUT, |
|
636 |
cdef.sym.flatname.toString(), |
|
637 |
JavaFileObject.Kind.SOURCE, |
|
638 |
null); |
|
639 |
if (inputFiles.contains(outFile)) { |
|
640 |
log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile); |
|
641 |
return null; |
|
642 |
} else { |
|
643 |
BufferedWriter out = new BufferedWriter(outFile.openWriter()); |
|
644 |
try { |
|
645 |
new Pretty(out, true).printUnit(env.toplevel, cdef); |
|
646 |
if (verbose) |
|
647 |
printVerbose("wrote.file", outFile); |
|
648 |
} finally { |
|
649 |
out.close(); |
|
650 |
} |
|
651 |
return outFile; |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
/** Generate code and emit a class file for a given class |
|
656 |
* @param env The attribution environment of the outermost class |
|
657 |
* containing this class. |
|
658 |
* @param cdef The class definition from which code is generated. |
|
659 |
*/ |
|
660 |
JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException { |
|
661 |
try { |
|
662 |
if (gen.genClass(env, cdef)) |
|
663 |
return writer.writeClass(cdef.sym); |
|
664 |
} catch (ClassWriter.PoolOverflow ex) { |
|
665 |
log.error(cdef.pos(), "limit.pool"); |
|
666 |
} catch (ClassWriter.StringOverflow ex) { |
|
667 |
log.error(cdef.pos(), "limit.string.overflow", |
|
668 |
ex.value.substring(0, 20)); |
|
669 |
} catch (CompletionFailure ex) { |
|
670 |
chk.completionError(cdef.pos(), ex); |
|
671 |
} |
|
672 |
return null; |
|
673 |
} |
|
674 |
||
675 |
/** Complete compiling a source file that has been accessed |
|
676 |
* by the class file reader. |
|
677 |
* @param c The class the source file of which needs to be compiled. |
|
678 |
* @param filename The name of the source file. |
|
679 |
* @param f An input stream that reads the source file. |
|
680 |
*/ |
|
681 |
public void complete(ClassSymbol c) throws CompletionFailure { |
|
682 |
// System.err.println("completing " + c);//DEBUG |
|
683 |
if (completionFailureName == c.fullname) { |
|
684 |
throw new CompletionFailure(c, "user-selected completion failure by class name"); |
|
685 |
} |
|
686 |
JCCompilationUnit tree; |
|
687 |
JavaFileObject filename = c.classfile; |
|
688 |
JavaFileObject prev = log.useSource(filename); |
|
689 |
||
690 |
try { |
|
691 |
tree = parse(filename, filename.getCharContent(false)); |
|
692 |
} catch (IOException e) { |
|
693 |
log.error("error.reading.file", filename, e); |
|
694 |
tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil()); |
|
695 |
} finally { |
|
696 |
log.useSource(prev); |
|
697 |
} |
|
698 |
||
699 |
if (taskListener != null) { |
|
700 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); |
|
701 |
taskListener.started(e); |
|
702 |
} |
|
703 |
||
704 |
enter.complete(List.of(tree), c); |
|
705 |
||
706 |
if (taskListener != null) { |
|
707 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); |
|
708 |
taskListener.finished(e); |
|
709 |
} |
|
710 |
||
711 |
if (enter.getEnv(c) == null) { |
|
712 |
boolean isPkgInfo = |
|
713 |
tree.sourcefile.isNameCompatible("package-info", |
|
714 |
JavaFileObject.Kind.SOURCE); |
|
715 |
if (isPkgInfo) { |
|
716 |
if (enter.getEnv(tree.packge) == null) { |
|
169 | 717 |
JCDiagnostic diag = |
718 |
diagFactory.fragment("file.does.not.contain.package", |
|
10 | 719 |
c.location()); |
169 | 720 |
throw reader.new BadClassFile(c, filename, diag); |
10 | 721 |
} |
722 |
} else { |
|
169 | 723 |
JCDiagnostic diag = |
724 |
diagFactory.fragment("file.doesnt.contain.class", |
|
725 |
c.getQualifiedName()); |
|
726 |
throw reader.new BadClassFile(c, filename, diag); |
|
10 | 727 |
} |
728 |
} |
|
729 |
||
730 |
implicitSourceFilesRead = true; |
|
731 |
} |
|
732 |
||
733 |
/** Track when the JavaCompiler has been used to compile something. */ |
|
734 |
private boolean hasBeenUsed = false; |
|
735 |
private long start_msec = 0; |
|
736 |
public long elapsed_msec = 0; |
|
737 |
||
738 |
/** Track whether any errors occurred while parsing source text. */ |
|
739 |
private boolean parseErrors = false; |
|
740 |
||
741 |
public void compile(List<JavaFileObject> sourceFileObject) |
|
742 |
throws Throwable { |
|
743 |
compile(sourceFileObject, List.<String>nil(), null); |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* Main method: compile a list of files, return all compiled classes |
|
748 |
* |
|
749 |
* @param sourceFileObjects file objects to be compiled |
|
750 |
* @param classnames class names to process for annotations |
|
751 |
* @param processors user provided annotation processors to bypass |
|
752 |
* discovery, {@code null} means that no processors were provided |
|
753 |
*/ |
|
754 |
public void compile(List<JavaFileObject> sourceFileObjects, |
|
755 |
List<String> classnames, |
|
756 |
Iterable<? extends Processor> processors) |
|
757 |
throws IOException // TODO: temp, from JavacProcessingEnvironment |
|
758 |
{ |
|
759 |
if (processors != null && processors.iterator().hasNext()) |
|
760 |
explicitAnnotationProcessingRequested = true; |
|
761 |
// as a JavaCompiler can only be used once, throw an exception if |
|
762 |
// it has been used before. |
|
763 |
if (hasBeenUsed) |
|
764 |
throw new AssertionError("attempt to reuse JavaCompiler"); |
|
765 |
hasBeenUsed = true; |
|
766 |
||
767 |
start_msec = now(); |
|
768 |
try { |
|
769 |
initProcessAnnotations(processors); |
|
770 |
||
771 |
// These method calls must be chained to avoid memory leaks |
|
772 |
delegateCompiler = processAnnotations(enterTrees(stopIfError(parseFiles(sourceFileObjects))), |
|
773 |
classnames); |
|
774 |
||
775 |
delegateCompiler.compile2(); |
|
776 |
delegateCompiler.close(); |
|
777 |
elapsed_msec = delegateCompiler.elapsed_msec; |
|
778 |
} catch (Abort ex) { |
|
779 |
if (devVerbose) |
|
780 |
ex.printStackTrace(); |
|
781 |
} |
|
782 |
} |
|
783 |
||
784 |
/** |
|
785 |
* The phases following annotation processing: attribution, |
|
786 |
* desugar, and finally code generation. |
|
787 |
*/ |
|
788 |
private void compile2() { |
|
789 |
try { |
|
790 |
switch (compilePolicy) { |
|
791 |
case ATTR_ONLY: |
|
792 |
attribute(todo); |
|
793 |
break; |
|
794 |
||
795 |
case CHECK_ONLY: |
|
796 |
flow(attribute(todo)); |
|
797 |
break; |
|
798 |
||
799 |
case SIMPLE: |
|
800 |
generate(desugar(flow(attribute(todo)))); |
|
801 |
break; |
|
802 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
803 |
case BY_FILE: { |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
804 |
Queue<Queue<Env<AttrContext>>> q = todo.groupByFile(); |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
805 |
while (!q.isEmpty() && errorCount() == 0) { |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
806 |
generate(desugar(flow(attribute(q.remove())))); |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
807 |
} |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
808 |
} |
10 | 809 |
break; |
810 |
||
811 |
case BY_TODO: |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
812 |
while (!todo.isEmpty()) |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
813 |
generate(desugar(flow(attribute(todo.remove())))); |
10 | 814 |
break; |
815 |
||
816 |
default: |
|
817 |
assert false: "unknown compile policy"; |
|
818 |
} |
|
819 |
} catch (Abort ex) { |
|
820 |
if (devVerbose) |
|
821 |
ex.printStackTrace(); |
|
822 |
} |
|
823 |
||
824 |
if (verbose) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
825 |
elapsed_msec = elapsed(start_msec); |
10 | 826 |
printVerbose("total", Long.toString(elapsed_msec)); |
827 |
} |
|
828 |
||
829 |
reportDeferredDiagnostics(); |
|
830 |
||
831 |
if (!log.hasDiagnosticListener()) { |
|
832 |
printCount("error", errorCount()); |
|
833 |
printCount("warn", warningCount()); |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
private List<JCClassDecl> rootClasses; |
|
838 |
||
839 |
/** |
|
840 |
* Parses a list of files. |
|
841 |
*/ |
|
842 |
public List<JCCompilationUnit> parseFiles(List<JavaFileObject> fileObjects) throws IOException { |
|
843 |
if (errorCount() > 0) |
|
844 |
return List.nil(); |
|
845 |
||
846 |
//parse all files |
|
847 |
ListBuffer<JCCompilationUnit> trees = lb(); |
|
848 |
for (JavaFileObject fileObject : fileObjects) |
|
849 |
trees.append(parse(fileObject)); |
|
850 |
return trees.toList(); |
|
851 |
} |
|
852 |
||
853 |
/** |
|
854 |
* Enter the symbols found in a list of parse trees. |
|
855 |
* As a side-effect, this puts elements on the "todo" list. |
|
856 |
* Also stores a list of all top level classes in rootClasses. |
|
857 |
*/ |
|
858 |
public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) { |
|
859 |
//enter symbols for all files |
|
860 |
if (taskListener != null) { |
|
861 |
for (JCCompilationUnit unit: roots) { |
|
862 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); |
|
863 |
taskListener.started(e); |
|
864 |
} |
|
865 |
} |
|
866 |
||
867 |
enter.main(roots); |
|
868 |
||
869 |
if (taskListener != null) { |
|
870 |
for (JCCompilationUnit unit: roots) { |
|
871 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); |
|
872 |
taskListener.finished(e); |
|
873 |
} |
|
874 |
} |
|
875 |
||
876 |
//If generating source, remember the classes declared in |
|
877 |
//the original compilation units listed on the command line. |
|
878 |
if (sourceOutput || stubOutput) { |
|
879 |
ListBuffer<JCClassDecl> cdefs = lb(); |
|
880 |
for (JCCompilationUnit unit : roots) { |
|
881 |
for (List<JCTree> defs = unit.defs; |
|
882 |
defs.nonEmpty(); |
|
883 |
defs = defs.tail) { |
|
884 |
if (defs.head instanceof JCClassDecl) |
|
885 |
cdefs.append((JCClassDecl)defs.head); |
|
886 |
} |
|
887 |
} |
|
888 |
rootClasses = cdefs.toList(); |
|
889 |
} |
|
890 |
return roots; |
|
891 |
} |
|
892 |
||
893 |
/** |
|
894 |
* Set to true to enable skeleton annotation processing code. |
|
895 |
* Currently, we assume this variable will be replaced more |
|
896 |
* advanced logic to figure out if annotation processing is |
|
897 |
* needed. |
|
898 |
*/ |
|
899 |
boolean processAnnotations = false; |
|
900 |
||
901 |
/** |
|
902 |
* Object to handle annotation processing. |
|
903 |
*/ |
|
904 |
JavacProcessingEnvironment procEnvImpl = null; |
|
905 |
||
906 |
/** |
|
907 |
* Check if we should process annotations. |
|
908 |
* If so, and if no scanner is yet registered, then set up the DocCommentScanner |
|
909 |
* to catch doc comments, and set keepComments so the parser records them in |
|
910 |
* the compilation unit. |
|
911 |
* |
|
912 |
* @param processors user provided annotation processors to bypass |
|
913 |
* discovery, {@code null} means that no processors were provided |
|
914 |
*/ |
|
915 |
public void initProcessAnnotations(Iterable<? extends Processor> processors) { |
|
916 |
// Process annotations if processing is not disabled and there |
|
917 |
// is at least one Processor available. |
|
918 |
Options options = Options.instance(context); |
|
919 |
if (options.get("-proc:none") != null) { |
|
920 |
processAnnotations = false; |
|
921 |
} else if (procEnvImpl == null) { |
|
922 |
procEnvImpl = new JavacProcessingEnvironment(context, processors); |
|
923 |
processAnnotations = procEnvImpl.atLeastOneProcessor(); |
|
924 |
||
925 |
if (processAnnotations) { |
|
926 |
if (context.get(Scanner.Factory.scannerFactoryKey) == null) |
|
927 |
DocCommentScanner.Factory.preRegister(context); |
|
928 |
options.put("save-parameter-names", "save-parameter-names"); |
|
929 |
reader.saveParameterNames = true; |
|
930 |
keepComments = true; |
|
931 |
if (taskListener != null) |
|
932 |
taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); |
|
933 |
||
934 |
||
935 |
} else { // free resources |
|
936 |
procEnvImpl.close(); |
|
937 |
} |
|
938 |
} |
|
939 |
} |
|
940 |
||
941 |
// TODO: called by JavacTaskImpl |
|
942 |
public JavaCompiler processAnnotations(List<JCCompilationUnit> roots) throws IOException { |
|
943 |
return processAnnotations(roots, List.<String>nil()); |
|
944 |
} |
|
945 |
||
946 |
/** |
|
947 |
* Process any anotations found in the specifed compilation units. |
|
948 |
* @param roots a list of compilation units |
|
949 |
* @return an instance of the compiler in which to complete the compilation |
|
950 |
*/ |
|
951 |
public JavaCompiler processAnnotations(List<JCCompilationUnit> roots, |
|
952 |
List<String> classnames) |
|
953 |
throws IOException { // TODO: see TEMP note in JavacProcessingEnvironment |
|
954 |
if (errorCount() != 0) { |
|
955 |
// Errors were encountered. If todo is empty, then the |
|
956 |
// encountered errors were parse errors. Otherwise, the |
|
957 |
// errors were found during the enter phase which should |
|
958 |
// be ignored when processing annotations. |
|
959 |
||
960 |
if (todo.isEmpty()) |
|
961 |
return this; |
|
962 |
} |
|
963 |
||
964 |
// ASSERT: processAnnotations and procEnvImpl should have been set up by |
|
965 |
// by initProcessAnnotations |
|
966 |
||
967 |
// NOTE: The !classnames.isEmpty() checks should be refactored to Main. |
|
968 |
||
969 |
if (!processAnnotations) { |
|
970 |
// If there are no annotation processors present, and |
|
971 |
// annotation processing is to occur with compilation, |
|
972 |
// emit a warning. |
|
973 |
Options options = Options.instance(context); |
|
974 |
if (options.get("-proc:only") != null) { |
|
975 |
log.warning("proc.proc-only.requested.no.procs"); |
|
976 |
todo.clear(); |
|
977 |
} |
|
978 |
// If not processing annotations, classnames must be empty |
|
979 |
if (!classnames.isEmpty()) { |
|
980 |
log.error("proc.no.explicit.annotation.processing.requested", |
|
981 |
classnames); |
|
982 |
} |
|
983 |
return this; // continue regular compilation |
|
984 |
} |
|
985 |
||
986 |
try { |
|
987 |
List<ClassSymbol> classSymbols = List.nil(); |
|
988 |
List<PackageSymbol> pckSymbols = List.nil(); |
|
989 |
if (!classnames.isEmpty()) { |
|
990 |
// Check for explicit request for annotation |
|
991 |
// processing |
|
992 |
if (!explicitAnnotationProcessingRequested()) { |
|
993 |
log.error("proc.no.explicit.annotation.processing.requested", |
|
994 |
classnames); |
|
995 |
return this; // TODO: Will this halt compilation? |
|
996 |
} else { |
|
997 |
boolean errors = false; |
|
998 |
for (String nameStr : classnames) { |
|
999 |
Symbol sym = resolveIdent(nameStr); |
|
1000 |
if (sym == null || (sym.kind == Kinds.PCK && !processPcks)) { |
|
1001 |
log.error("proc.cant.find.class", nameStr); |
|
1002 |
errors = true; |
|
1003 |
continue; |
|
1004 |
} |
|
1005 |
try { |
|
1006 |
if (sym.kind == Kinds.PCK) |
|
1007 |
sym.complete(); |
|
1008 |
if (sym.exists()) { |
|
1009 |
Name name = names.fromString(nameStr); |
|
1010 |
if (sym.kind == Kinds.PCK) |
|
1011 |
pckSymbols = pckSymbols.prepend((PackageSymbol)sym); |
|
1012 |
else |
|
1013 |
classSymbols = classSymbols.prepend((ClassSymbol)sym); |
|
1014 |
continue; |
|
1015 |
} |
|
1016 |
assert sym.kind == Kinds.PCK; |
|
1017 |
log.warning("proc.package.does.not.exist", nameStr); |
|
1018 |
pckSymbols = pckSymbols.prepend((PackageSymbol)sym); |
|
1019 |
} catch (CompletionFailure e) { |
|
1020 |
log.error("proc.cant.find.class", nameStr); |
|
1021 |
errors = true; |
|
1022 |
continue; |
|
1023 |
} |
|
1024 |
} |
|
1025 |
if (errors) |
|
1026 |
return this; |
|
1027 |
} |
|
1028 |
} |
|
1029 |
JavaCompiler c = procEnvImpl.doProcessing(context, roots, classSymbols, pckSymbols); |
|
1030 |
if (c != this) |
|
1031 |
annotationProcessingOccurred = c.annotationProcessingOccurred = true; |
|
1032 |
return c; |
|
1033 |
} catch (CompletionFailure ex) { |
|
169 | 1034 |
log.error("cant.access", ex.sym, ex.getDetailValue()); |
10 | 1035 |
return this; |
1036 |
||
1037 |
} |
|
1038 |
} |
|
1039 |
||
1040 |
boolean explicitAnnotationProcessingRequested() { |
|
1041 |
Options options = Options.instance(context); |
|
1042 |
return |
|
1043 |
explicitAnnotationProcessingRequested || |
|
1044 |
options.get("-processor") != null || |
|
1045 |
options.get("-processorpath") != null || |
|
1046 |
options.get("-proc:only") != null || |
|
1047 |
options.get("-Xprint") != null; |
|
1048 |
} |
|
1049 |
||
1050 |
/** |
|
1051 |
* Attribute a list of parse trees, such as found on the "todo" list. |
|
1052 |
* Note that attributing classes may cause additional files to be |
|
1053 |
* parsed and entered via the SourceCompleter. |
|
1054 |
* Attribution of the entries in the list does not stop if any errors occur. |
|
1055 |
* @returns a list of environments for attributd classes. |
|
1056 |
*/ |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1057 |
public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) { |
10 | 1058 |
ListBuffer<Env<AttrContext>> results = lb(); |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1059 |
while (!envs.isEmpty()) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1060 |
results.append(attribute(envs.remove())); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1061 |
return results; |
10 | 1062 |
} |
1063 |
||
1064 |
/** |
|
1065 |
* Attribute a parse tree. |
|
1066 |
* @returns the attributed parse tree |
|
1067 |
*/ |
|
1068 |
public Env<AttrContext> attribute(Env<AttrContext> env) { |
|
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1069 |
if (compileStates.isDone(env, CompileState.ATTR)) |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1070 |
return env; |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1071 |
|
10 | 1072 |
if (verboseCompilePolicy) |
1073 |
log.printLines(log.noticeWriter, "[attribute " + env.enclClass.sym + "]"); |
|
1074 |
if (verbose) |
|
1075 |
printVerbose("checking.attribution", env.enclClass.sym); |
|
1076 |
||
1077 |
if (taskListener != null) { |
|
1078 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); |
|
1079 |
taskListener.started(e); |
|
1080 |
} |
|
1081 |
||
1082 |
JavaFileObject prev = log.useSource( |
|
1083 |
env.enclClass.sym.sourcefile != null ? |
|
1084 |
env.enclClass.sym.sourcefile : |
|
1085 |
env.toplevel.sourcefile); |
|
1086 |
try { |
|
1087 |
attr.attribClass(env.tree.pos(), env.enclClass.sym); |
|
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1088 |
compileStates.put(env, CompileState.ATTR); |
10 | 1089 |
} |
1090 |
finally { |
|
1091 |
log.useSource(prev); |
|
1092 |
} |
|
1093 |
||
1094 |
return env; |
|
1095 |
} |
|
1096 |
||
1097 |
/** |
|
1098 |
* Perform dataflow checks on attributed parse trees. |
|
1099 |
* These include checks for definite assignment and unreachable statements. |
|
1100 |
* If any errors occur, an empty list will be returned. |
|
1101 |
* @returns the list of attributed parse trees |
|
1102 |
*/ |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1103 |
public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) { |
10 | 1104 |
ListBuffer<Env<AttrContext>> results = lb(); |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1105 |
for (Env<AttrContext> env: envs) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1106 |
flow(env, results); |
10 | 1107 |
} |
1108 |
return stopIfError(results); |
|
1109 |
} |
|
1110 |
||
1111 |
/** |
|
1112 |
* Perform dataflow checks on an attributed parse tree. |
|
1113 |
*/ |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1114 |
public Queue<Env<AttrContext>> flow(Env<AttrContext> env) { |
10 | 1115 |
ListBuffer<Env<AttrContext>> results = lb(); |
1116 |
flow(env, results); |
|
1117 |
return stopIfError(results); |
|
1118 |
} |
|
1119 |
||
1120 |
/** |
|
1121 |
* Perform dataflow checks on an attributed parse tree. |
|
1122 |
*/ |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
1123 |
protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) { |
10 | 1124 |
try { |
1125 |
if (errorCount() > 0) |
|
1126 |
return; |
|
1127 |
||
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1128 |
if (relax || compileStates.isDone(env, CompileState.FLOW)) { |
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
1129 |
results.add(env); |
10 | 1130 |
return; |
1131 |
} |
|
1132 |
||
1133 |
if (verboseCompilePolicy) |
|
1134 |
log.printLines(log.noticeWriter, "[flow " + env.enclClass.sym + "]"); |
|
1135 |
JavaFileObject prev = log.useSource( |
|
1136 |
env.enclClass.sym.sourcefile != null ? |
|
1137 |
env.enclClass.sym.sourcefile : |
|
1138 |
env.toplevel.sourcefile); |
|
1139 |
try { |
|
1140 |
make.at(Position.FIRSTPOS); |
|
1141 |
TreeMaker localMake = make.forToplevel(env.toplevel); |
|
1142 |
flow.analyzeTree(env.tree, localMake); |
|
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1143 |
compileStates.put(env, CompileState.FLOW); |
10 | 1144 |
|
1145 |
if (errorCount() > 0) |
|
1146 |
return; |
|
1147 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
1148 |
results.add(env); |
10 | 1149 |
} |
1150 |
finally { |
|
1151 |
log.useSource(prev); |
|
1152 |
} |
|
1153 |
} |
|
1154 |
finally { |
|
1155 |
if (taskListener != null) { |
|
1156 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); |
|
1157 |
taskListener.finished(e); |
|
1158 |
} |
|
1159 |
} |
|
1160 |
} |
|
1161 |
||
1162 |
/** |
|
1163 |
* Prepare attributed parse trees, in conjunction with their attribution contexts, |
|
1164 |
* for source or code generation. |
|
1165 |
* If any errors occur, an empty list will be returned. |
|
1166 |
* @returns a list containing the classes to be generated |
|
1167 |
*/ |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1168 |
public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) { |
10 | 1169 |
ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = lb(); |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1170 |
for (Env<AttrContext> env: envs) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1171 |
desugar(env, results); |
10 | 1172 |
return stopIfError(results); |
1173 |
} |
|
1174 |
||
1175 |
/** |
|
1176 |
* Prepare attributed parse trees, in conjunction with their attribution contexts, |
|
1177 |
* for source or code generation. If the file was not listed on the command line, |
|
1178 |
* the current implicitSourcePolicy is taken into account. |
|
1179 |
* The preparation stops as soon as an error is found. |
|
1180 |
*/ |
|
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1181 |
protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) { |
10 | 1182 |
if (errorCount() > 0) |
1183 |
return; |
|
1184 |
||
1185 |
if (implicitSourcePolicy == ImplicitSourcePolicy.NONE |
|
1186 |
&& !inputFiles.contains(env.toplevel.sourcefile)) { |
|
1187 |
return; |
|
1188 |
} |
|
1189 |
||
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1190 |
/** |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1191 |
* As erasure (TransTypes) destroys information needed in flow analysis, |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1192 |
* including information in supertypes, we need to ensure that supertypes |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1193 |
* are processed through attribute and flow before subtypes are translated. |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1194 |
*/ |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1195 |
class ScanNested extends TreeScanner { |
1046
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1196 |
Set<Env<AttrContext>> dependencies = new LinkedHashSet<Env<AttrContext>>(); |
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1197 |
public void visitClassDef(JCClassDecl node) { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1198 |
Type st = types.supertype(node.sym.type); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1199 |
if (st.tag == TypeTags.CLASS) { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1200 |
ClassSymbol c = st.tsym.outermostClass(); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1201 |
Env<AttrContext> stEnv = enter.getEnv(c); |
1046
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1202 |
if (stEnv != null && env != stEnv) { |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1203 |
if (dependencies.add(stEnv)) |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1204 |
scan(stEnv.tree); |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1205 |
} |
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1206 |
} |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1207 |
super.visitClassDef(node); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1208 |
} |
10 | 1209 |
} |
936
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1210 |
ScanNested scanner = new ScanNested(); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1211 |
scanner.scan(env.tree); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1212 |
for (Env<AttrContext> dep: scanner.dependencies) { |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1213 |
if (!compileStates.isDone(dep, CompileState.FLOW)) |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1214 |
flow(attribute(dep)); |
1d395a623f16
6726015: JavaCompiler: replace desugarLater by compileStates
jjg
parents:
864
diff
changeset
|
1215 |
} |
10 | 1216 |
|
1046
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1217 |
//We need to check for error another time as more classes might |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1218 |
//have been attributed and analyzed at this stage |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1219 |
if (errorCount() > 0) |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1220 |
return; |
e395ce284cbf
6734819: Javac performs flows analysis on already translated classes
mcimadamore
parents:
936
diff
changeset
|
1221 |
|
10 | 1222 |
if (verboseCompilePolicy) |
1223 |
log.printLines(log.noticeWriter, "[desugar " + env.enclClass.sym + "]"); |
|
1224 |
||
1225 |
JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? |
|
1226 |
env.enclClass.sym.sourcefile : |
|
1227 |
env.toplevel.sourcefile); |
|
1228 |
try { |
|
1229 |
//save tree prior to rewriting |
|
1230 |
JCTree untranslated = env.tree; |
|
1231 |
||
1232 |
make.at(Position.FIRSTPOS); |
|
1233 |
TreeMaker localMake = make.forToplevel(env.toplevel); |
|
1234 |
||
1235 |
if (env.tree instanceof JCCompilationUnit) { |
|
1236 |
if (!(stubOutput || sourceOutput || printFlat)) { |
|
1237 |
List<JCTree> pdef = lower.translateTopLevelClass(env, env.tree, localMake); |
|
1238 |
if (pdef.head != null) { |
|
1239 |
assert pdef.tail.isEmpty(); |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1240 |
results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, (JCClassDecl)pdef.head)); |
10 | 1241 |
} |
1242 |
} |
|
1243 |
return; |
|
1244 |
} |
|
1245 |
||
1246 |
if (stubOutput) { |
|
1247 |
//emit stub Java source file, only for compilation |
|
1248 |
//units enumerated explicitly on the command line |
|
1249 |
JCClassDecl cdef = (JCClassDecl)env.tree; |
|
1250 |
if (untranslated instanceof JCClassDecl && |
|
1251 |
rootClasses.contains((JCClassDecl)untranslated) && |
|
1252 |
((cdef.mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
|
1253 |
cdef.sym.packge().getQualifiedName() == names.java_lang)) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1254 |
results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, removeMethodBodies(cdef))); |
10 | 1255 |
} |
1256 |
return; |
|
1257 |
} |
|
1258 |
||
1259 |
env.tree = transTypes.translateTopLevelClass(env.tree, localMake); |
|
1260 |
||
1261 |
if (errorCount() != 0) |
|
1262 |
return; |
|
1263 |
||
1264 |
if (sourceOutput) { |
|
1265 |
//emit standard Java source file, only for compilation |
|
1266 |
//units enumerated explicitly on the command line |
|
1267 |
JCClassDecl cdef = (JCClassDecl)env.tree; |
|
1268 |
if (untranslated instanceof JCClassDecl && |
|
1269 |
rootClasses.contains((JCClassDecl)untranslated)) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1270 |
results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef)); |
10 | 1271 |
} |
1272 |
return; |
|
1273 |
} |
|
1274 |
||
1275 |
//translate out inner classes |
|
1276 |
List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake); |
|
1277 |
||
1278 |
if (errorCount() != 0) |
|
1279 |
return; |
|
1280 |
||
1281 |
//generate code for each class |
|
1282 |
for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) { |
|
1283 |
JCClassDecl cdef = (JCClassDecl)l.head; |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1284 |
results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef)); |
10 | 1285 |
} |
1286 |
} |
|
1287 |
finally { |
|
1288 |
log.useSource(prev); |
|
1289 |
} |
|
1290 |
||
1291 |
} |
|
1292 |
||
1293 |
/** Generates the source or class file for a list of classes. |
|
1294 |
* The decision to generate a source file or a class file is |
|
1295 |
* based upon the compiler's options. |
|
1296 |
* Generation stops if an error occurs while writing files. |
|
1297 |
*/ |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1298 |
public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1299 |
generate(queue, null); |
10 | 1300 |
} |
1301 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
1302 |
public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) { |
10 | 1303 |
boolean usePrintSource = (stubOutput || sourceOutput || printFlat); |
1304 |
||
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1305 |
for (Pair<Env<AttrContext>, JCClassDecl> x: queue) { |
10 | 1306 |
Env<AttrContext> env = x.fst; |
1307 |
JCClassDecl cdef = x.snd; |
|
1308 |
||
1309 |
if (verboseCompilePolicy) { |
|
1310 |
log.printLines(log.noticeWriter, "[generate " |
|
1311 |
+ (usePrintSource ? " source" : "code") |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
1312 |
+ " " + cdef.sym + "]"); |
10 | 1313 |
} |
1314 |
||
1315 |
if (taskListener != null) { |
|
1316 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym); |
|
1317 |
taskListener.started(e); |
|
1318 |
} |
|
1319 |
||
1320 |
JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? |
|
1321 |
env.enclClass.sym.sourcefile : |
|
1322 |
env.toplevel.sourcefile); |
|
1323 |
try { |
|
1324 |
JavaFileObject file; |
|
1325 |
if (usePrintSource) |
|
1326 |
file = printSource(env, cdef); |
|
1327 |
else |
|
1328 |
file = genCode(env, cdef); |
|
1329 |
if (results != null && file != null) |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1260
diff
changeset
|
1330 |
results.add(file); |
10 | 1331 |
} catch (IOException ex) { |
1332 |
log.error(cdef.pos(), "class.cant.write", |
|
1333 |
cdef.sym, ex.getMessage()); |
|
1334 |
return; |
|
1335 |
} finally { |
|
1336 |
log.useSource(prev); |
|
1337 |
} |
|
1338 |
||
1339 |
if (taskListener != null) { |
|
1340 |
TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym); |
|
1341 |
taskListener.finished(e); |
|
1342 |
} |
|
1343 |
} |
|
1344 |
} |
|
1345 |
||
1346 |
// where |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1347 |
Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) { |
10 | 1348 |
// use a LinkedHashMap to preserve the order of the original list as much as possible |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1349 |
Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<JCCompilationUnit, Queue<Env<AttrContext>>>(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1350 |
for (Env<AttrContext> env: envs) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1351 |
Queue<Env<AttrContext>> sublist = map.get(env.toplevel); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1352 |
if (sublist == null) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1353 |
sublist = new ListBuffer<Env<AttrContext>>(); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1354 |
map.put(env.toplevel, sublist); |
10 | 1355 |
} |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
1356 |
sublist.add(env); |
10 | 1357 |
} |
1358 |
return map; |
|
1359 |
} |
|
1360 |
||
1361 |
JCClassDecl removeMethodBodies(JCClassDecl cdef) { |
|
1362 |
final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0; |
|
1363 |
class MethodBodyRemover extends TreeTranslator { |
|
1364 |
public void visitMethodDef(JCMethodDecl tree) { |
|
1365 |
tree.mods.flags &= ~Flags.SYNCHRONIZED; |
|
1366 |
for (JCVariableDecl vd : tree.params) |
|
1367 |
vd.mods.flags &= ~Flags.FINAL; |
|
1368 |
tree.body = null; |
|
1369 |
super.visitMethodDef(tree); |
|
1370 |
} |
|
1371 |
public void visitVarDef(JCVariableDecl tree) { |
|
1372 |
if (tree.init != null && tree.init.type.constValue() == null) |
|
1373 |
tree.init = null; |
|
1374 |
super.visitVarDef(tree); |
|
1375 |
} |
|
1376 |
public void visitClassDef(JCClassDecl tree) { |
|
1377 |
ListBuffer<JCTree> newdefs = lb(); |
|
1378 |
for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) { |
|
1379 |
JCTree t = it.head; |
|
1380 |
switch (t.getTag()) { |
|
1381 |
case JCTree.CLASSDEF: |
|
1382 |
if (isInterface || |
|
1383 |
(((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
|
1384 |
(((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
|
1385 |
newdefs.append(t); |
|
1386 |
break; |
|
1387 |
case JCTree.METHODDEF: |
|
1388 |
if (isInterface || |
|
1389 |
(((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
|
1390 |
((JCMethodDecl) t).sym.name == names.init || |
|
1391 |
(((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
|
1392 |
newdefs.append(t); |
|
1393 |
break; |
|
1394 |
case JCTree.VARDEF: |
|
1395 |
if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
|
1396 |
(((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
|
1397 |
newdefs.append(t); |
|
1398 |
break; |
|
1399 |
default: |
|
1400 |
break; |
|
1401 |
} |
|
1402 |
} |
|
1403 |
tree.defs = newdefs.toList(); |
|
1404 |
super.visitClassDef(tree); |
|
1405 |
} |
|
1406 |
} |
|
1407 |
MethodBodyRemover r = new MethodBodyRemover(); |
|
1408 |
return r.translate(cdef); |
|
1409 |
} |
|
1410 |
||
1411 |
public void reportDeferredDiagnostics() { |
|
1412 |
if (annotationProcessingOccurred |
|
1413 |
&& implicitSourceFilesRead |
|
1414 |
&& implicitSourcePolicy == ImplicitSourcePolicy.UNSET) { |
|
1415 |
if (explicitAnnotationProcessingRequested()) |
|
1416 |
log.warning("proc.use.implicit"); |
|
1417 |
else |
|
1418 |
log.warning("proc.use.proc.or.implicit"); |
|
1419 |
} |
|
1420 |
chk.reportDeferredDiagnostics(); |
|
1421 |
} |
|
1422 |
||
1423 |
/** Close the compiler, flushing the logs |
|
1424 |
*/ |
|
1425 |
public void close() { |
|
1426 |
close(true); |
|
1427 |
} |
|
1428 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1258
diff
changeset
|
1429 |
public void close(boolean disposeNames) { |
10 | 1430 |
rootClasses = null; |
1431 |
reader = null; |
|
1432 |
make = null; |
|
1433 |
writer = null; |
|
1434 |
enter = null; |
|
1435 |
if (todo != null) |
|
1436 |
todo.clear(); |
|
1437 |
todo = null; |
|
1438 |
parserFactory = null; |
|
1439 |
syms = null; |
|
1440 |
source = null; |
|
1441 |
attr = null; |
|
1442 |
chk = null; |
|
1443 |
gen = null; |
|
1444 |
flow = null; |
|
1445 |
transTypes = null; |
|
1446 |
lower = null; |
|
1447 |
annotate = null; |
|
1448 |
types = null; |
|
1449 |
||
1450 |
log.flush(); |
|
1451 |
try { |
|
1452 |
fileManager.flush(); |
|
1453 |
} catch (IOException e) { |
|
1454 |
throw new Abort(e); |
|
1455 |
} finally { |
|
1456 |
if (names != null && disposeNames) |
|
1457 |
names.dispose(); |
|
1458 |
names = null; |
|
1459 |
} |
|
1460 |
} |
|
1461 |
||
1462 |
/** Output for "-verbose" option. |
|
1463 |
* @param key The key to look up the correct internationalized string. |
|
1464 |
* @param arg An argument for substitution into the output string. |
|
1465 |
*/ |
|
1466 |
protected void printVerbose(String key, Object arg) { |
|
1467 |
Log.printLines(log.noticeWriter, log.getLocalizedString("verbose." + key, arg)); |
|
1468 |
} |
|
1469 |
||
1470 |
/** Print numbers of errors and warnings. |
|
1471 |
*/ |
|
1472 |
protected void printCount(String kind, int count) { |
|
1473 |
if (count != 0) { |
|
1474 |
String text; |
|
1475 |
if (count == 1) |
|
1476 |
text = log.getLocalizedString("count." + kind, String.valueOf(count)); |
|
1477 |
else |
|
1478 |
text = log.getLocalizedString("count." + kind + ".plural", String.valueOf(count)); |
|
1479 |
Log.printLines(log.errWriter, text); |
|
1480 |
log.errWriter.flush(); |
|
1481 |
} |
|
1482 |
} |
|
1483 |
||
1484 |
private static long now() { |
|
1485 |
return System.currentTimeMillis(); |
|
1486 |
} |
|
1487 |
||
1488 |
private static long elapsed(long then) { |
|
1489 |
return now() - then; |
|
1490 |
} |
|
1491 |
||
1492 |
public void initRound(JavaCompiler prev) { |
|
1493 |
keepComments = prev.keepComments; |
|
1494 |
start_msec = prev.start_msec; |
|
1495 |
hasBeenUsed = true; |
|
1496 |
} |
|
1497 |
||
1498 |
public static void enableLogging() { |
|
1499 |
Logger logger = Logger.getLogger(com.sun.tools.javac.Main.class.getPackage().getName()); |
|
1500 |
logger.setLevel(Level.ALL); |
|
1501 |
for (Handler h : logger.getParent().getHandlers()) { |
|
1502 |
h.setLevel(Level.ALL); |
|
1503 |
} |
|
1504 |
||
1505 |
} |
|
1506 |
} |