JavaCompiler.java revision 3981:8be741555fa6
1/*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 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.
24 */
25
26package com.sun.tools.javac.main;
27
28import java.io.*;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.HashSet;
33import java.util.LinkedHashMap;
34import java.util.LinkedHashSet;
35import java.util.Map;
36import java.util.MissingResourceException;
37import java.util.Queue;
38import java.util.ResourceBundle;
39import java.util.Set;
40import java.util.function.Function;
41
42import javax.annotation.processing.Processor;
43import javax.lang.model.SourceVersion;
44import javax.lang.model.element.ElementVisitor;
45import javax.tools.DiagnosticListener;
46import javax.tools.JavaFileManager;
47import javax.tools.JavaFileObject;
48import javax.tools.JavaFileObject.Kind;
49import javax.tools.StandardLocation;
50
51import com.sun.source.util.TaskEvent;
52import com.sun.tools.javac.api.MultiTaskListener;
53import com.sun.tools.javac.code.*;
54import com.sun.tools.javac.code.Lint.LintCategory;
55import com.sun.tools.javac.code.Symbol.ClassSymbol;
56import com.sun.tools.javac.code.Symbol.CompletionFailure;
57import com.sun.tools.javac.code.Symbol.PackageSymbol;
58import com.sun.tools.javac.comp.*;
59import com.sun.tools.javac.comp.CompileStates.CompileState;
60import com.sun.tools.javac.file.JavacFileManager;
61import com.sun.tools.javac.jvm.*;
62import com.sun.tools.javac.parser.*;
63import com.sun.tools.javac.platform.PlatformDescription;
64import com.sun.tools.javac.processing.*;
65import com.sun.tools.javac.tree.*;
66import com.sun.tools.javac.tree.JCTree.JCClassDecl;
67import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
68import com.sun.tools.javac.tree.JCTree.JCExpression;
69import com.sun.tools.javac.tree.JCTree.JCLambda;
70import com.sun.tools.javac.tree.JCTree.JCMemberReference;
71import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
72import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
73import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
74import com.sun.tools.javac.tree.JCTree.Tag;
75import com.sun.tools.javac.util.*;
76import com.sun.tools.javac.util.DefinedBy.Api;
77import com.sun.tools.javac.util.JCDiagnostic.Factory;
78import com.sun.tools.javac.util.Log.WriterKind;
79
80import static com.sun.tools.javac.code.Kinds.Kind.*;
81
82import com.sun.tools.javac.code.Symbol.ModuleSymbol;
83import com.sun.tools.javac.resources.CompilerProperties.Errors;
84import com.sun.tools.javac.resources.CompilerProperties.Warnings;
85
86import static com.sun.tools.javac.code.TypeTag.CLASS;
87import static com.sun.tools.javac.main.Option.*;
88import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
89
90import static javax.tools.StandardLocation.CLASS_OUTPUT;
91
92/** This class could be the main entry point for GJC when GJC is used as a
93 *  component in a larger software system. It provides operations to
94 *  construct a new compiler, and to run a new compiler on a set of source
95 *  files.
96 *
97 *  <p><b>This is NOT part of any supported API.
98 *  If you write code that depends on this, you do so at your own risk.
99 *  This code and its internal interfaces are subject to change or
100 *  deletion without notice.</b>
101 */
102public class JavaCompiler {
103    /** The context key for the compiler. */
104    public static final Context.Key<JavaCompiler> compilerKey = new Context.Key<>();
105
106    /** Get the JavaCompiler instance for this context. */
107    public static JavaCompiler instance(Context context) {
108        JavaCompiler instance = context.get(compilerKey);
109        if (instance == null)
110            instance = new JavaCompiler(context);
111        return instance;
112    }
113
114    /** The current version number as a string.
115     */
116    public static String version() {
117        return version("release");  // mm.nn.oo[-milestone]
118    }
119
120    /** The current full version number as a string.
121     */
122    public static String fullVersion() {
123        return version("full"); // mm.mm.oo[-milestone]-build
124    }
125
126    private static final String versionRBName = "com.sun.tools.javac.resources.version";
127    private static ResourceBundle versionRB;
128
129    private static String version(String key) {
130        if (versionRB == null) {
131            try {
132                versionRB = ResourceBundle.getBundle(versionRBName);
133            } catch (MissingResourceException e) {
134                return Log.getLocalizedString("version.not.available");
135            }
136        }
137        try {
138            return versionRB.getString(key);
139        }
140        catch (MissingResourceException e) {
141            return Log.getLocalizedString("version.not.available");
142        }
143    }
144
145    /**
146     * Control how the compiler's latter phases (attr, flow, desugar, generate)
147     * are connected. Each individual file is processed by each phase in turn,
148     * but with different compile policies, you can control the order in which
149     * each class is processed through its next phase.
150     *
151     * <p>Generally speaking, the compiler will "fail fast" in the face of
152     * errors, although not aggressively so. flow, desugar, etc become no-ops
153     * once any errors have occurred. No attempt is currently made to determine
154     * if it might be safe to process a class through its next phase because
155     * it does not depend on any unrelated errors that might have occurred.
156     */
157    protected static enum CompilePolicy {
158        /**
159         * Just attribute the parse trees.
160         */
161        ATTR_ONLY,
162
163        /**
164         * Just attribute and do flow analysis on the parse trees.
165         * This should catch most user errors.
166         */
167        CHECK_ONLY,
168
169        /**
170         * Attribute everything, then do flow analysis for everything,
171         * then desugar everything, and only then generate output.
172         * This means no output will be generated if there are any
173         * errors in any classes.
174         */
175        SIMPLE,
176
177        /**
178         * Groups the classes for each source file together, then process
179         * each group in a manner equivalent to the {@code SIMPLE} policy.
180         * This means no output will be generated if there are any
181         * errors in any of the classes in a source file.
182         */
183        BY_FILE,
184
185        /**
186         * Completely process each entry on the todo list in turn.
187         * -- this is the same for 1.5.
188         * Means output might be generated for some classes in a compilation unit
189         * and not others.
190         */
191        BY_TODO;
192
193        static CompilePolicy decode(String option) {
194            if (option == null)
195                return DEFAULT_COMPILE_POLICY;
196            else if (option.equals("attr"))
197                return ATTR_ONLY;
198            else if (option.equals("check"))
199                return CHECK_ONLY;
200            else if (option.equals("simple"))
201                return SIMPLE;
202            else if (option.equals("byfile"))
203                return BY_FILE;
204            else if (option.equals("bytodo"))
205                return BY_TODO;
206            else
207                return DEFAULT_COMPILE_POLICY;
208        }
209    }
210
211    private static final CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO;
212
213    protected static enum ImplicitSourcePolicy {
214        /** Don't generate or process implicitly read source files. */
215        NONE,
216        /** Generate classes for implicitly read source files. */
217        CLASS,
218        /** Like CLASS, but generate warnings if annotation processing occurs */
219        UNSET;
220
221        static ImplicitSourcePolicy decode(String option) {
222            if (option == null)
223                return UNSET;
224            else if (option.equals("none"))
225                return NONE;
226            else if (option.equals("class"))
227                return CLASS;
228            else
229                return UNSET;
230        }
231    }
232
233    /** The log to be used for error reporting.
234     */
235    public Log log;
236
237    /** Factory for creating diagnostic objects
238     */
239    JCDiagnostic.Factory diagFactory;
240
241    /** The tree factory module.
242     */
243    protected TreeMaker make;
244
245    /** The class finder.
246     */
247    protected ClassFinder finder;
248
249    /** The class reader.
250     */
251    protected ClassReader reader;
252
253    /** The class writer.
254     */
255    protected ClassWriter writer;
256
257    /** The native header writer.
258     */
259    protected JNIWriter jniWriter;
260
261    /** The module for the symbol table entry phases.
262     */
263    protected Enter enter;
264
265    /** The symbol table.
266     */
267    protected Symtab syms;
268
269    /** The language version.
270     */
271    protected Source source;
272
273    /** The module for code generation.
274     */
275    protected Gen gen;
276
277    /** The name table.
278     */
279    protected Names names;
280
281    /** The attributor.
282     */
283    protected Attr attr;
284
285    /** The attributor.
286     */
287    protected Check chk;
288
289    /** The flow analyzer.
290     */
291    protected Flow flow;
292
293    /** The modules visitor
294     */
295    protected Modules modules;
296
297    /** The module finder
298     */
299    protected ModuleFinder moduleFinder;
300
301    /** The diagnostics factory
302     */
303    protected JCDiagnostic.Factory diags;
304
305    /** The type eraser.
306     */
307    protected TransTypes transTypes;
308
309    /** The syntactic sugar desweetener.
310     */
311    protected Lower lower;
312
313    /** The annotation annotator.
314     */
315    protected Annotate annotate;
316
317    /** Force a completion failure on this name
318     */
319    protected final Name completionFailureName;
320
321    /** Type utilities.
322     */
323    protected Types types;
324
325    /** Access to file objects.
326     */
327    protected JavaFileManager fileManager;
328
329    /** Factory for parsers.
330     */
331    protected ParserFactory parserFactory;
332
333    /** Broadcasting listener for progress events
334     */
335    protected MultiTaskListener taskListener;
336
337    /**
338     * SourceCompleter that delegates to the readSourceFile method of this class.
339     */
340    protected final Symbol.Completer sourceCompleter =
341            sym -> readSourceFile((ClassSymbol) sym);
342
343    protected final ModuleFinder.ModuleInfoSourceFileCompleter moduleInfoSourceFileCompleter =
344            fo -> (ModuleSymbol) readSourceFile(parseImplicitFile(fo), null, tl -> {
345                return tl.defs.nonEmpty() && tl.defs.head.hasTag(Tag.MODULEDEF) ?
346                        ((JCModuleDecl) tl.defs.head).sym.module_info :
347                        syms.defineClass(names.module_info, syms.errModule);
348            }).owner;
349
350    /**
351     * Command line options.
352     */
353    protected Options options;
354
355    protected Context context;
356
357    /**
358     * Flag set if any annotation processing occurred.
359     **/
360    protected boolean annotationProcessingOccurred;
361
362    /**
363     * Flag set if any implicit source files read.
364     **/
365    protected boolean implicitSourceFilesRead;
366
367    private boolean enterDone;
368
369    protected CompileStates compileStates;
370
371    /** Construct a new compiler using a shared context.
372     */
373    public JavaCompiler(Context context) {
374        this.context = context;
375        context.put(compilerKey, this);
376
377        // if fileManager not already set, register the JavacFileManager to be used
378        if (context.get(JavaFileManager.class) == null)
379            JavacFileManager.preRegister(context);
380
381        names = Names.instance(context);
382        log = Log.instance(context);
383        diagFactory = JCDiagnostic.Factory.instance(context);
384        finder = ClassFinder.instance(context);
385        reader = ClassReader.instance(context);
386        make = TreeMaker.instance(context);
387        writer = ClassWriter.instance(context);
388        jniWriter = JNIWriter.instance(context);
389        enter = Enter.instance(context);
390        todo = Todo.instance(context);
391
392        fileManager = context.get(JavaFileManager.class);
393        parserFactory = ParserFactory.instance(context);
394        compileStates = CompileStates.instance(context);
395
396        try {
397            // catch completion problems with predefineds
398            syms = Symtab.instance(context);
399        } catch (CompletionFailure ex) {
400            // inlined Check.completionError as it is not initialized yet
401            log.error("cant.access", ex.sym, ex.getDetailValue());
402            if (ex instanceof ClassFinder.BadClassFile)
403                throw new Abort();
404        }
405        source = Source.instance(context);
406        attr = Attr.instance(context);
407        chk = Check.instance(context);
408        gen = Gen.instance(context);
409        flow = Flow.instance(context);
410        transTypes = TransTypes.instance(context);
411        lower = Lower.instance(context);
412        annotate = Annotate.instance(context);
413        types = Types.instance(context);
414        taskListener = MultiTaskListener.instance(context);
415        modules = Modules.instance(context);
416        moduleFinder = ModuleFinder.instance(context);
417        diags = Factory.instance(context);
418
419        finder.sourceCompleter = sourceCompleter;
420        moduleFinder.sourceFileCompleter = moduleInfoSourceFileCompleter;
421
422        options = Options.instance(context);
423
424        verbose       = options.isSet(VERBOSE);
425        sourceOutput  = options.isSet(PRINTSOURCE); // used to be -s
426        lineDebugInfo = options.isUnset(G_CUSTOM) ||
427                        options.isSet(G_CUSTOM, "lines");
428        genEndPos     = options.isSet(XJCOV) ||
429                        context.get(DiagnosticListener.class) != null;
430        devVerbose    = options.isSet("dev");
431        processPcks   = options.isSet("process.packages");
432        werror        = options.isSet(WERROR);
433
434        verboseCompilePolicy = options.isSet("verboseCompilePolicy");
435
436        if (options.isSet("should-stop.at") &&
437            CompileState.valueOf(options.get("should-stop.at")) == CompileState.ATTR)
438            compilePolicy = CompilePolicy.ATTR_ONLY;
439        else
440            compilePolicy = CompilePolicy.decode(options.get("compilePolicy"));
441
442        implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit"));
443
444        completionFailureName =
445            options.isSet("failcomplete")
446            ? names.fromString(options.get("failcomplete"))
447            : null;
448
449        shouldStopPolicyIfError =
450            options.isSet("should-stop.at") // backwards compatible
451            ? CompileState.valueOf(options.get("should-stop.at"))
452            : options.isSet("should-stop.ifError")
453            ? CompileState.valueOf(options.get("should-stop.ifError"))
454            : CompileState.INIT;
455        shouldStopPolicyIfNoError =
456            options.isSet("should-stop.ifNoError")
457            ? CompileState.valueOf(options.get("should-stop.ifNoError"))
458            : CompileState.GENERATE;
459
460        if (options.isUnset("diags.legacy"))
461            log.setDiagnosticFormatter(RichDiagnosticFormatter.instance(context));
462
463        PlatformDescription platformProvider = context.get(PlatformDescription.class);
464
465        if (platformProvider != null)
466            closeables = closeables.prepend(platformProvider);
467
468        silentFail = new Symbol(ABSENT_TYP, 0, names.empty, Type.noType, syms.rootPackage) {
469            @DefinedBy(Api.LANGUAGE_MODEL)
470            public <R, P> R accept(ElementVisitor<R, P> v, P p) {
471                return v.visitUnknown(this, p);
472            }
473            @Override
474            public boolean exists() {
475                return false;
476            }
477        };
478
479    }
480
481    /* Switches:
482     */
483
484    /** Verbose output.
485     */
486    public boolean verbose;
487
488    /** Emit plain Java source files rather than class files.
489     */
490    public boolean sourceOutput;
491
492
493    /** Generate code with the LineNumberTable attribute for debugging
494     */
495    public boolean lineDebugInfo;
496
497    /** Switch: should we store the ending positions?
498     */
499    public boolean genEndPos;
500
501    /** Switch: should we debug ignored exceptions
502     */
503    protected boolean devVerbose;
504
505    /** Switch: should we (annotation) process packages as well
506     */
507    protected boolean processPcks;
508
509    /** Switch: treat warnings as errors
510     */
511    protected boolean werror;
512
513    /** Switch: is annotation processing requested explicitly via
514     * CompilationTask.setProcessors?
515     */
516    protected boolean explicitAnnotationProcessingRequested = false;
517
518    /**
519     * The policy for the order in which to perform the compilation
520     */
521    protected CompilePolicy compilePolicy;
522
523    /**
524     * The policy for what to do with implicitly read source files
525     */
526    protected ImplicitSourcePolicy implicitSourcePolicy;
527
528    /**
529     * Report activity related to compilePolicy
530     */
531    public boolean verboseCompilePolicy;
532
533    /**
534     * Policy of how far to continue compilation after errors have occurred.
535     * Set this to minimum CompileState (INIT) to stop as soon as possible
536     * after errors.
537     */
538    public CompileState shouldStopPolicyIfError;
539
540    /**
541     * Policy of how far to continue compilation when no errors have occurred.
542     * Set this to maximum CompileState (GENERATE) to perform full compilation.
543     * Set this lower to perform partial compilation, such as -proc:only.
544     */
545    public CompileState shouldStopPolicyIfNoError;
546
547    /** A queue of all as yet unattributed classes.
548     */
549    public Todo todo;
550
551    /** A list of items to be closed when the compilation is complete.
552     */
553    public List<Closeable> closeables = List.nil();
554
555    /** The set of currently compiled inputfiles, needed to ensure
556     *  we don't accidentally overwrite an input file when -s is set.
557     *  initialized by `compile'.
558     */
559    protected Set<JavaFileObject> inputFiles = new HashSet<>();
560
561    /** Used by the resolveBinaryNameOrIdent to say that the given type cannot be found, and that
562     *  an error has already been produced about that.
563     */
564    private final Symbol silentFail;
565
566    protected boolean shouldStop(CompileState cs) {
567        CompileState shouldStopPolicy = (errorCount() > 0 || unrecoverableError())
568            ? shouldStopPolicyIfError
569            : shouldStopPolicyIfNoError;
570        return cs.isAfter(shouldStopPolicy);
571    }
572
573    /** The number of errors reported so far.
574     */
575    public int errorCount() {
576        if (werror && log.nerrors == 0 && log.nwarnings > 0) {
577            log.error("warnings.and.werror");
578        }
579        return log.nerrors;
580    }
581
582    protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) {
583        return shouldStop(cs) ? new ListBuffer<T>() : queue;
584    }
585
586    protected final <T> List<T> stopIfError(CompileState cs, List<T> list) {
587        return shouldStop(cs) ? List.nil() : list;
588    }
589
590    /** The number of warnings reported so far.
591     */
592    public int warningCount() {
593        return log.nwarnings;
594    }
595
596    /** Try to open input stream with given name.
597     *  Report an error if this fails.
598     *  @param filename   The file name of the input stream to be opened.
599     */
600    public CharSequence readSource(JavaFileObject filename) {
601        try {
602            inputFiles.add(filename);
603            return filename.getCharContent(false);
604        } catch (IOException e) {
605            log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
606            return null;
607        }
608    }
609
610    /** Parse contents of input stream.
611     *  @param filename     The name of the file from which input stream comes.
612     *  @param content      The characters to be parsed.
613     */
614    protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) {
615        long msec = now();
616        JCCompilationUnit tree = make.TopLevel(List.nil());
617        if (content != null) {
618            if (verbose) {
619                log.printVerbose("parsing.started", filename);
620            }
621            if (!taskListener.isEmpty()) {
622                TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename);
623                taskListener.started(e);
624                keepComments = true;
625                genEndPos = true;
626            }
627            Parser parser = parserFactory.newParser(content, keepComments(), genEndPos,
628                                lineDebugInfo, filename.isNameCompatible("module-info", Kind.SOURCE));
629            tree = parser.parseCompilationUnit();
630            if (verbose) {
631                log.printVerbose("parsing.done", Long.toString(elapsed(msec)));
632            }
633        }
634
635        tree.sourcefile = filename;
636
637        if (content != null && !taskListener.isEmpty()) {
638            TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree);
639            taskListener.finished(e);
640        }
641
642        return tree;
643    }
644    // where
645        public boolean keepComments = false;
646        protected boolean keepComments() {
647            return keepComments || sourceOutput;
648        }
649
650
651    /** Parse contents of file.
652     *  @param filename     The name of the file to be parsed.
653     */
654    @Deprecated
655    public JCTree.JCCompilationUnit parse(String filename) {
656        JavacFileManager fm = (JavacFileManager)fileManager;
657        return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next());
658    }
659
660    /** Parse contents of file.
661     *  @param filename     The name of the file to be parsed.
662     */
663    public JCTree.JCCompilationUnit parse(JavaFileObject filename) {
664        JavaFileObject prev = log.useSource(filename);
665        try {
666            JCTree.JCCompilationUnit t = parse(filename, readSource(filename));
667            if (t.endPositions != null)
668                log.setEndPosTable(filename, t.endPositions);
669            return t;
670        } finally {
671            log.useSource(prev);
672        }
673    }
674
675    /** Resolve an identifier which may be the binary name of a class or
676     * the Java name of a class or package.
677     * @param name      The name to resolve
678     */
679    public Symbol resolveBinaryNameOrIdent(String name) {
680        ModuleSymbol msym;
681        String typeName;
682        int sep = name.indexOf('/');
683        if (sep == -1) {
684            msym = modules.getDefaultModule();
685            typeName = name;
686        } else if (source.allowModules()) {
687            Name modName = names.fromString(name.substring(0, sep));
688
689            msym = moduleFinder.findModule(modName);
690            typeName = name.substring(sep + 1);
691        } else {
692            log.error(Errors.InvalidModuleSpecifier(name));
693            return silentFail;
694        }
695
696        return resolveBinaryNameOrIdent(msym, typeName);
697    }
698
699    /** Resolve an identifier which may be the binary name of a class or
700     * the Java name of a class or package.
701     * @param msym      The module in which the search should be performed
702     * @param name      The name to resolve
703     */
704    public Symbol resolveBinaryNameOrIdent(ModuleSymbol msym, String name) {
705        try {
706            Name flatname = names.fromString(name.replace("/", "."));
707            return finder.loadClass(msym, flatname);
708        } catch (CompletionFailure ignore) {
709            return resolveIdent(msym, name);
710        }
711    }
712
713    /** Resolve an identifier.
714     * @param msym      The module in which the search should be performed
715     * @param name      The identifier to resolve
716     */
717    public Symbol resolveIdent(ModuleSymbol msym, String name) {
718        if (name.equals(""))
719            return syms.errSymbol;
720        JavaFileObject prev = log.useSource(null);
721        try {
722            JCExpression tree = null;
723            for (String s : name.split("\\.", -1)) {
724                if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords
725                    return syms.errSymbol;
726                tree = (tree == null) ? make.Ident(names.fromString(s))
727                                      : make.Select(tree, names.fromString(s));
728            }
729            JCCompilationUnit toplevel =
730                make.TopLevel(List.nil());
731            toplevel.modle = msym;
732            toplevel.packge = msym.unnamedPackage;
733            return attr.attribIdent(tree, toplevel);
734        } finally {
735            log.useSource(prev);
736        }
737    }
738
739    /** Generate code and emit a class file for a given class
740     *  @param env    The attribution environment of the outermost class
741     *                containing this class.
742     *  @param cdef   The class definition from which code is generated.
743     */
744    JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
745        try {
746            if (gen.genClass(env, cdef) && (errorCount() == 0))
747                return writer.writeClass(cdef.sym);
748        } catch (ClassWriter.PoolOverflow ex) {
749            log.error(cdef.pos(), "limit.pool");
750        } catch (ClassWriter.StringOverflow ex) {
751            log.error(cdef.pos(), "limit.string.overflow",
752                      ex.value.substring(0, 20));
753        } catch (CompletionFailure ex) {
754            chk.completionError(cdef.pos(), ex);
755        }
756        return null;
757    }
758
759    /** Emit plain Java source for a class.
760     *  @param env    The attribution environment of the outermost class
761     *                containing this class.
762     *  @param cdef   The class definition to be printed.
763     */
764    JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
765        JavaFileObject outFile
766           = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
767                                               cdef.sym.flatname.toString(),
768                                               JavaFileObject.Kind.SOURCE,
769                                               null);
770        if (inputFiles.contains(outFile)) {
771            log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile);
772            return null;
773        } else {
774            try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) {
775                new Pretty(out, true).printUnit(env.toplevel, cdef);
776                if (verbose)
777                    log.printVerbose("wrote.file", outFile);
778            }
779            return outFile;
780        }
781    }
782
783    /** Compile a source file that has been accessed by the class finder.
784     *  @param c          The class the source file of which needs to be compiled.
785     */
786    private void readSourceFile(ClassSymbol c) throws CompletionFailure {
787        readSourceFile(null, c);
788    }
789
790    private JCTree.JCCompilationUnit parseImplicitFile(JavaFileObject filename) {
791        JavaFileObject prev = log.useSource(filename);
792        try {
793            JCTree.JCCompilationUnit t = parse(filename, filename.getCharContent(false));
794            return t;
795        } catch (IOException e) {
796            log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
797            return make.TopLevel(List.nil());
798        } finally {
799            log.useSource(prev);
800        }
801    }
802
803    /** Compile a ClassSymbol from source, optionally using the given compilation unit as
804     *  the source tree.
805     *  @param tree the compilation unit in which the given ClassSymbol resides,
806     *              or null if should be parsed from source
807     *  @param c    the ClassSymbol to complete
808     */
809    public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
810        if (completionFailureName == c.fullname) {
811            throw new CompletionFailure(c, "user-selected completion failure by class name");
812        }
813
814        if (tree == null) {
815            tree = parseImplicitFile(c.classfile);
816        }
817
818        readSourceFile(tree, c, cut -> c);
819    }
820
821    private ClassSymbol readSourceFile(JCCompilationUnit tree,
822                                       ClassSymbol expectedSymbol,
823                                       Function<JCCompilationUnit, ClassSymbol> symbolGetter)
824                                           throws CompletionFailure {
825        Assert.checkNonNull(tree);
826
827        if (!taskListener.isEmpty()) {
828            TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
829            taskListener.started(e);
830        }
831
832        // Process module declarations.
833        // If module resolution fails, ignore trees, and if trying to
834        // complete a specific symbol, throw CompletionFailure.
835        // Note that if module resolution failed, we may not even
836        // have enough modules available to access java.lang, and
837        // so risk getting FatalError("no.java.lang") from MemberEnter.
838        if (!modules.enter(List.of(tree), expectedSymbol)) {
839            throw new CompletionFailure(symbolGetter.apply(tree),
840                                        diags.fragment("cant.resolve.modules"));
841        }
842
843        enter.complete(List.of(tree), expectedSymbol);
844
845        if (!taskListener.isEmpty()) {
846            TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
847            taskListener.finished(e);
848        }
849
850        ClassSymbol sym = symbolGetter.apply(tree);
851        if (sym == null || enter.getEnv(sym) == null) {
852            boolean isPkgInfo =
853                tree.sourcefile.isNameCompatible("package-info",
854                                                 JavaFileObject.Kind.SOURCE);
855            boolean isModuleInfo =
856                tree.sourcefile.isNameCompatible("module-info",
857                                                 JavaFileObject.Kind.SOURCE);
858            if (isModuleInfo) {
859                if (enter.getEnv(tree.modle) == null) {
860                    JCDiagnostic diag =
861                        diagFactory.fragment("file.does.not.contain.module");
862                    throw new ClassFinder.BadClassFile(sym, tree.sourcefile, diag, diagFactory);
863                }
864            } else if (isPkgInfo) {
865                if (enter.getEnv(tree.packge) == null) {
866                    JCDiagnostic diag =
867                        diagFactory.fragment("file.does.not.contain.package",
868                                                 sym.location());
869                    throw new ClassFinder.BadClassFile(sym, tree.sourcefile, diag, diagFactory);
870                }
871            } else {
872                JCDiagnostic diag =
873                        diagFactory.fragment("file.doesnt.contain.class",
874                                            sym.getQualifiedName());
875                throw new ClassFinder.BadClassFile(sym, tree.sourcefile, diag, diagFactory);
876            }
877        }
878
879        implicitSourceFilesRead = true;
880
881        return sym;
882    }
883
884    /** Track when the JavaCompiler has been used to compile something. */
885    private boolean hasBeenUsed = false;
886    private long start_msec = 0;
887    public long elapsed_msec = 0;
888
889    public void compile(List<JavaFileObject> sourceFileObject)
890        throws Throwable {
891        compile(sourceFileObject, List.nil(), null);
892    }
893
894    /**
895     * Main method: compile a list of files, return all compiled classes
896     *
897     * @param sourceFileObjects file objects to be compiled
898     * @param classnames class names to process for annotations
899     * @param processors user provided annotation processors to bypass
900     * discovery, {@code null} means that no processors were provided
901     */
902    public void compile(Collection<JavaFileObject> sourceFileObjects,
903                        Collection<String> classnames,
904                        Iterable<? extends Processor> processors)
905    {
906        if (!taskListener.isEmpty()) {
907            taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION));
908        }
909
910        if (processors != null && processors.iterator().hasNext())
911            explicitAnnotationProcessingRequested = true;
912        // as a JavaCompiler can only be used once, throw an exception if
913        // it has been used before.
914        if (hasBeenUsed)
915            checkReusable();
916        hasBeenUsed = true;
917
918        // forcibly set the equivalent of -Xlint:-options, so that no further
919        // warnings about command line options are generated from this point on
920        options.put(XLINT_CUSTOM.primaryName + "-" + LintCategory.OPTIONS.option, "true");
921        options.remove(XLINT_CUSTOM.primaryName + LintCategory.OPTIONS.option);
922
923        start_msec = now();
924
925        try {
926            initProcessAnnotations(processors, sourceFileObjects, classnames);
927
928            for (String className : classnames) {
929                int sep = className.indexOf('/');
930                if (sep != -1) {
931                    modules.addExtraAddModules(className.substring(0, sep));
932                }
933            }
934
935            // These method calls must be chained to avoid memory leaks
936            processAnnotations(
937                enterTrees(
938                        stopIfError(CompileState.PARSE,
939                                initModules(stopIfError(CompileState.PARSE, parseFiles(sourceFileObjects))))
940                ),
941                classnames
942            );
943
944            // If it's safe to do so, skip attr / flow / gen for implicit classes
945            if (taskListener.isEmpty() &&
946                    implicitSourcePolicy == ImplicitSourcePolicy.NONE) {
947                todo.retainFiles(inputFiles);
948            }
949
950            switch (compilePolicy) {
951            case ATTR_ONLY:
952                attribute(todo);
953                break;
954
955            case CHECK_ONLY:
956                flow(attribute(todo));
957                break;
958
959            case SIMPLE:
960                generate(desugar(flow(attribute(todo))));
961                break;
962
963            case BY_FILE: {
964                    Queue<Queue<Env<AttrContext>>> q = todo.groupByFile();
965                    while (!q.isEmpty() && !shouldStop(CompileState.ATTR)) {
966                        generate(desugar(flow(attribute(q.remove()))));
967                    }
968                }
969                break;
970
971            case BY_TODO:
972                while (!todo.isEmpty())
973                    generate(desugar(flow(attribute(todo.remove()))));
974                break;
975
976            default:
977                Assert.error("unknown compile policy");
978            }
979        } catch (Abort ex) {
980            if (devVerbose)
981                ex.printStackTrace(System.err);
982        } finally {
983            if (verbose) {
984                elapsed_msec = elapsed(start_msec);
985                log.printVerbose("total", Long.toString(elapsed_msec));
986            }
987
988            reportDeferredDiagnostics();
989
990            if (!log.hasDiagnosticListener()) {
991                printCount("error", errorCount());
992                printCount("warn", warningCount());
993            }
994            if (!taskListener.isEmpty()) {
995                taskListener.finished(new TaskEvent(TaskEvent.Kind.COMPILATION));
996            }
997            close();
998            if (procEnvImpl != null)
999                procEnvImpl.close();
1000        }
1001    }
1002
1003    protected void checkReusable() {
1004        throw new AssertionError("attempt to reuse JavaCompiler");
1005    }
1006
1007    /**
1008     * The list of classes explicitly supplied on the command line for compilation.
1009     * Not always populated.
1010     */
1011    private List<JCClassDecl> rootClasses;
1012
1013    /**
1014     * Parses a list of files.
1015     */
1016   public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
1017       if (shouldStop(CompileState.PARSE))
1018           return List.nil();
1019
1020        //parse all files
1021        ListBuffer<JCCompilationUnit> trees = new ListBuffer<>();
1022        Set<JavaFileObject> filesSoFar = new HashSet<>();
1023        for (JavaFileObject fileObject : fileObjects) {
1024            if (!filesSoFar.contains(fileObject)) {
1025                filesSoFar.add(fileObject);
1026                trees.append(parse(fileObject));
1027            }
1028        }
1029        return trees.toList();
1030    }
1031
1032    /**
1033     * Enter the symbols found in a list of parse trees if the compilation
1034     * is expected to proceed beyond anno processing into attr.
1035     * As a side-effect, this puts elements on the "todo" list.
1036     * Also stores a list of all top level classes in rootClasses.
1037     */
1038    public List<JCCompilationUnit> enterTreesIfNeeded(List<JCCompilationUnit> roots) {
1039       if (shouldStop(CompileState.ATTR))
1040           return List.nil();
1041        return enterTrees(initModules(roots));
1042    }
1043
1044    public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {
1045        modules.initModules(roots);
1046        if (roots.isEmpty()) {
1047            enterDone();
1048        }
1049        return roots;
1050    }
1051
1052    /**
1053     * Enter the symbols found in a list of parse trees.
1054     * As a side-effect, this puts elements on the "todo" list.
1055     * Also stores a list of all top level classes in rootClasses.
1056     */
1057    public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
1058        //enter symbols for all files
1059        if (!taskListener.isEmpty()) {
1060            for (JCCompilationUnit unit: roots) {
1061                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1062                taskListener.started(e);
1063            }
1064        }
1065
1066        enter.main(roots);
1067
1068        enterDone();
1069
1070        if (!taskListener.isEmpty()) {
1071            for (JCCompilationUnit unit: roots) {
1072                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1073                taskListener.finished(e);
1074            }
1075        }
1076
1077        // If generating source, or if tracking public apis,
1078        // then remember the classes declared in
1079        // the original compilation units listed on the command line.
1080        if (sourceOutput) {
1081            ListBuffer<JCClassDecl> cdefs = new ListBuffer<>();
1082            for (JCCompilationUnit unit : roots) {
1083                for (List<JCTree> defs = unit.defs;
1084                     defs.nonEmpty();
1085                     defs = defs.tail) {
1086                    if (defs.head instanceof JCClassDecl)
1087                        cdefs.append((JCClassDecl)defs.head);
1088                }
1089            }
1090            rootClasses = cdefs.toList();
1091        }
1092
1093        // Ensure the input files have been recorded. Although this is normally
1094        // done by readSource, it may not have been done if the trees were read
1095        // in a prior round of annotation processing, and the trees have been
1096        // cleaned and are being reused.
1097        for (JCCompilationUnit unit : roots) {
1098            inputFiles.add(unit.sourcefile);
1099        }
1100
1101        return roots;
1102    }
1103
1104    /**
1105     * Set to true to enable skeleton annotation processing code.
1106     * Currently, we assume this variable will be replaced more
1107     * advanced logic to figure out if annotation processing is
1108     * needed.
1109     */
1110    boolean processAnnotations = false;
1111
1112    Log.DeferredDiagnosticHandler deferredDiagnosticHandler;
1113
1114    /**
1115     * Object to handle annotation processing.
1116     */
1117    private JavacProcessingEnvironment procEnvImpl = null;
1118
1119    /**
1120     * Check if we should process annotations.
1121     * If so, and if no scanner is yet registered, then set up the DocCommentScanner
1122     * to catch doc comments, and set keepComments so the parser records them in
1123     * the compilation unit.
1124     *
1125     * @param processors user provided annotation processors to bypass
1126     * discovery, {@code null} means that no processors were provided
1127     */
1128    public void initProcessAnnotations(Iterable<? extends Processor> processors,
1129                                       Collection<? extends JavaFileObject> initialFiles,
1130                                       Collection<String> initialClassNames) {
1131        // Process annotations if processing is not disabled and there
1132        // is at least one Processor available.
1133        if (options.isSet(PROC, "none")) {
1134            processAnnotations = false;
1135        } else if (procEnvImpl == null) {
1136            procEnvImpl = JavacProcessingEnvironment.instance(context);
1137            procEnvImpl.setProcessors(processors);
1138            processAnnotations = procEnvImpl.atLeastOneProcessor();
1139
1140            if (processAnnotations) {
1141                options.put("parameters", "parameters");
1142                reader.saveParameterNames = true;
1143                keepComments = true;
1144                genEndPos = true;
1145                if (!taskListener.isEmpty())
1146                    taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
1147                deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
1148                procEnvImpl.getFiler().setInitialState(initialFiles, initialClassNames);
1149            } else { // free resources
1150                procEnvImpl.close();
1151            }
1152        }
1153    }
1154
1155    // TODO: called by JavacTaskImpl
1156    public void processAnnotations(List<JCCompilationUnit> roots) {
1157        processAnnotations(roots, List.nil());
1158    }
1159
1160    /**
1161     * Process any annotations found in the specified compilation units.
1162     * @param roots a list of compilation units
1163     */
1164    // Implementation note: when this method is called, log.deferredDiagnostics
1165    // will have been set true by initProcessAnnotations, meaning that any diagnostics
1166    // that are reported will go into the log.deferredDiagnostics queue.
1167    // By the time this method exits, log.deferDiagnostics must be set back to false,
1168    // and all deferredDiagnostics must have been handled: i.e. either reported
1169    // or determined to be transient, and therefore suppressed.
1170    public void processAnnotations(List<JCCompilationUnit> roots,
1171                                   Collection<String> classnames) {
1172        if (shouldStop(CompileState.PROCESS)) {
1173            // Errors were encountered.
1174            // Unless all the errors are resolve errors, the errors were parse errors
1175            // or other errors during enter which cannot be fixed by running
1176            // any annotation processors.
1177            if (unrecoverableError()) {
1178                deferredDiagnosticHandler.reportDeferredDiagnostics();
1179                log.popDiagnosticHandler(deferredDiagnosticHandler);
1180                return ;
1181            }
1182        }
1183
1184        // ASSERT: processAnnotations and procEnvImpl should have been set up by
1185        // by initProcessAnnotations
1186
1187        // NOTE: The !classnames.isEmpty() checks should be refactored to Main.
1188
1189        if (!processAnnotations) {
1190            // If there are no annotation processors present, and
1191            // annotation processing is to occur with compilation,
1192            // emit a warning.
1193            if (options.isSet(PROC, "only")) {
1194                log.warning("proc.proc-only.requested.no.procs");
1195                todo.clear();
1196            }
1197            // If not processing annotations, classnames must be empty
1198            if (!classnames.isEmpty()) {
1199                log.error("proc.no.explicit.annotation.processing.requested",
1200                          classnames);
1201            }
1202            Assert.checkNull(deferredDiagnosticHandler);
1203            return ; // continue regular compilation
1204        }
1205
1206        Assert.checkNonNull(deferredDiagnosticHandler);
1207
1208        try {
1209            List<ClassSymbol> classSymbols = List.nil();
1210            List<PackageSymbol> pckSymbols = List.nil();
1211            if (!classnames.isEmpty()) {
1212                 // Check for explicit request for annotation
1213                 // processing
1214                if (!explicitAnnotationProcessingRequested()) {
1215                    log.error("proc.no.explicit.annotation.processing.requested",
1216                              classnames);
1217                    deferredDiagnosticHandler.reportDeferredDiagnostics();
1218                    log.popDiagnosticHandler(deferredDiagnosticHandler);
1219                    return ; // TODO: Will this halt compilation?
1220                } else {
1221                    boolean errors = false;
1222                    for (String nameStr : classnames) {
1223                        Symbol sym = resolveBinaryNameOrIdent(nameStr);
1224                        if (sym == null ||
1225                            (sym.kind == PCK && !processPcks) ||
1226                            sym.kind == ABSENT_TYP) {
1227                            if (sym != silentFail)
1228                                log.error(Errors.ProcCantFindClass(nameStr));
1229                            errors = true;
1230                            continue;
1231                        }
1232                        try {
1233                            if (sym.kind == PCK)
1234                                sym.complete();
1235                            if (sym.exists()) {
1236                                if (sym.kind == PCK)
1237                                    pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
1238                                else
1239                                    classSymbols = classSymbols.prepend((ClassSymbol)sym);
1240                                continue;
1241                            }
1242                            Assert.check(sym.kind == PCK);
1243                            log.warning(Warnings.ProcPackageDoesNotExist(nameStr));
1244                            pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
1245                        } catch (CompletionFailure e) {
1246                            log.error(Errors.ProcCantFindClass(nameStr));
1247                            errors = true;
1248                            continue;
1249                        }
1250                    }
1251                    if (errors) {
1252                        deferredDiagnosticHandler.reportDeferredDiagnostics();
1253                        log.popDiagnosticHandler(deferredDiagnosticHandler);
1254                        return ;
1255                    }
1256                }
1257            }
1258            try {
1259                annotationProcessingOccurred =
1260                        procEnvImpl.doProcessing(roots,
1261                                                 classSymbols,
1262                                                 pckSymbols,
1263                                                 deferredDiagnosticHandler);
1264                // doProcessing will have handled deferred diagnostics
1265            } finally {
1266                procEnvImpl.close();
1267            }
1268        } catch (CompletionFailure ex) {
1269            log.error("cant.access", ex.sym, ex.getDetailValue());
1270            if (deferredDiagnosticHandler != null) {
1271                deferredDiagnosticHandler.reportDeferredDiagnostics();
1272                log.popDiagnosticHandler(deferredDiagnosticHandler);
1273            }
1274        }
1275    }
1276
1277    private boolean unrecoverableError() {
1278        if (deferredDiagnosticHandler != null) {
1279            for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) {
1280                if (d.getKind() == JCDiagnostic.Kind.ERROR && !d.isFlagSet(RECOVERABLE))
1281                    return true;
1282            }
1283        }
1284        return false;
1285    }
1286
1287    boolean explicitAnnotationProcessingRequested() {
1288        return
1289            explicitAnnotationProcessingRequested ||
1290            explicitAnnotationProcessingRequested(options);
1291    }
1292
1293    static boolean explicitAnnotationProcessingRequested(Options options) {
1294        return
1295            options.isSet(PROCESSOR) ||
1296            options.isSet(PROCESSOR_PATH) ||
1297            options.isSet(PROCESSOR_MODULE_PATH) ||
1298            options.isSet(PROC, "only") ||
1299            options.isSet(XPRINT);
1300    }
1301
1302    public void setDeferredDiagnosticHandler(Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
1303        this.deferredDiagnosticHandler = deferredDiagnosticHandler;
1304    }
1305
1306    /**
1307     * Attribute a list of parse trees, such as found on the "todo" list.
1308     * Note that attributing classes may cause additional files to be
1309     * parsed and entered via the SourceCompleter.
1310     * Attribution of the entries in the list does not stop if any errors occur.
1311     * @return a list of environments for attribute classes.
1312     */
1313    public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) {
1314        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
1315        while (!envs.isEmpty())
1316            results.append(attribute(envs.remove()));
1317        return stopIfError(CompileState.ATTR, results);
1318    }
1319
1320    /**
1321     * Attribute a parse tree.
1322     * @return the attributed parse tree
1323     */
1324    public Env<AttrContext> attribute(Env<AttrContext> env) {
1325        if (compileStates.isDone(env, CompileState.ATTR))
1326            return env;
1327
1328        if (verboseCompilePolicy)
1329            printNote("[attribute " + env.enclClass.sym + "]");
1330        if (verbose)
1331            log.printVerbose("checking.attribution", env.enclClass.sym);
1332
1333        if (!taskListener.isEmpty()) {
1334            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
1335            taskListener.started(e);
1336        }
1337
1338        JavaFileObject prev = log.useSource(
1339                                  env.enclClass.sym.sourcefile != null ?
1340                                  env.enclClass.sym.sourcefile :
1341                                  env.toplevel.sourcefile);
1342        try {
1343            attr.attrib(env);
1344            if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
1345                //if in fail-over mode, ensure that AST expression nodes
1346                //are correctly initialized (e.g. they have a type/symbol)
1347                attr.postAttr(env.tree);
1348            }
1349            compileStates.put(env, CompileState.ATTR);
1350        }
1351        finally {
1352            log.useSource(prev);
1353        }
1354
1355        return env;
1356    }
1357
1358    /**
1359     * Perform dataflow checks on attributed parse trees.
1360     * These include checks for definite assignment and unreachable statements.
1361     * If any errors occur, an empty list will be returned.
1362     * @return the list of attributed parse trees
1363     */
1364    public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) {
1365        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
1366        for (Env<AttrContext> env: envs) {
1367            flow(env, results);
1368        }
1369        return stopIfError(CompileState.FLOW, results);
1370    }
1371
1372    /**
1373     * Perform dataflow checks on an attributed parse tree.
1374     */
1375    public Queue<Env<AttrContext>> flow(Env<AttrContext> env) {
1376        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
1377        flow(env, results);
1378        return stopIfError(CompileState.FLOW, results);
1379    }
1380
1381    /**
1382     * Perform dataflow checks on an attributed parse tree.
1383     */
1384    protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
1385        if (compileStates.isDone(env, CompileState.FLOW)) {
1386            results.add(env);
1387            return;
1388        }
1389
1390        try {
1391            if (shouldStop(CompileState.FLOW))
1392                return;
1393
1394            if (verboseCompilePolicy)
1395                printNote("[flow " + env.enclClass.sym + "]");
1396            JavaFileObject prev = log.useSource(
1397                                                env.enclClass.sym.sourcefile != null ?
1398                                                env.enclClass.sym.sourcefile :
1399                                                env.toplevel.sourcefile);
1400            try {
1401                make.at(Position.FIRSTPOS);
1402                TreeMaker localMake = make.forToplevel(env.toplevel);
1403                flow.analyzeTree(env, localMake);
1404                compileStates.put(env, CompileState.FLOW);
1405
1406                if (shouldStop(CompileState.FLOW))
1407                    return;
1408
1409                results.add(env);
1410            }
1411            finally {
1412                log.useSource(prev);
1413            }
1414        }
1415        finally {
1416            if (!taskListener.isEmpty()) {
1417                TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
1418                taskListener.finished(e);
1419            }
1420        }
1421    }
1422
1423    /**
1424     * Prepare attributed parse trees, in conjunction with their attribution contexts,
1425     * for source or code generation.
1426     * If any errors occur, an empty list will be returned.
1427     * @return a list containing the classes to be generated
1428     */
1429    public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) {
1430        ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = new ListBuffer<>();
1431        for (Env<AttrContext> env: envs)
1432            desugar(env, results);
1433        return stopIfError(CompileState.FLOW, results);
1434    }
1435
1436    HashMap<Env<AttrContext>, Queue<Pair<Env<AttrContext>, JCClassDecl>>> desugaredEnvs = new HashMap<>();
1437
1438    /**
1439     * Prepare attributed parse trees, in conjunction with their attribution contexts,
1440     * for source or code generation. If the file was not listed on the command line,
1441     * the current implicitSourcePolicy is taken into account.
1442     * The preparation stops as soon as an error is found.
1443     */
1444    protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
1445        if (shouldStop(CompileState.TRANSTYPES))
1446            return;
1447
1448        if (implicitSourcePolicy == ImplicitSourcePolicy.NONE
1449                && !inputFiles.contains(env.toplevel.sourcefile)) {
1450            return;
1451        }
1452
1453        if (!modules.multiModuleMode && env.toplevel.modle != modules.getDefaultModule()) {
1454            //can only generate classfiles for a single module:
1455            return;
1456        }
1457
1458        if (compileStates.isDone(env, CompileState.LOWER)) {
1459            results.addAll(desugaredEnvs.get(env));
1460            return;
1461        }
1462
1463        /**
1464         * Ensure that superclasses of C are desugared before C itself. This is
1465         * required for two reasons: (i) as erasure (TransTypes) destroys
1466         * information needed in flow analysis and (ii) as some checks carried
1467         * out during lowering require that all synthetic fields/methods have
1468         * already been added to C and its superclasses.
1469         */
1470        class ScanNested extends TreeScanner {
1471            Set<Env<AttrContext>> dependencies = new LinkedHashSet<>();
1472            protected boolean hasLambdas;
1473            @Override
1474            public void visitClassDef(JCClassDecl node) {
1475                Type st = types.supertype(node.sym.type);
1476                boolean envForSuperTypeFound = false;
1477                while (!envForSuperTypeFound && st.hasTag(CLASS)) {
1478                    ClassSymbol c = st.tsym.outermostClass();
1479                    Env<AttrContext> stEnv = enter.getEnv(c);
1480                    if (stEnv != null && env != stEnv) {
1481                        if (dependencies.add(stEnv)) {
1482                            boolean prevHasLambdas = hasLambdas;
1483                            try {
1484                                scan(stEnv.tree);
1485                            } finally {
1486                                /*
1487                                 * ignore any updates to hasLambdas made during
1488                                 * the nested scan, this ensures an initalized
1489                                 * LambdaToMethod is available only to those
1490                                 * classes that contain lambdas
1491                                 */
1492                                hasLambdas = prevHasLambdas;
1493                            }
1494                        }
1495                        envForSuperTypeFound = true;
1496                    }
1497                    st = types.supertype(st);
1498                }
1499                super.visitClassDef(node);
1500            }
1501            @Override
1502            public void visitLambda(JCLambda tree) {
1503                hasLambdas = true;
1504                super.visitLambda(tree);
1505            }
1506            @Override
1507            public void visitReference(JCMemberReference tree) {
1508                hasLambdas = true;
1509                super.visitReference(tree);
1510            }
1511        }
1512        ScanNested scanner = new ScanNested();
1513        scanner.scan(env.tree);
1514        for (Env<AttrContext> dep: scanner.dependencies) {
1515        if (!compileStates.isDone(dep, CompileState.FLOW))
1516            desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
1517        }
1518
1519        //We need to check for error another time as more classes might
1520        //have been attributed and analyzed at this stage
1521        if (shouldStop(CompileState.TRANSTYPES))
1522            return;
1523
1524        if (verboseCompilePolicy)
1525            printNote("[desugar " + env.enclClass.sym + "]");
1526
1527        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1528                                  env.enclClass.sym.sourcefile :
1529                                  env.toplevel.sourcefile);
1530        try {
1531            //save tree prior to rewriting
1532            JCTree untranslated = env.tree;
1533
1534            make.at(Position.FIRSTPOS);
1535            TreeMaker localMake = make.forToplevel(env.toplevel);
1536
1537            if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1538                if (!(sourceOutput)) {
1539                    if (shouldStop(CompileState.LOWER))
1540                        return;
1541                    List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1542                    if (def.head != null) {
1543                        Assert.check(def.tail.isEmpty());
1544                        results.add(new Pair<>(env, (JCClassDecl)def.head));
1545                    }
1546                }
1547                return;
1548            }
1549
1550            if (shouldStop(CompileState.TRANSTYPES))
1551                return;
1552
1553            env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1554            compileStates.put(env, CompileState.TRANSTYPES);
1555
1556            if (source.allowLambda() && scanner.hasLambdas) {
1557                if (shouldStop(CompileState.UNLAMBDA))
1558                    return;
1559
1560                env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
1561                compileStates.put(env, CompileState.UNLAMBDA);
1562            }
1563
1564            if (shouldStop(CompileState.LOWER))
1565                return;
1566
1567            if (sourceOutput) {
1568                //emit standard Java source file, only for compilation
1569                //units enumerated explicitly on the command line
1570                JCClassDecl cdef = (JCClassDecl)env.tree;
1571                if (untranslated instanceof JCClassDecl &&
1572                    rootClasses.contains((JCClassDecl)untranslated)) {
1573                    results.add(new Pair<>(env, cdef));
1574                }
1575                return;
1576            }
1577
1578            //translate out inner classes
1579            List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
1580            compileStates.put(env, CompileState.LOWER);
1581
1582            if (shouldStop(CompileState.LOWER))
1583                return;
1584
1585            //generate code for each class
1586            for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1587                JCClassDecl cdef = (JCClassDecl)l.head;
1588                results.add(new Pair<>(env, cdef));
1589            }
1590        }
1591        finally {
1592            log.useSource(prev);
1593        }
1594
1595    }
1596
1597    /** Generates the source or class file for a list of classes.
1598     * The decision to generate a source file or a class file is
1599     * based upon the compiler's options.
1600     * Generation stops if an error occurs while writing files.
1601     */
1602    public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1603        generate(queue, null);
1604    }
1605
1606    public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1607        if (shouldStop(CompileState.GENERATE))
1608            return;
1609
1610        for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1611            Env<AttrContext> env = x.fst;
1612            JCClassDecl cdef = x.snd;
1613
1614            if (verboseCompilePolicy) {
1615                printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1616            }
1617
1618            if (!taskListener.isEmpty()) {
1619                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
1620                taskListener.started(e);
1621            }
1622
1623            JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1624                                      env.enclClass.sym.sourcefile :
1625                                      env.toplevel.sourcefile);
1626            try {
1627                JavaFileObject file;
1628                if (sourceOutput) {
1629                    file = printSource(env, cdef);
1630                } else {
1631                    if (fileManager.hasLocation(StandardLocation.NATIVE_HEADER_OUTPUT)
1632                            && jniWriter.needsHeader(cdef.sym)) {
1633                        jniWriter.write(cdef.sym);
1634                    }
1635                    file = genCode(env, cdef);
1636                }
1637                if (results != null && file != null)
1638                    results.add(file);
1639            } catch (IOException ex) {
1640                log.error(cdef.pos(), "class.cant.write",
1641                          cdef.sym, ex.getMessage());
1642                return;
1643            } finally {
1644                log.useSource(prev);
1645            }
1646
1647            if (!taskListener.isEmpty()) {
1648                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
1649                taskListener.finished(e);
1650            }
1651        }
1652    }
1653
1654        // where
1655        Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) {
1656            // use a LinkedHashMap to preserve the order of the original list as much as possible
1657            Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<>();
1658            for (Env<AttrContext> env: envs) {
1659                Queue<Env<AttrContext>> sublist = map.get(env.toplevel);
1660                if (sublist == null) {
1661                    sublist = new ListBuffer<>();
1662                    map.put(env.toplevel, sublist);
1663                }
1664                sublist.add(env);
1665            }
1666            return map;
1667        }
1668
1669        JCClassDecl removeMethodBodies(JCClassDecl cdef) {
1670            final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
1671            class MethodBodyRemover extends TreeTranslator {
1672                @Override
1673                public void visitMethodDef(JCMethodDecl tree) {
1674                    tree.mods.flags &= ~Flags.SYNCHRONIZED;
1675                    for (JCVariableDecl vd : tree.params)
1676                        vd.mods.flags &= ~Flags.FINAL;
1677                    tree.body = null;
1678                    super.visitMethodDef(tree);
1679                }
1680                @Override
1681                public void visitVarDef(JCVariableDecl tree) {
1682                    if (tree.init != null && tree.init.type.constValue() == null)
1683                        tree.init = null;
1684                    super.visitVarDef(tree);
1685                }
1686                @Override
1687                public void visitClassDef(JCClassDecl tree) {
1688                    ListBuffer<JCTree> newdefs = new ListBuffer<>();
1689                    for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) {
1690                        JCTree t = it.head;
1691                        switch (t.getTag()) {
1692                        case CLASSDEF:
1693                            if (isInterface ||
1694                                (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1695                                (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1696                                newdefs.append(t);
1697                            break;
1698                        case METHODDEF:
1699                            if (isInterface ||
1700                                (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1701                                ((JCMethodDecl) t).sym.name == names.init ||
1702                                (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1703                                newdefs.append(t);
1704                            break;
1705                        case VARDEF:
1706                            if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1707                                (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1708                                newdefs.append(t);
1709                            break;
1710                        default:
1711                            break;
1712                        }
1713                    }
1714                    tree.defs = newdefs.toList();
1715                    super.visitClassDef(tree);
1716                }
1717            }
1718            MethodBodyRemover r = new MethodBodyRemover();
1719            return r.translate(cdef);
1720        }
1721
1722    public void reportDeferredDiagnostics() {
1723        if (errorCount() == 0
1724                && annotationProcessingOccurred
1725                && implicitSourceFilesRead
1726                && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
1727            if (explicitAnnotationProcessingRequested())
1728                log.warning("proc.use.implicit");
1729            else
1730                log.warning("proc.use.proc.or.implicit");
1731        }
1732        chk.reportDeferredDiagnostics();
1733        if (log.compressedOutput) {
1734            log.mandatoryNote(null, "compressed.diags");
1735        }
1736    }
1737
1738    public void enterDone() {
1739        enterDone = true;
1740        annotate.enterDone();
1741    }
1742
1743    public boolean isEnterDone() {
1744        return enterDone;
1745    }
1746
1747    /** Close the compiler, flushing the logs
1748     */
1749    public void close() {
1750        rootClasses = null;
1751        finder = null;
1752        reader = null;
1753        make = null;
1754        writer = null;
1755        enter = null;
1756        if (todo != null)
1757            todo.clear();
1758        todo = null;
1759        parserFactory = null;
1760        syms = null;
1761        source = null;
1762        attr = null;
1763        chk = null;
1764        gen = null;
1765        flow = null;
1766        transTypes = null;
1767        lower = null;
1768        annotate = null;
1769        types = null;
1770
1771        log.flush();
1772        try {
1773            fileManager.flush();
1774        } catch (IOException e) {
1775            throw new Abort(e);
1776        } finally {
1777            if (names != null)
1778                names.dispose();
1779            names = null;
1780
1781            for (Closeable c: closeables) {
1782                try {
1783                    c.close();
1784                } catch (IOException e) {
1785                    // When javac uses JDK 7 as a baseline, this code would be
1786                    // better written to set any/all exceptions from all the
1787                    // Closeables as suppressed exceptions on the FatalError
1788                    // that is thrown.
1789                    JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close");
1790                    throw new FatalError(msg, e);
1791                }
1792            }
1793            closeables = List.nil();
1794        }
1795    }
1796
1797    protected void printNote(String lines) {
1798        log.printRawLines(Log.WriterKind.NOTICE, lines);
1799    }
1800
1801    /** Print numbers of errors and warnings.
1802     */
1803    public void printCount(String kind, int count) {
1804        if (count != 0) {
1805            String key;
1806            if (count == 1)
1807                key = "count." + kind;
1808            else
1809                key = "count." + kind + ".plural";
1810            log.printLines(WriterKind.ERROR, key, String.valueOf(count));
1811            log.flush(Log.WriterKind.ERROR);
1812        }
1813    }
1814
1815    private static long now() {
1816        return System.currentTimeMillis();
1817    }
1818
1819    private static long elapsed(long then) {
1820        return now() - then;
1821    }
1822
1823    public void newRound() {
1824        inputFiles.clear();
1825        todo.clear();
1826    }
1827}
1828