src/share/classes/com/sun/tools/javac/main/Main.java

Sat, 18 Sep 2010 09:54:51 -0700

author
mcimadamore
date
Sat, 18 Sep 2010 09:54:51 -0700
changeset 688
50f9ac2f4730
parent 674
584365f256a7
child 700
7b413ac1a720
permissions
-rw-r--r--

6980862: too aggressive compiler optimization causes stale results of Types.implementation()
Summary: use a scope counter in order to determine when/if the implementation cache entries are stale
Reviewed-by: jjg

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

mercurial