Search in sources :

Example 21 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class InvocableHandlerMethod method getMethodArgumentValues.

/**
	 * Get the method argument values for the current request.
	 */
private Object[] getMethodArgumentValues(Message<?> message, Object... providedArgs) throws Exception {
    MethodParameter[] parameters = getMethodParameters();
    Object[] args = new Object[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        MethodParameter parameter = parameters[i];
        parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
        args[i] = resolveProvidedArgument(parameter, providedArgs);
        if (args[i] != null) {
            continue;
        }
        if (this.argumentResolvers.supportsParameter(parameter)) {
            try {
                args[i] = this.argumentResolvers.resolveArgument(parameter, message);
                continue;
            } catch (Exception ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug(getArgumentResolutionErrorMessage("Failed to resolve", i), ex);
                }
                throw ex;
            }
        }
        if (args[i] == null) {
            throw new MethodArgumentResolutionException(message, parameter, getArgumentResolutionErrorMessage("No suitable resolver for", i));
        }
    }
    return args;
}
Also used : MethodParameter(org.springframework.core.MethodParameter) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MappingJackson2MessageConverter method getSerializationView.

/**
	 * Determine a Jackson serialization view based on the given conversion hint.
	 * @param conversionHint the conversion hint Object as passed into the
	 * converter for the current conversion attempt
	 * @return the serialization view class, or {@code null} if none
	 * @since 4.2
	 */
protected Class<?> getSerializationView(Object conversionHint) {
    if (conversionHint instanceof MethodParameter) {
        MethodParameter param = (MethodParameter) conversionHint;
        JsonView annotation = (param.getParameterIndex() >= 0 ? param.getParameterAnnotation(JsonView.class) : param.getMethodAnnotation(JsonView.class));
        if (annotation != null) {
            return extractViewClass(annotation, conversionHint);
        }
    } else if (conversionHint instanceof JsonView) {
        return extractViewClass((JsonView) conversionHint, conversionHint);
    } else if (conversionHint instanceof Class) {
        return (Class<?>) conversionHint;
    }
    // No JSON view specified...
    return null;
}
Also used : JsonView(com.fasterxml.jackson.annotation.JsonView) MethodParameter(org.springframework.core.MethodParameter)

Example 23 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MappingJackson2MessageConverterTests method toTextMessageWithReturnTypeAndNoJsonView.

@Test
public void toTextMessageWithReturnTypeAndNoJsonView() throws JMSException, NoSuchMethodException {
    Method method = this.getClass().getDeclaredMethod("none");
    MethodParameter returnType = new MethodParameter(method, -1);
    testToTextMessageWithReturnType(returnType);
    verify(sessionMock).createTextMessage("{\"name\":\"test\",\"description\":\"lengthy description\"}");
}
Also used : Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 24 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MappingJackson2MessageConverterTests method toTextMessageWithReturnTypeAndMultipleJsonViews.

@Test
public void toTextMessageWithReturnTypeAndMultipleJsonViews() throws JMSException, NoSuchMethodException {
    Method method = this.getClass().getDeclaredMethod("invalid");
    MethodParameter returnType = new MethodParameter(method, -1);
    thrown.expect(IllegalArgumentException.class);
    testToTextMessageWithReturnType(returnType);
}
Also used : Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 25 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class HeadersMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    this.resolver = new HeadersMethodArgumentResolver();
    Method method = getClass().getDeclaredMethod("handleMessage", Map.class, String.class, MessageHeaders.class, MessageHeaderAccessor.class, TestMessageHeaderAccessor.class);
    this.paramAnnotated = new MethodParameter(method, 0);
    this.paramAnnotatedNotMap = new MethodParameter(method, 1);
    this.paramMessageHeaders = new MethodParameter(method, 2);
    this.paramMessageHeaderAccessor = new MethodParameter(method, 3);
    this.paramMessageHeaderAccessorSubclass = new MethodParameter(method, 4);
    Map<String, Object> headers = new HashMap<>();
    headers.put("foo", "bar");
    this.message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers).build();
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) Before(org.junit.Before)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)320 Test (org.junit.Test)251 Method (java.lang.reflect.Method)64 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 HandlerMethod (org.springframework.web.method.HandlerMethod)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19