From 15c19411f68228fd450c5da2fb65a958e5eabe98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Thu, 12 Oct 2023 13:55:33 +0200 Subject: [PATCH] Harmonize exception handling This commit applies the same catch block to orderForConsistence(Map) than the one that's used for sets. Closes gh-31419 --- ...nDefinitionPropertyValueCodeGenerator.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java index c05a966b0b..3d624b076a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java @@ -449,17 +449,17 @@ class BeanDefinitionPropertyValueCodeGenerator { return CodeBlock.of("new $T($L)", LinkedHashSet.class, generateCollectionOf(set, List.class, elementType)); } + return super.generateCollectionCode(elementType, orderForCodeConsistency(set)); + } + + private Set orderForCodeConsistency(Set set) { try { - set = orderForCodeConsistency(set); + return new TreeSet(set); } catch (ClassCastException ex) { // If elements are not comparable, just keep the original set + return set; } - return super.generateCollectionCode(elementType, set); - } - - private Set orderForCodeConsistency(Set set) { - return new TreeSet(set); } } @@ -515,7 +515,13 @@ class BeanDefinitionPropertyValueCodeGenerator { } private Map orderForCodeConsistency(Map map) { - return new TreeMap<>(map); + try { + return new TreeMap<>(map); + } + catch (ClassCastException ex) { + // If elements are not comparable, just keep the original map + return map; + } } private CodeBlock generateLinkedHashMapCode(Map map,