如何在运行时以编程方式添加xjc插件?

如何在运行时以编程方式添加xjc插件?,第1张

如何在运行时以编程方式添加xjc插件

这是该

#generateCode()
方法的实际实现在2.2.11版中的样子

// com.sun.tools.xjc.api.impl.s2j.JAXBModelImpl.javapublic JCodeModel generateCode(Plugin[] extensions,ErrorListener errorListener) {    // we no longer do any pre generation    return outline.getCodeModel();}

如您所见,参数只是被吞没了。

要添加插件,您要访问中

Options
找到的插件,然后在其中
SchemaCompiler
添加插件。另请注意,
parseArgument()
调用应在选项而不是插件上进行。

这是完成此 *** 作的方法:

SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();schemaCompiler.forcePackageName(packageRoot);// JAXB Plugin used to get the proper @Size annotations on all fields.JaxbValidationsPlugins jaxbValidationPlugin = new JaxbValidationsPlugins();// Build up list of options for the plugin.// First option must be the name of the plugin itself.// Options must be prefixed with dashesString[] args = new String[] { "-" + JaxbValidationsPlugins.PLUGIN_OPTION_NAME };// Get the options for the schema compiler, this is where we add plugins.Options schemaCompilerOptions = ((SchemaCompilerImpl) schemaCompiler).getOptions();schemaCompilerOptions.getAllPlugins().add(jaxbValidationPlugin);// Call the parseArgument method on the options, not the plugin// Passing in zero because we want to parse the first argument in the arraytry {    schemaCompilerOptions.parseArgument(args, 0);} catch (BadCommandLineException e1) {    e1.printStackTrace();}InputSource inputSource = new InputSource(schemaFile.toURI().toString());schemaCompiler.parseSchema(inputSource);S2JJAXBModel model = schemaCompiler.bind();JCodeModel jCodeModel = model.generateCode(null, null);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5439303.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存