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

Sat, 10 Aug 2013 13:27:38 +0100

author
vromero
date
Sat, 10 Aug 2013 13:27:38 +0100
changeset 1942
0d9bc764cac7
parent 1860
c674b396827c
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8009640: -profile <compact> does not work when -bootclasspath specified
Reviewed-by: jjg

duke@1 1 /*
jjg@1668 2 * Copyright (c) 1999, 2013, 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@1055 38 import java.util.Set;
jjg@1416 39
jjg@1416 40 import javax.annotation.processing.Processor;
jjg@700 41 import javax.tools.JavaFileManager;
jjg@700 42 import javax.tools.JavaFileObject;
duke@1 43
jjg@1416 44 import com.sun.source.util.JavacTask;
jjg@1416 45 import com.sun.source.util.Plugin;
jjg@1463 46 import com.sun.tools.doclint.DocLint;
jjg@1463 47 import com.sun.tools.javac.api.BasicJavacTask;
duke@1 48 import com.sun.tools.javac.code.Source;
jjg@106 49 import com.sun.tools.javac.file.CacheFSInfo;
jjg@50 50 import com.sun.tools.javac.file.JavacFileManager;
jjg@1569 51 import com.sun.tools.javac.jvm.Profile;
duke@1 52 import com.sun.tools.javac.jvm.Target;
jjg@1416 53 import com.sun.tools.javac.processing.AnnotationProcessingError;
jjg@1416 54 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
duke@1 55 import com.sun.tools.javac.util.*;
jjg@1416 56 import com.sun.tools.javac.util.Log.PrefixKind;
jjg@1136 57 import com.sun.tools.javac.util.Log.WriterKind;
jjg@1728 58 import com.sun.tools.javac.util.ServiceLoader;
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
vromero@1942 265 if (options.get(PROFILE) != null && options.get(BOOTCLASSPATH) != null) {
vromero@1942 266 error("err.profile.bootclasspath.conflict");
vromero@1942 267 return null;
vromero@1942 268 }
vromero@1942 269
jjh@1187 270 if (this.classnames != null && classNames != null) {
jjh@1187 271 this.classnames.addAll(Arrays.asList(classNames));
jjh@1187 272 }
jjh@1187 273
jjg@700 274 if (!checkDirectory(D))
duke@1 275 return null;
jjg@700 276 if (!checkDirectory(S))
duke@1 277 return null;
duke@1 278
jjg@700 279 String sourceString = options.get(SOURCE);
duke@1 280 Source source = (sourceString != null)
duke@1 281 ? Source.lookup(sourceString)
duke@1 282 : Source.DEFAULT;
jjg@700 283 String targetString = options.get(TARGET);
duke@1 284 Target target = (targetString != null)
duke@1 285 ? Target.lookup(targetString)
duke@1 286 : Target.DEFAULT;
duke@1 287 // We don't check source/target consistency for CLDC, as J2ME
duke@1 288 // profiles are not aligned with J2SE targets; moreover, a
duke@1 289 // single CLDC target may have many profiles. In addition,
duke@1 290 // this is needed for the continued functioning of the JSR14
duke@1 291 // prototype.
duke@1 292 if (Character.isDigit(target.name.charAt(0))) {
duke@1 293 if (target.compareTo(source.requiredTarget()) < 0) {
duke@1 294 if (targetString != null) {
duke@1 295 if (sourceString == null) {
duke@1 296 warning("warn.target.default.source.conflict",
duke@1 297 targetString,
duke@1 298 source.requiredTarget().name);
duke@1 299 } else {
duke@1 300 warning("warn.source.target.conflict",
duke@1 301 sourceString,
duke@1 302 source.requiredTarget().name);
duke@1 303 }
duke@1 304 return null;
duke@1 305 } else {
jrose@267 306 target = source.requiredTarget();
jrose@267 307 options.put("-target", target.name);
duke@1 308 }
duke@1 309 } else {
duke@1 310 if (targetString == null && !source.allowGenerics()) {
jrose@267 311 target = Target.JDK1_4;
jrose@267 312 options.put("-target", target.name);
duke@1 313 }
duke@1 314 }
duke@1 315 }
jjg@535 316
jjg@1569 317 String profileString = options.get(PROFILE);
jjg@1569 318 if (profileString != null) {
jjg@1569 319 Profile profile = Profile.lookup(profileString);
jjg@1569 320 if (!profile.isValid(target)) {
jjg@1569 321 warning("warn.profile.target.conflict", profileString, target.name);
jjg@1569 322 return null;
jjg@1569 323 }
jjg@1569 324 }
jjg@1569 325
jjg@535 326 // handle this here so it works even if no other options given
jjg@535 327 String showClass = options.get("showClass");
jjg@535 328 if (showClass != null) {
jjg@535 329 if (showClass.equals("showClass")) // no value given for option
jjg@535 330 showClass = "com.sun.tools.javac.Main";
jjg@535 331 showClass(showClass);
jjg@535 332 }
jjg@535 333
jjg@1135 334 options.notifyListeners();
jjg@1135 335
jjg@1055 336 return filenames;
duke@1 337 }
duke@1 338 // where
jjg@1157 339 private boolean checkDirectory(Option option) {
jjg@1157 340 String value = options.get(option);
duke@1 341 if (value == null)
duke@1 342 return true;
duke@1 343 File file = new File(value);
duke@1 344 if (!file.exists()) {
duke@1 345 error("err.dir.not.found", value);
duke@1 346 return false;
duke@1 347 }
duke@1 348 if (!file.isDirectory()) {
duke@1 349 error("err.file.not.directory", value);
duke@1 350 return false;
duke@1 351 }
duke@1 352 return true;
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 = new Context();
duke@1 360 JavacFileManager.preRegister(context); // can't create it until Log has been set up
jjg@1097 361 Result result = compile(args, context);
duke@1 362 if (fileManager instanceof JavacFileManager) {
duke@1 363 // A fresh context was created above, so jfm must be a JavacFileManager
duke@1 364 ((JavacFileManager)fileManager).close();
duke@1 365 }
duke@1 366 return result;
duke@1 367 }
duke@1 368
jjg@1097 369 public Result compile(String[] args, Context context) {
duke@1 370 return compile(args, context, List.<JavaFileObject>nil(), null);
duke@1 371 }
duke@1 372
duke@1 373 /** Programmatic interface for main function.
duke@1 374 * @param args The command line parameters.
duke@1 375 */
jjg@1097 376 public Result compile(String[] args,
duke@1 377 Context context,
duke@1 378 List<JavaFileObject> fileObjects,
duke@1 379 Iterable<? extends Processor> processors)
duke@1 380 {
jjh@1187 381 return compile(args, null, context, fileObjects, processors);
jjh@1187 382 }
jjh@1187 383
jjh@1187 384 public Result compile(String[] args,
emc@1860 385 String[] classNames,
emc@1860 386 Context context,
emc@1860 387 List<JavaFileObject> fileObjects,
emc@1860 388 Iterable<? extends Processor> processors)
jjh@1187 389 {
jjg@1135 390 context.put(Log.outKey, out);
jjg@1135 391 log = Log.instance(context);
jjg@1135 392
duke@1 393 if (options == null)
duke@1 394 options = Options.instance(context); // creates a new one
duke@1 395
jjg@1055 396 filenames = new LinkedHashSet<File>();
duke@1 397 classnames = new ListBuffer<String>();
duke@1 398 JavaCompiler comp = null;
duke@1 399 /*
duke@1 400 * TODO: Logic below about what is an acceptable command line
duke@1 401 * should be updated to take annotation processing semantics
duke@1 402 * into account.
duke@1 403 */
duke@1 404 try {
jjh@1187 405 if (args.length == 0
jjh@1187 406 && (classNames == null || classNames.length == 0)
jjh@1187 407 && fileObjects.isEmpty()) {
jjg@1157 408 Option.HELP.process(optionHelper, "-help");
jjg@1097 409 return Result.CMDERR;
duke@1 410 }
duke@1 411
jjg@1055 412 Collection<File> files;
duke@1 413 try {
jjh@1187 414 files = processArgs(CommandLine.parse(args), classNames);
jjg@194 415 if (files == null) {
duke@1 416 // null signals an error in options, abort
jjg@1097 417 return Result.CMDERR;
jjg@194 418 } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
duke@1 419 // it is allowed to compile nothing if just asking for help or version info
jjg@700 420 if (options.isSet(HELP)
jjg@700 421 || options.isSet(X)
jjg@700 422 || options.isSet(VERSION)
jjg@700 423 || options.isSet(FULLVERSION))
jjg@1097 424 return Result.OK;
jjg@902 425 if (JavaCompiler.explicitAnnotationProcessingRequested(options)) {
jjg@902 426 error("err.no.source.files.classes");
jjg@902 427 } else {
jjg@902 428 error("err.no.source.files");
jjg@902 429 }
jjg@1097 430 return Result.CMDERR;
duke@1 431 }
duke@1 432 } catch (java.io.FileNotFoundException e) {
jjg@1136 433 warning("err.file.not.found", e.getMessage());
jjg@1097 434 return Result.SYSERR;
duke@1 435 }
duke@1 436
jjg@700 437 boolean forceStdOut = options.isSet("stdout");
duke@1 438 if (forceStdOut) {
jjg@1135 439 log.flush();
jjg@1157 440 log.setWriters(new PrintWriter(System.out, true));
duke@1 441 }
duke@1 442
jjg@106 443 // allow System property in following line as a Mustang legacy
jjg@700 444 boolean batchMode = (options.isUnset("nonBatchMode")
jjg@106 445 && System.getProperty("nonBatchMode") == null);
jjg@106 446 if (batchMode)
jjg@106 447 CacheFSInfo.preRegister(context);
jjg@106 448
jjg@1463 449 // FIXME: this code will not be invoked if using JavacTask.parse/analyze/generate
jjg@1416 450 // invoke any available plugins
jjg@1416 451 String plugins = options.get(PLUGIN);
jjg@1416 452 if (plugins != null) {
jjg@1416 453 JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context);
jjg@1416 454 ClassLoader cl = pEnv.getProcessorClassLoader();
jjg@1416 455 ServiceLoader<Plugin> sl = ServiceLoader.load(Plugin.class, cl);
jjg@1416 456 Set<List<String>> pluginsToCall = new LinkedHashSet<List<String>>();
jjg@1416 457 for (String plugin: plugins.split("\\x00")) {
jjg@1416 458 pluginsToCall.add(List.from(plugin.split("\\s+")));
jjg@1416 459 }
jjg@1416 460 JavacTask task = null;
jjg@1416 461 Iterator<Plugin> iter = sl.iterator();
jjg@1416 462 while (iter.hasNext()) {
jjg@1416 463 Plugin plugin = iter.next();
jjg@1416 464 for (List<String> p: pluginsToCall) {
jjg@1416 465 if (plugin.getName().equals(p.head)) {
jjg@1416 466 pluginsToCall.remove(p);
jjg@1416 467 try {
jjg@1416 468 if (task == null)
jjg@1416 469 task = JavacTask.instance(pEnv);
jjg@1457 470 plugin.init(task, p.tail.toArray(new String[p.tail.size()]));
jjg@1416 471 } catch (Throwable ex) {
jjg@1416 472 if (apiMode)
jjg@1416 473 throw new RuntimeException(ex);
jjg@1416 474 pluginMessage(ex);
jjg@1416 475 return Result.SYSERR;
jjg@1416 476 }
jjg@1416 477 }
jjg@1416 478 }
jjg@1416 479 }
jjg@1416 480 for (List<String> p: pluginsToCall) {
jjg@1416 481 log.printLines(PrefixKind.JAVAC, "msg.plugin.not.found", p.head);
jjg@1416 482 }
jjg@1416 483 }
jjg@1416 484
jjg@1463 485 comp = JavaCompiler.instance(context);
jjg@1463 486
jjg@1463 487 // FIXME: this code will not be invoked if using JavacTask.parse/analyze/generate
jjg@1463 488 String xdoclint = options.get(XDOCLINT);
jjg@1463 489 String xdoclintCustom = options.get(XDOCLINT_CUSTOM);
jjg@1463 490 if (xdoclint != null || xdoclintCustom != null) {
jjg@1463 491 Set<String> doclintOpts = new LinkedHashSet<String>();
jjg@1463 492 if (xdoclint != null)
jjg@1463 493 doclintOpts.add(DocLint.XMSGS_OPTION);
jjg@1463 494 if (xdoclintCustom != null) {
jjg@1463 495 for (String s: xdoclintCustom.split("\\s+")) {
jjg@1463 496 if (s.isEmpty())
jjg@1463 497 continue;
jjg@1463 498 doclintOpts.add(s.replace(XDOCLINT_CUSTOM.text, DocLint.XMSGS_CUSTOM_PREFIX));
jjg@1463 499 }
jjg@1463 500 }
jjg@1463 501 if (!(doclintOpts.size() == 1
jjg@1463 502 && doclintOpts.iterator().next().equals(DocLint.XMSGS_CUSTOM_PREFIX + "none"))) {
jjg@1463 503 JavacTask t = BasicJavacTask.instance(context);
jjg@1668 504 // standard doclet normally generates H1, H2
jjg@1668 505 doclintOpts.add(DocLint.XIMPLICIT_HEADERS + "2");
jjg@1463 506 new DocLint().init(t, doclintOpts.toArray(new String[doclintOpts.size()]));
jjg@1463 507 comp.keepComments = true;
jjg@1463 508 }
jjg@1463 509 }
jjg@1463 510
duke@1 511 fileManager = context.get(JavaFileManager.class);
duke@1 512
jjg@194 513 if (!files.isEmpty()) {
duke@1 514 // add filenames to fileObjects
duke@1 515 comp = JavaCompiler.instance(context);
duke@1 516 List<JavaFileObject> otherFiles = List.nil();
duke@1 517 JavacFileManager dfm = (JavacFileManager)fileManager;
jjg@194 518 for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files))
duke@1 519 otherFiles = otherFiles.prepend(fo);
duke@1 520 for (JavaFileObject fo : otherFiles)
duke@1 521 fileObjects = fileObjects.prepend(fo);
duke@1 522 }
duke@1 523 comp.compile(fileObjects,
duke@1 524 classnames.toList(),
duke@1 525 processors);
duke@1 526
jjg@194 527 if (log.expectDiagKeys != null) {
jjg@757 528 if (log.expectDiagKeys.isEmpty()) {
jjg@1136 529 log.printRawLines("all expected diagnostics found");
jjg@1097 530 return Result.OK;
jjg@194 531 } else {
jjg@1136 532 log.printRawLines("expected diagnostic keys not found: " + log.expectDiagKeys);
jjg@1097 533 return Result.ERROR;
jjg@194 534 }
jjg@194 535 }
jjg@194 536
jjg@215 537 if (comp.errorCount() != 0)
jjg@1097 538 return Result.ERROR;
duke@1 539 } catch (IOException ex) {
duke@1 540 ioMessage(ex);
jjg@1097 541 return Result.SYSERR;
duke@1 542 } catch (OutOfMemoryError ex) {
duke@1 543 resourceMessage(ex);
jjg@1097 544 return Result.SYSERR;
duke@1 545 } catch (StackOverflowError ex) {
duke@1 546 resourceMessage(ex);
jjg@1097 547 return Result.SYSERR;
duke@1 548 } catch (FatalError ex) {
duke@1 549 feMessage(ex);
jjg@1097 550 return Result.SYSERR;
jjg@946 551 } catch (AnnotationProcessingError ex) {
jjg@946 552 if (apiMode)
jjg@946 553 throw new RuntimeException(ex.getCause());
duke@1 554 apMessage(ex);
jjg@1097 555 return Result.SYSERR;
duke@1 556 } catch (ClientCodeException ex) {
duke@1 557 // as specified by javax.tools.JavaCompiler#getTask
duke@1 558 // and javax.tools.JavaCompiler.CompilationTask#call
duke@1 559 throw new RuntimeException(ex.getCause());
duke@1 560 } catch (PropagatedException ex) {
duke@1 561 throw ex.getCause();
duke@1 562 } catch (Throwable ex) {
duke@1 563 // Nasty. If we've already reported an error, compensate
duke@1 564 // for buggy compiler error recovery by swallowing thrown
duke@1 565 // exceptions.
duke@1 566 if (comp == null || comp.errorCount() == 0 ||
jjg@700 567 options == null || options.isSet("dev"))
duke@1 568 bugMessage(ex);
jjg@1097 569 return Result.ABNORMAL;
duke@1 570 } finally {
jjg@946 571 if (comp != null) {
jjg@946 572 try {
jjg@946 573 comp.close();
jjg@946 574 } catch (ClientCodeException ex) {
jjg@946 575 throw new RuntimeException(ex.getCause());
jjg@946 576 }
jjg@946 577 }
duke@1 578 filenames = null;
duke@1 579 options = null;
duke@1 580 }
jjg@1097 581 return Result.OK;
duke@1 582 }
duke@1 583
duke@1 584 /** Print a message reporting an internal error.
duke@1 585 */
duke@1 586 void bugMessage(Throwable ex) {
jjg@1136 587 log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
jjg@1136 588 ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
duke@1 589 }
duke@1 590
jjg@663 591 /** Print a message reporting a fatal error.
duke@1 592 */
duke@1 593 void feMessage(Throwable ex) {
jjg@1136 594 log.printRawLines(ex.getMessage());
jjg@700 595 if (ex.getCause() != null && options.isSet("dev")) {
jjg@1136 596 ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
jjg@663 597 }
duke@1 598 }
duke@1 599
duke@1 600 /** Print a message reporting an input/output error.
duke@1 601 */
duke@1 602 void ioMessage(Throwable ex) {
jjg@1136 603 log.printLines(PrefixKind.JAVAC, "msg.io");
jjg@1136 604 ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
duke@1 605 }
duke@1 606
duke@1 607 /** Print a message reporting an out-of-resources error.
duke@1 608 */
duke@1 609 void resourceMessage(Throwable ex) {
jjg@1136 610 log.printLines(PrefixKind.JAVAC, "msg.resource");
jjg@1136 611 ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
duke@1 612 }
duke@1 613
duke@1 614 /** Print a message reporting an uncaught exception from an
duke@1 615 * annotation processor.
duke@1 616 */
duke@1 617 void apMessage(AnnotationProcessingError ex) {
jjg@1398 618 log.printLines(PrefixKind.JAVAC, "msg.proc.annotation.uncaught.exception");
jjg@1136 619 ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
duke@1 620 }
duke@1 621
jjg@1416 622 /** Print a message reporting an uncaught exception from an
jjg@1416 623 * annotation processor.
jjg@1416 624 */
jjg@1416 625 void pluginMessage(Throwable ex) {
jjg@1416 626 log.printLines(PrefixKind.JAVAC, "msg.plugin.uncaught.exception");
jjg@1416 627 ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
jjg@1416 628 }
jjg@1416 629
jjg@535 630 /** Display the location and checksum of a class. */
jjg@535 631 void showClass(String className) {
jjg@1136 632 PrintWriter pw = log.getWriter(WriterKind.NOTICE);
jjg@1136 633 pw.println("javac: show class: " + className);
jjg@535 634 URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
jjg@535 635 if (url == null)
jjg@1136 636 pw.println(" class not found");
jjg@535 637 else {
jjg@1136 638 pw.println(" " + url);
jjg@535 639 try {
jjg@535 640 final String algorithm = "MD5";
jjg@535 641 byte[] digest;
jjg@535 642 MessageDigest md = MessageDigest.getInstance(algorithm);
jjg@535 643 DigestInputStream in = new DigestInputStream(url.openStream(), md);
jjg@535 644 try {
jjg@535 645 byte[] buf = new byte[8192];
jjg@535 646 int n;
jjg@535 647 do { n = in.read(buf); } while (n > 0);
jjg@535 648 digest = md.digest();
jjg@535 649 } finally {
jjg@535 650 in.close();
jjg@535 651 }
jjg@535 652 StringBuilder sb = new StringBuilder();
jjg@535 653 for (byte b: digest)
jjg@535 654 sb.append(String.format("%02x", b));
jjg@1136 655 pw.println(" " + algorithm + " checksum: " + sb);
jjg@535 656 } catch (Exception e) {
jjg@1136 657 pw.println(" cannot compute digest: " + e);
jjg@535 658 }
jjg@535 659 }
jjg@535 660 }
jjg@535 661
duke@1 662 private JavaFileManager fileManager;
duke@1 663
duke@1 664 /* ************************************************************************
duke@1 665 * Internationalization
duke@1 666 *************************************************************************/
duke@1 667
jjg@1136 668 // /** Find a localized string in the resource bundle.
jjg@1136 669 // * @param key The key for the localized string.
jjg@1136 670 // */
jjg@1136 671 // public static String getLocalizedString(String key, Object... args) { // FIXME sb private
jjg@1136 672 // try {
jjg@1136 673 // if (messages == null)
jjg@1136 674 // messages = new JavacMessages(javacBundleName);
jjg@1136 675 // return messages.getLocalizedString("javac." + key, args);
jjg@1136 676 // }
jjg@1136 677 // catch (MissingResourceException e) {
jjg@1136 678 // throw new Error("Fatal Error: Resource for javac is missing", e);
jjg@1136 679 // }
jjg@1136 680 // }
jjg@1136 681 //
jjg@1136 682 // public static void useRawMessages(boolean enable) {
jjg@1136 683 // if (enable) {
jjg@1136 684 // messages = new JavacMessages(javacBundleName) {
jjg@1136 685 // @Override
jjg@1136 686 // public String getLocalizedString(String key, Object... args) {
jjg@1136 687 // return key;
jjg@1136 688 // }
jjg@1136 689 // };
jjg@1136 690 // } else {
jjg@1136 691 // messages = new JavacMessages(javacBundleName);
jjg@1136 692 // }
jjg@1136 693 // }
duke@1 694
jjg@1136 695 public static final String javacBundleName =
duke@1 696 "com.sun.tools.javac.resources.javac";
jjg@1136 697 //
jjg@1136 698 // private static JavacMessages messages;
duke@1 699 }

mercurial