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