ReflectionUtil.calculateHierarchyDistance should return superinterfaces - #1369
ReflectionUtil.calculateHierarchyDistance should return superinterfaces#1369SimoneGiusso wants to merge 1 commit into
Conversation
| Class<?>[] currentInterfaces = current.getInterfaces(); | ||
| while (currentInterfaces.length != 0) { | ||
| for (final Class<?> i : currentInterfaces) { | ||
| if (!interfaces.contains(i)) { | ||
| interfaces.add(i); | ||
| } | ||
| } | ||
|
|
||
| currentInterfaces = Arrays.stream(currentInterfaces) | ||
| .flatMap(currentInterface -> Arrays.stream(currentInterface.getInterfaces())) | ||
| .toArray(Class<?>[]::new); |
There was a problem hiding this comment.
In Java 21 SortedMap doesn't directly extend the Map interface. Instead it extends SequencedMap and SequencedMap extends Map. Given an interface or class, getInterfaces(), returns only the interfaces directly implemented. At the end of this method the interfaces variable should contains also the possible super interfaces.
If we do this, SortedMap in Java 21 will "see" its super interface Map and consequently mapped to the MapType since the mapping Map -> MapType is one of the entry used to initialize the TypeMapperEngine here.
| def "should calculate hierarchy distance as follows (parents first, interfaces last)"() { | ||
| expect: | ||
| ReflectionUtil.calculateHierarchyDistance(HashMap) == [AbstractMap, Map, Cloneable, Serializable] | ||
| ReflectionUtil.calculateHierarchyDistance(TreeMap) == [AbstractMap, NavigableMap, Cloneable, Serializable, SortedMap, Map] |
There was a problem hiding this comment.
With the old implementation SortedMap is not part of the output here as we do not iterate the parent interfaces as we do for classes.
|
@SimoneGiusso thanks for the detailed bug report, I will do CR of your PR |
|
You are right, btw can you give me a hint how to run a test on higher Java version than specified in |
|
merged #1371 |
|
the issue is fixed, released in 7.4.2 |



This is related to this issue. Actually we noticed this problem when we upgraded our project from Java 17 to Java 21.
In fact the call to
Javers#compareon objects which have a property of TypeSortedMap(assigned toTreeMapconcrete class) fails in Java 21 with an error similar to:While in Java 17, it works.
The reason is that the mapping between
SortedMapand JaversType changes when using the two Java versions:SortedMapis mapped toMapType(not aManagedType) ✅SortedMapis mapped toValueObjectType(subclass ofManagedType) ❌This is the issue
SortedMapis not correctly mapped when using Java 21.Full stack error:
P.S. The message exception, as it is written, is due to the fact that, at first the
SortedMapproperty is considered asManagedTypeas explained above. Therefore the call toObjectGraphBuilder.buildSingleEdges(ObjectGraphBuilder.java:107)with this property. However since the instance is ofTreeMap, when retrieving theJaversManagedTypeatTypeMapper.getJaversManagedType(TypeMapper.java:170) ,right before the exception, it returnsMapType.It may be related to the following issue as well:
#1090