Search in sources :

Example 46 with MethodParameter

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

the class ServletRequestMethodArgumentResolverTests method reader.

@Test
public void reader() throws Exception {
    MethodParameter readerParameter = new MethodParameter(method, 6);
    assertTrue("Reader not supported", resolver.supportsParameter(readerParameter));
    Object result = resolver.resolveArgument(readerParameter, null, webRequest, null);
    assertSame("Invalid result", webRequest.getRequest().getReader(), result);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 47 with MethodParameter

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

the class ServletRequestMethodArgumentResolverTests method httpMethod.

@Test
public void httpMethod() throws Exception {
    MethodParameter httpMethodParameter = new MethodParameter(method, 10);
    assertTrue("HttpMethod not supported", resolver.supportsParameter(httpMethodParameter));
    Object result = resolver.resolveArgument(httpMethodParameter, null, webRequest, null);
    assertSame("Invalid result", HttpMethod.valueOf(webRequest.getRequest().getMethod()), result);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 48 with MethodParameter

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

the class Spr7538Tests method repro.

@Ignore
@Test
public void repro() throws Exception {
    AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy();
    BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setTypeConverter(converter);
    List<Foo> arguments = new ArrayList<>();
    // !!!! With the below line commented you'll get NPE. Uncomment and everything is OK!
    //arguments.add(new Foo());
    List<TypeDescriptor> paramDescriptors = new ArrayList<>();
    Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class);
    paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, 0)));
    List<TypeDescriptor> argumentTypes = new ArrayList<>();
    argumentTypes.add(TypeDescriptor.forObject(arguments));
    ReflectiveMethodResolver resolver = new ReflectiveMethodResolver();
    MethodExecutor executor = resolver.resolve(context, target, "checkCompleteness", argumentTypes);
    Object result = executor.execute(context, target, arguments);
    System.out.println("Result: " + result);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) MethodExecutor(org.springframework.expression.MethodExecutor) MethodParameter(org.springframework.core.MethodParameter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 49 with MethodParameter

use of org.springframework.core.MethodParameter in project java-chassis by ServiceComb.

the class ParamUtils method getParameterName.

public static String getParameterName(Method method, int paramIdx) {
    MethodParameter methodParameter = new MethodParameter(method, paramIdx);
    methodParameter.initParameterNameDiscovery(parameterNameDiscoverer);
    String paramName = methodParameter.getParameterName();
    if (paramName == null) {
        // 小于jdk8的场景中,即使有debug参数,也无法对着interface获取参数名,此时直接使用arg + paramIndex来表示
        paramName = "arg" + paramIdx;
    }
    return paramName;
}
Also used : MethodParameter(org.springframework.core.MethodParameter)

Example 50 with MethodParameter

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

the class ModelFactory method findSessionAttributeArguments.

/**
	 * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}.
	 */
private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) {
    List<String> result = new ArrayList<>();
    for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
        if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
            String name = getNameForParameter(parameter);
            Class<?> paramType = parameter.getParameterType();
            if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) {
                result.add(name);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) MethodParameter(org.springframework.core.MethodParameter)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)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