I'm migrating a project from JAXB 1.0 to JAXB 2.1 and I'm having problems with the datatype mapping.
I'm using the Ant xjc binding compiler, and I've successfully configured the global bindings such that (for example) xs:date maps to java.util.Calendar.
However I'm getting generated methods which return Boolean
, whereas I want boolean
.
Here is the complex type:
<xs:element name="usage-auth-rate-charge">
<xs:complexType>
<xs:sequence>
<xs:element name="service-id" type="xs:string"/>
<xs:element name="pricepoint_custom_fields_required" type="xs:boolean" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
And the generated class looks like this:
public class UsageAuthRateCharge {
........
public Boolean isPricepointCustomFieldsRequired() {
return pricepointCustomFieldsRequired;
}
The problem is that although boxing will work, if the supplied XML doesn't contain a value for pricepoint_custom_fields_required
, the class's Boolean field is null, instead of false. So I get NullPointerExceptions when doing something like this:
methodWhichTakesPrimitiveBooleanArg(myUsageAuthRateChargeInstance.isPricepointCustomFieldsRequired());
because it tries to unbox the Boolean passed in - except it's null.
I can't change the schema, and I can't adjust all the client code to do the null checks.
I've set the optionalProperty attribute in my binding.xml as follows:
<globalBindings optionalProperty="primitive">
In the spec, it says: "If the attribute’s value is "primitive", it binds as it did in JAXB 1.0"
Yet this is clearly not happening.
How can I solve this problem?
UPDATE:
This is now fixed in jaxb 2.2.9: https://java.net/jira/browse/JAXB/fixforversion/16850
public boolean isSetPricepointCustomFieldsRequired() { return (this.pricepointCustomFieldsRequired!= null); }
However I don't want this method, as I would have to change all my application code in order to use it. – mdarwin