main.java

来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 499 行 · 第 1/2 页

JAVA
499
字号
/* * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.tools.javac.main;import com.sun.tools.javac.util.Options;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.MissingResourceException;import com.sun.tools.javac.code.Source;import com.sun.tools.javac.jvm.Target;import com.sun.tools.javac.main.JavacOption.Option;import com.sun.tools.javac.main.RecognizedOptions.OptionHelper;import com.sun.tools.javac.util.*;import com.sun.tools.javac.processing.AnnotationProcessingError;import javax.tools.JavaFileManager;import javax.tools.JavaFileObject;import javax.annotation.processing.Processor;/** This class provides a commandline interface to the GJC compiler. * *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If *  you write code that depends on this, you do so at your own risk. *  This code and its internal interfaces are subject to change or *  deletion without notice.</b> */public class Main {    /** The name of the compiler, for use in diagnostics.     */    String ownName;    /** The writer to use for diagnostic output.     */    PrintWriter out;    /**     * If true, any command line arg errors will cause an exception.     */    boolean fatalErrors;    /** Result codes.     */    static final int        EXIT_OK = 0,        // Compilation completed with no errors.        EXIT_ERROR = 1,     // Completed but reported errors.        EXIT_CMDERR = 2,    // Bad command-line arguments        EXIT_SYSERR = 3,    // System error or resource exhaustion.        EXIT_ABNORMAL = 4;  // Compiler terminated abnormally    private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() {        public void setOut(PrintWriter out) {            Main.this.out = out;        }        public void error(String key, Object... args) {            Main.this.error(key, args);        }        public void printVersion() {            Log.printLines(out, getLocalizedString("version", ownName,  JavaCompiler.version()));        }        public void printFullVersion() {            Log.printLines(out, getLocalizedString("fullVersion", ownName,  JavaCompiler.fullVersion()));        }        public void printHelp() {            help();        }        public void printXhelp() {            xhelp();        }        public void addFile(File f) {            if (!filenames.contains(f))                filenames.append(f);        }        public void addClassName(String s) {            classnames.append(s);        }    });    /**     * 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) {        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 source files to process     */    public ListBuffer<File> filenames = null; // XXX sb protected    /** List of class files names passed on the command line     */    public ListBuffer<String> classnames = null; // XXX sb protected    /** 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);        }        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);        }        out.println();        Log.printLines(out, getLocalizedString("msg.usage.nonstandard.footer"));    }    /** Report a usage error.     */    void error(String key, Object... args) {        if (fatalErrors) {            String msg = getLocalizedString(key, args);            throw new PropagatedException(new IllegalStateException(msg));        }        warning(key, args);        Log.printLines(out, getLocalizedString("msg.usage", ownName));    }    /** Report a warning.     */    void warning(String key, Object... args) {        Log.printLines(out, ownName + ": "                       + getLocalizedString(key, args));    }    public Option getOption(String flag) {        for (Option option : recognizedOptions) {            if (option.matches(flag))                return option;        }        return null;    }    public void setOptions(Options options) {        if (options == null)            throw new NullPointerException();        this.options = options;    }    public void setFatalErrors(boolean fatalErrors) {        this.fatalErrors = fatalErrors;    }    /** Process command line arguments: store all command line options     *  in `options' table and return all source filenames.     *  @param flags    The array of command line arguments.     */    public List<File> processArgs(String[] flags) { // XXX sb protected        int ac = 0;        while (ac < flags.length) {            String flag = flags[ac];            ac++;            int j;            // quick hack to speed up file processing:             // if the option does not begin with '-', there is no need to check            // most of the compiler options.            int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1;            for (j=firstOptionToCheck; 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(options, flag, operand))                    return null;            } else {                if (option.process(options, flag))                    return null;            }        }                if (!checkDirectory("-d"))            return null;        if (!checkDirectory("-s"))            return null;        String sourceString = options.get("-source");        Source source = (sourceString != null)            ? Source.lookup(sourceString)            : Source.DEFAULT;        String targetString = options.get("-target");        Target target = (targetString != null)            ? Target.lookup(targetString)            : Target.DEFAULT;        // We don't check source/target consistency for CLDC, as J2ME        // profiles are not aligned with J2SE targets; moreover, a        // single CLDC target may have many profiles.  In addition,        // this is needed for the continued functioning of the JSR14        // prototype.        if (Character.isDigit(target.name.charAt(0))) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?