castor-data-binding / castor

http://castor-data-binding.github.io/castor/
35 stars 29 forks source link

Setting collection type to "set" in bindings.xml generates un-compilable code #47

Closed aner-perez closed 8 years ago

aner-perez commented 8 years ago

Adding the following in bindings.xml generates code that does not compile.

    <elementBinding name="capability">
        <member collection="set" />
    </elementBinding>

Specifically, the generated class includes the following code:

    private java.util.Set<java.lang.String> capabilityList;

...

    /**
     * Method getCapability.
     * 
     * @param index
     * @throws java.lang.IndexOutOfBoundsException if the index
     * given is outside the bounds of the collection
     * @return the value of the java.lang.String at the given index
     */
    public java.lang.String getCapability(final int index) throws java.lang.IndexOutOfBoundsException {
        // check bounds for index
        if (index < 0 || index >= this.capabilityList.size()) {
            throw new IndexOutOfBoundsException("getCapability: Index value '" + index + "' not in range [0.." + (this.capabilityList.size() - 1) + "]");
        }

        return (java.lang.String) capabilityList.get(index);
    }

This code does not compile because java.util.Set does not have a get(int index) method

Looking at org.exolab.castor.builder.factory.CollectionMemberAndAccessorFactory line 331 calls:

this.createGetByIndexMethod(fieldInfo, jClass, useJava50);

Either createGetAndSetMethods(...) or createGetByIndexMethod(...) should check if the collection is a Set or SortedSet and skip the generation of this method. You could also add a boolean hasGetByIndex() method to JCollectionType to make this code cleaner.

wguttmn commented 8 years ago

Why not come up with a pull request that showed at least the results of your analysis ;-) ?

aner-perez commented 8 years ago

Pull request #48 was created. Previous analysis was wrong, it turned out to be a method signature change in CollectionMemberAndAccessorFactory which was not applied to the overridden method in CollectionJ2NoIndexMemberAndAccessorFactory.