JavaCompiler.java revision 3999:ae88ea1b7649
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, List.nil());
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     * @param addModules additional root modules to be used during
902     * module resolution.
903     */
904    public void compile(Collection<JavaFileObject> sourceFileObjects,
905                        Collection<String> classnames,
906                        Iterable<? extends Processor> processors,
907                        Collection<String> addModules)
908    {
909        if (!taskListener.isEmpty()) {
910            taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION));
911        }
912
913        if (processors != null && processors.iterator().hasNext())
914            explicitAnnotationProcessingRequested = true;
915        // as a JavaCompiler can only be used once, throw an exception if
916        // it has been used before.
917        if (hasBeenUsed)
918            checkReusable();
919        hasBeenUsed = true;
920
921        // forcibly set the equivalent of -Xlint:-options, so that no further
922        // warnings about command line options are generated from this point on
923        options.put(XLINT_CUSTOM.primaryName + "-" + LintCategory.OPTIONS.option, "true");
924        options.remove(XLINT_CUSTOM.primaryName + LintCategory.OPTIONS.option);
925
926        start_msec = now();
927
928        try {
929            initProcessAnnotations(processors, sourceFileObjects, classnames);
930
931            for (String className : classnames) {
932                int sep = className.indexOf('/');
933                if (sep != -1) {
934                    modules.addExtraAddModules(className.substring(0, sep));
935                }
936            }
937
938            for (String moduleName : addModules) {
939                modules.addExtraAddModules(moduleName);
940            }
941
942            // These method calls must be chained to avoid memory leaks
943            processAnnotations(
944                enterTrees(
945                        stopIfError(CompileState.PARSE,
946                                initModules(stopIfError(CompileState.PARSE, parseFiles(sourceFileObjects))))
947                ),
948                classnames
949            );
950
951            // If it's safe to do so, skip attr / flow / gen for implicit classes
952            if (taskListener.isEmpty() &&
953                    implicitSourcePolicy == ImplicitSourcePolicy.NONE) {
954                todo.retainFiles(inputFiles);
955            }
956
957            switch (compilePolicy) {
958            case ATTR_ONLY:
959                attribute(todo);
960                break;
961
962            case CHECK_ONLY:
963                flow(attribute(todo));
964                break;
965
966            case SIMPLE:
967                generate(desugar(flow(attribute(todo))));
968                break;
969
970            case BY_FILE: {
971                    Queue<Queue<Env<AttrContext>>> q = todo.groupByFile();
972                    while (!q.isEmpty() && !shouldStop(CompileState.ATTR)) {
973                        generate(desugar(flow(attribute(q.remove()))));
974                    }
975                }
976                break;
977
978            case BY_TODO:
979                while (!todo.isEmpty())
980                    generate(desugar(flow(attribute(todo.remove()))));
981                break;
982
983            default:
984                Assert.error("unknown compile policy");
985            }
986        } catch (Abort ex) {
987            if (devVerbose)
988                ex.printStackTrace(System.err);
989        } finally {
990            if (verbose) {
991                elapsed_msec = elapsed(start_msec);
992                log.printVerbose("total", Long.toString(elapsed_msec));
993            }
994
995            reportDeferredDiagnostics();
996
997            if (!log.hasDiagnosticListener()) {
998                printCount("error", errorCount());
999                printCount("warn", warningCount());
1000            }
1001            if (!taskListener.isEmpty()) {
1002                taskListener.finished(new TaskEvent(TaskEvent.Kind.COMPILATION));
1003            }
1004            close();
1005            if (procEnvImpl != null)
1006                procEnvImpl.close();
1007        }
1008    }
1009
1010    protected void checkReusable() {
1011        throw new AssertionError("attempt to reuse JavaCompiler");
1012    }
1013
1014    /**
1015     * The list of classes explicitly supplied on the command line for compilation.
1016     * Not always populated.
1017     */
1018    private List<JCClassDecl> rootClasses;
1019
1020    /**
1021     * Parses a list of files.
1022     */
1023   public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
1024       if (shouldStop(CompileState.PARSE))
1025           return List.nil();
1026
1027        //parse all files
1028        ListBuffer<JCCompilationUnit> trees = new ListBuffer<>();
1029        Set<JavaFileObject> filesSoFar = new HashSet<>();
1030        for (JavaFileObject fileObject : fileObjects) {
1031            if (!filesSoFar.contains(fileObject)) {
1032                filesSoFar.add(fileObject);
1033                trees.append(parse(fileObject));
1034            }
1035        }
1036        return trees.toList();
1037    }
1038
1039    /**
1040     * Enter the symbols found in a list of parse trees if the compilation
1041     * is expected to proceed beyond anno processing into attr.
1042     * As a side-effect, this puts elements on the "todo" list.
1043     * Also stores a list of all top level classes in rootClasses.
1044     */
1045    public List<JCCompilationUnit> enterTreesIfNeeded(List<JCCompilationUnit> roots) {
1046       if (shouldStop(CompileState.ATTR))
1047           return List.nil();
1048        return enterTrees(initModules(roots));
1049    }
1050
1051    public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {
1052        modules.initModules(roots);
1053        if (roots.isEmpty()) {
1054            enterDone();
1055        }
1056        return roots;
1057    }
1058
1059    /**
1060     * Enter the symbols found in a list of parse trees.
1061     * As a side-effect, this puts elements on the "todo" list.
1062     * Also stores a list of all top level classes in rootClasses.
1063     */
1064    public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
1065        //enter symbols for all files
1066        if (!taskListener.isEmpty()) {
1067            for (JCCompilationUnit unit: roots) {
1068                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1069                taskListener.started(e);
1070            }
1071        }
1072
1073        enter.main(roots);
1074
1075        enterDone();
1076
1077        if (!taskListener.isEmpty()) {
1078            for (JCCompilationUnit unit: roots) {
1079                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
1080                taskListener.finished(e);
1081            }
1082        }
1083
1084        // If generating source, or if tracking public apis,
1085        // then remember the classes declared in
1086        // the original compilation units listed on the command line.
1087        if (sourceOutput) {
1088            ListBuffer<JCClassDecl> cdefs = new ListBuffer<>();
1089            for (JCCompilationUnit unit : roots) {
1090                for (List<JCTree> defs = unit.defs;
1091                     defs.nonEmpty();
1092                     defs = defs.tail) {
1093                    if (defs.head instanceof JCClassDecl)
1094                        cdefs.append((JCClassDecl)defs.head);
1095                }
1096            }
1097            rootClasses = cdefs.toList();
1098        }
1099
1100        // Ensure the input files have been recorded. Although this is normally
1101        // done by readSource, it may not have been done if the trees were read
1102        // in a prior round of annotation processing, and the trees have been
1103        // cleaned and are being reused.
1104        for (JCCompilationUnit unit : roots) {
1105            inputFiles.add(unit.sourcefile);
1106        }
1107
1108        return roots;
1109    }
1110
1111    /**
1112     * Set to true to enable skeleton annotation processing code.
1113     * Currently, we assume this variable will be replaced more
1114     * advanced logic to figure out if annotation processing is
1115     * needed.
1116     */
1117    boolean processAnnotations = false;
1118
1119    Log.DeferredDiagnosticHandler deferredDiagnosticHandler;
1120
1121    /**
1122     * Object to handle annotation processing.
1123     */
1124    private JavacProcessingEnvironment procEnvImpl = null;
1125
1126    /**
1127     * Check if we should process annotations.
1128     * If so, and if no scanner is yet registered, then set up the DocCommentScanner
1129     * to catch doc comments, and set keepComments so the parser records them in
1130     * the compilation unit.
1131     *
1132     * @param processors user provided annotation processors to bypass
1133     * discovery, {@code null} means that no processors were provided
1134     */
1135    public void initProcessAnnotations(Iterable<? extends Processor> processors,
1136                                       Collection<? extends JavaFileObject> initialFiles,
1137                                       Collection<String> initialClassNames) {
1138        // Process annotations if processing is not disabled and there
1139        // is at least one Processor available.
1140        if (options.isSet(PROC, "none")) {
1141            processAnnotations = false;
1142        } else if (procEnvImpl == null) {
1143            procEnvImpl = JavacProcessingEnvironment.instance(context);
1144            procEnvImpl.setProcessors(processors);
1145            processAnnotations = procEnvImpl.atLeastOneProcessor();
1146
1147            if (processAnnotations) {
1148                options.put("parameters", "parameters");
1149                reader.saveParameterNames = true;
1150                keepComments = true;
1151                genEndPos = true;
1152                if (!taskListener.isEmpty())
1153                    taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
1154                deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
1155                procEnvImpl.getFiler().setInitialState(initialFiles, initialClassNames);
1156            } else { // free resources
1157                procEnvImpl.close();
1158            }
1159        }
1160    }
1161
1162    // TODO: called by JavacTaskImpl
1163    public void processAnnotations(List<JCCompilationUnit> roots) {
1164        processAnnotations(roots, List.nil());
1165    }
1166
1167    /**
1168     * Process any annotations found in the specified compilation units.
1169     * @param roots a list of compilation units
1170     */
1171    // Implementation note: when this method is called, log.deferredDiagnostics
1172    // will have been set true by initProcessAnnotations, meaning that any diagnostics
1173    // that are reported will go into the log.deferredDiagnostics queue.
1174    // By the time this method exits, log.deferDiagnostics must be set back to false,
1175    // and all deferredDiagnostics must have been handled: i.e. either reported
1176    // or determined to be transient, and therefore suppressed.
1177    public void processAnnotations(List<JCCompilationUnit> roots,
1178                                   Collection<String> classnames) {
1179        if (shouldStop(CompileState.PROCESS)) {
1180            // Errors were encountered.
1181            // Unless all the errors are resolve errors, the errors were parse errors
1182            // or other errors during enter which cannot be fixed by running
1183            // any annotation processors.
1184            if (unrecoverableError()) {
1185                deferredDiagnosticHandler.reportDeferredDiagnostics();
1186                log.popDiagnosticHandler(deferredDiagnosticHandler);
1187                return ;
1188            }
1189        }
1190
1191        // ASSERT: processAnnotations and procEnvImpl should have been set up by
1192        // by initProcessAnnotations
1193
1194        // NOTE: The !classnames.isEmpty() checks should be refactored to Main.
1195
1196        if (!processAnnotations) {
1197            // If there are no annotation processors present, and
1198            // annotation processing is to occur with compilation,
1199            // emit a warning.
1200            if (options.isSet(PROC, "only")) {
1201                log.warning("proc.proc-only.requested.no.procs");
1202                todo.clear();
1203            }
1204            // If not processing annotations, classnames must be empty
1205            if (!classnames.isEmpty()) {
1206                log.error("proc.no.explicit.annotation.processing.requested",
1207                          classnames);
1208            }
1209            Assert.checkNull(deferredDiagnosticHandler);
1210            return ; // continue regular compilation
1211        }
1212
1213        Assert.checkNonNull(deferredDiagnosticHandler);
1214
1215        try {
1216            List<ClassSymbol> classSymbols = List.nil();
1217            List<PackageSymbol> pckSymbols = List.nil();
1218            if (!classnames.isEmpty()) {
1219                 // Check for explicit request for annotation
1220                 // processing
1221                if (!explicitAnnotationProcessingRequested()) {
1222                    log.error("proc.no.explicit.annotation.processing.requested",
1223                              classnames);
1224                    deferredDiagnosticHandler.reportDeferredDiagnostics();
1225                    log.popDiagnosticHandler(deferredDiagnosticHandler);
1226                    return ; // TODO: Will this halt compilation?
1227                } else {
1228                    boolean errors = false;
1229                    for (String nameStr : classnames) {
1230                        Symbol sym = resolveBinaryNameOrIdent(nameStr);
1231                        if (sym == null ||
1232                            (sym.kind == PCK && !processPcks) ||
1233                            sym.kind == ABSENT_TYP) {
1234                            if (sym != silentFail)
1235                                log.error(Errors.ProcCantFindClass(nameStr));
1236                            errors = true;
1237                            continue;
1238                        }
1239                        try {
1240                            if (sym.kind == PCK)
1241                                sym.complete();
1242                            if (sym.exists()) {
1243                                if (sym.kind == PCK)
1244                                    pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
1245                                else
1246                                    classSymbols = classSymbols.prepend((ClassSymbol)sym);
1247                                continue;
1248                            }
1249                            Assert.check(sym.kind == PCK);
1250                            log.warning(Warnings.ProcPackageDoesNotExist(nameStr));
1251                            pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
1252                        } catch (CompletionFailure e) {
1253                            log.error(Errors.ProcCantFindClass(nameStr));
1254                            errors = true;
1255                            continue;
1256                        }
1257                    }
1258                    if (errors) {
1259                        deferredDiagnosticHandler.reportDeferredDiagnostics();
1260                        log.popDiagnosticHandler(deferredDiagnosticHandler);
1261                        return ;
1262                    }
1263                }
1264            }
1265            try {
1266                annotationProcessingOccurred =
1267                        procEnvImpl.doProcessing(roots,
1268                                                 classSymbols,
1269                                                 pckSymbols,
1270                                                 deferredDiagnosticHandler);
1271                // doProcessing will have handled deferred diagnostics
1272            } finally {
1273                procEnvImpl.close();
1274            }
1275        } catch (CompletionFailure ex) {
1276            log.error("cant.access", ex.sym, ex.getDetailValue());
1277            if (deferredDiagnosticHandler != null) {
1278                deferredDiagnosticHandler.reportDeferredDiagnostics();
1279                log.popDiagnosticHandler(deferredDiagnosticHandler);
1280            }
1281        }
1282    }
1283
1284    private boolean unrecoverableError() {
1285        if (deferredDiagnosticHandler != null) {
1286            for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) {
1287                if (d.getKind() == JCDiagnostic.Kind.ERROR && !d.isFlagSet(RECOVERABLE))
1288                    return true;
1289            }
1290        }
1291        return false;
1292    }
1293
1294    boolean explicitAnnotationProcessingRequested() {
1295        return
1296            explicitAnnotationProcessingRequested ||
1297            explicitAnnotationProcessingRequested(options);
1298    }
1299
1300    static boolean explicitAnnotationProcessingRequested(Options options) {
1301        return
1302            options.isSet(PROCESSOR) ||
1303            options.isSet(PROCESSOR_PATH) ||
1304            options.isSet(PROCESSOR_MODULE_PATH) ||
1305            options.isSet(PROC, "only") ||
1306            options.isSet(XPRINT);
1307    }
1308
1309    public void setDeferredDiagnosticHandler(Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
1310        this.deferredDiagnosticHandler = deferredDiagnosticHandler;
1311    }
1312
1313    /**
1314     * Attribute a list of parse trees, such as found on the "todo" list.
1315     * Note that attributing classes may cause additional files to be
1316     * parsed and entered via the SourceCompleter.
1317     * Attribution of the entries in the list does not stop if any errors occur.
1318     * @return a list of environments for attribute classes.
1319     */
1320    public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) {
1321        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
1322        while (!envs.isEmpty())
1323            results.append(attribute(envs.remove()));
1324        return stopIfError(CompileState.ATTR, results);
1325    }
1326
1327    /**
1328     * Attribute a parse tree.
1329     * @return the attributed parse tree
1330     */
1331    public Env<AttrContext> attribute(Env<AttrContext> env) {
1332        if (compileStates.isDone(env, CompileState.ATTR))
1333            return env;
1334
1335        if (verboseCompilePolicy)
1336            printNote("[attribute " + env.enclClass.sym + "]");
1337        if (verbose)
1338            log.printVerbose("checking.attribution", env.enclClass.sym);
1339
1340        if (!taskListener.isEmpty()) {
1341            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
1342            taskListener.started(e);
1343        }
1344
1345        JavaFileObject prev = log.useSource(
1346                                  env.enclClass.sym.sourcefile != null ?
1347                                  env.enclClass.sym.sourcefile :
1348                                  env.toplevel.sourcefile);
1349        try {
1350            attr.attrib(env);
1351            if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) {
1352                //if in fail-over mode, ensure that AST expression nodes
1353                //are correctly initialized (e.g. they have a type/symbol)
1354                attr.postAttr(env.tree);
1355            }
1356            compileStates.put(env, CompileState.ATTR);
1357        }
1358        finally {
1359            log.useSource(prev);
1360        }
1361
1362        return env;
1363    }
1364
1365    /**
1366     * Perform dataflow checks on attributed parse trees.
1367     * These include checks for definite assignment and unreachable statements.
1368     * If any errors occur, an empty list will be returned.
1369     * @return the list of attributed parse trees
1370     */
1371    public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) {
1372        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
1373        for (Env<AttrContext> env: envs) {
1374            flow(env, results);
1375        }
1376        return stopIfError(CompileState.FLOW, results);
1377    }
1378
1379    /**
1380     * Perform dataflow checks on an attributed parse tree.
1381     */
1382    public Queue<Env<AttrContext>> flow(Env<AttrContext> env) {
1383        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
1384        flow(env, results);
1385        return stopIfError(CompileState.FLOW, results);
1386    }
1387
1388    /**
1389     * Perform dataflow checks on an attributed parse tree.
1390     */
1391    protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
1392        if (compileStates.isDone(env, CompileState.FLOW)) {
1393            results.add(env);
1394            return;
1395        }
1396
1397        try {
1398            if (shouldStop(CompileState.FLOW))
1399                return;
1400
1401            if (verboseCompilePolicy)
1402                printNote("[flow " + env.enclClass.sym + "]");
1403            JavaFileObject prev = log.useSource(
1404                                                env.enclClass.sym.sourcefile != null ?
1405                                                env.enclClass.sym.sourcefile :
1406                                                env.toplevel.sourcefile);
1407            try {
1408                make.at(Position.FIRSTPOS);
1409                TreeMaker localMake = make.forToplevel(env.toplevel);
1410                flow.analyzeTree(env, localMake);
1411                compileStates.put(env, CompileState.FLOW);
1412
1413                if (shouldStop(CompileState.FLOW))
1414                    return;
1415
1416                results.add(env);
1417            }
1418            finally {
1419                log.useSource(prev);
1420            }
1421        }
1422        finally {
1423            if (!taskListener.isEmpty()) {
1424                TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
1425                taskListener.finished(e);
1426            }
1427        }
1428    }
1429
1430    /**
1431     * Prepare attributed parse trees, in conjunction with their attribution contexts,
1432     * for source or code generation.
1433     * If any errors occur, an empty list will be returned.
1434     * @return a list containing the classes to be generated
1435     */
1436    public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) {
1437        ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = new ListBuffer<>();
1438        for (Env<AttrContext> env: envs)
1439            desugar(env, results);
1440        return stopIfError(CompileState.FLOW, results);
1441    }
1442
1443    HashMap<Env<AttrContext>, Queue<Pair<Env<AttrContext>, JCClassDecl>>> desugaredEnvs = new HashMap<>();
1444
1445    /**
1446     * Prepare attributed parse trees, in conjunction with their attribution contexts,
1447     * for source or code generation. If the file was not listed on the command line,
1448     * the current implicitSourcePolicy is taken into account.
1449     * The preparation stops as soon as an error is found.
1450     */
1451    protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
1452        if (shouldStop(CompileState.TRANSTYPES))
1453            return;
1454
1455        if (implicitSourcePolicy == ImplicitSourcePolicy.NONE
1456                && !inputFiles.contains(env.toplevel.sourcefile)) {
1457            return;
1458        }
1459
1460        if (!modules.multiModuleMode && env.toplevel.modle != modules.getDefaultModule()) {
1461            //can only generate classfiles for a single module:
1462            return;
1463        }
1464
1465        if (compileStates.isDone(env, CompileState.LOWER)) {
1466            results.addAll(desugaredEnvs.get(env));
1467            return;
1468        }
1469
1470        /**
1471         * Ensure that superclasses of C are desugared before C itself. This is
1472         * required for two reasons: (i) as erasure (TransTypes) destroys
1473         * information needed in flow analysis and (ii) as some checks carried
1474         * out during lowering require that all synthetic fields/methods have
1475         * already been added to C and its superclasses.
1476         */
1477        class ScanNested extends TreeScanner {
1478            Set<Env<AttrContext>> dependencies = new LinkedHashSet<>();
1479            protected boolean hasLambdas;
1480            @Override
1481            public void visitClassDef(JCClassDecl node) {
1482                Type st = types.supertype(node.sym.type);
1483                boolean envForSuperTypeFound = false;
1484                while (!envForSuperTypeFound && st.hasTag(CLASS)) {
1485                    ClassSymbol c = st.tsym.outermostClass();
1486                    Env<AttrContext> stEnv = enter.getEnv(c);
1487                    if (stEnv != null && env != stEnv) {
1488                        if (dependencies.add(stEnv)) {
1489                            boolean prevHasLambdas = hasLambdas;
1490                            try {
1491                                scan(stEnv.tree);
1492                            } finally {
1493                                /*
1494                                 * ignore any updates to hasLambdas made during
1495                                 * the nested scan, this ensures an initalized
1496                                 * LambdaToMethod is available only to those
1497                                 * classes that contain lambdas
1498                                 */
1499                                hasLambdas = prevHasLambdas;
1500                            }
1501                        }
1502                        envForSuperTypeFound = true;
1503                    }
1504                    st = types.supertype(st);
1505                }
1506                super.visitClassDef(node);
1507            }
1508            @Override
1509            public void visitLambda(JCLambda tree) {
1510                hasLambdas = true;
1511                super.visitLambda(tree);
1512            }
1513            @Override
1514            public void visitReference(JCMemberReference tree) {
1515                hasLambdas = true;
1516                super.visitReference(tree);
1517            }
1518        }
1519        ScanNested scanner = new ScanNested();
1520        scanner.scan(env.tree);
1521        for (Env<AttrContext> dep: scanner.dependencies) {
1522        if (!compileStates.isDone(dep, CompileState.FLOW))
1523            desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
1524        }
1525
1526        //We need to check for error another time as more classes might
1527        //have been attributed and analyzed at this stage
1528        if (shouldStop(CompileState.TRANSTYPES))
1529            return;
1530
1531        if (verboseCompilePolicy)
1532            printNote("[desugar " + env.enclClass.sym + "]");
1533
1534        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1535                                  env.enclClass.sym.sourcefile :
1536                                  env.toplevel.sourcefile);
1537        try {
1538            //save tree prior to rewriting
1539            JCTree untranslated = env.tree;
1540
1541            make.at(Position.FIRSTPOS);
1542            TreeMaker localMake = make.forToplevel(env.toplevel);
1543
1544            if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1545                if (!(sourceOutput)) {
1546                    if (shouldStop(CompileState.LOWER))
1547                        return;
1548                    List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1549                    if (def.head != null) {
1550                        Assert.check(def.tail.isEmpty());
1551                        results.add(new Pair<>(env, (JCClassDecl)def.head));
1552                    }
1553                }
1554                return;
1555            }
1556
1557            if (shouldStop(CompileState.TRANSTYPES))
1558                return;
1559
1560            env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1561            compileStates.put(env, CompileState.TRANSTYPES);
1562
1563            if (source.allowLambda() && scanner.hasLambdas) {
1564                if (shouldStop(CompileState.UNLAMBDA))
1565                    return;
1566
1567                env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
1568                compileStates.put(env, CompileState.UNLAMBDA);
1569            }
1570
1571            if (shouldStop(CompileState.LOWER))
1572                return;
1573
1574            if (sourceOutput) {
1575                //emit standard Java source file, only for compilation
1576                //units enumerated explicitly on the command line
1577                JCClassDecl cdef = (JCClassDecl)env.tree;
1578                if (untranslated instanceof JCClassDecl &&
1579                    rootClasses.contains((JCClassDecl)untranslated)) {
1580                    results.add(new Pair<>(env, cdef));
1581                }
1582                return;
1583            }
1584
1585            //translate out inner classes
1586            List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
1587            compileStates.put(env, CompileState.LOWER);
1588
1589            if (shouldStop(CompileState.LOWER))
1590                return;
1591
1592            //generate code for each class
1593            for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1594                JCClassDecl cdef = (JCClassDecl)l.head;
1595                results.add(new Pair<>(env, cdef));
1596            }
1597        }
1598        finally {
1599            log.useSource(prev);
1600        }
1601
1602    }
1603
1604    /** Generates the source or class file for a list of classes.
1605     * The decision to generate a source file or a class file is
1606     * based upon the compiler's options.
1607     * Generation stops if an error occurs while writing files.
1608     */
1609    public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1610        generate(queue, null);
1611    }
1612
1613    public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1614        if (shouldStop(CompileState.GENERATE))
1615            return;
1616
1617        for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1618            Env<AttrContext> env = x.fst;
1619            JCClassDecl cdef = x.snd;
1620
1621            if (verboseCompilePolicy) {
1622                printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1623            }
1624
1625            if (!taskListener.isEmpty()) {
1626                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
1627                taskListener.started(e);
1628            }
1629
1630            JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1631                                      env.enclClass.sym.sourcefile :
1632                                      env.toplevel.sourcefile);
1633            try {
1634                JavaFileObject file;
1635                if (sourceOutput) {
1636                    file = printSource(env, cdef);
1637                } else {
1638                    if (fileManager.hasLocation(StandardLocation.NATIVE_HEADER_OUTPUT)
1639                            && jniWriter.needsHeader(cdef.sym)) {
1640                        jniWriter.write(cdef.sym);
1641                    }
1642                    file = genCode(env, cdef);
1643                }
1644                if (results != null && file != null)
1645                    results.add(file);
1646            } catch (IOException ex) {
1647                log.error(cdef.pos(), "class.cant.write",
1648                          cdef.sym, ex.getMessage());
1649                return;
1650            } finally {
1651                log.useSource(prev);
1652            }
1653
1654            if (!taskListener.isEmpty()) {
1655                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
1656                taskListener.finished(e);
1657            }
1658        }
1659    }
1660
1661        // where
1662        Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) {
1663            // use a LinkedHashMap to preserve the order of the original list as much as possible
1664            Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<>();
1665            for (Env<AttrContext> env: envs) {
1666                Queue<Env<AttrContext>> sublist = map.get(env.toplevel);
1667                if (sublist == null) {
1668                    sublist = new ListBuffer<>();
1669                    map.put(env.toplevel, sublist);
1670                }
1671                sublist.add(env);
1672            }
1673            return map;
1674        }
1675
1676        JCClassDecl removeMethodBodies(JCClassDecl cdef) {
1677            final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
1678            class MethodBodyRemover extends TreeTranslator {
1679                @Override
1680                public void visitMethodDef(JCMethodDecl tree) {
1681                    tree.mods.flags &= ~Flags.SYNCHRONIZED;
1682                    for (JCVariableDecl vd : tree.params)
1683                        vd.mods.flags &= ~Flags.FINAL;
1684                    tree.body = null;
1685                    super.visitMethodDef(tree);
1686                }
1687                @Override
1688                public void visitVarDef(JCVariableDecl tree) {
1689                    if (tree.init != null && tree.init.type.constValue() == null)
1690                        tree.init = null;
1691                    super.visitVarDef(tree);
1692                }
1693                @Override
1694                public void visitClassDef(JCClassDecl tree) {
1695                    ListBuffer<JCTree> newdefs = new ListBuffer<>();
1696                    for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) {
1697                        JCTree t = it.head;
1698                        switch (t.getTag()) {
1699                        case CLASSDEF:
1700                            if (isInterface ||
1701                                (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1702                                (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1703                                newdefs.append(t);
1704                            break;
1705                        case METHODDEF:
1706                            if (isInterface ||
1707                                (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1708                                ((JCMethodDecl) t).sym.name == names.init ||
1709                                (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1710                                newdefs.append(t);
1711                            break;
1712                        case VARDEF:
1713                            if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1714                                (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1715                                newdefs.append(t);
1716                            break;
1717                        default:
1718                            break;
1719                        }
1720                    }
1721                    tree.defs = newdefs.toList();
1722                    super.visitClassDef(tree);
1723                }
1724            }
1725            MethodBodyRemover r = new MethodBodyRemover();
1726            return r.translate(cdef);
1727        }
1728
1729    public void reportDeferredDiagnostics() {
1730        if (errorCount() == 0
1731                && annotationProcessingOccurred
1732                && implicitSourceFilesRead
1733                && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
1734            if (explicitAnnotationProcessingRequested())
1735                log.warning("proc.use.implicit");
1736            else
1737                log.warning("proc.use.proc.or.implicit");
1738        }
1739        chk.reportDeferredDiagnostics();
1740        if (log.compressedOutput) {
1741            log.mandatoryNote(null, "compressed.diags");
1742        }
1743    }
1744
1745    public void enterDone() {
1746        enterDone = true;
1747        annotate.enterDone();
1748    }
1749
1750    public boolean isEnterDone() {
1751        return enterDone;
1752    }
1753
1754    /** Close the compiler, flushing the logs
1755     */
1756    public void close() {
1757        rootClasses = null;
1758        finder = null;
1759        reader = null;
1760        make = null;
1761        writer = null;
1762        enter = null;
1763        if (todo != null)
1764            todo.clear();
1765        todo = null;
1766        parserFactory = null;
1767        syms = null;
1768        source = null;
1769        attr = null;
1770        chk = null;
1771        gen = null;
1772        flow = null;
1773        transTypes = null;
1774        lower = null;
1775        annotate = null;
1776        types = null;
1777
1778        log.flush();
1779        try {
1780            fileManager.flush();
1781        } catch (IOException e) {
1782            throw new Abort(e);
1783        } finally {
1784            if (names != null)
1785                names.dispose();
1786            names = null;
1787
1788            for (Closeable c: closeables) {
1789                try {
1790                    c.close();
1791                } catch (IOException e) {
1792                    // When javac uses JDK 7 as a baseline, this code would be
1793                    // better written to set any/all exceptions from all the
1794                    // Closeables as suppressed exceptions on the FatalError
1795                    // that is thrown.
1796                    JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close");
1797                    throw new FatalError(msg, e);
1798                }
1799            }
1800            closeables = List.nil();
1801        }
1802    }
1803
1804    protected void printNote(String lines) {
1805        log.printRawLines(Log.WriterKind.NOTICE, lines);
1806    }
1807
1808    /** Print numbers of errors and warnings.
1809     */
1810    public void printCount(String kind, int count) {
1811        if (count != 0) {
1812            String key;
1813            if (count == 1)
1814                key = "count." + kind;
1815            else
1816                key = "count." + kind + ".plural";
1817            log.printLines(WriterKind.ERROR, key, String.valueOf(count));
1818            log.flush(Log.WriterKind.ERROR);
1819        }
1820    }
1821
1822    private static long now() {
1823        return System.currentTimeMillis();
1824    }
1825
1826    private static long elapsed(long then) {
1827        return now() - then;
1828    }
1829
1830    public void newRound() {
1831        inputFiles.clear();
1832        todo.clear();
1833    }
1834}
1835