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