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

Tue, 20 Jan 2009 18:23:13 -0800

author
jjg
date
Tue, 20 Jan 2009 18:23:13 -0800
changeset 198
b4b1f7732289
parent 194
d0b33fe8e710
child 215
9d541fd2916b
permissions
-rw-r--r--

6795903: fix latent build warnings in langtools repository
Reviewed-by: darcy

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

mercurial