main.java

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

JAVA
499
字号
            if (target.compareTo(source.requiredTarget()) < 0) {                if (targetString != null) {                    if (sourceString == null) {                        warning("warn.target.default.source.conflict",                                targetString,                                source.requiredTarget().name);                    } else {                        warning("warn.source.target.conflict",                                sourceString,                                source.requiredTarget().name);                    }                    return null;                } else {                    options.put("-target", source.requiredTarget().name);                }            } else {                if (targetString == null && !source.allowGenerics()) {                    options.put("-target", Target.JDK1_4.name);                }            }        }        return filenames.toList();    }    // where        private boolean checkDirectory(String optName) {            String value = options.get(optName);            if (value == null)                return true;            File file = new File(value);            if (!file.exists()) {                error("err.dir.not.found", value);                return false;            }            if (!file.isDirectory()) {                error("err.file.not.directory", value);                return false;            }            return true;        }    /** Programmatic interface for main function.     * @param args    The command line parameters.     */    public int compile(String[] args) {        Context context = new Context();        JavacFileManager.preRegister(context); // can't create it until Log has been set up        int result = compile(args, context);        if (fileManager instanceof JavacFileManager) {            // A fresh context was created above, so jfm must be a JavacFileManager            ((JavacFileManager)fileManager).close();        }        return result;    }    public int compile(String[] args, Context context) {        return compile(args, context, List.<JavaFileObject>nil(), null);    }    /** Programmatic interface for main function.     * @param args    The command line parameters.     */    public int compile(String[] args,                       Context context,                       List<JavaFileObject> fileObjects,                       Iterable<? extends Processor> processors)    {        if (options == null)            options = Options.instance(context); // creates a new one        filenames = new ListBuffer<File>();        classnames = new ListBuffer<String>();        JavaCompiler comp = null;        /*         * TODO: Logic below about what is an acceptable command line         * should be updated to take annotation processing semantics         * into account.         */        try {            if (args.length == 0 && fileObjects.isEmpty()) {                help();                return EXIT_CMDERR;            }            List<File> filenames;            try {                filenames = processArgs(CommandLine.parse(args));                if (filenames == null) {                    // null signals an error in options, abort                    return EXIT_CMDERR;                } else if (filenames.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {                    // it is allowed to compile nothing if just asking for help or version info                    if (options.get("-help") != null                        || options.get("-X") != null                        || options.get("-version") != null                        || options.get("-fullversion") != 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;            }            boolean forceStdOut = options.get("stdout") != null;            if (forceStdOut) {                out.flush();                out = new PrintWriter(System.out, true);            }            context.put(Log.outKey, out);            fileManager = context.get(JavaFileManager.class);            comp = JavaCompiler.instance(context);            if (comp == null) return EXIT_SYSERR;            if (!filenames.isEmpty()) {                // add filenames to fileObjects                comp = JavaCompiler.instance(context);                List<JavaFileObject> otherFiles = List.nil();                JavacFileManager dfm = (JavacFileManager)fileManager;                for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(filenames))                    otherFiles = otherFiles.prepend(fo);                for (JavaFileObject fo : otherFiles)                    fileObjects = fileObjects.prepend(fo);            }            comp.compile(fileObjects,                         classnames.toList(),                         processors);            if (comp.errorCount() != 0 ||                options.get("-Werror") != null && comp.warningCount() != 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(AnnotationProcessingError ex) {            apMessage(ex);            return EXIT_SYSERR;        } catch (ClientCodeException ex) {            // as specified by javax.tools.JavaCompiler#getTask            // and javax.tools.JavaCompiler.CompilationTask#call            throw new RuntimeException(ex.getCause());        } catch (PropagatedException ex) {            throw ex.getCause();        } catch (Throwable ex) {            // Nasty.  If we've already reported an error, compensate            // for buggy compiler error recovery by swallowing thrown            // exceptions.            if (comp == null || comp.errorCount() == 0 ||                options == null || options.get("dev") != null)                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"));//      System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG        ex.printStackTrace(out);    }    /** Print a message reporting an uncaught exception from an     * annotation processor.     */    void apMessage(AnnotationProcessingError ex) {        Log.printLines(out,                       getLocalizedString("msg.proc.annotation.uncaught.exception"));        ex.getCause().printStackTrace();    }    private JavaFileManager fileManager;    /* ************************************************************************     * Internationalization     *************************************************************************/    /** Find a localized string in the resource bundle.     *  @param key     The key for the localized string.     */    public static String getLocalizedString(String key, Object... args) { // FIXME sb private        try {            if (messages == null)                messages = new Messages(javacBundleName);            return messages.getLocalizedString("javac." + key, args);        }        catch (MissingResourceException e) {            throw new Error("Fatal Error: Resource for javac is missing", e);        }    }    public static void useRawMessages(boolean enable) {        if (enable) {            messages = new Messages(javacBundleName) {                    public String getLocalizedString(String key, Object... args) {                        return key;                    }                };        } else {            messages = new Messages(javacBundleName);        }    }    private static final String javacBundleName =        "com.sun.tools.javac.resources.javac";    private static Messages messages;}

⌨️ 快捷键说明

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