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