Example usage for org.springframework.core MethodParameter getNestedParameterType

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

Introduction

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

Prototype

public Class<?> getNestedParameterType() 

Source Link

Document

Return the nested type of the method/constructor parameter.

Usage

From source file:de.escalon.hypermedia.spring.ActionInputParameter.java

private Object[] getPossibleValues(MethodParameter methodParameter, AnnotatedParameters actionDescriptor) {
    try {/*w  w  w  .j ava 2  s.  c  om*/
        Class<?> parameterType = methodParameter.getNestedParameterType();
        Object[] possibleValues;
        Class<?> nested;
        if (Enum[].class.isAssignableFrom(parameterType)) {
            possibleValues = parameterType.getComponentType().getEnumConstants();
        } else if (Enum.class.isAssignableFrom(parameterType)) {
            possibleValues = parameterType.getEnumConstants();
        } else if (Collection.class.isAssignableFrom(parameterType)
                && Enum.class.isAssignableFrom(nested = TypeDescriptor.nested(methodParameter, 1).getType())) {
            possibleValues = nested.getEnumConstants();
        } else {
            Select select = methodParameter.getParameterAnnotation(Select.class);
            if (select != null) {
                Class<? extends Options> optionsClass = select.options();
                Options options = optionsClass.newInstance();
                // collect call values to pass to options.get
                List<Object> from = new ArrayList<Object>();
                for (String paramName : select.args()) {
                    AnnotatedParameter parameterValue = actionDescriptor.getAnnotatedParameter(paramName);
                    if (parameterValue != null) {
                        from.add(parameterValue.getCallValue());
                    }
                }

                Object[] args = from.toArray();
                possibleValues = options.get(select.value(), args);
            } else {
                possibleValues = new Object[0];
            }
        }
        return possibleValues;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.escalon.hypermedia.spring.SpringActionInputParameter.java

public Object[] getPossibleValues(MethodParameter methodParameter, ActionDescriptor actionDescriptor) {
    try {/*from   w  ww .  j  av a  2  s  .c  om*/
        Class<?> parameterType = methodParameter.getNestedParameterType();
        Object[] possibleValues;
        Class<?> nested;
        if (Enum[].class.isAssignableFrom(parameterType)) {
            possibleValues = parameterType.getComponentType().getEnumConstants();
        } else if (Enum.class.isAssignableFrom(parameterType)) {
            possibleValues = parameterType.getEnumConstants();
        } else if (Collection.class.isAssignableFrom(parameterType)
                && Enum.class.isAssignableFrom(nested = TypeDescriptor.nested(methodParameter, 1).getType())) {
            possibleValues = nested.getEnumConstants();
        } else {
            Select select = methodParameter.getParameterAnnotation(Select.class);
            if (select != null) {
                Class<? extends Options> optionsClass = select.options();
                Options options = optionsClass.newInstance();
                // collect call values to pass to options.get
                List<Object> from = new ArrayList<Object>();
                for (String paramName : select.args()) {
                    ActionInputParameter parameterValue = actionDescriptor.getActionInputParameter(paramName);
                    if (parameterValue != null) {
                        from.add(parameterValue.getValue());
                    }
                }

                Object[] args = from.toArray();
                possibleValues = options.get(select.value(), args);
            } else {
                possibleValues = new Object[0];
            }
        }
        return possibleValues;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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  w w.j av  a 2  s  .co 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./*ww  w . j av a 2s .  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;
}