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