Harmonize exception handling

This commit applies the same catch block to orderForConsistence(Map)
than the one that's used for sets.

Closes gh-31419
This commit is contained in:
Stéphane Nicoll 2023-10-12 13:55:33 +02:00
parent aab538d1f0
commit 15c19411f6
1 changed files with 15 additions and 9 deletions

View File

@ -449,18 +449,18 @@ class BeanDefinitionPropertyValueCodeGenerator {
return CodeBlock.of("new $T($L)", LinkedHashSet.class, return CodeBlock.of("new $T($L)", LinkedHashSet.class,
generateCollectionOf(set, List.class, elementType)); generateCollectionOf(set, List.class, elementType));
} }
try { return super.generateCollectionCode(elementType, orderForCodeConsistency(set));
set = orderForCodeConsistency(set);
}
catch (ClassCastException ex) {
// If elements are not comparable, just keep the original set
}
return super.generateCollectionCode(elementType, set);
} }
private Set<?> orderForCodeConsistency(Set<?> set) { private Set<?> orderForCodeConsistency(Set<?> set) {
try {
return new TreeSet<Object>(set); return new TreeSet<Object>(set);
} }
catch (ClassCastException ex) {
// If elements are not comparable, just keep the original set
return set;
}
}
} }
@ -515,8 +515,14 @@ class BeanDefinitionPropertyValueCodeGenerator {
} }
private <K, V> Map<K, V> orderForCodeConsistency(Map<K, V> map) { private <K, V> Map<K, V> orderForCodeConsistency(Map<K, V> map) {
try {
return new TreeMap<>(map); return new TreeMap<>(map);
} }
catch (ClassCastException ex) {
// If elements are not comparable, just keep the original map
return map;
}
}
private <K, V> CodeBlock generateLinkedHashMapCode(Map<K, V> map, private <K, V> CodeBlock generateLinkedHashMapCode(Map<K, V> map,
ResolvableType keyType, ResolvableType valueType) { ResolvableType keyType, ResolvableType valueType) {