📄 main.java
字号:
argsNameKey); } boolean hasArg() { return false; } boolean process(String option) { return super.process(name, option.substring( name.length())); } } , new XOption("-Xmaxerrs", "opt.arg.number", "opt.maxerrs"), new XOption("-Xmaxwarns", "opt.arg.number", "opt.maxwarns"), new XOption("-Xswitchcheck", "opt.switchcheck"), new XOption("-Xstdout", "opt.arg.file", "opt.Xstdout") { boolean process(String option, String arg) { try { out = new PrintWriter( new FileWriter(arg) , true); } catch (java.io.IOException e) { error("err.error.writing.file", arg, e.toString()); return true; } return super.process(option, arg); } } , new XOption("-X", "opt.X") { boolean process(String option) { Main.this.xhelp(); return super.process(option); } } , new HiddenOption("-O"), new HiddenOption("-Xjcov"), new HiddenOption("-XD") { String s; boolean matches(String s) { this.s = s; return s.startsWith(name); } boolean process(String option) { s = s.substring(name.length()); int eq = s.indexOf('='); String key = (eq < 0) ? s : s.substring(0, eq); String value = (eq < 0) ? s : s.substring(eq + 1); options.put(key, value); return false; } } , new HiddenOption("sourcefile") { String s; boolean matches(String s) { this.s = s; return s.endsWith(".java"); } boolean process(String option) { filenames.append(s); return false; } } }; /** * Construct a compiler instance. */ public Main(String name) { this(name, new PrintWriter(System.err, true)); } /** * Construct a compiler instance. */ public Main(String name, PrintWriter out) { super(); this.ownName = name; this.out = out; } /** * A table of all options that's passed to the JavaCompiler constructor. */ private Options options = null; /** * The list of files to process */ ListBuffer filenames = null; /** * Print a string that explains usage. */ void help() { Log.printLines(out, getLocalizedString("msg.usage.header", ownName)); for (int i = 0; i < recognizedOptions.length; i++) { recognizedOptions[i].help(); } out.println(); } /** * Print a string that explains usage for X options. */ void xhelp() { for (int i = 0; i < recognizedOptions.length; i++) { recognizedOptions[i].xhelp(); } out.println(); Log.printLines(out, getLocalizedString("msg.usage.nonstandard.footer")); } /** * Report a usage error. */ void error(String key) { Log.printLines(out, ownName + ": " + getLocalizedString(key, null, null)); help(); } /** * Report a usage error. */ void error(String key, String arg) { Log.printLines(out, ownName + ": " + getLocalizedString(key, arg, null)); help(); } /** * Report a usage error. */ void error(String key, String arg0, String arg1) { Log.printLines(out, ownName + ": " + getLocalizedString(key, arg0, arg1)); help(); } /** * Process command line arguments: store all command line options * in `options' table and return all source filenames. * @param args The array of command line arguments. */ protected List processArgs(String[] flags) { int ac = 0; while (ac < flags.length) { String flag = flags[ac]; ac++; int j; for (j = 0; j < recognizedOptions.length; j++) if (recognizedOptions[j].matches(flag)) break; if (j == recognizedOptions.length) { error("err.invalid.flag", flag); return null; } Option option = recognizedOptions[j]; if (option.hasArg()) { if (ac == flags.length) { error("err.req.arg", flag); return null; } String operand = flags[ac]; ac++; if (option.process(flag, operand)) return null; } else { if (option.process(flag)) return null; } } return filenames.toList(); } /** * Programmatic interface for main function. * @param args The command line parameters. */ public int compile(String[] args) { Context context = new Context(); options = Options.instance(context); filenames = new ListBuffer(); JavaCompiler comp = null; try { if (args.length == 0) { help(); return EXIT_CMDERR; } processArgs(forcedOpts); List filenames; try { filenames = processArgs(CommandLine.parse(args)); if (filenames == null) { return EXIT_CMDERR; } else if (filenames.isEmpty()) { if (options.get("-help") != null || options.get("-X") != null) return EXIT_OK; error("err.no.source.files"); return EXIT_CMDERR; } } catch (java.io.FileNotFoundException e) { Log.printLines(out, ownName + ": " + getLocalizedString("err.file.not.found", e.getMessage())); return EXIT_SYSERR; } context.put(Log.outKey, out); comp = JavaCompiler.make(context); if (comp == null) return EXIT_SYSERR; List cs = comp.compile(filenames); if (comp.errorCount() != 0) return EXIT_ERROR; } catch (IOException ex) { ioMessage(ex); return EXIT_SYSERR; } catch (OutOfMemoryError ex) { resourceMessage(ex); return EXIT_SYSERR; } catch (StackOverflowError ex) { resourceMessage(ex); return EXIT_SYSERR; } catch (FatalError ex) { feMessage(ex); return EXIT_SYSERR; } catch (Throwable ex) { bugMessage(ex); return EXIT_ABNORMAL; } finally { if (comp != null) comp.close(); filenames = null; options = null; } return EXIT_OK; } /** * Print a message reporting an internal error. */ void bugMessage(Throwable ex) { Log.printLines(out, getLocalizedString("msg.bug", JavaCompiler.version())); ex.printStackTrace(out); } /** * Print a message reporting an fatal error. */ void feMessage(Throwable ex) { Log.printLines(out, ex.getMessage()); } /** * Print a message reporting an input/output error. */ void ioMessage(Throwable ex) { Log.printLines(out, getLocalizedString("msg.io")); ex.printStackTrace(out); } /** * Print a message reporting an out-of-resources error. */ void resourceMessage(Throwable ex) { Log.printLines(out, getLocalizedString("msg.resource")); ex.printStackTrace(out); } /** * Find a localized string in the resource bundle. * @param key The key for the localized string. */ private static String getLocalizedString(String key) { return getText("javac." + key, null, null); } private static String getLocalizedString(String key, String arg0) { return getText("javac." + key, arg0, null); } private static String getLocalizedString(String key, String arg0, String arg1) { return getText("javac." + key, arg0, arg1); } private static final String javacRB = "com.sun.tools.javac.v8.resources.javac"; private static ResourceBundle messageRB; /** * Initialize ResourceBundle. */ private static void initResource() { try { messageRB = ResourceBundle.getBundle(javacRB); } catch (MissingResourceException e) { Error x = new FatalError("Fatal Error: Resource for javac is missing"); x.initCause(e); throw x; } } /** * Get and format message string from resource. */ private static String getText(String key, String arg0, String arg1) { if (messageRB == null) initResource(); try { String[] args = {arg0, arg1}; return MessageFormat.format(messageRB.getString(key), args); } catch (MissingResourceException e) { if (arg0 == null) arg0 = "null"; if (arg1 == null) arg1 = "null"; String[] args = {key, arg0, arg1}; String msg = "javac message file broken: key={0} arguments={1}, {2}"; return MessageFormat.format(msg, args); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -