我正在编写一个从JAXB创建和生成JavaFileObjects的程序。目前我已经这样做了,因此我的.java文件是使用JAXB制作的,然后加载到一个数组中。我想获取JavaFileObjects并将它们传递给JAXB并让JAXB编译并加载它们以供进一步处理。
当前生成.java文件的代码是:
String outputDirectory = jaxbPackageGenFolderCreation();
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName(getPackageName());
LOG.info("PackageName: " + getPackageName());
LOG.info("creating BINDINGS_XJB: " + MageekConstants.BINDINGS_XJB);
File bindings = new File(MageekConstants.BINDINGS_XJB);
/*
* sc.getOptions.addBindFile adds the xjc bindings file to the the schema compiler.
* This method shows deprecated, however it is not being removed from future
* versions of the API. It is not being maintained in future versions so the
* developers have it annotated as depreciated as a notification tool. Future
* stability of this call is NOT guaranteed.
*/
sc.getOptions().addBindFile(bindings);
// Setup SAX InputSource
LOG.info("schema file: " + getXsdFile());
File schemaFile = new File(getXsdFile());
InputSource is = new InputSource(schemaFile.toURI().toString());
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
String[] splitArray = null;
try
{
PrintStream printStream = new PrintStream(new File(MageekConstants.TEMP_DIR
+ "GeneratedFiles.mageek"));
jCodeModel.build(new File(outputDirectory), printStream);
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// CodeWriter codeWriter = new SingleStreamCodeWriter(baos);
// jCodeModel.build(codeWriter);
我已经研究过内存中的java编译和其他方法,但似乎没有任何东西可以做我想要的而没有任何IO到磁盘。有朋友告诉我,JAXB可以获取.java文件然后加载它们。我没有找到关于此或示例的文档。任何帮助都会很棒。
答案 0 :(得分:0)
您可以像(by answer)一样动态编译类:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, files);
请查看类似的问题以寻求其他选择: