Skip to content

Commit 1e47f31

Browse files
committed
Support code generation for Set with non-comparable elements
Closes gh-29792
1 parent 57b6f7e commit 1e47f31

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@
5555
*
5656
* @author Stephane Nicoll
5757
* @author Phillip Webb
58+
* @author Sebastien Deleuze
5859
* @since 6.0
5960
*/
6061
class BeanDefinitionPropertyValueCodeGenerator {
@@ -448,7 +449,12 @@ protected CodeBlock generateCollectionCode(ResolvableType elementType, Set<?> se
448449
return CodeBlock.of("new $T($L)", LinkedHashSet.class,
449450
generateCollectionOf(set, List.class, elementType));
450451
}
451-
set = orderForCodeConsistency(set);
452+
try {
453+
set = orderForCodeConsistency(set);
454+
}
455+
catch (ClassCastException ex) {
456+
// If elements are not comparable, just keep the original set
457+
}
452458
return super.generateCollectionCode(elementType, set);
453459
}
454460

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@
5959
*
6060
* @author Stephane Nicoll
6161
* @author Phillip Webb
62+
* @author Sebastien Deleuze
6263
* @since 6.0
6364
* @see BeanDefinitionPropertyValueCodeGeneratorTests
6465
*/
@@ -438,6 +439,12 @@ void generateWhenLinkedHashSet() {
438439
});
439440
}
440441

442+
@Test
443+
void generateWhenSetOfClass() {
444+
Set<Class<?>> set = Set.of(String.class, Integer.class, Long.class);
445+
compile(set, (instance, compiler) -> assertThat(instance).isEqualTo(set));
446+
}
447+
441448
}
442449

443450
@Nested

0 commit comments

Comments
 (0)