Refine record canonical constructor support in BeanUtils
This commit refines the contribution with the following changes: - Move the support to findPrimaryConstructor - Use a for loop instead of a Stream for more efficiency - Support other visibilities than public - Polishing Closes gh-33707
This commit is contained in:
@@ -522,26 +522,36 @@ class BeanUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveRecordConstructor() throws NoSuchMethodException {
|
||||
void resolveMultipleRecordPublicConstructor() throws NoSuchMethodException {
|
||||
assertThat(BeanUtils.getResolvableConstructor(RecordWithMultiplePublicConstructors.class))
|
||||
.isEqualTo(getRecordWithMultipleVariationsConstructor());
|
||||
.isEqualTo(RecordWithMultiplePublicConstructors.class.getDeclaredConstructor(String.class, String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveMultipleRecordePackagePrivateConstructor() throws NoSuchMethodException {
|
||||
assertThat(BeanUtils.getResolvableConstructor(RecordWithMultiplePackagePrivateConstructors.class))
|
||||
.isEqualTo(RecordWithMultiplePackagePrivateConstructors.class.getDeclaredConstructor(String.class, String.class));
|
||||
}
|
||||
|
||||
private void assertSignatureEquals(Method desiredMethod, String signature) {
|
||||
assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod);
|
||||
}
|
||||
|
||||
|
||||
public record RecordWithMultiplePublicConstructors(String value, String name) {
|
||||
@SuppressWarnings("unused")
|
||||
public RecordWithMultiplePublicConstructors(String value) {
|
||||
this(value, "default value");
|
||||
}
|
||||
}
|
||||
|
||||
private Constructor<RecordWithMultiplePublicConstructors> getRecordWithMultipleVariationsConstructor() throws NoSuchMethodException {
|
||||
return RecordWithMultiplePublicConstructors.class.getConstructor(String.class, String.class);
|
||||
record RecordWithMultiplePackagePrivateConstructors(String value, String name) {
|
||||
@SuppressWarnings("unused")
|
||||
RecordWithMultiplePackagePrivateConstructors(String value) {
|
||||
this(value, "default value");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class NumberHolder {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user