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