⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javactaskimpl.java

📁 是一款用JAVA 编写的编译器 具有很强的编译功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2005-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.api;import java.io.File;import java.io.IOException;import java.util.*;import java.util.concurrent.atomic.AtomicBoolean;import javax.annotation.processing.Processor;import javax.lang.model.element.Element;import javax.lang.model.element.TypeElement;import javax.lang.model.type.TypeMirror;import javax.tools.*;import com.sun.source.tree.Tree;import com.sun.source.tree.*;import com.sun.source.util.*;import com.sun.tools.javac.code.*;import com.sun.tools.javac.code.Symbol.*;import com.sun.tools.javac.comp.*;import com.sun.tools.javac.main.*;import com.sun.tools.javac.model.*;import com.sun.tools.javac.parser.Parser;import com.sun.tools.javac.parser.Scanner;import com.sun.tools.javac.tree.*;import com.sun.tools.javac.tree.JCTree.*;import com.sun.tools.javac.util.*;import com.sun.tools.javac.util.List;import com.sun.tools.javac.main.JavaCompiler;/** * Provides access to functionality specific to the Sun Java Compiler, javac. * * <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></p> * * @author Peter von der Ah&eacute; * @author Jonathan Gibbons */public class JavacTaskImpl extends JavacTask {    private JavacTool tool;    private Main compilerMain;    private JavaCompiler compiler;    private String[] args;    private Context context;    private List<JavaFileObject> fileObjects;    private Map<JavaFileObject, JCCompilationUnit> notYetEntered;    private ListBuffer<Env<AttrContext>> genList;    private TaskListener taskListener;    private AtomicBoolean used = new AtomicBoolean();    private Iterable<? extends Processor> processors;    private Integer result = null;    JavacTaskImpl(JavacTool tool,                Main compilerMain,                String[] args,                Context context,                List<JavaFileObject> fileObjects) {        this.tool = tool;        this.compilerMain = compilerMain;        this.args = args;        this.context = context;        this.fileObjects = fileObjects;        // null checks        compilerMain.getClass();        args.getClass();        context.getClass();        fileObjects.getClass();    }    JavacTaskImpl(JavacTool tool,                Main compilerMain,                Iterable<String> flags,                Context context,                Iterable<String> classes,                Iterable<? extends JavaFileObject> fileObjects) {        this(tool, compilerMain, toArray(flags, classes), context, toList(fileObjects));    }    static private String[] toArray(Iterable<String> flags, Iterable<String> classes) {        ListBuffer<String> result = new ListBuffer<String>();        if (flags != null)            for (String flag : flags)                result.append(flag);        if (classes != null)            for (String cls : classes)                result.append(cls);        return result.toArray(new String[result.length()]);    }    static private List<JavaFileObject> toList(Iterable<? extends JavaFileObject> fileObjects) {        if (fileObjects == null)            return List.nil();        ListBuffer<JavaFileObject> result = new ListBuffer<JavaFileObject>();        for (JavaFileObject fo : fileObjects)            result.append(fo);        return result.toList();    }    public Boolean call() {        if (!used.getAndSet(true)) {            beginContext();            try {                compilerMain.setFatalErrors(true);                result = compilerMain.compile(args, context, fileObjects, processors);            } finally {                endContext();            }            compilerMain = null;            args = null;            context = null;            fileObjects = null;            return result == 0;        } else {            throw new IllegalStateException("multiple calls to method 'call'");        }    }    public void setProcessors(Iterable<? extends Processor> processors) {        processors.getClass(); // null check        // not mt-safe        if (used.get())            throw new IllegalStateException();        this.processors = processors;    }    public void setLocale(Locale locale) {        // locale argument is ignored, see RFE 6443132        if (used.get())            throw new IllegalStateException();    }    private void prepareCompiler() throws IOException {        if (!used.getAndSet(true)) {            beginContext();            compilerMain.setOptions(Options.instance(context));            compilerMain.filenames = new ListBuffer<File>();            List<File> filenames = compilerMain.processArgs(CommandLine.parse(args));            if (!filenames.isEmpty())                throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));            compiler = JavaCompiler.instance(context);            // force the use of the scanner that captures Javadoc comments            com.sun.tools.javac.parser.DocCommentScanner.Factory.preRegister(context);            compiler.keepComments = true;            compiler.genEndPos = true;            // NOTE: this value will be updated after annotation processing            compiler.initProcessAnnotations(processors);            notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();            for (JavaFileObject file: fileObjects)                notYetEntered.put(file, null);            genList = new ListBuffer<Env<AttrContext>>();            // endContext will be called when all classes have been generated            // TODO: should handle the case after each phase if errors have occurred            args = null;        }    }    private void beginContext() {        context.put(JavacTaskImpl.class, this);        if (context.get(TaskListener.class) != null)            context.put(TaskListener.class, (TaskListener)null);        if (taskListener != null)            context.put(TaskListener.class, wrap(taskListener));        tool.beginContext(context);    }    // where    private TaskListener wrap(final TaskListener tl) {        tl.getClass(); // null check        return new TaskListener() {            public void started(TaskEvent e) {                try {                    tl.started(e);                } catch (Throwable t) {                    throw new ClientCodeException(t);                }            }            public void finished(TaskEvent e) {                try {                    tl.finished(e);                } catch (Throwable t) {                    throw new ClientCodeException(t);                }            }        };    }    private void endContext() {        tool.endContext();    }    /**     * Construct a JavaFileObject from the given file.     *     * <p><b>TODO: this method is useless here</b></p>     *     * @param file a file     * @return a JavaFileObject from the standard file manager.     */    public JavaFileObject asJavaFileObject(File file) {        JavacFileManager fm = (JavacFileManager)context.get(JavaFileManager.class);        return fm.getRegularFile(file);    }    public void setTaskListener(TaskListener taskListener) {        this.taskListener = taskListener;    }    /**     * Parse the specified files returning a list of abstract syntax trees.     *     * @throws java.io.IOException TODO     * @return a list of abstract syntax trees     */    public Iterable<? extends CompilationUnitTree> parse() throws IOException {        try {            prepareCompiler();            List<JCCompilationUnit> units = compiler.parseFiles(fileObjects);            for (JCCompilationUnit unit: units) {                JavaFileObject file = unit.getSourceFile();                if (notYetEntered.containsKey(file))                    notYetEntered.put(file, unit);            }            return units;        }        finally {            parsed = true;            if (compiler != null && compiler.log != null)                compiler.log.flush();        }    }    private boolean parsed = false;    /**     * Translate all the abstract syntax trees to elements.     *     * @throws IOException TODO     * @return a list of elements corresponding to the top level     * classes in the abstract syntax trees     */

⌨️ 快捷键说明

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