private String getNameForReturnValue(MethodParameter returnType) { return Optional.ofNullable(returnType.getMethodAnnotation(ModelAttribute.class)) .filter(ann -> StringUtils.hasText(ann.value())) .map(ModelAttribute::value) .orElseGet(() -> Conventions.getVariableNameForParameter(returnType)); }
private String getAttributeName(MethodParameter param) { return Optional .ofNullable(AnnotatedElementUtils.findMergedAnnotation(param.getAnnotatedElement(), ModelAttribute.class)) .filter(ann -> StringUtils.hasText(ann.value())) .map(ModelAttribute::value) .orElseGet(() -> Conventions.getVariableNameForParameter(param)); }
/** * Derive the model attribute name for the given method parameter based on * a {@code @ModelAttribute} parameter annotation (if present) or falling * back on parameter type based conventions. * @param parameter a descriptor for the method parameter * @return the derived name * @see Conventions#getVariableNameForParameter(MethodParameter) */ 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 method parameter based on * a {@code @ModelAttribute} parameter annotation (if present) or falling * back on parameter type based conventions. * @param parameter a descriptor for the method parameter * @return the derived name * @see Conventions#getVariableNameForParameter(MethodParameter) */ 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. Results will be * 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(@Nullable 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(); Assert.state(method != null, "No handler method"); Class<?> containingClass = returnType.getContainingClass(); Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass); return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); } }
/** * Derive the model attribute name for the given method parameter based on * a {@code @ModelAttribute} parameter annotation (if present) or falling * back on parameter type based conventions. * @param parameter a descriptor for the method parameter * @return the derived name * @see Conventions#getVariableNameForParameter(MethodParameter) */ 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)); }
private Object getErrors(MethodParameter parameter, BindingContext context) { Assert.isTrue(parameter.getParameterIndex() > 0, "Errors argument must be declared immediately after a model attribute argument"); int index = parameter.getParameterIndex() - 1; MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType()); Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " + "cannot both be declared with an async type wrapper. " + "Either declare the @ModelAttribute without an async wrapper type or " + "handle a WebExchangeBindException error signal through the async type."); ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); String name = (ann != null && StringUtils.hasText(ann.value()) ? ann.value() : Conventions.getVariableNameForParameter(attributeParam)); Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name); Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " + "immediately after the @ModelAttribute argument to which it applies. " + "For @RequestBody and @RequestPart arguments, please declare them with a reactive " + "type wrapper and use its onError operators to handle WebExchangeBindException: " + parameter.getMethod()); return errors; }
/** * Derive the model attribute name for the given return value. Results will be * 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(@Nullable 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(); Assert.state(method != null, "No handler method"); Class<?> containingClass = returnType.getContainingClass(); Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass); return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); } }
@Override public String apply(ModelAttribute input) { return input.value(); } };
@Override public String apply(ModelAttribute input) { return input.value(); } };
private String getAttributeName(Class<?> valueType, MethodParameter parameter) { ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class); if (annot != null && StringUtils.hasText(annot.value())) { return annot.value(); } // TODO: Conventions does not deal with async wrappers return ClassUtils.getShortNameAsProperty(valueType); }
/** * Return the name of a model attribute return value based on the method * {@code @ModelAttribute} annotation, if present, or derived from the type * of the return value otherwise. */ private String getNameForReturnValue(Class<?> returnValueType, MethodParameter returnType) { ModelAttribute annotation = returnType.getMethodAnnotation(ModelAttribute.class); if (annotation != null && StringUtils.hasText(annotation.value())) { return annotation.value(); } // TODO: Conventions does not deal with async wrappers return ClassUtils.getShortNameAsProperty(returnValueType); }
private String getAttributeName(Class<?> valueType, MethodParameter parameter) { Method method = parameter.getMethod(); ModelAttribute annot = AnnotatedElementUtils.findMergedAnnotation(method, ModelAttribute.class); if (annot != null && StringUtils.hasText(annot.value())) { return annot.value(); } // TODO: Conventions does not deal with async wrappers return ClassUtils.getShortNameAsProperty(valueType); }
/** * Derive the model attribute name for the given method parameter based on * a {@code @ModelAttribute} parameter annotation (if present) or falling * back on parameter type based conventions. * @param parameter a descriptor for the method parameter * @return the derived name * @see Conventions#getVariableNameForParameter(MethodParameter) */ 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 method parameter based on * a {@code @ModelAttribute} parameter annotation (if present) or falling * back on parameter type based conventions. * @param parameter a descriptor for the method parameter * @return the derived name * @see Conventions#getVariableNameForParameter(MethodParameter) */ 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)); }
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType, Object returnValue, ExtendedModelMap implicitModel) { ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class); String attrName = (attr != null ? attr.value() : ""); if ("".equals(attrName)) { Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType); attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue); } implicitModel.addAttribute(attrName, returnValue); }
static String extractModelAttributeName(JoinPoint jp) { Signature sig=jp.getSignature(); Method method=(sig instanceof MethodSignature) ? ((MethodSignature) sig).getMethod() : null; ModelAttribute ma=(method == null) ? null : method.getAnnotation(ModelAttribute.class); String modelAttrName=(ma == null) ? null : ma.value(); if(!StringUtil.isEmpty(modelAttrName)) { return modelAttrName; } if (method == null) { return ModelAttribute.class.getSimpleName(); } else { return Conventions.getVariableNameForReturnType(method); } } }
private String getModelAttributeName(MethodParameter parameter) { Assert.isTrue(parameter.getParameterIndex() > 0, "Errors argument must be immediately after a model attribute argument."); int index = parameter.getParameterIndex() - 1; MethodParameter attributeParam = new MethodParameter(parameter.getMethod(), index); Class<?> attributeType = attributeParam.getParameterType(); ResolvableType type = ResolvableType.forMethodParameter(attributeParam); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve()); Assert.isNull(adapter, "Errors/BindingResult cannot be used with an async model attribute. " + "Either declare the model attribute without the async wrapper type " + "or handle WebExchangeBindException through the async type."); ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class); if (annot != null && StringUtils.hasText(annot.value())) { return annot.value(); } // TODO: Conventions does not deal with async wrappers return ClassUtils.getShortNameAsProperty(attributeType); }
private String findParameterNameFromAnnotations(MethodParameter methodParameter) { Annotation[] methodAnnotations = methodParameter.getParameterAnnotations(); if (null != methodAnnotations) { for (Annotation annotation : methodAnnotations) { if (annotation instanceof PathVariable) { return ((PathVariable) annotation).value(); } else if (annotation instanceof ModelAttribute) { return ((ModelAttribute) annotation).value(); } else if (annotation instanceof RequestParam) { return ((RequestParam) annotation).value(); } else if (annotation instanceof RequestHeader) { return ((RequestHeader) annotation).value(); } } } return null; } }