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