简体   繁体   中英

JAXB: Setter methods with return types being generated from XSD files when maxOccurs=“unbounded”

I am working on upgrading a JAXB 1.0 and max JDK 1.6 web application to JAXB 2.0 and JDK 1.7 support.

When trying to run the application in Java 7, we run into issues with JAXB classes which appears to be related to this:

Why did PropertyDescriptor behavior change from Java 1.6 to 1.7?

It seems like the change made in JDK 1.7 was to not support any setters that had a return type other than void. I thought switching to JAXB 2.0 would cause these setters to no longer be generated, but they are still getting generated and causing issues.

For our application, we define XSD files and then as part of our maven build process we generate the java files from these XSDs.

Here is the complexType that I am trying to create a list of:

<xs:complexType name="connection">
    <xs:annotation>
        <xs:documentation>Common connection info - switch name, host IP name and port</xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:element name="switchName">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="4"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="hostIPName">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="32"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="hostIPPortNumber">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="5"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

Here is the line where I am defining it in the main object:

<xs:element name="connectionList" type="dncommon:connection" maxOccurs="unbounded" />

And here is what gets generated in the JAXB object for this field:

public Connection[] getConnectionList() {
    if (this.connectionList == null) {
        return new Connection[ 0 ] ;
    }
    Connection[] retVal = new Connection[this.connectionList.length] ;
    System.arraycopy(this.connectionList, 0, retVal, 0, this.connectionList.length);
    return (retVal);
}

public Connection getConnectionList(int idx) {
    if (this.connectionList == null) {
        throw new IndexOutOfBoundsException();
    }
    return this.connectionList[idx];
}

public int getConnectionListLength() {
    if (this.connectionList == null) {
        return  0;
    }
    return this.connectionList.length;
}

public void setConnectionList(Connection[] values) {
    int len = values.length;
    this.connectionList = ((Connection[]) new Connection[len] );
    for (int i = 0; (i<len); i ++) {
        this.connectionList[i] = values[i];
    }
}

public Connection setConnectionList(int idx, Connection value) {
    return this.connectionList[idx] = value;
}

The last method is what it is complaining about - we are trying to set a specific index of the connection list, but it finds the setConnectionList method and does not like that it has a return type of Connection.

Here is what I have set in the pom.xml that is generating these classes:

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.9.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <generateDirectory>src/main/java</generateDirectory>
                <forceRegenerate>true</forceRegenerate>
            </configuration>
        </plugin>
    </plugins>
</build>

Does anyone know how to get JAXB to not generate setters with return types for array setters? I don't see anything wrong with my XSD setup - all of my research tells me to be defining it as I have. Any help is much appreciated.

Thanks,

Dan

try with this maven plugin configuration :

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <args>
                            <arg>-Xcollection-setter-injector</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>net.java.dev.vcc.thirdparty</groupId>
                                <artifactId>collection-setter-injector</artifactId>
                                <version>0.5.0-1</version>
                            </plugin>
                        </plugins>
                        <specVersion>2.2</specVersion>
                        <extension>true</extension>
                        <schemaDirectory><!--your schema directory--></schemaDirectory>
                        <schemaInclude>
                            <include>*.xsd</include>
                        </schemaInclude>
                    </configuration>
                </execution>
            </executions>
        </plugin>

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