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