Tue, 25 Sep 2012 13:11:05 -0700
7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
jjh@1305 | 2 | * Copyright (c) 1999, 2012, 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.File; |
duke@1 | 29 | import java.io.IOException; |
duke@1 | 30 | import java.io.PrintWriter; |
jjg@535 | 31 | import java.net.URL; |
jjg@535 | 32 | import java.security.DigestInputStream; |
jjg@535 | 33 | import java.security.MessageDigest; |
jjh@1187 | 34 | import java.util.Arrays; |
jjg@1055 | 35 | import java.util.Collection; |
jjg@1055 | 36 | import java.util.LinkedHashSet; |
jjg@1055 | 37 | import java.util.Set; |
jjg@700 | 38 | import javax.tools.JavaFileManager; |
jjg@700 | 39 | import javax.tools.JavaFileObject; |
jjg@700 | 40 | import javax.annotation.processing.Processor; |
duke@1 | 41 | |
duke@1 | 42 | import com.sun.tools.javac.code.Source; |
jjg@106 | 43 | import com.sun.tools.javac.file.CacheFSInfo; |
jjg@50 | 44 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 45 | import com.sun.tools.javac.jvm.Target; |
duke@1 | 46 | import com.sun.tools.javac.util.*; |
jjg@1136 | 47 | import com.sun.tools.javac.util.Log.WriterKind; |
jjg@1136 | 48 | import com.sun.tools.javac.util.Log.PrefixKind; |
duke@1 | 49 | import com.sun.tools.javac.processing.AnnotationProcessingError; |
jjg@700 | 50 | |
jjg@1157 | 51 | import static com.sun.tools.javac.main.Option.*; |
duke@1 | 52 | |
duke@1 | 53 | /** This class provides a commandline interface to the GJC compiler. |
duke@1 | 54 | * |
jjg@581 | 55 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 56 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 57 | * This code and its internal interfaces are subject to change or |
duke@1 | 58 | * deletion without notice.</b> |
duke@1 | 59 | */ |
duke@1 | 60 | public class Main { |
duke@1 | 61 | |
duke@1 | 62 | /** The name of the compiler, for use in diagnostics. |
duke@1 | 63 | */ |
duke@1 | 64 | String ownName; |
duke@1 | 65 | |
duke@1 | 66 | /** The writer to use for diagnostic output. |
duke@1 | 67 | */ |
duke@1 | 68 | PrintWriter out; |
duke@1 | 69 | |
jjg@1135 | 70 | /** The log to use for diagnostic output. |
jjg@1135 | 71 | */ |
jjg@1135 | 72 | Log log; |
jjg@1135 | 73 | |
duke@1 | 74 | /** |
jjg@946 | 75 | * If true, certain errors will cause an exception, such as command line |
jjg@946 | 76 | * arg errors, or exceptions in user provided code. |
duke@1 | 77 | */ |
jjg@946 | 78 | boolean apiMode; |
jjg@946 | 79 | |
duke@1 | 80 | |
duke@1 | 81 | /** Result codes. |
duke@1 | 82 | */ |
jjg@1097 | 83 | public enum Result { |
jjg@1097 | 84 | OK(0), // Compilation completed with no errors. |
jjg@1097 | 85 | ERROR(1), // Completed but reported errors. |
jjg@1097 | 86 | CMDERR(2), // Bad command-line arguments |
jjg@1097 | 87 | SYSERR(3), // System error or resource exhaustion. |
jjg@1097 | 88 | ABNORMAL(4); // Compiler terminated abnormally |
jjg@1097 | 89 | |
jjg@1097 | 90 | Result(int exitCode) { |
jjg@1097 | 91 | this.exitCode = exitCode; |
jjg@1097 | 92 | } |
jjg@1097 | 93 | |
jjg@1097 | 94 | public boolean isOK() { |
jjg@1097 | 95 | return (exitCode == 0); |
jjg@1097 | 96 | } |
jjg@1097 | 97 | |
jjg@1097 | 98 | public final int exitCode; |
jjg@1097 | 99 | } |
duke@1 | 100 | |
jjg@1157 | 101 | private Option[] recognizedOptions = |
jjg@1157 | 102 | Option.getJavaCompilerOptions().toArray(new Option[0]); |
duke@1 | 103 | |
jjg@1157 | 104 | private OptionHelper optionHelper = new OptionHelper() { |
jjg@1157 | 105 | @Override |
jjg@1157 | 106 | public String get(Option option) { |
jjg@1157 | 107 | return options.get(option); |
duke@1 | 108 | } |
duke@1 | 109 | |
jjg@1157 | 110 | @Override |
jjg@1157 | 111 | public void put(String name, String value) { |
jjg@1157 | 112 | options.put(name, value); |
jjg@1157 | 113 | } |
jjg@1157 | 114 | |
jjg@1157 | 115 | @Override |
jjg@1157 | 116 | public void remove(String name) { |
jjg@1157 | 117 | options.remove(name); |
jjg@1157 | 118 | } |
jjg@1157 | 119 | |
jjg@1157 | 120 | @Override |
jjg@1157 | 121 | public Log getLog() { |
jjg@1157 | 122 | return log; |
jjg@1157 | 123 | } |
jjg@1157 | 124 | |
jjg@1157 | 125 | @Override |
jjg@1157 | 126 | public String getOwnName() { |
jjg@1157 | 127 | return ownName; |
jjg@1157 | 128 | } |
jjg@1157 | 129 | |
jjg@1157 | 130 | @Override |
duke@1 | 131 | public void error(String key, Object... args) { |
duke@1 | 132 | Main.this.error(key, args); |
duke@1 | 133 | } |
duke@1 | 134 | |
jjg@1157 | 135 | @Override |
duke@1 | 136 | public void addFile(File f) { |
jjg@1055 | 137 | filenames.add(f); |
duke@1 | 138 | } |
duke@1 | 139 | |
jjg@1157 | 140 | @Override |
duke@1 | 141 | public void addClassName(String s) { |
duke@1 | 142 | classnames.append(s); |
duke@1 | 143 | } |
duke@1 | 144 | |
jjg@1157 | 145 | }; |
duke@1 | 146 | |
duke@1 | 147 | /** |
duke@1 | 148 | * Construct a compiler instance. |
duke@1 | 149 | */ |
duke@1 | 150 | public Main(String name) { |
duke@1 | 151 | this(name, new PrintWriter(System.err, true)); |
duke@1 | 152 | } |
duke@1 | 153 | |
duke@1 | 154 | /** |
duke@1 | 155 | * Construct a compiler instance. |
duke@1 | 156 | */ |
duke@1 | 157 | public Main(String name, PrintWriter out) { |
duke@1 | 158 | this.ownName = name; |
duke@1 | 159 | this.out = out; |
duke@1 | 160 | } |
duke@1 | 161 | /** A table of all options that's passed to the JavaCompiler constructor. */ |
duke@1 | 162 | private Options options = null; |
duke@1 | 163 | |
duke@1 | 164 | /** The list of source files to process |
duke@1 | 165 | */ |
jjg@1055 | 166 | public Set<File> filenames = null; // XXX sb protected |
duke@1 | 167 | |
duke@1 | 168 | /** List of class files names passed on the command line |
duke@1 | 169 | */ |
duke@1 | 170 | public ListBuffer<String> classnames = null; // XXX sb protected |
duke@1 | 171 | |
duke@1 | 172 | /** Report a usage error. |
duke@1 | 173 | */ |
duke@1 | 174 | void error(String key, Object... args) { |
jjg@946 | 175 | if (apiMode) { |
jjg@1136 | 176 | String msg = log.localize(PrefixKind.JAVAC, key, args); |
duke@1 | 177 | throw new PropagatedException(new IllegalStateException(msg)); |
duke@1 | 178 | } |
duke@1 | 179 | warning(key, args); |
jjg@1136 | 180 | log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | /** Report a warning. |
duke@1 | 184 | */ |
duke@1 | 185 | void warning(String key, Object... args) { |
jjg@1136 | 186 | log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args)); |
duke@1 | 187 | } |
duke@1 | 188 | |
duke@1 | 189 | public Option getOption(String flag) { |
duke@1 | 190 | for (Option option : recognizedOptions) { |
duke@1 | 191 | if (option.matches(flag)) |
duke@1 | 192 | return option; |
duke@1 | 193 | } |
duke@1 | 194 | return null; |
duke@1 | 195 | } |
duke@1 | 196 | |
duke@1 | 197 | public void setOptions(Options options) { |
duke@1 | 198 | if (options == null) |
duke@1 | 199 | throw new NullPointerException(); |
duke@1 | 200 | this.options = options; |
duke@1 | 201 | } |
duke@1 | 202 | |
jjg@946 | 203 | public void setAPIMode(boolean apiMode) { |
jjg@946 | 204 | this.apiMode = apiMode; |
duke@1 | 205 | } |
duke@1 | 206 | |
duke@1 | 207 | /** Process command line arguments: store all command line options |
duke@1 | 208 | * in `options' table and return all source filenames. |
duke@1 | 209 | * @param flags The array of command line arguments. |
duke@1 | 210 | */ |
jjg@1055 | 211 | public Collection<File> processArgs(String[] flags) { // XXX sb protected |
jjh@1187 | 212 | return processArgs(flags, null); |
jjh@1187 | 213 | } |
jjh@1187 | 214 | |
jjh@1187 | 215 | public Collection<File> processArgs(String[] flags, String[] classNames) { // XXX sb protected |
duke@1 | 216 | int ac = 0; |
duke@1 | 217 | while (ac < flags.length) { |
duke@1 | 218 | String flag = flags[ac]; |
duke@1 | 219 | ac++; |
duke@1 | 220 | |
duke@1 | 221 | Option option = null; |
duke@1 | 222 | |
duke@1 | 223 | if (flag.length() > 0) { |
duke@1 | 224 | // quick hack to speed up file processing: |
duke@1 | 225 | // if the option does not begin with '-', there is no need to check |
duke@1 | 226 | // most of the compiler options. |
duke@1 | 227 | int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1; |
duke@1 | 228 | for (int j=firstOptionToCheck; j<recognizedOptions.length; j++) { |
duke@1 | 229 | if (recognizedOptions[j].matches(flag)) { |
duke@1 | 230 | option = recognizedOptions[j]; |
duke@1 | 231 | break; |
duke@1 | 232 | } |
duke@1 | 233 | } |
duke@1 | 234 | } |
duke@1 | 235 | |
duke@1 | 236 | if (option == null) { |
duke@1 | 237 | error("err.invalid.flag", flag); |
duke@1 | 238 | return null; |
duke@1 | 239 | } |
duke@1 | 240 | |
duke@1 | 241 | if (option.hasArg()) { |
duke@1 | 242 | if (ac == flags.length) { |
duke@1 | 243 | error("err.req.arg", flag); |
duke@1 | 244 | return null; |
duke@1 | 245 | } |
duke@1 | 246 | String operand = flags[ac]; |
duke@1 | 247 | ac++; |
jjg@1157 | 248 | if (option.process(optionHelper, flag, operand)) |
duke@1 | 249 | return null; |
duke@1 | 250 | } else { |
jjg@1157 | 251 | if (option.process(optionHelper, flag)) |
duke@1 | 252 | return null; |
duke@1 | 253 | } |
duke@1 | 254 | } |
duke@1 | 255 | |
jjh@1187 | 256 | if (this.classnames != null && classNames != null) { |
jjh@1187 | 257 | this.classnames.addAll(Arrays.asList(classNames)); |
jjh@1187 | 258 | } |
jjh@1187 | 259 | |
jjg@700 | 260 | if (!checkDirectory(D)) |
duke@1 | 261 | return null; |
jjg@700 | 262 | if (!checkDirectory(S)) |
duke@1 | 263 | return null; |
duke@1 | 264 | |
jjg@700 | 265 | String sourceString = options.get(SOURCE); |
duke@1 | 266 | Source source = (sourceString != null) |
duke@1 | 267 | ? Source.lookup(sourceString) |
duke@1 | 268 | : Source.DEFAULT; |
jjg@700 | 269 | String targetString = options.get(TARGET); |
duke@1 | 270 | Target target = (targetString != null) |
duke@1 | 271 | ? Target.lookup(targetString) |
duke@1 | 272 | : Target.DEFAULT; |
duke@1 | 273 | // We don't check source/target consistency for CLDC, as J2ME |
duke@1 | 274 | // profiles are not aligned with J2SE targets; moreover, a |
duke@1 | 275 | // single CLDC target may have many profiles. In addition, |
duke@1 | 276 | // this is needed for the continued functioning of the JSR14 |
duke@1 | 277 | // prototype. |
duke@1 | 278 | if (Character.isDigit(target.name.charAt(0))) { |
duke@1 | 279 | if (target.compareTo(source.requiredTarget()) < 0) { |
duke@1 | 280 | if (targetString != null) { |
duke@1 | 281 | if (sourceString == null) { |
duke@1 | 282 | warning("warn.target.default.source.conflict", |
duke@1 | 283 | targetString, |
duke@1 | 284 | source.requiredTarget().name); |
duke@1 | 285 | } else { |
duke@1 | 286 | warning("warn.source.target.conflict", |
duke@1 | 287 | sourceString, |
duke@1 | 288 | source.requiredTarget().name); |
duke@1 | 289 | } |
duke@1 | 290 | return null; |
duke@1 | 291 | } else { |
jrose@267 | 292 | target = source.requiredTarget(); |
jrose@267 | 293 | options.put("-target", target.name); |
duke@1 | 294 | } |
duke@1 | 295 | } else { |
duke@1 | 296 | if (targetString == null && !source.allowGenerics()) { |
jrose@267 | 297 | target = Target.JDK1_4; |
jrose@267 | 298 | options.put("-target", target.name); |
duke@1 | 299 | } |
duke@1 | 300 | } |
duke@1 | 301 | } |
jjg@535 | 302 | |
jjg@535 | 303 | // handle this here so it works even if no other options given |
jjg@535 | 304 | String showClass = options.get("showClass"); |
jjg@535 | 305 | if (showClass != null) { |
jjg@535 | 306 | if (showClass.equals("showClass")) // no value given for option |
jjg@535 | 307 | showClass = "com.sun.tools.javac.Main"; |
jjg@535 | 308 | showClass(showClass); |
jjg@535 | 309 | } |
jjg@535 | 310 | |
jjg@1135 | 311 | options.notifyListeners(); |
jjg@1135 | 312 | |
jjg@1055 | 313 | return filenames; |
duke@1 | 314 | } |
duke@1 | 315 | // where |
jjg@1157 | 316 | private boolean checkDirectory(Option option) { |
jjg@1157 | 317 | String value = options.get(option); |
duke@1 | 318 | if (value == null) |
duke@1 | 319 | return true; |
duke@1 | 320 | File file = new File(value); |
duke@1 | 321 | if (!file.exists()) { |
duke@1 | 322 | error("err.dir.not.found", value); |
duke@1 | 323 | return false; |
duke@1 | 324 | } |
duke@1 | 325 | if (!file.isDirectory()) { |
duke@1 | 326 | error("err.file.not.directory", value); |
duke@1 | 327 | return false; |
duke@1 | 328 | } |
duke@1 | 329 | return true; |
duke@1 | 330 | } |
duke@1 | 331 | |
duke@1 | 332 | /** Programmatic interface for main function. |
duke@1 | 333 | * @param args The command line parameters. |
duke@1 | 334 | */ |
jjg@1097 | 335 | public Result compile(String[] args) { |
duke@1 | 336 | Context context = new Context(); |
duke@1 | 337 | JavacFileManager.preRegister(context); // can't create it until Log has been set up |
jjg@1097 | 338 | Result result = compile(args, context); |
duke@1 | 339 | if (fileManager instanceof JavacFileManager) { |
duke@1 | 340 | // A fresh context was created above, so jfm must be a JavacFileManager |
duke@1 | 341 | ((JavacFileManager)fileManager).close(); |
duke@1 | 342 | } |
duke@1 | 343 | return result; |
duke@1 | 344 | } |
duke@1 | 345 | |
jjg@1097 | 346 | public Result compile(String[] args, Context context) { |
duke@1 | 347 | return compile(args, context, List.<JavaFileObject>nil(), null); |
duke@1 | 348 | } |
duke@1 | 349 | |
duke@1 | 350 | /** Programmatic interface for main function. |
duke@1 | 351 | * @param args The command line parameters. |
duke@1 | 352 | */ |
jjg@1097 | 353 | public Result compile(String[] args, |
duke@1 | 354 | Context context, |
duke@1 | 355 | List<JavaFileObject> fileObjects, |
duke@1 | 356 | Iterable<? extends Processor> processors) |
duke@1 | 357 | { |
jjh@1187 | 358 | return compile(args, null, context, fileObjects, processors); |
jjh@1187 | 359 | } |
jjh@1187 | 360 | |
jjh@1187 | 361 | public Result compile(String[] args, |
jjh@1187 | 362 | String[] classNames, |
jjh@1187 | 363 | Context context, |
jjh@1187 | 364 | List<JavaFileObject> fileObjects, |
jjh@1187 | 365 | Iterable<? extends Processor> processors) |
jjh@1187 | 366 | { |
jjg@1135 | 367 | context.put(Log.outKey, out); |
jjg@1135 | 368 | log = Log.instance(context); |
jjg@1135 | 369 | |
duke@1 | 370 | if (options == null) |
duke@1 | 371 | options = Options.instance(context); // creates a new one |
duke@1 | 372 | |
jjg@1055 | 373 | filenames = new LinkedHashSet<File>(); |
duke@1 | 374 | classnames = new ListBuffer<String>(); |
duke@1 | 375 | JavaCompiler comp = null; |
duke@1 | 376 | /* |
duke@1 | 377 | * TODO: Logic below about what is an acceptable command line |
duke@1 | 378 | * should be updated to take annotation processing semantics |
duke@1 | 379 | * into account. |
duke@1 | 380 | */ |
duke@1 | 381 | try { |
jjh@1187 | 382 | if (args.length == 0 |
jjh@1187 | 383 | && (classNames == null || classNames.length == 0) |
jjh@1187 | 384 | && fileObjects.isEmpty()) { |
jjg@1157 | 385 | Option.HELP.process(optionHelper, "-help"); |
jjg@1097 | 386 | return Result.CMDERR; |
duke@1 | 387 | } |
duke@1 | 388 | |
jjg@1055 | 389 | Collection<File> files; |
duke@1 | 390 | try { |
jjh@1187 | 391 | files = processArgs(CommandLine.parse(args), classNames); |
jjg@194 | 392 | if (files == null) { |
duke@1 | 393 | // null signals an error in options, abort |
jjg@1097 | 394 | return Result.CMDERR; |
jjg@194 | 395 | } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { |
duke@1 | 396 | // it is allowed to compile nothing if just asking for help or version info |
jjg@700 | 397 | if (options.isSet(HELP) |
jjg@700 | 398 | || options.isSet(X) |
jjg@700 | 399 | || options.isSet(VERSION) |
jjg@700 | 400 | || options.isSet(FULLVERSION)) |
jjg@1097 | 401 | return Result.OK; |
jjg@902 | 402 | if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { |
jjg@902 | 403 | error("err.no.source.files.classes"); |
jjg@902 | 404 | } else { |
jjg@902 | 405 | error("err.no.source.files"); |
jjg@902 | 406 | } |
jjg@1097 | 407 | return Result.CMDERR; |
duke@1 | 408 | } |
duke@1 | 409 | } catch (java.io.FileNotFoundException e) { |
jjg@1136 | 410 | warning("err.file.not.found", e.getMessage()); |
jjg@1097 | 411 | return Result.SYSERR; |
duke@1 | 412 | } |
duke@1 | 413 | |
jjg@700 | 414 | boolean forceStdOut = options.isSet("stdout"); |
duke@1 | 415 | if (forceStdOut) { |
jjg@1135 | 416 | log.flush(); |
jjg@1157 | 417 | log.setWriters(new PrintWriter(System.out, true)); |
duke@1 | 418 | } |
duke@1 | 419 | |
jjg@106 | 420 | // allow System property in following line as a Mustang legacy |
jjg@700 | 421 | boolean batchMode = (options.isUnset("nonBatchMode") |
jjg@106 | 422 | && System.getProperty("nonBatchMode") == null); |
jjg@106 | 423 | if (batchMode) |
jjg@106 | 424 | CacheFSInfo.preRegister(context); |
jjg@106 | 425 | |
duke@1 | 426 | fileManager = context.get(JavaFileManager.class); |
duke@1 | 427 | |
duke@1 | 428 | comp = JavaCompiler.instance(context); |
jjg@1097 | 429 | if (comp == null) return Result.SYSERR; |
duke@1 | 430 | |
jjg@194 | 431 | if (!files.isEmpty()) { |
duke@1 | 432 | // add filenames to fileObjects |
duke@1 | 433 | comp = JavaCompiler.instance(context); |
duke@1 | 434 | List<JavaFileObject> otherFiles = List.nil(); |
duke@1 | 435 | JavacFileManager dfm = (JavacFileManager)fileManager; |
jjg@194 | 436 | for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) |
duke@1 | 437 | otherFiles = otherFiles.prepend(fo); |
duke@1 | 438 | for (JavaFileObject fo : otherFiles) |
duke@1 | 439 | fileObjects = fileObjects.prepend(fo); |
duke@1 | 440 | } |
duke@1 | 441 | comp.compile(fileObjects, |
duke@1 | 442 | classnames.toList(), |
duke@1 | 443 | processors); |
duke@1 | 444 | |
jjg@194 | 445 | if (log.expectDiagKeys != null) { |
jjg@757 | 446 | if (log.expectDiagKeys.isEmpty()) { |
jjg@1136 | 447 | log.printRawLines("all expected diagnostics found"); |
jjg@1097 | 448 | return Result.OK; |
jjg@194 | 449 | } else { |
jjg@1136 | 450 | log.printRawLines("expected diagnostic keys not found: " + log.expectDiagKeys); |
jjg@1097 | 451 | return Result.ERROR; |
jjg@194 | 452 | } |
jjg@194 | 453 | } |
jjg@194 | 454 | |
jjg@215 | 455 | if (comp.errorCount() != 0) |
jjg@1097 | 456 | return Result.ERROR; |
duke@1 | 457 | } catch (IOException ex) { |
duke@1 | 458 | ioMessage(ex); |
jjg@1097 | 459 | return Result.SYSERR; |
duke@1 | 460 | } catch (OutOfMemoryError ex) { |
duke@1 | 461 | resourceMessage(ex); |
jjg@1097 | 462 | return Result.SYSERR; |
duke@1 | 463 | } catch (StackOverflowError ex) { |
duke@1 | 464 | resourceMessage(ex); |
jjg@1097 | 465 | return Result.SYSERR; |
duke@1 | 466 | } catch (FatalError ex) { |
duke@1 | 467 | feMessage(ex); |
jjg@1097 | 468 | return Result.SYSERR; |
jjg@946 | 469 | } catch (AnnotationProcessingError ex) { |
jjg@946 | 470 | if (apiMode) |
jjg@946 | 471 | throw new RuntimeException(ex.getCause()); |
duke@1 | 472 | apMessage(ex); |
jjg@1097 | 473 | return Result.SYSERR; |
duke@1 | 474 | } catch (ClientCodeException ex) { |
duke@1 | 475 | // as specified by javax.tools.JavaCompiler#getTask |
duke@1 | 476 | // and javax.tools.JavaCompiler.CompilationTask#call |
duke@1 | 477 | throw new RuntimeException(ex.getCause()); |
duke@1 | 478 | } catch (PropagatedException ex) { |
duke@1 | 479 | throw ex.getCause(); |
duke@1 | 480 | } catch (Throwable ex) { |
duke@1 | 481 | // Nasty. If we've already reported an error, compensate |
duke@1 | 482 | // for buggy compiler error recovery by swallowing thrown |
duke@1 | 483 | // exceptions. |
duke@1 | 484 | if (comp == null || comp.errorCount() == 0 || |
jjg@700 | 485 | options == null || options.isSet("dev")) |
duke@1 | 486 | bugMessage(ex); |
jjg@1097 | 487 | return Result.ABNORMAL; |
duke@1 | 488 | } finally { |
jjg@946 | 489 | if (comp != null) { |
jjg@946 | 490 | try { |
jjg@946 | 491 | comp.close(); |
jjg@946 | 492 | } catch (ClientCodeException ex) { |
jjg@946 | 493 | throw new RuntimeException(ex.getCause()); |
jjg@946 | 494 | } |
jjg@946 | 495 | } |
duke@1 | 496 | filenames = null; |
duke@1 | 497 | options = null; |
duke@1 | 498 | } |
jjg@1097 | 499 | return Result.OK; |
duke@1 | 500 | } |
duke@1 | 501 | |
duke@1 | 502 | /** Print a message reporting an internal error. |
duke@1 | 503 | */ |
duke@1 | 504 | void bugMessage(Throwable ex) { |
jjg@1136 | 505 | log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version()); |
jjg@1136 | 506 | ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
duke@1 | 507 | } |
duke@1 | 508 | |
jjg@663 | 509 | /** Print a message reporting a fatal error. |
duke@1 | 510 | */ |
duke@1 | 511 | void feMessage(Throwable ex) { |
jjg@1136 | 512 | log.printRawLines(ex.getMessage()); |
jjg@700 | 513 | if (ex.getCause() != null && options.isSet("dev")) { |
jjg@1136 | 514 | ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
jjg@663 | 515 | } |
duke@1 | 516 | } |
duke@1 | 517 | |
duke@1 | 518 | /** Print a message reporting an input/output error. |
duke@1 | 519 | */ |
duke@1 | 520 | void ioMessage(Throwable ex) { |
jjg@1136 | 521 | log.printLines(PrefixKind.JAVAC, "msg.io"); |
jjg@1136 | 522 | ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
duke@1 | 523 | } |
duke@1 | 524 | |
duke@1 | 525 | /** Print a message reporting an out-of-resources error. |
duke@1 | 526 | */ |
duke@1 | 527 | void resourceMessage(Throwable ex) { |
jjg@1136 | 528 | log.printLines(PrefixKind.JAVAC, "msg.resource"); |
jjg@1136 | 529 | ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
duke@1 | 530 | } |
duke@1 | 531 | |
duke@1 | 532 | /** Print a message reporting an uncaught exception from an |
duke@1 | 533 | * annotation processor. |
duke@1 | 534 | */ |
duke@1 | 535 | void apMessage(AnnotationProcessingError ex) { |
jjg@1136 | 536 | log.printLines("msg.proc.annotation.uncaught.exception"); |
jjg@1136 | 537 | ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
duke@1 | 538 | } |
duke@1 | 539 | |
jjg@535 | 540 | /** Display the location and checksum of a class. */ |
jjg@535 | 541 | void showClass(String className) { |
jjg@1136 | 542 | PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
jjg@1136 | 543 | pw.println("javac: show class: " + className); |
jjg@535 | 544 | URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); |
jjg@535 | 545 | if (url == null) |
jjg@1136 | 546 | pw.println(" class not found"); |
jjg@535 | 547 | else { |
jjg@1136 | 548 | pw.println(" " + url); |
jjg@535 | 549 | try { |
jjg@535 | 550 | final String algorithm = "MD5"; |
jjg@535 | 551 | byte[] digest; |
jjg@535 | 552 | MessageDigest md = MessageDigest.getInstance(algorithm); |
jjg@535 | 553 | DigestInputStream in = new DigestInputStream(url.openStream(), md); |
jjg@535 | 554 | try { |
jjg@535 | 555 | byte[] buf = new byte[8192]; |
jjg@535 | 556 | int n; |
jjg@535 | 557 | do { n = in.read(buf); } while (n > 0); |
jjg@535 | 558 | digest = md.digest(); |
jjg@535 | 559 | } finally { |
jjg@535 | 560 | in.close(); |
jjg@535 | 561 | } |
jjg@535 | 562 | StringBuilder sb = new StringBuilder(); |
jjg@535 | 563 | for (byte b: digest) |
jjg@535 | 564 | sb.append(String.format("%02x", b)); |
jjg@1136 | 565 | pw.println(" " + algorithm + " checksum: " + sb); |
jjg@535 | 566 | } catch (Exception e) { |
jjg@1136 | 567 | pw.println(" cannot compute digest: " + e); |
jjg@535 | 568 | } |
jjg@535 | 569 | } |
jjg@535 | 570 | } |
jjg@535 | 571 | |
duke@1 | 572 | private JavaFileManager fileManager; |
duke@1 | 573 | |
duke@1 | 574 | /* ************************************************************************ |
duke@1 | 575 | * Internationalization |
duke@1 | 576 | *************************************************************************/ |
duke@1 | 577 | |
jjg@1136 | 578 | // /** Find a localized string in the resource bundle. |
jjg@1136 | 579 | // * @param key The key for the localized string. |
jjg@1136 | 580 | // */ |
jjg@1136 | 581 | // public static String getLocalizedString(String key, Object... args) { // FIXME sb private |
jjg@1136 | 582 | // try { |
jjg@1136 | 583 | // if (messages == null) |
jjg@1136 | 584 | // messages = new JavacMessages(javacBundleName); |
jjg@1136 | 585 | // return messages.getLocalizedString("javac." + key, args); |
jjg@1136 | 586 | // } |
jjg@1136 | 587 | // catch (MissingResourceException e) { |
jjg@1136 | 588 | // throw new Error("Fatal Error: Resource for javac is missing", e); |
jjg@1136 | 589 | // } |
jjg@1136 | 590 | // } |
jjg@1136 | 591 | // |
jjg@1136 | 592 | // public static void useRawMessages(boolean enable) { |
jjg@1136 | 593 | // if (enable) { |
jjg@1136 | 594 | // messages = new JavacMessages(javacBundleName) { |
jjg@1136 | 595 | // @Override |
jjg@1136 | 596 | // public String getLocalizedString(String key, Object... args) { |
jjg@1136 | 597 | // return key; |
jjg@1136 | 598 | // } |
jjg@1136 | 599 | // }; |
jjg@1136 | 600 | // } else { |
jjg@1136 | 601 | // messages = new JavacMessages(javacBundleName); |
jjg@1136 | 602 | // } |
jjg@1136 | 603 | // } |
duke@1 | 604 | |
jjg@1136 | 605 | public static final String javacBundleName = |
duke@1 | 606 | "com.sun.tools.javac.resources.javac"; |
jjg@1136 | 607 | // |
jjg@1136 | 608 | // private static JavacMessages messages; |
duke@1 | 609 | } |