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

Mon, 17 Dec 2012 10:55:40 -0800

author
jjg
date
Mon, 17 Dec 2012 10:55:40 -0800
changeset 1457
064e372f273d
parent 1416
c0f0c41cafa0
child 1463
67b01d295cd2
permissions
-rw-r--r--

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

mercurial