diff --git a/spring-context/src/main/java/org/springframework/validation/DataBinder.java b/spring-context/src/main/java/org/springframework/validation/DataBinder.java index 275de639fb1..a67819969e2 100644 --- a/spring-context/src/main/java/org/springframework/validation/DataBinder.java +++ b/spring-context/src/main/java/org/springframework/validation/DataBinder.java @@ -99,6 +99,7 @@ import org.springframework.util.StringUtils; * @author Juergen Hoeller * @author Rob Harrop * @author Stephane Nicoll + * @author Kazuki Shimizu * @see #setAllowedFields * @see #setRequiredFields * @see #registerCustomEditor diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java index 960d0f72b5d..49fa3a3118f 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java @@ -185,44 +185,6 @@ public final class ModelFactory { return result; } - /** - * Derives the model attribute name for a method parameter based on: - *
    - *
  1. The parameter {@code @ModelAttribute} annotation value - *
  2. The parameter type - *
- * @return the derived name; never {@code null} or an empty string - */ - public static String getNameForParameter(MethodParameter parameter) { - ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); - String name = (ann != null ? ann.value() : null); - return StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter); - } - - /** - * Derive the model attribute name for the given return value using one of: - *
    - *
  1. The method {@code ModelAttribute} annotation value - *
  2. The declared return type if it is more specific than {@code Object} - *
  3. The actual return value type - *
- * @param returnValue the value returned from a method invocation - * @param returnType the return type of the method - * @return the model name, never {@code null} nor empty - */ - public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) { - ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class); - if (ann != null && StringUtils.hasText(ann.value())) { - return ann.value(); - } - else { - Method method = returnType.getMethod(); - Class containingClass = returnType.getContainingClass(); - Class resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass); - return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); - } - } - /** * Promote model attributes listed as {@code @SessionAttributes} to the session. * Add {@link BindingResult} attributes where necessary. @@ -278,6 +240,46 @@ public final class ModelFactory { } + /** + * Derive the model attribute name for a method parameter based on: + *
    + *
  1. the parameter {@code @ModelAttribute} annotation value + *
  2. the parameter type + *
+ * @param parameter a descriptor for the method parameter + * @return the derived name (never {@code null} or empty String) + */ + public static String getNameForParameter(MethodParameter parameter) { + ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); + String name = (ann != null ? ann.value() : null); + return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter)); + } + + /** + * Derive the model attribute name for the given return value based on: + *
    + *
  1. the method {@code ModelAttribute} annotation value + *
  2. the declared return type if it is more specific than {@code Object} + *
  3. the actual return value type + *
+ * @param returnValue the value returned from a method invocation + * @param returnType a descriptor for the return type of the method + * @return the derived name (never {@code null} or empty String) + */ + public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) { + ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class); + if (ann != null && StringUtils.hasText(ann.value())) { + return ann.value(); + } + else { + Method method = returnType.getMethod(); + Class containingClass = returnType.getContainingClass(); + Class resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass); + return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); + } + } + + private static class ModelMethod { private final InvocableHandlerMethod handlerMethod;