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