0

我按照通过 Java 编译器 API 动态生成 Java 类的教程进行操作,代码可以工作,但我看到的是程序在编译后会创建一个类文件。

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.*;

public class Compiler {

    static final Logger logger = Logger.getLogger(Compiler.class.getName());
    static String sourceCode = "class HelloWorld{"
        + "public static void main (String args[]){"
        + "System.out.println (\"Hello, dynamic compilation world!\");"
        + "}"
        + "}";

    public void doCompilation() {

        SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject("HelloWorld", sourceCode);
        JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject};

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);

        Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(javaFileObjects);

        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

        CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics, null, null, compilationUnits);

        boolean status = compilerTask.call();

        if (!status) {
            for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
                System.out.format("Error on line %d in %s\n", diagnostic.getLineNumber(), diagnostic);
            }
        }
        try {
            stdFileManager.close();
        } catch (IOException ex) {
            Logger.getLogger(Compiler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {
        new Compiler().doCompilation();
    }
}

class DynamicJavaSourceCodeObject extends SimpleJavaFileObject {

    private String qualifiedName;
    private String sourceCode;

    protected DynamicJavaSourceCodeObject(String name, String code) {
        super(URI.create("string:///" + name.replaceAll("\\.", "/") + JavaFileObject.Kind.SOURCE.extension), JavaFileObject.Kind.SOURCE);
        this.qualifiedName = name;
        this.sourceCode = code;
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors)
        throws IOException {
        return sourceCode;
    }

    public String getQualifiedName() {
        return qualifiedName;
    }

    public void setQualifiedName(String qualifiedName) {
        this.qualifiedName = qualifiedName;
    }

    public String getSourceCode() {
        return sourceCode;
    }

    public void setSourceCode(String sourceCode) {
        this.sourceCode = sourceCode;
    }
}

是否有可能在调用compilerTask.call();后不创建类文件?如果是的话怎么做?

4

2 回答 2

4

对于您的工作,我会使用Janino。仅使用 . 似乎是可行的JavaCompiler,但没有很好的记录。有关使用 JavaCompiler 进行处理的示例,请参阅我添加的带有链接问题的评论。

编辑:我发现了一个使用JavaCompiler.

于 2012-04-12T06:31:05.257 回答
0

为避免 JavaCompiler 创建类文件,请使用参数:“-proc:only”

于 2015-03-17T14:55:34.050 回答