In short
Is there any JAXB binding that can tell the JAXB code generator to generate a Java class as abstract without having to mark the corresponding XML type as abstract in the XSD?
Description
The situation is as follows:
- I define a schema in XSD:
mySchema.xsd I use inline JAXB bindings ("inline" == "directly in the schema") to indicate the package where the JAXB classes should be generated (
my.package.jaxb):<xs:annotation> <xs:appinfo> <jxb:schemaBindings> <jxb:package name="my.package.jaxb"/> </jxb:schemaBindings> </xs:appinfo> </xs:annotation>I use inline JAXB bindings to indicate the name of the implementation class for each of my complex types (in this example
my.package.impl.MyAbstractClass,my.package.impl.MyAClassandmy.package.impl.MyBClass):<xs:complexType name="myAbstractType" abstract="true"> <xs:annotation> <xs:appinfo> <jxb:class implClass="my.package.impl.MyAbstractClass"/> </xs:appinfo> </xs:annotation> ... </xs:complexType> <xs:complexType name="myAType"> <xs:annotation> <xs:appinfo> <jxb:class implClass="my.package.impl.MyAClass"/> </xs:appinfo> </xs:annotation> <xs:complexContent> <xs:extension base="myAbstractType"> ... </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="myBType"> <xs:annotation> <xs:appinfo> <jxb:class implClass="my.package.impl.MyBClass"/> </xs:appinfo> </xs:annotation> <xs:complexContent> <xs:extension base="myAbstractType"> ... </xs:extension> </xs:complexContent> </xs:complexType>I generate the JAXB class from the schema. This results in:
my.package.jaxb |- MyAbstractType |- MyAType (extends MyAbstractClass) |- MyBType (extends MyAbstractClass)I write my own classes:
my.package.impl |- MyAbstractClass (extends MyAbstractType) |- MyAClass (extends MyAType) |- MyBClass (extends MyBType)
The reason I do it like this, with these 2 class hierarchies, is so that I can separate the generated code (my.package.jaxb.*) from the manual (my.package.impl.*). This way when there are changes in the XSD I can regenerate the my.package.jaxb.* classes and make a few changes in my manual my.package.impl.* classes to incorporate the new behaviour.
So far so good. The issue is that in MyAbstractClass I want to define an abstract method...
protected abstract void doSomething();
...that is then implemented differently by MyAClass and MyBClass.
However, the generated MyAType and MyBType classes now have compilation errors because they are not declared as abstract but they now inherit an abstract method (notice that they both extend MyAbstractClass).
I cannot declare them as abstract in the XSD (abstract="true") because doing so would result in the following error whenever I declare an element of type myAType or myBType in an XML:
cvc-type.2: The type definition cannot be abstract for element someElementName.
What I would like is to use some JAXB binding to tell the JAXB code generator to generate the classes MyAType and MyBType as abstract without having to mark the XML types as abstract. Is there such a binding? I haven't been able to find it so far.
Sorry for the long explanation, and thanks in advance.