Example usage for org.springframework.core MethodParameter nestedIfOptional

List of usage examples for org.springframework.core MethodParameter nestedIfOptional

Introduction

In this page you can find the example usage for org.springframework.core MethodParameter nestedIfOptional.

Prototype

public MethodParameter nestedIfOptional() 

Source Link

Document

Return a variant of this MethodParameter which points to the same parameter but one nesting level deeper in case of a java.util.Optional declaration.

Usage

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

/**
 * Extension point to create the model attribute if not found in the model,
 * with subsequent parameter binding through bean properties (unless suppressed).
 * <p>The default implementation typically uses the unique public no-arg constructor
 * if available but also handles a "primary constructor" approach for data classes:
 * It understands the JavaBeans {@link ConstructorProperties} annotation as well as
 * runtime-retained parameter names in the bytecode, associating request parameters
 * with constructor arguments by name. If no such constructor is found, the default
 * constructor will be used (even if not public), assuming subsequent bean property
 * bindings through setter methods.//w  ww  . j a v  a  2 s  . c  o m
 * @param attributeName the name of the attribute (never {@code null})
 * @param parameter the method parameter declaration
 * @param binderFactory for creating WebDataBinder instance
 * @param webRequest the current request
 * @return the created model attribute (never {@code null})
 * @throws BindException in case of constructor argument binding failure
 * @throws Exception in case of constructor invocation failure
 * @see #constructAttribute(Constructor, String, WebDataBinderFactory, NativeWebRequest)
 * @see BeanUtils#findPrimaryConstructor(Class)
 */
protected Object createAttribute(String attributeName, MethodParameter parameter,
        WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception {

    MethodParameter nestedParameter = parameter.nestedIfOptional();
    Class<?> clazz = nestedParameter.getNestedParameterType();

    Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz);
    if (ctor == null) {
        Constructor<?>[] ctors = clazz.getConstructors();
        if (ctors.length == 1) {
            ctor = ctors[0];
        } else {
            try {
                ctor = clazz.getDeclaredConstructor();
            } catch (NoSuchMethodException ex) {
                throw new IllegalStateException("No primary or default constructor found for " + clazz, ex);
            }
        }
    }

    Object attribute = constructAttribute(ctor, attributeName, binderFactory, webRequest);
    if (parameter != nestedParameter) {
        attribute = Optional.of(attribute);
    }
    return attribute;
}

From source file:org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport.java

/**
 * Evaluate the {@code Predicate} on the method parameter type if it has the
 * given annotation, nesting within {@link java.util.Optional} if necessary,
 * but raise an {@code IllegalStateException} if the same matches the generic
 * type within a reactive type wrapper./*from w ww.  jav  a 2 s . c o m*/
 */
protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper(MethodParameter parameter,
        Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) {

    A annotation = parameter.getParameterAnnotation(annotationType);
    if (annotation == null) {
        return false;
    }

    parameter = parameter.nestedIfOptional();
    Class<?> type = parameter.getNestedParameterType();

    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
    if (adapter != null) {
        assertHasValues(adapter, parameter);
        parameter = parameter.nested();
        type = parameter.getNestedParameterType();
    }

    if (typePredicate.test(annotation, type)) {
        if (adapter == null) {
            return true;
        }
        throw buildReactiveWrapperException(parameter);
    }

    return false;
}