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