Expose parameter/field name for non-JavaBeans type conversion

Supports name-bound PropertyEditor registrations on data classes.
Includes consistent support for field-aware method parameters.

Closes gh-28284
This commit is contained in:
Juergen Hoeller
2023-06-02 20:42:05 +02:00
parent 322cbca0dc
commit 8c6287ef7b
6 changed files with 103 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@ public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport
@Override
@Nullable
public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType) throws TypeMismatchException {
return convertIfNecessary(value, requiredType, TypeDescriptor.valueOf(requiredType));
return convertIfNecessary(null, value, requiredType, TypeDescriptor.valueOf(requiredType));
}
@Override
@@ -50,7 +50,7 @@ public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport
public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,
@Nullable MethodParameter methodParam) throws TypeMismatchException {
return convertIfNecessary(value, requiredType,
return convertIfNecessary((methodParam != null ? methodParam.getParameterName() : null), value, requiredType,
(methodParam != null ? new TypeDescriptor(methodParam) : TypeDescriptor.valueOf(requiredType)));
}
@@ -59,18 +59,26 @@ public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport
public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType, @Nullable Field field)
throws TypeMismatchException {
return convertIfNecessary(value, requiredType,
return convertIfNecessary((field != null ? field.getName() : null), value, requiredType,
(field != null ? new TypeDescriptor(field) : TypeDescriptor.valueOf(requiredType)));
}
@Nullable
@Override
@Nullable
public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,
@Nullable TypeDescriptor typeDescriptor) throws TypeMismatchException {
return convertIfNecessary(null, value, requiredType, typeDescriptor);
}
@Nullable
private <T> T convertIfNecessary(@Nullable String propertyName, @Nullable Object value,
@Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws TypeMismatchException {
Assert.state(this.typeConverterDelegate != null, "No TypeConverterDelegate");
try {
return this.typeConverterDelegate.convertIfNecessary(null, null, value, requiredType, typeDescriptor);
return this.typeConverterDelegate.convertIfNecessary(
propertyName, null, value, requiredType, typeDescriptor);
}
catch (ConverterNotFoundException | IllegalStateException ex) {
throw new ConversionNotSupportedException(value, requiredType, ex);