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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1569
475eb15dfdad
child 1668
991f11e13598
permissions
-rw-r--r--

Merge

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

mercurial