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

Sat, 19 Nov 2011 15:54:04 -0800

author
jjh
date
Sat, 19 Nov 2011 15:54:04 -0800
changeset 1140
07599bd780ca
parent 1136
ae361e7f435a
child 1157
3809292620c9
permissions
-rw-r--r--

7110611: compiler message file broken for javac -fullversion
Reviewed-by: jjg

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

mercurial