0

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

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;
}

...
4

1 回答 1

0

标准Java文件管理器

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

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

于 2016-06-06T09:08:03.390 回答