Support code generation for Set with non-comparable elements
Closes gh-29792
This commit is contained in:
parent
57b6f7e494
commit
1e47f31210
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -55,6 +55,7 @@ import org.springframework.util.ObjectUtils;
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Sebastien Deleuze
|
||||||
* @since 6.0
|
* @since 6.0
|
||||||
*/
|
*/
|
||||||
class BeanDefinitionPropertyValueCodeGenerator {
|
class BeanDefinitionPropertyValueCodeGenerator {
|
||||||
|
@ -448,7 +449,12 @@ 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 {
|
||||||
set = orderForCodeConsistency(set);
|
set = orderForCodeConsistency(set);
|
||||||
|
}
|
||||||
|
catch (ClassCastException ex) {
|
||||||
|
// If elements are not comparable, just keep the original set
|
||||||
|
}
|
||||||
return super.generateCollectionCode(elementType, set);
|
return super.generateCollectionCode(elementType, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -59,6 +59,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Sebastien Deleuze
|
||||||
* @since 6.0
|
* @since 6.0
|
||||||
* @see BeanDefinitionPropertyValueCodeGeneratorTests
|
* @see BeanDefinitionPropertyValueCodeGeneratorTests
|
||||||
*/
|
*/
|
||||||
|
@ -438,6 +439,12 @@ class BeanDefinitionPropertyValueCodeGeneratorTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generateWhenSetOfClass() {
|
||||||
|
Set<Class<?>> set = Set.of(String.class, Integer.class, Long.class);
|
||||||
|
compile(set, (instance, compiler) -> assertThat(instance).isEqualTo(set));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
|
|
Loading…
Reference in New Issue