jaxb2-basics icon indicating copy to clipboard operation
jaxb2-basics copied to clipboard

simpleEquals does not work for JAXBElement in Lists

Open victornoel opened this issue 9 years ago • 2 comments
trafficstars

Hi,

I have a situation where a JAXBElement is in a List, and the simpleEquals plugins generated code contains something like this:

            List<Object> leftAny;
            leftAny = (((this.any!= null)&&(!this.any.isEmpty()))?this.getAny():null);
            List<Object> rightAny;
            rightAny = (((that.any!= null)&&(!that.any.isEmpty()))?that.getAny():null);
            if (leftAny!= null) {
                if (rightAny!= null) {
                    if (!leftAny.equals(rightAny)) {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                if (rightAny!= null) {
                    return false;
                }
            }

So when equals is called on the ArrayList, of course JAXBElement are not compared by content as they should… Apparently the problem does not happen with the runtime-library-based equals plugin.

victornoel avatar Feb 19 '16 13:02 victornoel

Well, the runtime-library-based equals wasn't developed for fun. (Well, it was but that's not what I'm trying to say.)

The problem is that JAXBElement does not implement equals. The only solution here would be to generate list comparing code which would instanceof JAXBElement and then do a correct comparison on that.

This will be ugly as hell, I don't really want to do this. I'd really suggest switching to the "powerful" equals/hashCode.

On Fri, Feb 19, 2016 at 2:08 PM, Victor Noël [email protected] wrote:

Hi,

I have a situation where a JAXBElement is in a List, and the simpleEquals plugins generated code contains something like this:

        List<Object> leftAny;
        leftAny = (((this.any!= null)&&(!this.any.isEmpty()))?this.getAny():null);
        List<Object> rightAny;
        rightAny = (((that.any!= null)&&(!that.any.isEmpty()))?that.getAny():null);
        if (leftAny!= null) {
            if (rightAny!= null) {
                if (!leftAny.equals(rightAny)) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            if (rightAny!= null) {
                return false;
            }
        }

So when equals is called on the ArrayList, of course JAXBElement are not compared by content as they should… Apparently the problem does not happen with the runtime-library-based equals plugin.

— Reply to this email directly or view it on GitHub https://github.com/highsource/jaxb2-basics/issues/66.

highsource avatar Feb 19 '16 13:02 highsource

That's what I was fearing… anyway, so it's a known limitation then!

Maybe we should add a disclaimer on the page for SimpleHashCode and SimpleEquals, because the current one is maybe a bit misleading when it says it supports JAXBElement :)

victornoel avatar Feb 19 '16 13:02 victornoel