boolean绑定到Java布尔包装类

boolean绑定到Java布尔包装类

本文介绍了JAXB编译器将xs:boolean绑定到Java布尔包装类,而不是布尔基元类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将项目从JAXB 1.0迁移到JAXB 2.1,我遇到了数据类型映射问题。

I'm migrating a project from JAXB 1.0 to JAXB 2.1 and I'm having problems with the datatype mapping.

我正在使用Ant xjc 绑定编译器,我已成功配置全局绑定,以便(例如) xs:date 映射到 java.util.Calendar

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.

但是我得到的生成方法返回 Boolean ,而我想要 boolean

However I'm getting generated methods which return Boolean, whereas I want boolean.

以下是复杂类型:

<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;
}

问题是虽然装箱会起作用,但如果提供的XML没有包含 pricepoint_custom_fields_required 的值,类的布尔字段为空,而不是false。所以我得到的 NullPointerException异常的时候做这样的事情:

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.

我在 binding.xml 中设置了 optionalProperty 属性,如下所示:

I've set the optionalProperty attribute in my binding.xml as follows:

<globalBindings optionalProperty="primitive">

在规范中,它说:如果属性的值是原始的,它会绑定为在JAXB 1.0中做了

In the spec, it says: "If the attribute’s value is "primitive", it binds as it did in JAXB 1.0"

然而这显然没有发生。

我该如何解决这个问题?

How can I solve this problem?

更新:

现在修正了jaxb 2.2.9:

This is now fixed in jaxb 2.2.9:https://java.net/jira/browse/JAXB/fixforversion/16850

推荐答案

我厌倦了等待开发团队的修复,所以我卷起袖子自己做了。

I got bored of waiting for a fix from the dev team so I rolled up my sleeves and did it myself.

我包含以下代码,以帮助那些也是问题的人。

I'm including the code below to help people for whom this is also an issue.

免责声明:我的代码可能不是解决问题的最佳方法,但它对我有用。

Disclaimer: my code probably isn't the best way to solve the problem but it works for me.

生成的代码现在如下所示:

The generated code now looks like this:

public boolean isPricepointCustomFieldsRequired() {
    if (pricepointCustomFieldsRequired == null) {
        return false;
    } else {
        return pricepointCustomFieldsRequired;
    }
}

修改如下:

com.sun.tools.xjc.reader.xmlschema.bindinfo.BIProperty:createElementProperty,line~358

com.sun.tools.xjc.reader.xmlschema.bindinfo.BIProperty:createElementProperty, line ~358

行后

types.addTo(prop);

插入以下代码:

if (prop.isOptionalPrimitive() && getOptionalPropertyMode() == OptionalPropertyMode.PRIMITIVE &&
    !prop.getTypes().isEmpty() && "boolean".equals(prop.getTypes().get(0).getTypeName().getLocalPart()) )   {
        prop.defaultValue= CDefaultValue.create(CBuiltinLeafInfo.BOOLEAN, new XmlString("false"));
    }

这篇关于JAXB编译器将xs:boolean绑定到Java布尔包装类,而不是布尔基元类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:19