2

I am using maven-jaxb2-plugin to create java classes from xsd. In xsd I have defined two status: Status1 and Status2. This two classes has a lot of similar variables. Because I cant edit xsd is there a option to create class Status in my project and then add change generated class to extend this class?

Example

now:

Class Status1 {...}

Class Status2 {...}
  • this is generated by maven-jaxb2-plugin

What I want:

Class Status1 extends Status {...}

Class Status2 extends Status {...}
  • this is generated by maven-jaxb2-plugin

Class Status {...} - my own class

I hope this can be done by bindings but I dont know how

UPDATE:

second option how to achieve this question is to add to class Status1 new variable. Is this possible ?

What I want:

Class Status1 {... NewVariable newVariable ...}

Class Status2 {...}
  • this is generated by maven-jaxb2-plugin
hudi
  • 15,555
  • 47
  • 142
  • 246
  • Do never change generated class files... just work with inheritance in your XSD... – home Nov 01 '12 at 09:23
  • I cant. Xsd is creating other firm and we just using their xsd to implement some web service. By the way binding are createted to change generated class and we already have some bindings to change generated classes – hudi Nov 01 '12 at 09:26
  • I agree, just thought you wanted to manually modify the generated source files, apologies. – home Nov 01 '12 at 09:28

2 Answers2

1

ok I found it: I use extend:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    jaxb:version="2.1" xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jaxb:extensionBindingPrefixes="inheritance">
    <jaxb:bindings schemaLocation="mySchema.xsd">
        <jaxb:bindings node="xsd:complexType[@name='StatusType']">
            <inheritance:extends>my.package.Status1</inheritance:extends>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

and in JAXB2 plugin I need to add:

                    <extension>true</extension>
                    <args>
                        <arg>-Xinheritance</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>
hudi
  • 15,555
  • 47
  • 142
  • 246
0

For the first part of the question see Specifying Super Class of a JAXB-Generated Class.

For the second part see Generate additional custom method with jaxb-xjc. It's about adding custom methods. But custom fields handling should be similar.

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • 1. hm I see there just global properties but I dont want to add parents to all class because I have there about 50 just these 2.hm creating xjc plugin to add just one variable to generated classes seems very difficult – hudi Nov 01 '12 at 13:08
  • Take a look at "Identifying customization targets by Schema Component Designator" http://jaxb.java.net/nonav/2.1.4/docs/vendorCustomizations.html#scd – Vadzim Nov 01 '12 at 13:29