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