Example usage for org.springframework.core Conventions getVariableNameForParameter

List of usage examples for org.springframework.core Conventions getVariableNameForParameter

Introduction

In this page you can find the example usage for org.springframework.core Conventions getVariableNameForParameter.

Prototype

public static String getVariableNameForParameter(MethodParameter parameter) 

Source Link

Document

Determine the conventional variable name for the given parameter taking the generic collection type, if any, into account.

Usage

From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java

private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
        ExtendedModelMap implicitModel, NativeWebRequest webRequest, Object handler) throws Exception {

    // Bind request parameter onto object...
    String name = attrName;//  ww w. ja v a 2  s  .c  om
    if ("".equals(name)) {
        name = Conventions.getVariableNameForParameter(methodParam);
    }
    Class<?> paramType = methodParam.getParameterType();
    Object bindObject;
    if (implicitModel.containsKey(name)) {
        bindObject = implicitModel.get(name);
    } else if (this.methodResolver.isSessionAttribute(name, paramType)) {
        bindObject = this.sessionAttributeStore.retrieveAttribute(webRequest, name);
        if (bindObject == null) {
            raiseSessionRequiredException("Session attribute '" + name + "' required - not found in session");
        }
    } else {
        bindObject = BeanUtils.instantiateClass(paramType);
    }
    WebDataBinder binder = createBinder(webRequest, bindObject, name);
    initBinder(handler, name, binder, webRequest);
    return binder;
}

From source file:org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver.java

@Nullable
private Consumer<Object> getValidator(Message<?> message, MethodParameter parameter) {
    if (this.validator == null) {
        return null;
    }//from www. j  ava  2s.c  o  m
    for (Annotation ann : parameter.getParameterAnnotations()) {
        Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
            Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            String name = Conventions.getVariableNameForParameter(parameter);
            return target -> {
                BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target, name);
                if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
                    ((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
                } else {
                    this.validator.validate(target, bindingResult);
                }
                if (bindingResult.hasErrors()) {
                    throw new MethodArgumentNotValidException(message, parameter, bindingResult);
                }
            };
        }
    }
    return null;
}

From source file:org.springframework.web.method.annotation.ModelFactory.java

/**
 * 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//from w w  w. j a  va 2  s .c om
 * @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));
}