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