Introduce DataClassRowMapper with record-style constructor binding support

Closes gh-24695
This commit is contained in:
Juergen Hoeller
2020-08-28 18:52:35 +02:00
parent d4192b9d35
commit d37eaa5941
10 changed files with 388 additions and 80 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.beans;
import java.beans.ConstructorProperties;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.lang.reflect.Constructor;
@@ -43,8 +44,10 @@ import kotlin.reflect.jvm.ReflectJvmMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.KotlinDetector;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -74,6 +77,9 @@ public abstract class BeanUtils {
private static final Log logger = LogFactory.getLog(BeanUtils.class);
private static final ParameterNameDiscoverer parameterNameDiscoverer =
new DefaultParameterNameDiscoverer();
private static final Set<Class<?>> unknownEditorTypes =
Collections.newSetFromMap(new ConcurrentReferenceHashMap<>(64));
@@ -443,8 +449,7 @@ public abstract class BeanUtils {
* @throws BeansException if PropertyDescriptor look fails
*/
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getPropertyDescriptors();
return CachedIntrospectionResults.forClass(clazz).getPropertyDescriptors();
}
/**
@@ -455,11 +460,8 @@ public abstract class BeanUtils {
* @throws BeansException if PropertyDescriptor lookup fails
*/
@Nullable
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName)
throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getPropertyDescriptor(propertyName);
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) throws BeansException {
return CachedIntrospectionResults.forClass(clazz).getPropertyDescriptor(propertyName);
}
/**
@@ -588,6 +590,24 @@ public abstract class BeanUtils {
}
}
/**
* Determine required parameter names for the given constructor,
* considering the JavaBeans {@link ConstructorProperties} annotation
* as well as Spring's {@link DefaultParameterNameDiscoverer}.
* @param ctor the constructor to find parameter names for
* @return the parameter names (matching the constructor's parameter count)
* @throws IllegalStateException if the parameter names are not resolvable
* @since 5.3
*/
public static String[] getParameterNames(Constructor<?> ctor) {
ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class);
String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor));
Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor);
Assert.state(paramNames.length == ctor.getParameterCount(),
() -> "Invalid number of parameter names: " + paramNames.length + " for constructor " + ctor);
return paramNames;
}
/**
* Check if the given type represents a "simple" property: a simple value
* type or an array of simple value types.