Consistent data class constructor resolution with clear error message

MVC data class processor constructs target instance even in case of binding failure, as long as the corresponding method parameter is not marked as optional.

Closes gh-24372
This commit is contained in:
Juergen Hoeller
2020-09-01 19:10:32 +02:00
parent 278c6d5cdb
commit e20bff9c64
5 changed files with 93 additions and 49 deletions

View File

@@ -226,6 +226,35 @@ public abstract class BeanUtils {
}
}
/**
* Return a resolvable constructor for the provided class, either a primary constructor
* or single public constructor or simply a default constructor. Callers have to be
* prepared to resolve arguments for the returned constructor's parameters, if any.
* @param clazz the class to check
* @since 5.3
* @see #findPrimaryConstructor
*/
@SuppressWarnings("unchecked")
public static <T> Constructor<T> getResolvableConstructor(Class<T> clazz) {
Constructor<T> ctor = findPrimaryConstructor(clazz);
if (ctor == null) {
Constructor<?>[] ctors = clazz.getConstructors();
if (ctors.length == 1) {
ctor = (Constructor<T>) ctors[0];
}
else {
try {
ctor = clazz.getDeclaredConstructor();
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException("No primary or single public constructor found for " +
clazz + " - and no default constructor found either");
}
}
}
return ctor;
}
/**
* Return the primary constructor of the provided class. For Kotlin classes, this
* returns the Java constructor corresponding to the Kotlin primary constructor