JavaCompiler.java revision 3259:700565092eb6
1178479Sjb/* 2178479Sjb * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3178479Sjb * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4178479Sjb * 5178479Sjb * This code is free software; you can redistribute it and/or modify it 6178479Sjb * under the terms of the GNU General Public License version 2 only, as 7178479Sjb * published by the Free Software Foundation. Oracle designates this 8178479Sjb * particular file as subject to the "Classpath" exception as provided 9178479Sjb * by Oracle in the LICENSE file that accompanied this code. 10178479Sjb * 11178479Sjb * This code is distributed in the hope that it will be useful, but WITHOUT 12178479Sjb * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13178479Sjb * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14178479Sjb * version 2 for more details (a copy is included in the LICENSE file that 15178479Sjb * accompanied this code). 16178479Sjb * 17178479Sjb * You should have received a copy of the GNU General Public License version 18178479Sjb * 2 along with this work; if not, write to the Free Software Foundation, 19178479Sjb * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20178479Sjb * 21178479Sjb * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22178479Sjb * or visit www.oracle.com if you need additional information or have any 23210767Srpaulo * questions. 24178479Sjb */ 25178479Sjb 26178479Sjbpackage com.sun.tools.javac.main; 27178479Sjb 28178479Sjbimport java.io.*; 29178479Sjbimport java.util.Collection; 30178479Sjbimport java.util.HashMap; 31178479Sjbimport java.util.HashSet; 32178479Sjbimport java.util.LinkedHashMap; 33178568Sjbimport java.util.LinkedHashSet; 34178479Sjbimport java.util.Map; 35178568Sjbimport java.util.MissingResourceException; 36178479Sjbimport java.util.Queue; 37178479Sjbimport java.util.ResourceBundle; 38178479Sjbimport java.util.Set; 39178479Sjb 40178479Sjbimport javax.annotation.processing.Processor; 41178479Sjbimport javax.lang.model.SourceVersion; 42178479Sjbimport javax.tools.DiagnosticListener; 43211554Srpauloimport javax.tools.JavaFileManager; 44211554Srpauloimport javax.tools.JavaFileObject; 45211554Srpauloimport javax.tools.StandardLocation; 46178479Sjb 47178479Sjbimport com.sun.source.util.TaskEvent; 48178479Sjbimport com.sun.tools.javac.api.MultiTaskListener; 49178479Sjbimport com.sun.tools.javac.code.*; 50178479Sjbimport com.sun.tools.javac.code.Lint.LintCategory; 51178479Sjbimport com.sun.tools.javac.code.Symbol.ClassSymbol; 52178479Sjbimport com.sun.tools.javac.code.Symbol.CompletionFailure; 53178479Sjbimport com.sun.tools.javac.code.Symbol.PackageSymbol; 54178479Sjbimport com.sun.tools.javac.comp.*; 55178479Sjbimport com.sun.tools.javac.comp.CompileStates.CompileState; 56178479Sjbimport com.sun.tools.javac.file.JavacFileManager; 57178479Sjbimport com.sun.tools.javac.jvm.*; 58178479Sjbimport com.sun.tools.javac.parser.*; 59178479Sjbimport com.sun.tools.javac.platform.PlatformDescription; 60178479Sjbimport com.sun.tools.javac.processing.*; 61178479Sjbimport com.sun.tools.javac.tree.*; 62178479Sjbimport com.sun.tools.javac.tree.JCTree.JCClassDecl; 63178479Sjbimport com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 64178479Sjbimport com.sun.tools.javac.tree.JCTree.JCExpression; 65178479Sjbimport com.sun.tools.javac.tree.JCTree.JCLambda; 66178479Sjbimport com.sun.tools.javac.tree.JCTree.JCMemberReference; 67178479Sjbimport com.sun.tools.javac.tree.JCTree.JCMethodDecl; 68178479Sjbimport com.sun.tools.javac.tree.JCTree.JCVariableDecl; 69178479Sjbimport com.sun.tools.javac.util.*; 70178479Sjbimport com.sun.tools.javac.util.Log.WriterKind; 71178479Sjb 72178568Sjbimport static com.sun.tools.javac.code.Kinds.Kind.*; 73178479Sjbimport static com.sun.tools.javac.code.TypeTag.CLASS; 74178479Sjbimport static com.sun.tools.javac.main.Option.*; 75178479Sjbimport static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*; 76178479Sjbimport static javax.tools.StandardLocation.CLASS_OUTPUT; 77178568Sjb 78178568Sjb/** This class could be the main entry point for GJC when GJC is used as a 79178568Sjb * component in a larger software system. It provides operations to 80178479Sjb * construct a new compiler, and to run a new compiler on a set of source 81178479Sjb * files. 82178479Sjb * 83178479Sjb * <p><b>This is NOT part of any supported API. 84178479Sjb * If you write code that depends on this, you do so at your own risk. 85178479Sjb * This code and its internal interfaces are subject to change or 86178479Sjb * deletion without notice.</b> 87178479Sjb */ 88178479Sjbpublic class JavaCompiler { 89178479Sjb /** The context key for the compiler. */ 90178479Sjb public static final Context.Key<JavaCompiler> compilerKey = new Context.Key<>(); 91178479Sjb 92178479Sjb /** Get the JavaCompiler instance for this context. */ 93178479Sjb public static JavaCompiler instance(Context context) { 94178479Sjb JavaCompiler instance = context.get(compilerKey); 95178479Sjb if (instance == null) 96178479Sjb instance = new JavaCompiler(context); 97178479Sjb return instance; 98178479Sjb } 99178479Sjb 100178479Sjb /** The current version number as a string. 101178479Sjb */ 102178479Sjb public static String version() { 103178479Sjb return version("release"); // mm.nn.oo[-milestone] 104178479Sjb } 105178479Sjb 106178479Sjb /** The current full version number as a string. 107178479Sjb */ 108178479Sjb public static String fullVersion() { 109178479Sjb return version("full"); // mm.mm.oo[-milestone]-build 110178479Sjb } 111178479Sjb 112178479Sjb private static final String versionRBName = "com.sun.tools.javac.resources.version"; 113178479Sjb private static ResourceBundle versionRB; 114178479Sjb 115178479Sjb private static String version(String key) { 116178479Sjb if (versionRB == null) { 117178479Sjb try { 118178479Sjb versionRB = ResourceBundle.getBundle(versionRBName); 119178479Sjb } catch (MissingResourceException e) { 120178479Sjb return Log.getLocalizedString("version.not.available"); 121178479Sjb } 122178479Sjb } 123178479Sjb try { 124178568Sjb return versionRB.getString(key); 125178479Sjb } 126178568Sjb catch (MissingResourceException e) { 127178568Sjb return Log.getLocalizedString("version.not.available"); 128178568Sjb } 129178479Sjb } 130178479Sjb 131178479Sjb /** 132178479Sjb * Control how the compiler's latter phases (attr, flow, desugar, generate) 133178479Sjb * are connected. Each individual file is processed by each phase in turn, 134178479Sjb * but with different compile policies, you can control the order in which 135178479Sjb * each class is processed through its next phase. 136178479Sjb * 137178479Sjb * <p>Generally speaking, the compiler will "fail fast" in the face of 138178479Sjb * errors, although not aggressively so. flow, desugar, etc become no-ops 139178479Sjb * once any errors have occurred. No attempt is currently made to determine 140178479Sjb * if it might be safe to process a class through its next phase because 141178479Sjb * it does not depend on any unrelated errors that might have occurred. 142178479Sjb */ 143178479Sjb protected static enum CompilePolicy { 144178479Sjb /** 145178479Sjb * Just attribute the parse trees. 146178479Sjb */ 147178479Sjb ATTR_ONLY, 148178479Sjb 149178479Sjb /** 150178479Sjb * Just attribute and do flow analysis on the parse trees. 151178479Sjb * This should catch most user errors. 152178479Sjb */ 153178479Sjb CHECK_ONLY, 154178479Sjb 155178479Sjb /** 156178479Sjb * Attribute everything, then do flow analysis for everything, 157178479Sjb * then desugar everything, and only then generate output. 158178479Sjb * This means no output will be generated if there are any 159178479Sjb * errors in any classes. 160178479Sjb */ 161178479Sjb SIMPLE, 162178479Sjb 163178479Sjb /** 164178479Sjb * Groups the classes for each source file together, then process 165178479Sjb * each group in a manner equivalent to the {@code SIMPLE} policy. 166178479Sjb * This means no output will be generated if there are any 167178479Sjb * errors in any of the classes in a source file. 168178479Sjb */ 169178479Sjb BY_FILE, 170178479Sjb 171178479Sjb /** 172178479Sjb * Completely process each entry on the todo list in turn. 173178479Sjb * -- this is the same for 1.5. 174178479Sjb * Means output might be generated for some classes in a compilation unit 175178479Sjb * and not others. 176178479Sjb */ 177178479Sjb BY_TODO; 178178479Sjb 179178479Sjb static CompilePolicy decode(String option) { 180178479Sjb if (option == null) 181178479Sjb return DEFAULT_COMPILE_POLICY; 182178479Sjb else if (option.equals("attr")) 183178479Sjb return ATTR_ONLY; 184178479Sjb else if (option.equals("check")) 185178479Sjb return CHECK_ONLY; 186178479Sjb else if (option.equals("simple")) 187178479Sjb return SIMPLE; 188178479Sjb else if (option.equals("byfile")) 189178479Sjb return BY_FILE; 190178479Sjb else if (option.equals("bytodo")) 191178479Sjb return BY_TODO; 192178479Sjb else 193178479Sjb return DEFAULT_COMPILE_POLICY; 194178479Sjb } 195178479Sjb } 196178479Sjb 197178479Sjb private static final CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO; 198178479Sjb 199178479Sjb protected static enum ImplicitSourcePolicy { 200178479Sjb /** Don't generate or process implicitly read source files. */ 201178479Sjb NONE, 202178479Sjb /** Generate classes for implicitly read source files. */ 203178479Sjb CLASS, 204178479Sjb /** Like CLASS, but generate warnings if annotation processing occurs */ 205178479Sjb UNSET; 206178479Sjb 207178479Sjb static ImplicitSourcePolicy decode(String option) { 208178479Sjb if (option == null) 209178479Sjb return UNSET; 210178479Sjb else if (option.equals("none")) 211178479Sjb return NONE; 212178479Sjb else if (option.equals("class")) 213178479Sjb return CLASS; 214178479Sjb else 215178479Sjb return UNSET; 216178479Sjb } 217178479Sjb } 218178479Sjb 219178479Sjb /** The log to be used for error reporting. 220178479Sjb */ 221178479Sjb public Log log; 222178479Sjb 223178479Sjb /** Factory for creating diagnostic objects 224178479Sjb */ 225178479Sjb JCDiagnostic.Factory diagFactory; 226178479Sjb 227178479Sjb /** The tree factory module. 228178479Sjb */ 229178479Sjb protected TreeMaker make; 230178479Sjb 231178479Sjb /** The class finder. 232178479Sjb */ 233178479Sjb protected ClassFinder finder; 234178479Sjb 235178479Sjb /** The class reader. 236178479Sjb */ 237178479Sjb protected ClassReader reader; 238178479Sjb 239178479Sjb /** The class writer. 240178479Sjb */ 241178479Sjb protected ClassWriter writer; 242178479Sjb 243178479Sjb /** The native header writer. 244178479Sjb */ 245178479Sjb protected JNIWriter jniWriter; 246178479Sjb 247178479Sjb /** The module for the symbol table entry phases. 248178479Sjb */ 249178479Sjb protected Enter enter; 250178479Sjb 251178479Sjb /** The symbol table. 252178479Sjb */ 253178479Sjb protected Symtab syms; 254178479Sjb 255178479Sjb /** The language version. 256178479Sjb */ 257178479Sjb protected Source source; 258178479Sjb 259178479Sjb /** The module for code generation. 260178479Sjb */ 261178479Sjb protected Gen gen; 262178479Sjb 263178479Sjb /** The name table. 264178479Sjb */ 265178479Sjb protected Names names; 266178479Sjb 267178479Sjb /** The attributor. 268178568Sjb */ 269178479Sjb protected Attr attr; 270178568Sjb 271178568Sjb /** The attributor. 272178479Sjb */ 273178479Sjb protected Check chk; 274178479Sjb 275178479Sjb /** The flow analyzer. 276178479Sjb */ 277178568Sjb protected Flow flow; 278178479Sjb 279178479Sjb /** The type eraser. 280178479Sjb */ 281178479Sjb protected TransTypes transTypes; 282178479Sjb 283178479Sjb /** The syntactic sugar desweetener. 284178479Sjb */ 285178479Sjb protected Lower lower; 286178479Sjb 287178479Sjb /** The annotation annotator. 288178479Sjb */ 289178479Sjb protected Annotate annotate; 290178479Sjb 291178479Sjb /** Force a completion failure on this name 292178479Sjb */ 293178479Sjb protected final Name completionFailureName; 294178479Sjb 295178479Sjb /** Type utilities. 296178479Sjb */ 297178479Sjb protected Types types; 298178479Sjb 299178479Sjb /** Access to file objects. 300178479Sjb */ 301178568Sjb protected JavaFileManager fileManager; 302211554Srpaulo 303211554Srpaulo /** Factory for parsers. 304211554Srpaulo */ 305211554Srpaulo protected ParserFactory parserFactory; 306178568Sjb 307178568Sjb /** Broadcasting listener for progress events 308178479Sjb */ 309178479Sjb protected MultiTaskListener taskListener; 310178479Sjb 311178479Sjb /** 312178479Sjb * SourceCompleter that delegates to the readSourceFile method of this class. 313178479Sjb */ 314178479Sjb protected final Symbol.Completer sourceCompleter = 315178479Sjb new Symbol.Completer() { 316178479Sjb @Override 317178479Sjb public void complete(Symbol sym) throws CompletionFailure { 318178479Sjb readSourceFile((ClassSymbol) sym); 319178479Sjb } 320178479Sjb }; 321178479Sjb 322178479Sjb /** 323178479Sjb * Command line options. 324178479Sjb */ 325178479Sjb protected Options options; 326178479Sjb 327178479Sjb protected Context context; 328178479Sjb 329178479Sjb /** 330178479Sjb * Flag set if any annotation processing occurred. 331178479Sjb **/ 332178479Sjb protected boolean annotationProcessingOccurred; 333178479Sjb 334178479Sjb /** 335178568Sjb * Flag set if any implicit source files read. 336178479Sjb **/ 337178479Sjb protected boolean implicitSourceFilesRead; 338178568Sjb 339178568Sjb protected CompileStates compileStates; 340178568Sjb 341178479Sjb /** Construct a new compiler using a shared context. 342178479Sjb */ 343178479Sjb public JavaCompiler(Context context) { 344178479Sjb this.context = context; 345178479Sjb context.put(compilerKey, this); 346178479Sjb 347178479Sjb // if fileManager not already set, register the JavacFileManager to be used 348178479Sjb if (context.get(JavaFileManager.class) == null) 349178479Sjb JavacFileManager.preRegister(context); 350178479Sjb 351178479Sjb names = Names.instance(context); 352178479Sjb log = Log.instance(context); 353178479Sjb diagFactory = JCDiagnostic.Factory.instance(context); 354178479Sjb finder = ClassFinder.instance(context); 355178479Sjb reader = ClassReader.instance(context); 356178479Sjb make = TreeMaker.instance(context); 357178479Sjb writer = ClassWriter.instance(context); 358178479Sjb jniWriter = JNIWriter.instance(context); 359178479Sjb enter = Enter.instance(context); 360178479Sjb todo = Todo.instance(context); 361178479Sjb 362178479Sjb fileManager = context.get(JavaFileManager.class); 363178568Sjb parserFactory = ParserFactory.instance(context); 364178479Sjb compileStates = CompileStates.instance(context); 365178479Sjb 366178568Sjb try { 367178479Sjb // catch completion problems with predefineds 368178479Sjb syms = Symtab.instance(context); 369178479Sjb } catch (CompletionFailure ex) { 370178479Sjb // inlined Check.completionError as it is not initialized yet 371178479Sjb log.error("cant.access", ex.sym, ex.getDetailValue()); 372178479Sjb if (ex instanceof ClassFinder.BadClassFile) 373178479Sjb throw new Abort(); 374178479Sjb } 375178479Sjb source = Source.instance(context); 376178479Sjb attr = Attr.instance(context); 377178479Sjb chk = Check.instance(context); 378178479Sjb gen = Gen.instance(context); 379178479Sjb flow = Flow.instance(context); 380178479Sjb transTypes = TransTypes.instance(context); 381178479Sjb lower = Lower.instance(context); 382178479Sjb annotate = Annotate.instance(context); 383178479Sjb types = Types.instance(context); 384178479Sjb taskListener = MultiTaskListener.instance(context); 385178479Sjb 386178479Sjb finder.sourceCompleter = sourceCompleter; 387178479Sjb 388178479Sjb options = Options.instance(context); 389178479Sjb 390178479Sjb verbose = options.isSet(VERBOSE); 391178479Sjb sourceOutput = options.isSet(PRINTSOURCE); // used to be -s 392178479Sjb lineDebugInfo = options.isUnset(G_CUSTOM) || 393178479Sjb options.isSet(G_CUSTOM, "lines"); 394178479Sjb genEndPos = options.isSet(XJCOV) || 395178479Sjb context.get(DiagnosticListener.class) != null; 396178479Sjb devVerbose = options.isSet("dev"); 397178479Sjb processPcks = options.isSet("process.packages"); 398178479Sjb werror = options.isSet(WERROR); 399178479Sjb 400178479Sjb verboseCompilePolicy = options.isSet("verboseCompilePolicy"); 401178479Sjb 402178568Sjb if (options.isSet("shouldStopPolicy") && 403178479Sjb CompileState.valueOf(options.get("shouldStopPolicy")) == CompileState.ATTR) 404178568Sjb compilePolicy = CompilePolicy.ATTR_ONLY; 405178568Sjb else 406178568Sjb compilePolicy = CompilePolicy.decode(options.get("compilePolicy")); 407178479Sjb 408178479Sjb implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit")); 409178479Sjb 410178479Sjb completionFailureName = 411178479Sjb options.isSet("failcomplete") 412178479Sjb ? names.fromString(options.get("failcomplete")) 413210767Srpaulo : null; 414210767Srpaulo 415178479Sjb shouldStopPolicyIfError = 416210775Srpaulo options.isSet("shouldStopPolicy") // backwards compatible 417210767Srpaulo ? CompileState.valueOf(options.get("shouldStopPolicy")) 418210775Srpaulo : options.isSet("shouldStopPolicyIfError") 419210767Srpaulo ? CompileState.valueOf(options.get("shouldStopPolicyIfError")) 420210767Srpaulo : CompileState.INIT; 421210767Srpaulo shouldStopPolicyIfNoError = 422178479Sjb options.isSet("shouldStopPolicyIfNoError") 423178479Sjb ? CompileState.valueOf(options.get("shouldStopPolicyIfNoError")) 424178479Sjb : CompileState.GENERATE; 425178479Sjb 426178479Sjb if (options.isUnset("oldDiags")) 427178479Sjb log.setDiagnosticFormatter(RichDiagnosticFormatter.instance(context)); 428178479Sjb 429178479Sjb PlatformDescription platformProvider = context.get(PlatformDescription.class); 430178479Sjb 431178479Sjb if (platformProvider != null) 432211554Srpaulo closeables = closeables.prepend(platformProvider); 433178479Sjb } 434211554Srpaulo 435211554Srpaulo /* Switches: 436211554Srpaulo */ 437178479Sjb 438178479Sjb /** Verbose output. 439178479Sjb */ 440211554Srpaulo public boolean verbose; 441178479Sjb 442178479Sjb /** Emit plain Java source files rather than class files. 443178479Sjb */ 444178479Sjb public boolean sourceOutput; 445178479Sjb 446178479Sjb 447178479Sjb /** Generate code with the LineNumberTable attribute for debugging 448178479Sjb */ 449178479Sjb public boolean lineDebugInfo; 450178479Sjb 451178479Sjb /** Switch: should we store the ending positions? 452178479Sjb */ 453178479Sjb public boolean genEndPos; 454178479Sjb 455178479Sjb /** Switch: should we debug ignored exceptions 456178479Sjb */ 457178479Sjb protected boolean devVerbose; 458178479Sjb 459178479Sjb /** Switch: should we (annotation) process packages as well 460178479Sjb */ 461211554Srpaulo protected boolean processPcks; 462211554Srpaulo 463211554Srpaulo /** Switch: treat warnings as errors 464178479Sjb */ 465178479Sjb protected boolean werror; 466178479Sjb 467178479Sjb /** Switch: is annotation processing requested explicitly via 468211554Srpaulo * CompilationTask.setProcessors? 469178479Sjb */ 470178479Sjb protected boolean explicitAnnotationProcessingRequested = false; 471178479Sjb 472178479Sjb /** 473178479Sjb * The policy for the order in which to perform the compilation 474178479Sjb */ 475178479Sjb protected CompilePolicy compilePolicy; 476211554Srpaulo 477178568Sjb /** 478178479Sjb * The policy for what to do with implicitly read source files 479178479Sjb */ 480178479Sjb protected ImplicitSourcePolicy implicitSourcePolicy; 481178479Sjb 482178479Sjb /** 483178479Sjb * Report activity related to compilePolicy 484178479Sjb */ 485178479Sjb public boolean verboseCompilePolicy; 486178479Sjb 487178479Sjb /** 488178479Sjb * Policy of how far to continue compilation after errors have occurred. 489178479Sjb * Set this to minimum CompileState (INIT) to stop as soon as possible 490178479Sjb * after errors. 491178479Sjb */ 492178479Sjb public CompileState shouldStopPolicyIfError; 493178479Sjb 494178479Sjb /** 495178479Sjb * Policy of how far to continue compilation when no errors have occurred. 496178568Sjb * Set this to maximum CompileState (GENERATE) to perform full compilation. 497178479Sjb * Set this lower to perform partial compilation, such as -proc:only. 498178479Sjb */ 499178479Sjb public CompileState shouldStopPolicyIfNoError; 500178479Sjb 501178479Sjb /** A queue of all as yet unattributed classes. 502178479Sjb */ 503178479Sjb public Todo todo; 504178479Sjb 505178479Sjb /** A list of items to be closed when the compilation is complete. 506178568Sjb */ 507178479Sjb public List<Closeable> closeables = List.nil(); 508178479Sjb 509178479Sjb /** The set of currently compiled inputfiles, needed to ensure 510178479Sjb * we don't accidentally overwrite an input file when -s is set. 511178479Sjb * initialized by `compile'. 512178479Sjb */ 513178479Sjb protected Set<JavaFileObject> inputFiles = new HashSet<>(); 514178479Sjb 515178479Sjb protected boolean shouldStop(CompileState cs) { 516178479Sjb CompileState shouldStopPolicy = (errorCount() > 0 || unrecoverableError()) 517178479Sjb ? shouldStopPolicyIfError 518178479Sjb : shouldStopPolicyIfNoError; 519178479Sjb return cs.isAfter(shouldStopPolicy); 520178479Sjb } 521178479Sjb 522178479Sjb /** The number of errors reported so far. 523178479Sjb */ 524178479Sjb public int errorCount() { 525178479Sjb if (werror && log.nerrors == 0 && log.nwarnings > 0) { 526178479Sjb log.error("warnings.and.werror"); 527178479Sjb } 528178479Sjb return log.nerrors; 529178479Sjb } 530178479Sjb 531178479Sjb protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) { 532178479Sjb return shouldStop(cs) ? new ListBuffer<T>() : queue; 533178479Sjb } 534178479Sjb 535178479Sjb protected final <T> List<T> stopIfError(CompileState cs, List<T> list) { 536178479Sjb return shouldStop(cs) ? List.<T>nil() : list; 537178479Sjb } 538178479Sjb 539178479Sjb /** The number of warnings reported so far. 540178479Sjb */ 541178479Sjb public int warningCount() { 542178479Sjb return log.nwarnings; 543178479Sjb } 544178479Sjb 545178479Sjb /** Try to open input stream with given name. 546178479Sjb * Report an error if this fails. 547178479Sjb * @param filename The file name of the input stream to be opened. 548178479Sjb */ 549178479Sjb public CharSequence readSource(JavaFileObject filename) { 550178479Sjb try { 551178479Sjb inputFiles.add(filename); 552178479Sjb return filename.getCharContent(false); 553178479Sjb } catch (IOException e) { 554178479Sjb log.error("error.reading.file", filename, JavacFileManager.getMessage(e)); 555178479Sjb return null; 556178479Sjb } 557178479Sjb } 558178479Sjb 559178479Sjb /** Parse contents of input stream. 560178479Sjb * @param filename The name of the file from which input stream comes. 561178479Sjb * @param content The characters to be parsed. 562178479Sjb */ 563178479Sjb protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { 564178479Sjb long msec = now(); 565178479Sjb JCCompilationUnit tree = make.TopLevel(List.<JCTree>nil()); 566178479Sjb if (content != null) { 567178479Sjb if (verbose) { 568178479Sjb log.printVerbose("parsing.started", filename); 569178568Sjb } 570178479Sjb if (!taskListener.isEmpty()) { 571178568Sjb TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename); 572178479Sjb taskListener.started(e); 573178479Sjb keepComments = true; 574178479Sjb genEndPos = true; 575178479Sjb } 576178479Sjb Parser parser = parserFactory.newParser(content, keepComments(), genEndPos, lineDebugInfo); 577178479Sjb tree = parser.parseCompilationUnit(); 578178479Sjb if (verbose) { 579178479Sjb log.printVerbose("parsing.done", Long.toString(elapsed(msec))); 580178479Sjb } 581178479Sjb } 582178479Sjb 583178479Sjb tree.sourcefile = filename; 584178479Sjb 585178479Sjb if (content != null && !taskListener.isEmpty()) { 586178479Sjb TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree); 587178479Sjb taskListener.finished(e); 588178479Sjb } 589178479Sjb 590178479Sjb return tree; 591178479Sjb } 592178479Sjb // where 593178479Sjb public boolean keepComments = false; 594178479Sjb protected boolean keepComments() { 595178479Sjb return keepComments || sourceOutput; 596178479Sjb } 597178479Sjb 598178479Sjb 599178479Sjb /** Parse contents of file. 600178479Sjb * @param filename The name of the file to be parsed. 601178479Sjb */ 602178479Sjb @Deprecated 603178479Sjb public JCTree.JCCompilationUnit parse(String filename) { 604178479Sjb JavacFileManager fm = (JavacFileManager)fileManager; 605178479Sjb return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next()); 606178479Sjb } 607178568Sjb 608178479Sjb /** Parse contents of file. 609178568Sjb * @param filename The name of the file to be parsed. 610178568Sjb */ 611178568Sjb public JCTree.JCCompilationUnit parse(JavaFileObject filename) { 612178479Sjb JavaFileObject prev = log.useSource(filename); 613211554Srpaulo try { 614178479Sjb JCTree.JCCompilationUnit t = parse(filename, readSource(filename)); 615178479Sjb if (t.endPositions != null) 616178479Sjb log.setEndPosTable(filename, t.endPositions); 617178479Sjb return t; 618178479Sjb } finally { 619178479Sjb log.useSource(prev); 620178479Sjb } 621178479Sjb } 622178479Sjb 623178568Sjb /** Resolve an identifier which may be the binary name of a class or 624178479Sjb * the Java name of a class or package. 625178479Sjb * @param name The name to resolve 626211554Srpaulo */ 627178479Sjb public Symbol resolveBinaryNameOrIdent(String name) { 628178479Sjb try { 629178568Sjb Name flatname = names.fromString(name.replace("/", ".")); 630178479Sjb return finder.loadClass(flatname); 631178479Sjb } catch (CompletionFailure ignore) { 632178479Sjb return resolveIdent(name); 633178479Sjb } 634178479Sjb } 635178479Sjb 636178479Sjb /** Resolve an identifier. 637178479Sjb * @param name The identifier to resolve 638178479Sjb */ 639178479Sjb public Symbol resolveIdent(String name) { 640178479Sjb if (name.equals("")) 641210775Srpaulo return syms.errSymbol; 642211554Srpaulo JavaFileObject prev = log.useSource(null); 643178479Sjb try { 644178479Sjb JCExpression tree = null; 645178479Sjb for (String s : name.split("\\.", -1)) { 646178479Sjb if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords 647178479Sjb return syms.errSymbol; 648178568Sjb tree = (tree == null) ? make.Ident(names.fromString(s)) 649178479Sjb : make.Select(tree, names.fromString(s)); 650178568Sjb } 651178568Sjb JCCompilationUnit toplevel = 652178568Sjb make.TopLevel(List.<JCTree>nil()); 653178479Sjb toplevel.packge = syms.unnamedPackage; 654212414Srpaulo return attr.attribIdent(tree, toplevel); 655212414Srpaulo } finally { 656212414Srpaulo log.useSource(prev); 657178479Sjb } 658178479Sjb } 659178479Sjb 660178479Sjb /** Generate code and emit a class file for a given class 661178479Sjb * @param env The attribution environment of the outermost class 662178479Sjb * containing this class. 663178479Sjb * @param cdef The class definition from which code is generated. 664178479Sjb */ 665178479Sjb JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException { 666178479Sjb try { 667178479Sjb if (gen.genClass(env, cdef) && (errorCount() == 0)) 668178479Sjb return writer.writeClass(cdef.sym); 669178479Sjb } catch (ClassWriter.PoolOverflow ex) { 670178479Sjb log.error(cdef.pos(), "limit.pool"); 671178479Sjb } catch (ClassWriter.StringOverflow ex) { 672178479Sjb log.error(cdef.pos(), "limit.string.overflow", 673178479Sjb ex.value.substring(0, 20)); 674178479Sjb } catch (CompletionFailure ex) { 675178479Sjb chk.completionError(cdef.pos(), ex); 676178479Sjb } 677178479Sjb return null; 678178479Sjb } 679178479Sjb 680178479Sjb /** Emit plain Java source for a class. 681178479Sjb * @param env The attribution environment of the outermost class 682178479Sjb * containing this class. 683178479Sjb * @param cdef The class definition to be printed. 684178479Sjb */ 685178479Sjb JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { 686178479Sjb JavaFileObject outFile 687178479Sjb = fileManager.getJavaFileForOutput(CLASS_OUTPUT, 688178479Sjb cdef.sym.flatname.toString(), 689178479Sjb JavaFileObject.Kind.SOURCE, 690178479Sjb null); 691178479Sjb if (inputFiles.contains(outFile)) { 692178479Sjb log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile); 693178479Sjb return null; 694178479Sjb } else { 695178479Sjb try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) { 696178479Sjb new Pretty(out, true).printUnit(env.toplevel, cdef); 697178479Sjb if (verbose) 698178479Sjb log.printVerbose("wrote.file", outFile); 699178479Sjb } 700178479Sjb return outFile; 701178479Sjb } 702178479Sjb } 703178479Sjb 704178479Sjb /** Compile a source file that has been accessed by the class finder. 705178479Sjb * @param c The class the source file of which needs to be compiled. 706178479Sjb */ 707178479Sjb private void readSourceFile(ClassSymbol c) throws CompletionFailure { 708178479Sjb readSourceFile(null, c); 709178479Sjb } 710178479Sjb 711178479Sjb /** Compile a ClassSymbol from source, optionally using the given compilation unit as 712178479Sjb * the source tree. 713178479Sjb * @param tree the compilation unit in which the given ClassSymbol resides, 714178479Sjb * or null if should be parsed from source 715178479Sjb * @param c the ClassSymbol to complete 716178479Sjb */ 717178479Sjb public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure { 718178479Sjb if (completionFailureName == c.fullname) { 719178479Sjb throw new CompletionFailure(c, "user-selected completion failure by class name"); 720178479Sjb } 721178479Sjb JavaFileObject filename = c.classfile; 722178479Sjb JavaFileObject prev = log.useSource(filename); 723178479Sjb 724178479Sjb if (tree == null) { 725178479Sjb try { 726178479Sjb tree = parse(filename, filename.getCharContent(false)); 727178479Sjb } catch (IOException e) { 728178479Sjb log.error("error.reading.file", filename, JavacFileManager.getMessage(e)); 729178479Sjb tree = make.TopLevel(List.<JCTree>nil()); 730178479Sjb } finally { 731178479Sjb log.useSource(prev); 732178479Sjb } 733178479Sjb } 734178479Sjb 735178479Sjb if (!taskListener.isEmpty()) { 736178479Sjb TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); 737178568Sjb taskListener.started(e); 738178568Sjb } 739178568Sjb 740178568Sjb enter.complete(List.of(tree), c); 741178568Sjb 742178568Sjb if (!taskListener.isEmpty()) { 743178568Sjb TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); 744178479Sjb taskListener.finished(e); 745178479Sjb } 746178479Sjb 747178479Sjb if (enter.getEnv(c) == null) { 748178479Sjb boolean isPkgInfo = 749178479Sjb tree.sourcefile.isNameCompatible("package-info", 750178479Sjb JavaFileObject.Kind.SOURCE); 751178479Sjb if (isPkgInfo) { 752178479Sjb if (enter.getEnv(tree.packge) == null) { 753178479Sjb JCDiagnostic diag = 754178479Sjb diagFactory.fragment("file.does.not.contain.package", 755178479Sjb c.location()); 756178479Sjb throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory); 757178479Sjb } 758178479Sjb } else { 759178479Sjb JCDiagnostic diag = 760178479Sjb diagFactory.fragment("file.doesnt.contain.class", 761178479Sjb c.getQualifiedName()); 762178479Sjb throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory); 763178479Sjb } 764178479Sjb } 765178479Sjb 766178479Sjb implicitSourceFilesRead = true; 767178479Sjb } 768178479Sjb 769178479Sjb /** Track when the JavaCompiler has been used to compile something. */ 770178479Sjb private boolean hasBeenUsed = false; 771178479Sjb private long start_msec = 0; 772178479Sjb public long elapsed_msec = 0; 773178479Sjb 774178479Sjb public void compile(List<JavaFileObject> sourceFileObject) 775178479Sjb throws Throwable { 776178479Sjb compile(sourceFileObject, List.<String>nil(), null); 777178479Sjb } 778178568Sjb 779178479Sjb /** 780178479Sjb * Main method: compile a list of files, return all compiled classes 781178479Sjb * 782178479Sjb * @param sourceFileObjects file objects to be compiled 783178479Sjb * @param classnames class names to process for annotations 784178479Sjb * @param processors user provided annotation processors to bypass 785178479Sjb * discovery, {@code null} means that no processors were provided 786178479Sjb */ 787178479Sjb public void compile(Collection<JavaFileObject> sourceFileObjects, 788178479Sjb Collection<String> classnames, 789178479Sjb Iterable<? extends Processor> processors) 790178479Sjb { 791178479Sjb if (!taskListener.isEmpty()) { 792178479Sjb taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION)); 793178479Sjb } 794178479Sjb 795178479Sjb if (processors != null && processors.iterator().hasNext()) 796178479Sjb explicitAnnotationProcessingRequested = true; 797178479Sjb // as a JavaCompiler can only be used once, throw an exception if 798178479Sjb // it has been used before. 799178479Sjb if (hasBeenUsed) 800178479Sjb checkReusable(); 801178479Sjb hasBeenUsed = true; 802178479Sjb 803178479Sjb // forcibly set the equivalent of -Xlint:-options, so that no further 804178479Sjb // warnings about command line options are generated from this point on 805178479Sjb options.put(XLINT_CUSTOM.text + "-" + LintCategory.OPTIONS.option, "true"); 806178479Sjb options.remove(XLINT_CUSTOM.text + LintCategory.OPTIONS.option); 807178479Sjb 808178479Sjb start_msec = now(); 809178479Sjb 810178479Sjb try { 811178479Sjb initProcessAnnotations(processors); 812178479Sjb 813178479Sjb // These method calls must be chained to avoid memory leaks 814178479Sjb processAnnotations( 815178479Sjb enterTrees(stopIfError(CompileState.PARSE, parseFiles(sourceFileObjects))), 816178479Sjb classnames); 817178479Sjb 818178479Sjb // If it's safe to do so, skip attr / flow / gen for implicit classes 819178479Sjb if (taskListener.isEmpty() && 820178479Sjb implicitSourcePolicy == ImplicitSourcePolicy.NONE) { 821178479Sjb todo.retainFiles(inputFiles); 822178479Sjb } 823178568Sjb 824178568Sjb switch (compilePolicy) { 825178568Sjb case ATTR_ONLY: 826178479Sjb attribute(todo); 827178479Sjb break; 828178479Sjb 829178479Sjb case CHECK_ONLY: 830 flow(attribute(todo)); 831 break; 832 833 case SIMPLE: 834 generate(desugar(flow(attribute(todo)))); 835 break; 836 837 case BY_FILE: { 838 Queue<Queue<Env<AttrContext>>> q = todo.groupByFile(); 839 while (!q.isEmpty() && !shouldStop(CompileState.ATTR)) { 840 generate(desugar(flow(attribute(q.remove())))); 841 } 842 } 843 break; 844 845 case BY_TODO: 846 while (!todo.isEmpty()) 847 generate(desugar(flow(attribute(todo.remove())))); 848 break; 849 850 default: 851 Assert.error("unknown compile policy"); 852 } 853 } catch (Abort ex) { 854 if (devVerbose) 855 ex.printStackTrace(System.err); 856 } finally { 857 if (verbose) { 858 elapsed_msec = elapsed(start_msec); 859 log.printVerbose("total", Long.toString(elapsed_msec)); 860 } 861 862 reportDeferredDiagnostics(); 863 864 if (!log.hasDiagnosticListener()) { 865 printCount("error", errorCount()); 866 printCount("warn", warningCount()); 867 } 868 if (!taskListener.isEmpty()) { 869 taskListener.finished(new TaskEvent(TaskEvent.Kind.COMPILATION)); 870 } 871 close(); 872 if (procEnvImpl != null) 873 procEnvImpl.close(); 874 } 875 } 876 877 protected void checkReusable() { 878 throw new AssertionError("attempt to reuse JavaCompiler"); 879 } 880 881 /** 882 * The list of classes explicitly supplied on the command line for compilation. 883 * Not always populated. 884 */ 885 private List<JCClassDecl> rootClasses; 886 887 /** 888 * Parses a list of files. 889 */ 890 public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) { 891 if (shouldStop(CompileState.PARSE)) 892 return List.nil(); 893 894 //parse all files 895 ListBuffer<JCCompilationUnit> trees = new ListBuffer<>(); 896 Set<JavaFileObject> filesSoFar = new HashSet<>(); 897 for (JavaFileObject fileObject : fileObjects) { 898 if (!filesSoFar.contains(fileObject)) { 899 filesSoFar.add(fileObject); 900 trees.append(parse(fileObject)); 901 } 902 } 903 return trees.toList(); 904 } 905 906 /** 907 * Enter the symbols found in a list of parse trees if the compilation 908 * is expected to proceed beyond anno processing into attr. 909 * As a side-effect, this puts elements on the "todo" list. 910 * Also stores a list of all top level classes in rootClasses. 911 */ 912 public List<JCCompilationUnit> enterTreesIfNeeded(List<JCCompilationUnit> roots) { 913 if (shouldStop(CompileState.ATTR)) 914 return List.nil(); 915 return enterTrees(roots); 916 } 917 918 /** 919 * Enter the symbols found in a list of parse trees. 920 * As a side-effect, this puts elements on the "todo" list. 921 * Also stores a list of all top level classes in rootClasses. 922 */ 923 public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) { 924 //enter symbols for all files 925 if (!taskListener.isEmpty()) { 926 for (JCCompilationUnit unit: roots) { 927 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); 928 taskListener.started(e); 929 } 930 } 931 932 enter.main(roots); 933 934 if (!taskListener.isEmpty()) { 935 for (JCCompilationUnit unit: roots) { 936 TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); 937 taskListener.finished(e); 938 } 939 } 940 941 // If generating source, or if tracking public apis, 942 // then remember the classes declared in 943 // the original compilation units listed on the command line. 944 if (sourceOutput) { 945 ListBuffer<JCClassDecl> cdefs = new ListBuffer<>(); 946 for (JCCompilationUnit unit : roots) { 947 for (List<JCTree> defs = unit.defs; 948 defs.nonEmpty(); 949 defs = defs.tail) { 950 if (defs.head instanceof JCClassDecl) 951 cdefs.append((JCClassDecl)defs.head); 952 } 953 } 954 rootClasses = cdefs.toList(); 955 } 956 957 // Ensure the input files have been recorded. Although this is normally 958 // done by readSource, it may not have been done if the trees were read 959 // in a prior round of annotation processing, and the trees have been 960 // cleaned and are being reused. 961 for (JCCompilationUnit unit : roots) { 962 inputFiles.add(unit.sourcefile); 963 } 964 965 return roots; 966 } 967 968 /** 969 * Set to true to enable skeleton annotation processing code. 970 * Currently, we assume this variable will be replaced more 971 * advanced logic to figure out if annotation processing is 972 * needed. 973 */ 974 boolean processAnnotations = false; 975 976 Log.DeferredDiagnosticHandler deferredDiagnosticHandler; 977 978 /** 979 * Object to handle annotation processing. 980 */ 981 private JavacProcessingEnvironment procEnvImpl = null; 982 983 /** 984 * Check if we should process annotations. 985 * If so, and if no scanner is yet registered, then set up the DocCommentScanner 986 * to catch doc comments, and set keepComments so the parser records them in 987 * the compilation unit. 988 * 989 * @param processors user provided annotation processors to bypass 990 * discovery, {@code null} means that no processors were provided 991 */ 992 public void initProcessAnnotations(Iterable<? extends Processor> processors) { 993 // Process annotations if processing is not disabled and there 994 // is at least one Processor available. 995 if (options.isSet(PROC, "none")) { 996 processAnnotations = false; 997 } else if (procEnvImpl == null) { 998 procEnvImpl = JavacProcessingEnvironment.instance(context); 999 procEnvImpl.setProcessors(processors); 1000 processAnnotations = procEnvImpl.atLeastOneProcessor(); 1001 1002 if (processAnnotations) { 1003 options.put("save-parameter-names", "save-parameter-names"); 1004 reader.saveParameterNames = true; 1005 keepComments = true; 1006 genEndPos = true; 1007 if (!taskListener.isEmpty()) 1008 taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); 1009 deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log); 1010 } else { // free resources 1011 procEnvImpl.close(); 1012 } 1013 } 1014 } 1015 1016 // TODO: called by JavacTaskImpl 1017 public void processAnnotations(List<JCCompilationUnit> roots) { 1018 processAnnotations(roots, List.<String>nil()); 1019 } 1020 1021 /** 1022 * Process any annotations found in the specified compilation units. 1023 * @param roots a list of compilation units 1024 */ 1025 // Implementation note: when this method is called, log.deferredDiagnostics 1026 // will have been set true by initProcessAnnotations, meaning that any diagnostics 1027 // that are reported will go into the log.deferredDiagnostics queue. 1028 // By the time this method exits, log.deferDiagnostics must be set back to false, 1029 // and all deferredDiagnostics must have been handled: i.e. either reported 1030 // or determined to be transient, and therefore suppressed. 1031 public void processAnnotations(List<JCCompilationUnit> roots, 1032 Collection<String> classnames) { 1033 if (shouldStop(CompileState.PROCESS)) { 1034 // Errors were encountered. 1035 // Unless all the errors are resolve errors, the errors were parse errors 1036 // or other errors during enter which cannot be fixed by running 1037 // any annotation processors. 1038 if (unrecoverableError()) { 1039 deferredDiagnosticHandler.reportDeferredDiagnostics(); 1040 log.popDiagnosticHandler(deferredDiagnosticHandler); 1041 return ; 1042 } 1043 } 1044 1045 // ASSERT: processAnnotations and procEnvImpl should have been set up by 1046 // by initProcessAnnotations 1047 1048 // NOTE: The !classnames.isEmpty() checks should be refactored to Main. 1049 1050 if (!processAnnotations) { 1051 // If there are no annotation processors present, and 1052 // annotation processing is to occur with compilation, 1053 // emit a warning. 1054 if (options.isSet(PROC, "only")) { 1055 log.warning("proc.proc-only.requested.no.procs"); 1056 todo.clear(); 1057 } 1058 // If not processing annotations, classnames must be empty 1059 if (!classnames.isEmpty()) { 1060 log.error("proc.no.explicit.annotation.processing.requested", 1061 classnames); 1062 } 1063 Assert.checkNull(deferredDiagnosticHandler); 1064 return ; // continue regular compilation 1065 } 1066 1067 Assert.checkNonNull(deferredDiagnosticHandler); 1068 1069 try { 1070 List<ClassSymbol> classSymbols = List.nil(); 1071 List<PackageSymbol> pckSymbols = List.nil(); 1072 if (!classnames.isEmpty()) { 1073 // Check for explicit request for annotation 1074 // processing 1075 if (!explicitAnnotationProcessingRequested()) { 1076 log.error("proc.no.explicit.annotation.processing.requested", 1077 classnames); 1078 deferredDiagnosticHandler.reportDeferredDiagnostics(); 1079 log.popDiagnosticHandler(deferredDiagnosticHandler); 1080 return ; // TODO: Will this halt compilation? 1081 } else { 1082 boolean errors = false; 1083 for (String nameStr : classnames) { 1084 Symbol sym = resolveBinaryNameOrIdent(nameStr); 1085 if (sym == null || 1086 (sym.kind == PCK && !processPcks) || 1087 sym.kind == ABSENT_TYP) { 1088 log.error("proc.cant.find.class", nameStr); 1089 errors = true; 1090 continue; 1091 } 1092 try { 1093 if (sym.kind == PCK) 1094 sym.complete(); 1095 if (sym.exists()) { 1096 if (sym.kind == PCK) 1097 pckSymbols = pckSymbols.prepend((PackageSymbol)sym); 1098 else 1099 classSymbols = classSymbols.prepend((ClassSymbol)sym); 1100 continue; 1101 } 1102 Assert.check(sym.kind == PCK); 1103 log.warning("proc.package.does.not.exist", nameStr); 1104 pckSymbols = pckSymbols.prepend((PackageSymbol)sym); 1105 } catch (CompletionFailure e) { 1106 log.error("proc.cant.find.class", nameStr); 1107 errors = true; 1108 continue; 1109 } 1110 } 1111 if (errors) { 1112 deferredDiagnosticHandler.reportDeferredDiagnostics(); 1113 log.popDiagnosticHandler(deferredDiagnosticHandler); 1114 return ; 1115 } 1116 } 1117 } 1118 try { 1119 annotationProcessingOccurred = 1120 procEnvImpl.doProcessing(roots, 1121 classSymbols, 1122 pckSymbols, 1123 deferredDiagnosticHandler); 1124 // doProcessing will have handled deferred diagnostics 1125 } finally { 1126 procEnvImpl.close(); 1127 } 1128 } catch (CompletionFailure ex) { 1129 log.error("cant.access", ex.sym, ex.getDetailValue()); 1130 if (deferredDiagnosticHandler != null) { 1131 deferredDiagnosticHandler.reportDeferredDiagnostics(); 1132 log.popDiagnosticHandler(deferredDiagnosticHandler); 1133 } 1134 } 1135 } 1136 1137 private boolean unrecoverableError() { 1138 if (deferredDiagnosticHandler != null) { 1139 for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) { 1140 if (d.getKind() == JCDiagnostic.Kind.ERROR && !d.isFlagSet(RECOVERABLE)) 1141 return true; 1142 } 1143 } 1144 return false; 1145 } 1146 1147 boolean explicitAnnotationProcessingRequested() { 1148 return 1149 explicitAnnotationProcessingRequested || 1150 explicitAnnotationProcessingRequested(options); 1151 } 1152 1153 static boolean explicitAnnotationProcessingRequested(Options options) { 1154 return 1155 options.isSet(PROCESSOR) || 1156 options.isSet(PROCESSORPATH) || 1157 options.isSet(PROC, "only") || 1158 options.isSet(XPRINT); 1159 } 1160 1161 public void setDeferredDiagnosticHandler(Log.DeferredDiagnosticHandler deferredDiagnosticHandler) { 1162 this.deferredDiagnosticHandler = deferredDiagnosticHandler; 1163 } 1164 1165 /** 1166 * Attribute a list of parse trees, such as found on the "todo" list. 1167 * Note that attributing classes may cause additional files to be 1168 * parsed and entered via the SourceCompleter. 1169 * Attribution of the entries in the list does not stop if any errors occur. 1170 * @return a list of environments for attribute classes. 1171 */ 1172 public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) { 1173 ListBuffer<Env<AttrContext>> results = new ListBuffer<>(); 1174 while (!envs.isEmpty()) 1175 results.append(attribute(envs.remove())); 1176 return stopIfError(CompileState.ATTR, results); 1177 } 1178 1179 /** 1180 * Attribute a parse tree. 1181 * @return the attributed parse tree 1182 */ 1183 public Env<AttrContext> attribute(Env<AttrContext> env) { 1184 if (compileStates.isDone(env, CompileState.ATTR)) 1185 return env; 1186 1187 if (verboseCompilePolicy) 1188 printNote("[attribute " + env.enclClass.sym + "]"); 1189 if (verbose) 1190 log.printVerbose("checking.attribution", env.enclClass.sym); 1191 1192 if (!taskListener.isEmpty()) { 1193 TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); 1194 taskListener.started(e); 1195 } 1196 1197 JavaFileObject prev = log.useSource( 1198 env.enclClass.sym.sourcefile != null ? 1199 env.enclClass.sym.sourcefile : 1200 env.toplevel.sourcefile); 1201 try { 1202 attr.attrib(env); 1203 if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) { 1204 //if in fail-over mode, ensure that AST expression nodes 1205 //are correctly initialized (e.g. they have a type/symbol) 1206 attr.postAttr(env.tree); 1207 } 1208 compileStates.put(env, CompileState.ATTR); 1209 } 1210 finally { 1211 log.useSource(prev); 1212 } 1213 1214 return env; 1215 } 1216 1217 /** 1218 * Perform dataflow checks on attributed parse trees. 1219 * These include checks for definite assignment and unreachable statements. 1220 * If any errors occur, an empty list will be returned. 1221 * @return the list of attributed parse trees 1222 */ 1223 public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) { 1224 ListBuffer<Env<AttrContext>> results = new ListBuffer<>(); 1225 for (Env<AttrContext> env: envs) { 1226 flow(env, results); 1227 } 1228 return stopIfError(CompileState.FLOW, results); 1229 } 1230 1231 /** 1232 * Perform dataflow checks on an attributed parse tree. 1233 */ 1234 public Queue<Env<AttrContext>> flow(Env<AttrContext> env) { 1235 ListBuffer<Env<AttrContext>> results = new ListBuffer<>(); 1236 flow(env, results); 1237 return stopIfError(CompileState.FLOW, results); 1238 } 1239 1240 /** 1241 * Perform dataflow checks on an attributed parse tree. 1242 */ 1243 protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) { 1244 if (compileStates.isDone(env, CompileState.FLOW)) { 1245 results.add(env); 1246 return; 1247 } 1248 1249 try { 1250 if (shouldStop(CompileState.FLOW)) 1251 return; 1252 1253 if (verboseCompilePolicy) 1254 printNote("[flow " + env.enclClass.sym + "]"); 1255 JavaFileObject prev = log.useSource( 1256 env.enclClass.sym.sourcefile != null ? 1257 env.enclClass.sym.sourcefile : 1258 env.toplevel.sourcefile); 1259 try { 1260 make.at(Position.FIRSTPOS); 1261 TreeMaker localMake = make.forToplevel(env.toplevel); 1262 flow.analyzeTree(env, localMake); 1263 compileStates.put(env, CompileState.FLOW); 1264 1265 if (shouldStop(CompileState.FLOW)) 1266 return; 1267 1268 results.add(env); 1269 } 1270 finally { 1271 log.useSource(prev); 1272 } 1273 } 1274 finally { 1275 if (!taskListener.isEmpty()) { 1276 TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); 1277 taskListener.finished(e); 1278 } 1279 } 1280 } 1281 1282 /** 1283 * Prepare attributed parse trees, in conjunction with their attribution contexts, 1284 * for source or code generation. 1285 * If any errors occur, an empty list will be returned. 1286 * @return a list containing the classes to be generated 1287 */ 1288 public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) { 1289 ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = new ListBuffer<>(); 1290 for (Env<AttrContext> env: envs) 1291 desugar(env, results); 1292 return stopIfError(CompileState.FLOW, results); 1293 } 1294 1295 HashMap<Env<AttrContext>, Queue<Pair<Env<AttrContext>, JCClassDecl>>> desugaredEnvs = new HashMap<>(); 1296 1297 /** 1298 * Prepare attributed parse trees, in conjunction with their attribution contexts, 1299 * for source or code generation. If the file was not listed on the command line, 1300 * the current implicitSourcePolicy is taken into account. 1301 * The preparation stops as soon as an error is found. 1302 */ 1303 protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) { 1304 if (shouldStop(CompileState.TRANSTYPES)) 1305 return; 1306 1307 if (implicitSourcePolicy == ImplicitSourcePolicy.NONE 1308 && !inputFiles.contains(env.toplevel.sourcefile)) { 1309 return; 1310 } 1311 1312 if (compileStates.isDone(env, CompileState.LOWER)) { 1313 results.addAll(desugaredEnvs.get(env)); 1314 return; 1315 } 1316 1317 /** 1318 * Ensure that superclasses of C are desugared before C itself. This is 1319 * required for two reasons: (i) as erasure (TransTypes) destroys 1320 * information needed in flow analysis and (ii) as some checks carried 1321 * out during lowering require that all synthetic fields/methods have 1322 * already been added to C and its superclasses. 1323 */ 1324 class ScanNested extends TreeScanner { 1325 Set<Env<AttrContext>> dependencies = new LinkedHashSet<>(); 1326 protected boolean hasLambdas; 1327 @Override 1328 public void visitClassDef(JCClassDecl node) { 1329 Type st = types.supertype(node.sym.type); 1330 boolean envForSuperTypeFound = false; 1331 while (!envForSuperTypeFound && st.hasTag(CLASS)) { 1332 ClassSymbol c = st.tsym.outermostClass(); 1333 Env<AttrContext> stEnv = enter.getEnv(c); 1334 if (stEnv != null && env != stEnv) { 1335 if (dependencies.add(stEnv)) { 1336 boolean prevHasLambdas = hasLambdas; 1337 try { 1338 scan(stEnv.tree); 1339 } finally { 1340 /* 1341 * ignore any updates to hasLambdas made during 1342 * the nested scan, this ensures an initalized 1343 * LambdaToMethod is available only to those 1344 * classes that contain lambdas 1345 */ 1346 hasLambdas = prevHasLambdas; 1347 } 1348 } 1349 envForSuperTypeFound = true; 1350 } 1351 st = types.supertype(st); 1352 } 1353 super.visitClassDef(node); 1354 } 1355 @Override 1356 public void visitLambda(JCLambda tree) { 1357 hasLambdas = true; 1358 super.visitLambda(tree); 1359 } 1360 @Override 1361 public void visitReference(JCMemberReference tree) { 1362 hasLambdas = true; 1363 super.visitReference(tree); 1364 } 1365 } 1366 ScanNested scanner = new ScanNested(); 1367 scanner.scan(env.tree); 1368 for (Env<AttrContext> dep: scanner.dependencies) { 1369 if (!compileStates.isDone(dep, CompileState.FLOW)) 1370 desugaredEnvs.put(dep, desugar(flow(attribute(dep)))); 1371 } 1372 1373 //We need to check for error another time as more classes might 1374 //have been attributed and analyzed at this stage 1375 if (shouldStop(CompileState.TRANSTYPES)) 1376 return; 1377 1378 if (verboseCompilePolicy) 1379 printNote("[desugar " + env.enclClass.sym + "]"); 1380 1381 JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? 1382 env.enclClass.sym.sourcefile : 1383 env.toplevel.sourcefile); 1384 try { 1385 //save tree prior to rewriting 1386 JCTree untranslated = env.tree; 1387 1388 make.at(Position.FIRSTPOS); 1389 TreeMaker localMake = make.forToplevel(env.toplevel); 1390 1391 if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF)) { 1392 if (!(sourceOutput)) { 1393 if (shouldStop(CompileState.LOWER)) 1394 return; 1395 List<JCTree> pdef = lower.translateTopLevelClass(env, env.tree, localMake); 1396 if (pdef.head != null) { 1397 Assert.check(pdef.tail.isEmpty()); 1398 results.add(new Pair<>(env, (JCClassDecl)pdef.head)); 1399 } 1400 } 1401 return; 1402 } 1403 1404 if (shouldStop(CompileState.TRANSTYPES)) 1405 return; 1406 1407 env.tree = transTypes.translateTopLevelClass(env.tree, localMake); 1408 compileStates.put(env, CompileState.TRANSTYPES); 1409 1410 if (source.allowLambda() && scanner.hasLambdas) { 1411 if (shouldStop(CompileState.UNLAMBDA)) 1412 return; 1413 1414 env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake); 1415 compileStates.put(env, CompileState.UNLAMBDA); 1416 } 1417 1418 if (shouldStop(CompileState.LOWER)) 1419 return; 1420 1421 if (sourceOutput) { 1422 //emit standard Java source file, only for compilation 1423 //units enumerated explicitly on the command line 1424 JCClassDecl cdef = (JCClassDecl)env.tree; 1425 if (untranslated instanceof JCClassDecl && 1426 rootClasses.contains((JCClassDecl)untranslated)) { 1427 results.add(new Pair<>(env, cdef)); 1428 } 1429 return; 1430 } 1431 1432 //translate out inner classes 1433 List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake); 1434 compileStates.put(env, CompileState.LOWER); 1435 1436 if (shouldStop(CompileState.LOWER)) 1437 return; 1438 1439 //generate code for each class 1440 for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) { 1441 JCClassDecl cdef = (JCClassDecl)l.head; 1442 results.add(new Pair<>(env, cdef)); 1443 } 1444 } 1445 finally { 1446 log.useSource(prev); 1447 } 1448 1449 } 1450 1451 /** Generates the source or class file for a list of classes. 1452 * The decision to generate a source file or a class file is 1453 * based upon the compiler's options. 1454 * Generation stops if an error occurs while writing files. 1455 */ 1456 public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) { 1457 generate(queue, null); 1458 } 1459 1460 public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) { 1461 if (shouldStop(CompileState.GENERATE)) 1462 return; 1463 1464 for (Pair<Env<AttrContext>, JCClassDecl> x: queue) { 1465 Env<AttrContext> env = x.fst; 1466 JCClassDecl cdef = x.snd; 1467 1468 if (verboseCompilePolicy) { 1469 printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]"); 1470 } 1471 1472 if (!taskListener.isEmpty()) { 1473 TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym); 1474 taskListener.started(e); 1475 } 1476 1477 JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? 1478 env.enclClass.sym.sourcefile : 1479 env.toplevel.sourcefile); 1480 try { 1481 JavaFileObject file; 1482 if (sourceOutput) { 1483 file = printSource(env, cdef); 1484 } else { 1485 if (fileManager.hasLocation(StandardLocation.NATIVE_HEADER_OUTPUT) 1486 && jniWriter.needsHeader(cdef.sym)) { 1487 jniWriter.write(cdef.sym); 1488 } 1489 file = genCode(env, cdef); 1490 } 1491 if (results != null && file != null) 1492 results.add(file); 1493 } catch (IOException ex) { 1494 log.error(cdef.pos(), "class.cant.write", 1495 cdef.sym, ex.getMessage()); 1496 return; 1497 } finally { 1498 log.useSource(prev); 1499 } 1500 1501 if (!taskListener.isEmpty()) { 1502 TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym); 1503 taskListener.finished(e); 1504 } 1505 } 1506 } 1507 1508 // where 1509 Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) { 1510 // use a LinkedHashMap to preserve the order of the original list as much as possible 1511 Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<>(); 1512 for (Env<AttrContext> env: envs) { 1513 Queue<Env<AttrContext>> sublist = map.get(env.toplevel); 1514 if (sublist == null) { 1515 sublist = new ListBuffer<>(); 1516 map.put(env.toplevel, sublist); 1517 } 1518 sublist.add(env); 1519 } 1520 return map; 1521 } 1522 1523 JCClassDecl removeMethodBodies(JCClassDecl cdef) { 1524 final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0; 1525 class MethodBodyRemover extends TreeTranslator { 1526 @Override 1527 public void visitMethodDef(JCMethodDecl tree) { 1528 tree.mods.flags &= ~Flags.SYNCHRONIZED; 1529 for (JCVariableDecl vd : tree.params) 1530 vd.mods.flags &= ~Flags.FINAL; 1531 tree.body = null; 1532 super.visitMethodDef(tree); 1533 } 1534 @Override 1535 public void visitVarDef(JCVariableDecl tree) { 1536 if (tree.init != null && tree.init.type.constValue() == null) 1537 tree.init = null; 1538 super.visitVarDef(tree); 1539 } 1540 @Override 1541 public void visitClassDef(JCClassDecl tree) { 1542 ListBuffer<JCTree> newdefs = new ListBuffer<>(); 1543 for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) { 1544 JCTree t = it.head; 1545 switch (t.getTag()) { 1546 case CLASSDEF: 1547 if (isInterface || 1548 (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || 1549 (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang) 1550 newdefs.append(t); 1551 break; 1552 case METHODDEF: 1553 if (isInterface || 1554 (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || 1555 ((JCMethodDecl) t).sym.name == names.init || 1556 (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang) 1557 newdefs.append(t); 1558 break; 1559 case VARDEF: 1560 if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || 1561 (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang) 1562 newdefs.append(t); 1563 break; 1564 default: 1565 break; 1566 } 1567 } 1568 tree.defs = newdefs.toList(); 1569 super.visitClassDef(tree); 1570 } 1571 } 1572 MethodBodyRemover r = new MethodBodyRemover(); 1573 return r.translate(cdef); 1574 } 1575 1576 public void reportDeferredDiagnostics() { 1577 if (errorCount() == 0 1578 && annotationProcessingOccurred 1579 && implicitSourceFilesRead 1580 && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) { 1581 if (explicitAnnotationProcessingRequested()) 1582 log.warning("proc.use.implicit"); 1583 else 1584 log.warning("proc.use.proc.or.implicit"); 1585 } 1586 chk.reportDeferredDiagnostics(); 1587 if (log.compressedOutput) { 1588 log.mandatoryNote(null, "compressed.diags"); 1589 } 1590 } 1591 1592 /** Close the compiler, flushing the logs 1593 */ 1594 public void close() { 1595 rootClasses = null; 1596 finder = null; 1597 reader = null; 1598 make = null; 1599 writer = null; 1600 enter = null; 1601 if (todo != null) 1602 todo.clear(); 1603 todo = null; 1604 parserFactory = null; 1605 syms = null; 1606 source = null; 1607 attr = null; 1608 chk = null; 1609 gen = null; 1610 flow = null; 1611 transTypes = null; 1612 lower = null; 1613 annotate = null; 1614 types = null; 1615 1616 log.flush(); 1617 try { 1618 fileManager.flush(); 1619 } catch (IOException e) { 1620 throw new Abort(e); 1621 } finally { 1622 if (names != null) 1623 names.dispose(); 1624 names = null; 1625 1626 for (Closeable c: closeables) { 1627 try { 1628 c.close(); 1629 } catch (IOException e) { 1630 // When javac uses JDK 7 as a baseline, this code would be 1631 // better written to set any/all exceptions from all the 1632 // Closeables as suppressed exceptions on the FatalError 1633 // that is thrown. 1634 JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close"); 1635 throw new FatalError(msg, e); 1636 } 1637 } 1638 closeables = List.nil(); 1639 } 1640 } 1641 1642 protected void printNote(String lines) { 1643 log.printRawLines(Log.WriterKind.NOTICE, lines); 1644 } 1645 1646 /** Print numbers of errors and warnings. 1647 */ 1648 public void printCount(String kind, int count) { 1649 if (count != 0) { 1650 String key; 1651 if (count == 1) 1652 key = "count." + kind; 1653 else 1654 key = "count." + kind + ".plural"; 1655 log.printLines(WriterKind.ERROR, key, String.valueOf(count)); 1656 log.flush(Log.WriterKind.ERROR); 1657 } 1658 } 1659 1660 private static long now() { 1661 return System.currentTimeMillis(); 1662 } 1663 1664 private static long elapsed(long then) { 1665 return now() - then; 1666 } 1667 1668 public void newRound() { 1669 inputFiles.clear(); 1670 todo.clear(); 1671 } 1672} 1673