highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

simpleEquals does not work for JAXBElement in Lists #66

Open victornoel opened 8 years ago

victornoel commented 8 years ago

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.

highsource commented 8 years ago

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 notifications@github.com 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.

victornoel commented 8 years ago

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 :)