How to add xjc code at runtime?

I am generating JAXB classes from xsd at runtime using XJC. But by default, the maxLength constraint in xsd is not annotated.

I found a plugin to handle this, krasa-jaxb-tools . I have added dependencies to my POM, but I cannot add a plugin to the XJC process.

I am using jaxb-xjc tools version 2.2.11. Here are my dependencies:

 <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-jxc</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.github.krasa</groupId> <artifactId>krasa-jaxb-tools</artifactId> <version>1.3</version> </dependency> 

I am trying to create a plugin instance by setting it up and passing it to the generateCode(Plugins[] extensions, ErrorListener errorListener) in my S2JJAXBModel , but it does not seem to have any effect. Here is my code:

 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 dashes String[] args = new String[] { "-" + JaxbValidationsPlugins.PLUGIN_OPTION_NAME }; try { // Activate plugin jaxbValidationPlugin.parseArgument(new Options(), args, 0); } catch (BadCommandLineException | IOException e1) { e1.printStackTrace(); } InputSource inputSource = new InputSource(schemaFile.toURI().toString()); schemaCompiler.parseSchema(inputSource); S2JJAXBModel model = schemaCompiler.bind(); // Passing the plugin to the method JCodeModel jCodeModel = model.generateCode(new Plugin[] {jaxbValidationPlugin}, null); 

What am I doing wrong?

+3
java xsd jaxb xjc
Oct 26 '15 at 21:05
source share
1 answer

This is how the actual implementation of the #generateCode() method #generateCode() in version 2.2.11

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

As you can see, the parameters are simply swallowed.

To add a plugin, you want to access the Options found in SchemaCompiler and add your plugin there. Also note that parseArgument() should be executed for options, not for the plugin.

Here's how to do it:

 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 dashes String[] 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 array try { 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); 
+6
Oct 26 '15 at 21:05
source share



All Articles