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 
  26 package com.sun.tools.javac.main;
  27 
  28 import java.io.*;
  29 import java.util.Collection;
  30 import java.util.Collections;
  31 import java.util.HashMap;
  32 import java.util.HashSet;
  33 import java.util.LinkedHashMap;
  34 import java.util.LinkedHashSet;
  35 import java.util.Map;
  36 import java.util.MissingResourceException;
  37 import java.util.Queue;
  38 import java.util.ResourceBundle;
  39 import java.util.Set;
  40 import java.util.function.Function;
  41 
  42 import javax.annotation.processing.Processor;
  43 import javax.lang.model.SourceVersion;
  44 import javax.lang.model.element.ElementVisitor;
  45 import javax.tools.DiagnosticListener;
  46 import javax.tools.JavaFileManager;
  47 import javax.tools.JavaFileObject;
  48 import javax.tools.JavaFileObject.Kind;
  49 import javax.tools.StandardLocation;
  50 
  51 import com.sun.source.util.TaskEvent;
  52 import com.sun.tools.javac.api.MultiTaskListener;
  53 import com.sun.tools.javac.code.*;
  54 import com.sun.tools.javac.code.Lint.LintCategory;
  55 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  56 import com.sun.tools.javac.code.Symbol.CompletionFailure;
  57 import com.sun.tools.javac.code.Symbol.PackageSymbol;
  58 import com.sun.tools.javac.comp.*;
  59 import com.sun.tools.javac.comp.CompileStates.CompileState;
  60 import com.sun.tools.javac.file.JavacFileManager;
  61 import com.sun.tools.javac.jvm.*;
  62 import com.sun.tools.javac.parser.*;
  63 import com.sun.tools.javac.platform.PlatformDescription;
  64 import com.sun.tools.javac.processing.*;
  65 import com.sun.tools.javac.tree.*;
  66 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  67 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  68 import com.sun.tools.javac.tree.JCTree.JCExpression;
  69 import com.sun.tools.javac.tree.JCTree.JCLambda;
  70 import com.sun.tools.javac.tree.JCTree.JCMemberReference;
  71 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
  72 import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
  73 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
  74 import com.sun.tools.javac.tree.JCTree.Tag;
  75 import com.sun.tools.javac.util.*;
  76 import com.sun.tools.javac.util.DefinedBy.Api;
  77 import com.sun.tools.javac.util.JCDiagnostic.Factory;
  78 import com.sun.tools.javac.util.Log.WriterKind;
  79 
  80 import static com.sun.tools.javac.code.Kinds.Kind.*;
  81 
  82 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  83 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  84 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  85 
  86 import static com.sun.tools.javac.code.TypeTag.CLASS;
  87 import static com.sun.tools.javac.main.Option.*;
  88 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
  89 
  90 import 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  */
 102 public 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 (compileStates.isDone(env, CompileState.LOWER)) {
1454             results.addAll(desugaredEnvs.get(env));
1455             return;
1456         }
1457 
1458         /**
1459          * Ensure that superclasses of C are desugared before C itself. This is
1460          * required for two reasons: (i) as erasure (TransTypes) destroys
1461          * information needed in flow analysis and (ii) as some checks carried
1462          * out during lowering require that all synthetic fields/methods have
1463          * already been added to C and its superclasses.
1464          */
1465         class ScanNested extends TreeScanner {
1466             Set<Env<AttrContext>> dependencies = new LinkedHashSet<>();
1467             protected boolean hasLambdas;
1468             @Override
1469             public void visitClassDef(JCClassDecl node) {
1470                 Type st = types.supertype(node.sym.type);
1471                 boolean envForSuperTypeFound = false;
1472                 while (!envForSuperTypeFound && st.hasTag(CLASS)) {
1473                     ClassSymbol c = st.tsym.outermostClass();
1474                     Env<AttrContext> stEnv = enter.getEnv(c);
1475                     if (stEnv != null && env != stEnv) {
1476                         if (dependencies.add(stEnv)) {
1477                             boolean prevHasLambdas = hasLambdas;
1478                             try {
1479                                 scan(stEnv.tree);
1480                             } finally {
1481                                 /*
1482                                  * ignore any updates to hasLambdas made during
1483                                  * the nested scan, this ensures an initalized
1484                                  * LambdaToMethod is available only to those
1485                                  * classes that contain lambdas
1486                                  */
1487                                 hasLambdas = prevHasLambdas;
1488                             }
1489                         }
1490                         envForSuperTypeFound = true;
1491                     }
1492                     st = types.supertype(st);
1493                 }
1494                 super.visitClassDef(node);
1495             }
1496             @Override
1497             public void visitLambda(JCLambda tree) {
1498                 hasLambdas = true;
1499                 super.visitLambda(tree);
1500             }
1501             @Override
1502             public void visitReference(JCMemberReference tree) {
1503                 hasLambdas = true;
1504                 super.visitReference(tree);
1505             }
1506         }
1507         ScanNested scanner = new ScanNested();
1508         scanner.scan(env.tree);
1509         for (Env<AttrContext> dep: scanner.dependencies) {
1510         if (!compileStates.isDone(dep, CompileState.FLOW))
1511             desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
1512         }
1513 
1514         //We need to check for error another time as more classes might
1515         //have been attributed and analyzed at this stage
1516         if (shouldStop(CompileState.TRANSTYPES))
1517             return;
1518 
1519         if (verboseCompilePolicy)
1520             printNote("[desugar " + env.enclClass.sym + "]");
1521 
1522         JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1523                                   env.enclClass.sym.sourcefile :
1524                                   env.toplevel.sourcefile);
1525         try {
1526             //save tree prior to rewriting
1527             JCTree untranslated = env.tree;
1528 
1529             make.at(Position.FIRSTPOS);
1530             TreeMaker localMake = make.forToplevel(env.toplevel);
1531 
1532             if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
1533                 if (!(sourceOutput)) {
1534                     if (shouldStop(CompileState.LOWER))
1535                         return;
1536                     List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
1537                     if (def.head != null) {
1538                         Assert.check(def.tail.isEmpty());
1539                         results.add(new Pair<>(env, (JCClassDecl)def.head));
1540                     }
1541                 }
1542                 return;
1543             }
1544 
1545             if (shouldStop(CompileState.TRANSTYPES))
1546                 return;
1547 
1548             env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
1549             compileStates.put(env, CompileState.TRANSTYPES);
1550 
1551             if (source.allowLambda() && scanner.hasLambdas) {
1552                 if (shouldStop(CompileState.UNLAMBDA))
1553                     return;
1554 
1555                 env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
1556                 compileStates.put(env, CompileState.UNLAMBDA);
1557             }
1558 
1559             if (shouldStop(CompileState.LOWER))
1560                 return;
1561 
1562             if (sourceOutput) {
1563                 //emit standard Java source file, only for compilation
1564                 //units enumerated explicitly on the command line
1565                 JCClassDecl cdef = (JCClassDecl)env.tree;
1566                 if (untranslated instanceof JCClassDecl &&
1567                     rootClasses.contains((JCClassDecl)untranslated)) {
1568                     results.add(new Pair<>(env, cdef));
1569                 }
1570                 return;
1571             }
1572 
1573             //translate out inner classes
1574             List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
1575             compileStates.put(env, CompileState.LOWER);
1576 
1577             if (shouldStop(CompileState.LOWER))
1578                 return;
1579 
1580             //generate code for each class
1581             for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
1582                 JCClassDecl cdef = (JCClassDecl)l.head;
1583                 results.add(new Pair<>(env, cdef));
1584             }
1585         }
1586         finally {
1587             log.useSource(prev);
1588         }
1589 
1590     }
1591 
1592     /** Generates the source or class file for a list of classes.
1593      * The decision to generate a source file or a class file is
1594      * based upon the compiler's options.
1595      * Generation stops if an error occurs while writing files.
1596      */
1597     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
1598         generate(queue, null);
1599     }
1600 
1601     public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
1602         if (shouldStop(CompileState.GENERATE))
1603             return;
1604 
1605         for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
1606             Env<AttrContext> env = x.fst;
1607             JCClassDecl cdef = x.snd;
1608 
1609             if (verboseCompilePolicy) {
1610                 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
1611             }
1612 
1613             if (!taskListener.isEmpty()) {
1614                 TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
1615                 taskListener.started(e);
1616             }
1617 
1618             JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
1619                                       env.enclClass.sym.sourcefile :
1620                                       env.toplevel.sourcefile);
1621             try {
1622                 JavaFileObject file;
1623                 if (sourceOutput) {
1624                     file = printSource(env, cdef);
1625                 } else {
1626                     if (fileManager.hasLocation(StandardLocation.NATIVE_HEADER_OUTPUT)
1627                             && jniWriter.needsHeader(cdef.sym)) {
1628                         jniWriter.write(cdef.sym);
1629                     }
1630                     file = genCode(env, cdef);
1631                 }
1632                 if (results != null && file != null)
1633                     results.add(file);
1634             } catch (IOException ex) {
1635                 log.error(cdef.pos(), "class.cant.write",
1636                           cdef.sym, ex.getMessage());
1637                 return;
1638             } finally {
1639                 log.useSource(prev);
1640             }
1641 
1642             if (!taskListener.isEmpty()) {
1643                 TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
1644                 taskListener.finished(e);
1645             }
1646         }
1647     }
1648 
1649         // where
1650         Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) {
1651             // use a LinkedHashMap to preserve the order of the original list as much as possible
1652             Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<>();
1653             for (Env<AttrContext> env: envs) {
1654                 Queue<Env<AttrContext>> sublist = map.get(env.toplevel);
1655                 if (sublist == null) {
1656                     sublist = new ListBuffer<>();
1657                     map.put(env.toplevel, sublist);
1658                 }
1659                 sublist.add(env);
1660             }
1661             return map;
1662         }
1663 
1664         JCClassDecl removeMethodBodies(JCClassDecl cdef) {
1665             final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
1666             class MethodBodyRemover extends TreeTranslator {
1667                 @Override
1668                 public void visitMethodDef(JCMethodDecl tree) {
1669                     tree.mods.flags &= ~Flags.SYNCHRONIZED;
1670                     for (JCVariableDecl vd : tree.params)
1671                         vd.mods.flags &= ~Flags.FINAL;
1672                     tree.body = null;
1673                     super.visitMethodDef(tree);
1674                 }
1675                 @Override
1676                 public void visitVarDef(JCVariableDecl tree) {
1677                     if (tree.init != null && tree.init.type.constValue() == null)
1678                         tree.init = null;
1679                     super.visitVarDef(tree);
1680                 }
1681                 @Override
1682                 public void visitClassDef(JCClassDecl tree) {
1683                     ListBuffer<JCTree> newdefs = new ListBuffer<>();
1684                     for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) {
1685                         JCTree t = it.head;
1686                         switch (t.getTag()) {
1687                         case CLASSDEF:
1688                             if (isInterface ||
1689                                 (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1690                                 (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1691                                 newdefs.append(t);
1692                             break;
1693                         case METHODDEF:
1694                             if (isInterface ||
1695                                 (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1696                                 ((JCMethodDecl) t).sym.name == names.init ||
1697                                 (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1698                                 newdefs.append(t);
1699                             break;
1700                         case VARDEF:
1701                             if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
1702                                 (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang)
1703                                 newdefs.append(t);
1704                             break;
1705                         default:
1706                             break;
1707                         }
1708                     }
1709                     tree.defs = newdefs.toList();
1710                     super.visitClassDef(tree);
1711                 }
1712             }
1713             MethodBodyRemover r = new MethodBodyRemover();
1714             return r.translate(cdef);
1715         }
1716 
1717     public void reportDeferredDiagnostics() {
1718         if (errorCount() == 0
1719                 && annotationProcessingOccurred
1720                 && implicitSourceFilesRead
1721                 && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
1722             if (explicitAnnotationProcessingRequested())
1723                 log.warning("proc.use.implicit");
1724             else
1725                 log.warning("proc.use.proc.or.implicit");
1726         }
1727         chk.reportDeferredDiagnostics();
1728         if (log.compressedOutput) {
1729             log.mandatoryNote(null, "compressed.diags");
1730         }
1731     }
1732 
1733     public void enterDone() {
1734         enterDone = true;
1735         annotate.enterDone();
1736     }
1737 
1738     public boolean isEnterDone() {
1739         return enterDone;
1740     }
1741 
1742     /** Close the compiler, flushing the logs
1743      */
1744     public void close() {
1745         rootClasses = null;
1746         finder = null;
1747         reader = null;
1748         make = null;
1749         writer = null;
1750         enter = null;
1751         if (todo != null)
1752             todo.clear();
1753         todo = null;
1754         parserFactory = null;
1755         syms = null;
1756         source = null;
1757         attr = null;
1758         chk = null;
1759         gen = null;
1760         flow = null;
1761         transTypes = null;
1762         lower = null;
1763         annotate = null;
1764         types = null;
1765 
1766         log.flush();
1767         try {
1768             fileManager.flush();
1769         } catch (IOException e) {
1770             throw new Abort(e);
1771         } finally {
1772             if (names != null)
1773                 names.dispose();
1774             names = null;
1775 
1776             for (Closeable c: closeables) {
1777                 try {
1778                     c.close();
1779                 } catch (IOException e) {
1780                     // When javac uses JDK 7 as a baseline, this code would be
1781                     // better written to set any/all exceptions from all the
1782                     // Closeables as suppressed exceptions on the FatalError
1783                     // that is thrown.
1784                     JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close");
1785                     throw new FatalError(msg, e);
1786                 }
1787             }
1788             closeables = List.nil();
1789         }
1790     }
1791 
1792     protected void printNote(String lines) {
1793         log.printRawLines(Log.WriterKind.NOTICE, lines);
1794     }
1795 
1796     /** Print numbers of errors and warnings.
1797      */
1798     public void printCount(String kind, int count) {
1799         if (count != 0) {
1800             String key;
1801             if (count == 1)
1802                 key = "count." + kind;
1803             else
1804                 key = "count." + kind + ".plural";
1805             log.printLines(WriterKind.ERROR, key, String.valueOf(count));
1806             log.flush(Log.WriterKind.ERROR);
1807         }
1808     }
1809 
1810     private static long now() {
1811         return System.currentTimeMillis();
1812     }
1813 
1814     private static long elapsed(long then) {
1815         return now() - then;
1816     }
1817 
1818     public void newRound() {
1819         inputFiles.clear();
1820         todo.clear();
1821     }
1822 }