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

Using -XhashCode and ID/IDREF causes StackOverflowException

Open Newtopian opened this issue 9 years ago • 1 comments

Marshaling an object graph that contains cycle can be broken using ID and IDREF.

The technique works perfectly with Jaxb and the maven-jaxb2-plugin. Adding bindings such that the IDREF elements return a properly typed instance makes this technique completely transparent at the Java class level. The Structure is serialized to XML without cycles as an XML tree and is returned with the proper cycles in place in Java objects.

Sadly, if enabling hashCode plugin from jaxb2-basics will create an infinite call loop where hashcode will call member hashcodes.

Take for example the following object structure:

AGraph object that contains a single Node Nodes contain a list of Edges Edges contain a start Node and an end Node

Hashcode for a Node would call Edge which in return would call Node again so on and so forth.

Workaround are possible but imply disabling one part of the chain. 1 - Disable the hashCode plugin, this implies we will have to re-code all the hashcode functions manually for all generated objects. 2 - Not declare the baseType bindings in the xjb file. Implies we will have to manually cast the reference into the proper object. This in turn can be worked around with the code injection plugin by creating another accessor that performs the cast for us. 3 - not use ID/IDREF and perform this logic manually in a proxy class of some sort.

Option 2 here provides biggest bang for the buck.

I suspect other parts would suffer the same fate (equals, toString etc)

Newtopian avatar Sep 20 '16 20:09 Newtopian

I quickly put together a project that exposes the issue, just mvn test to see it.

I'll try and see if I can investigate further tomorrow

https://github.com/Newtopian/jaxb2-basics-bug75

Newtopian avatar Sep 20 '16 20:09 Newtopian