Polishing

This commit is contained in:
Juergen Hoeller
2016-12-12 22:49:40 +01:00
parent aef1460a64
commit e49813f2c4
8 changed files with 80 additions and 75 deletions

View File

@@ -185,44 +185,6 @@ public final class ModelFactory {
return result;
}
/**
* Derives the model attribute name for a method parameter based on:
* <ol>
* <li>The parameter {@code @ModelAttribute} annotation value
* <li>The parameter type
* </ol>
* @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:
* <ol>
* <li>The method {@code ModelAttribute} annotation value
* <li>The declared return type if it is more specific than {@code Object}
* <li>The actual return value type
* </ol>
* @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.
@@ -250,10 +212,8 @@ public final class ModelFactory {
List<String> keyNames = new ArrayList<>(model.keySet());
for (String name : keyNames) {
Object value = model.get(name);
if (isBindingCandidate(name, value)) {
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name;
if (!model.containsAttribute(bindingResultKey)) {
WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name);
model.put(bindingResultKey, dataBinder.getBindingResult());
@@ -270,7 +230,7 @@ public final class ModelFactory {
return false;
}
Class<?> attrType = (value != null) ? value.getClass() : null;
Class<?> attrType = (value != null ? value.getClass() : null);
if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, attrType)) {
return true;
}
@@ -280,13 +240,53 @@ public final class ModelFactory {
}
/**
* Derive the model attribute name for a method parameter based on:
* <ol>
* <li>the parameter {@code @ModelAttribute} annotation value
* <li>the parameter type
* </ol>
* @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:
* <ol>
* <li>the method {@code ModelAttribute} annotation value
* <li>the declared return type if it is more specific than {@code Object}
* <li>the actual return value type
* </ol>
* @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;
private final Set<String> dependencies = new HashSet<>();
private ModelMethod(InvocableHandlerMethod handlerMethod) {
public ModelMethod(InvocableHandlerMethod handlerMethod) {
this.handlerMethod = handlerMethod;
for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
if (parameter.hasParameterAnnotation(ModelAttribute.class)) {

View File

@@ -195,7 +195,7 @@ final class HierarchicalUriComponents extends UriComponents {
String hostTo = encodeUriComponent(this.host, charset, getHostType());
PathComponent pathTo = this.path.encode(charset);
MultiValueMap<String, String> paramsTo = encodeQueryParams(charset);
String fragmentTo = encodeUriComponent(this.getFragment(), charset, Type.FRAGMENT);
String fragmentTo = encodeUriComponent(getFragment(), charset, Type.FRAGMENT);
return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, this.port,
pathTo, paramsTo, fragmentTo, true, false);
}
@@ -339,7 +339,7 @@ final class HierarchicalUriComponents extends UriComponents {
String portTo = expandUriComponent(this.port, uriVariables);
PathComponent pathTo = this.path.expand(uriVariables);
MultiValueMap<String, String> paramsTo = expandQueryParams(uriVariables);
String fragmentTo = expandUriComponent(this.getFragment(), uriVariables);
String fragmentTo = expandUriComponent(getFragment(), uriVariables);
return new HierarchicalUriComponents(schemeTo, userInfoTo, hostTo, portTo,
pathTo, paramsTo, fragmentTo, false, false);