简体   繁体   中英

Prevent jaxb to initialize a list when using getter

Jaxb generates this code for list type:

public List<String> getMyList() {
    if (myList == null) {
        myList = new ArrayList<String>();
    }
    return this.myList;
}

I would like it to just return the field instead of initializing.

How can I do it?

I'm having the same exact issue. I'm declaring a list in an XSD file as follows:

<xs:element name="parameter" nillable="true" minOccurs="0" maxOccurs="999">

Resulting java class has field declared as:

@XmlElement(nillable = true)
protected List<ServiceType.Parameter> parameter;

But the getter:

public List<ServiceType.Parameter> getParameter() {
    if (parameter == null) {
        parameter = new ArrayList<ServiceType.Parameter>();
    }
    return this.parameter;
}

I know that JAXB handles list as references but this clearly doesn't support XSD ability to define nillable optional list as the list will never be returned as null.

Do anyone know any plugin to get rid of the lazy initialization for nillable lists?

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM