通过代码将 Java 文件(从 JCodeModel)转换为 Java class(而不是 Javac 等)

Convert Java file (from JCodeModel) to Java class by code (instead of Javac etc.)

我有以下代码,它使用 XSD 文件创建 Java 文件。现在,我需要将 Java 文件(来自 JCodeModel)转换为 Java class,我可以在我的项目中 create/use。不幸的是,它是在运行时创建的,所以我无法将它添加到项目中。重要的是,解决方案是基于代码的。那么,如何通过代码将 Java 文件转换为 Java class(而不是使用 Javac 等)?

public static void xsdStructure() throws Exception
{
    String directory = "D:/PROJEKTE/";

    SchemaCompiler sc = XJC.createSchemaCompiler();
    sc.forcePackageName("de.monster.jaxb");

    File myXsd = new File("sql.xsd");
    InputSource source = new InputSource(myXsd.toURI().toString());

    sc.parseSchema(source);
    S2JJAXBModel model = sc.bind();
    JCodeModel jCodeModel = model.generateCode(null, null);
    jCodeModel.build(new File(directory));
}

jCodeModel.build(new File(directory)); 生成的文件如下所示:

public class QUERY {

@XmlElement(name = "SELECT", required = true)
protected QUERY.SELECT select;
@XmlElement(name = "TABLE")
protected List<QUERY.TABLE> table;
@XmlElement(name = "JOIN")
protected List<QUERY.JOIN> join;
@XmlElement(name = "WHERE", required = true)
protected String where;
@XmlElement(name = "GROUP", required = true)
protected QUERY.GROUP group;
@XmlElement(name = "ORDER", required = true)
protected QUERY.ORDER order;
@XmlAttribute(name = "author")
protected String author;

public QUERY.SELECT getSELECT() {
    return select;
}

public void setSELECT(QUERY.SELECT value) {
    this.select = value;
}

public List<QUERY.TABLE> getTABLE() {
    if (table == null) {
        table = new ArrayList<QUERY.TABLE>();
    }
    return this.table;
}

public List<QUERY.JOIN> getJOIN() {
    if (join == null) {
        join = new ArrayList<QUERY.JOIN>();
    }
    return this.join;
}

public String getWHERE() {
    return where;
}

public void setWHERE(String value) {
    this.where = value;
}

public QUERY.GROUP getGROUP() {
    return group;
}

public void setGROUP(QUERY.GROUP value) {
    this.group = value;
}

public QUERY.ORDER getORDER() {
    return order;
}

public void setORDER(QUERY.ORDER value) {
    this.order = value;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String value) {
    this.author = value;
}

...

StandardJavaFileManager

每个实现此接口的编译器都提供了一个标准文件管理器来操作常规文件。 StandardJavaFileManager 接口定义了用于从常规文件创建文件对象的附加方法。

http://docs.oracle.com/javase/8/docs/api/javax/tools/JavaCompiler.html