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