Search in sources :

Example 1 with SynthesizingMethodParameter

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

the class SendToMethodReturnValueHandlerTests method sendToUserWithSendToDefaultOverride.

// SPR-14238
@Test
public void sendToUserWithSendToDefaultOverride() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    Class<?> clazz = SendToUserWithSendToOverrideTestBean.class;
    Method method = clazz.getDeclaredMethod("handleAndSendToDefaultDestination");
    MethodParameter parameter = new SynthesizingMethodParameter(method, -1);
    String sessionId = "sess1";
    Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null);
    this.handler.handleReturnValue(PAYLOAD, parameter, inputMessage);
    verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
    assertResponse(parameter, sessionId, 0, "/user/sess1/dest-default");
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) Message(org.springframework.messaging.Message) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) Test(org.junit.Test)

Example 2 with SynthesizingMethodParameter

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

the class SendToMethodReturnValueHandlerTests method sendToUserWithSendToOverride.

// SPR-14238
@Test
public void sendToUserWithSendToOverride() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    Class<?> clazz = SendToUserWithSendToOverrideTestBean.class;
    Method method = clazz.getDeclaredMethod("handleAndSendToOverride");
    MethodParameter parameter = new SynthesizingMethodParameter(method, -1);
    String sessionId = "sess1";
    Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null);
    this.handler.handleReturnValue(PAYLOAD, parameter, inputMessage);
    verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
    assertResponse(parameter, sessionId, 0, "/dest3");
    assertResponse(parameter, sessionId, 1, "/dest4");
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) Message(org.springframework.messaging.Message) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) Test(org.junit.Test)

Example 3 with SynthesizingMethodParameter

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

the class ParameterAutowireUtils method resolveDependency.

/**
	 * Resolve the dependency for the supplied {@link Parameter} from the
	 * supplied {@link ApplicationContext}.
	 * <p>Provides comprehensive autowiring support for individual method parameters
	 * on par with Spring's dependency injection facilities for autowired fields and
	 * methods, including support for {@link Autowired @Autowired},
	 * {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
	 * placeholders and SpEL expressions in {@code @Value} declarations.
	 * <p>The dependency is required unless the parameter is annotated with
	 * {@link Autowired @Autowired} with the {@link Autowired#required required}
	 * flag set to {@code false}.
	 * <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
	 * will be used as the qualifier for resolving ambiguities.
	 * @param parameter the parameter whose dependency should be resolved
	 * @param containingClass the concrete class that contains the parameter; this may
	 * differ from the class that declares the parameter in that it may be a subclass
	 * thereof, potentially substituting type variables
	 * @param applicationContext the application context from which to resolve the
	 * dependency
	 * @return the resolved object, or {@code null} if none found
	 * @throws BeansException if dependency resolution failed
	 * @see #isAutowirable(Parameter)
	 * @see Autowired#required
	 * @see SynthesizingMethodParameter#forParameter(Parameter)
	 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
	 */
static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {
    boolean required = findMergedAnnotation(parameter, Autowired.class).map(Autowired::required).orElse(true);
    MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
    DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
    descriptor.setContainingClass(containingClass);
    return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MethodParameter(org.springframework.core.MethodParameter)

Example 4 with SynthesizingMethodParameter

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

the class RequestHeaderMapMethodArgumentResolverTests method setUp.

@Before
public void setUp() throws Exception {
    resolver = new RequestHeaderMapMethodArgumentResolver();
    Method method = getClass().getMethod("params", Map.class, MultiValueMap.class, HttpHeaders.class, Map.class);
    paramMap = new SynthesizingMethodParameter(method, 0);
    paramMultiValueMap = new SynthesizingMethodParameter(method, 1);
    paramHttpHeaders = new SynthesizingMethodParameter(method, 2);
    paramUnsupported = new SynthesizingMethodParameter(method, 3);
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 5 with SynthesizingMethodParameter

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

the class CookieValueMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
    this.resolver = new CookieValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
    this.bindingContext = new BindingContext();
    Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
    this.cookieParameter = new SynthesizingMethodParameter(method, 0);
    this.cookieStringParameter = new SynthesizingMethodParameter(method, 1);
    this.stringParameter = new SynthesizingMethodParameter(method, 2);
    this.cookieMonoParameter = new SynthesizingMethodParameter(method, 3);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) Method(java.lang.reflect.Method) BindingContext(org.springframework.web.reactive.BindingContext) Before(org.junit.Before)

Aggregations

SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)22 Method (java.lang.reflect.Method)17 Before (org.junit.Before)15 MethodParameter (org.springframework.core.MethodParameter)8 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)8 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)7 DefaultParameterNameDiscoverer (org.springframework.core.DefaultParameterNameDiscoverer)4 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)4 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)3 Test (org.junit.Test)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 LocalVariableTableParameterNameDiscoverer (org.springframework.core.LocalVariableTableParameterNameDiscoverer)2 Message (org.springframework.messaging.Message)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 BindingContext (org.springframework.web.reactive.BindingContext)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 DependencyDescriptor (org.springframework.beans.factory.config.DependencyDescriptor)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1