-
Notifications
You must be signed in to change notification settings - Fork 99
JAXB2 Inheritance Plugin
Laurent Schoelens edited this page Nov 23, 2023
·
4 revisions
Makes schema-derived classes extend certain class or implement certain interfaces.
Activate the plugin using -Xinheritance
switch.
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<bindingDirectory>
${basedir}/src/main/resources/....
</bindingDirectory>
<extension>true</extension>
<args>
<arg>-Xinheritance</arg>
</args>
<plugins>
<!-- users of jaxb2 - use jaxb2-basics artifact -->
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb2-basics</artifactId>
</plugin>
<!-- users of jaxb3 or jaxb4 - use jaxb-plugins artifact -->
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-plugins</artifactId>
</plugin>
</plugins>
</configuration>
</execution>
- Declare the http://jaxb2-commons.dev.java.net/basic/inheritance customization namespace (or starting with v3 of the plugin, the new
urn:jaxb.jvnet.org:plugin:inheritance
namespace). - Use
<inheritance:extends>com.acme.foo.MyClass</inheritance:extends>
or<inheritance:implements>com.acme.foo.MyInterface</inheritance:implements>
customization elements to specify which classes or interfaces should your schema-derived class extend or implement. - Use
<inheritance:objectFactory packageName="com.acme.foo"/>
to customize the object factory class.
Works with class outlines, enum outlines, element and package outlines.
schema.xsd
example file :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="inheritance">
<!-- ... -->
<xs:annotation>
<xs:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="com.acme.foo" />
</jaxb:schemaBindings>
<inheritance:objectFactory packageName="com.acme.foo">
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</inheritance:objectFactory>
</xs:appinfo>
</xs:annotation>
<!-- ... -->
<xs:complexType name="WillBeMadeCloneableType">
<xs:annotation>
<xs:appinfo>
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</xs:appinfo>
</xs:annotation>
<!-- ... -->
</xs:complexType>
<!-- ... -->
</xs:schema>
bindings.xjb
example file :
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jaxb:extensionBindingPrefixes="inheritance"
jaxb:version="2.1">
<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
<jaxb:bindings node="xsd:simpleType[@name='issueJIIB38Type']">
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</jaxb:bindings>
<jaxb:bindings node="xsd:element[@name='issueJIIB38']">
<jaxb:class/>
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
schema.xsd
example file :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:inheritance="urn:jaxb.jvnet.org:plugin:inheritance"
jaxb:version="3.0"
jaxb:extensionBindingPrefixes="inheritance">
<!-- ... -->
<xs:annotation>
<xs:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="com.acme.foo" />
</jaxb:schemaBindings>
<inheritance:objectFactory packageName="com.acme.foo">
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</inheritance:objectFactory>
</xs:appinfo>
</xs:annotation>
<!-- ... -->
<xs:complexType name="WillBeMadeCloneableType">
<xs:annotation>
<xs:appinfo>
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</xs:appinfo>
</xs:annotation>
<!-- ... -->
</xs:complexType>
<!-- ... -->
</xs:schema>
bindings.xjb
example file :
<jaxb:bindings
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:inheritance="urn:jaxb.jvnet.org:plugin:inheritance"
jaxb:extensionBindingPrefixes="inheritance"
jaxb:version="3.0">
<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
<jaxb:bindings node="xsd:simpleType[@name='issueJIIB38Type']">
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</jaxb:bindings>
<jaxb:bindings node="xsd:element[@name='issueJIIB38']">
<jaxb:class/>
<inheritance:implements>java.lang.Cloneable</inheritance:implements>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Assume you have a generic interface :
public interface IssueJIIB48Interface<T> {
public T getValue();
}
You can now use the following customization :
<xs:complexType name="issueJIIB48Type">
<xs:annotation>
<xs:appinfo>
<inheritance:implements>org.jvnet.jaxb2_commons.tests.issues.IssueJIIB48Interface<java.lang.String></inheritance:implements>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
You'll get the following code :
public class IssueJIIB48Type
implements IssueJIIB48Interface<String>
{
@XmlElement(required = true)
protected String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
// ...
}
- Home
- Migration guide
-
JAXB Maven Plugin
- Quick Start
-
User Guide
- Basic Usage
- Specifying What To Compile
- Referencing Resources in Maven Artifacts
- Using Catalogs
- Using Episodes
- Modular Schema Compilation
- Controlling the Output
- Using JAXB Plugins
- Using a Specific JAXB Version
- Configuring Extension, Validation and XML Security
- IDE Integration
- Miscellaneous
- Configuring Proxies
- Maven Documentation
- Configuration Cheat Sheet
- Common Pitfalls and Problems
-
JAXB2 Basics Plugins
- Using JAXB2 Basics Plugins
- JSR-305 Support
-
JAXB2 Basics Plugins List
- SimpleEquals Plugin
- SimpleHashCode Plugin
- Equals Plugin
- HashCode Plugin
- ToString Plugin
- Copyable Plugin
- Mergeable Plugin
- Inheritance Plugin
- AutoInheritance Plugin
- Wildcard Plugin
- Setters Plugin
- Simplify Plugin
- EnumValue Plugin
- JAXBIndex Plugin
- FixJAXB1058 Plugin
- Commons Lang Plugin
- Default Value Plugin
- Fluent API Plugin
- Namespace Prefix Plugin
- Value Constructor Plugin
- Boolean Getter Plugin
- CamelCase Plugin
- XML ElementWrapper Plugin
- Parent Pointer Plugin
- Property Listener Injector Plugin
- Annox
- JAXB Annotate Plugin
-
HyperJAXB3
- Build System Support
- Customization Guide
- Databases
- Development guide
- Extension guide
- FAQ
- IDE Support
- Java Persistence
- JAXB
- JDK Support
- Project Templates
-
Reference
- Adding vendor-specific annotations
- Features
- Integrating Hyperjaxb3 in builds
- Introduction
- Making schema-derived classes ready for JPA
- Adding required properties
- Applying workarounds for JAXB vs. JPA conflicts
- Enforcing top-level classes
- Generating equals and hashCode methods
- Generating ORM metadata
- Generating persistence unit descriptor
- JPA 2 Support
- Making classes serializable
- Testing generated mappings
- Reference - single page
- Related Projects
- Sample projects
- Solutions
- Target Scenarios
- Test Projects
- Tutorials
- Best Practices
- FAQ
- Sample Projects
- Support
- License
- Distribution