Support record canonical constructor in BeanUtils
See gh-33707
This commit is contained in:
parent
161d3995bf
commit
514d6000d1
|
@ -23,6 +23,7 @@ import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.lang.reflect.RecordComponent;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -252,6 +253,19 @@ public abstract class BeanUtils {
|
||||||
return (Constructor<T>) ctors[0];
|
return (Constructor<T>) ctors[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (clazz.isRecord()) {
|
||||||
|
try {
|
||||||
|
// if record -> use canonical constructor, which is always presented
|
||||||
|
Class<?>[] paramTypes
|
||||||
|
= Arrays.stream(clazz.getRecordComponents())
|
||||||
|
.map(RecordComponent::getType)
|
||||||
|
.toArray(Class<?>[]::new);
|
||||||
|
return clazz.getDeclaredConstructor(paramTypes);
|
||||||
|
}
|
||||||
|
catch (NoSuchMethodException ex) {
|
||||||
|
// Giving up with record...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Several constructors -> let's try to take the default constructor
|
// Several constructors -> let's try to take the default constructor
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -521,10 +521,26 @@ class BeanUtilsTests {
|
||||||
assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should not be a simple property").isFalse();
|
assertThat(BeanUtils.isSimpleProperty(type)).as("Type [" + type.getName() + "] should not be a simple property").isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void resolveRecordConstructor() throws NoSuchMethodException {
|
||||||
|
assertThat(BeanUtils.getResolvableConstructor(RecordWithMultiplePublicConstructors.class))
|
||||||
|
.isEqualTo(getRecordWithMultipleVariationsConstructor());
|
||||||
|
}
|
||||||
|
|
||||||
private void assertSignatureEquals(Method desiredMethod, String signature) {
|
private void assertSignatureEquals(Method desiredMethod, String signature) {
|
||||||
assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod);
|
assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record RecordWithMultiplePublicConstructors(String value, String name) {
|
||||||
|
public RecordWithMultiplePublicConstructors(String value) {
|
||||||
|
this(value, "default value");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Constructor<RecordWithMultiplePublicConstructors> getRecordWithMultipleVariationsConstructor() throws NoSuchMethodException {
|
||||||
|
return RecordWithMultiplePublicConstructors.class.getConstructor(String.class, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static class NumberHolder {
|
private static class NumberHolder {
|
||||||
|
|
Loading…
Reference in New Issue