org.springframework.core.annotation.SynthesizingMethodParameter Java Examples

The following examples show how to use org.springframework.core.annotation.SynthesizingMethodParameter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestHeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example #2
Source File: AnnotationMethodHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}.
 */
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
		NativeWebRequest webRequest, Exception thrownException) throws Exception {

	Class<?>[] paramTypes = handlerMethod.getParameterTypes();
	Object[] args = new Object[paramTypes.length];
	Class<?> handlerType = handler.getClass();
	for (int i = 0; i < args.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(handlerMethod, i);
		GenericTypeResolver.resolveParameterType(methodParam, handlerType);
		Class<?> paramType = methodParam.getParameterType();
		Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException);
		if (argValue != WebArgumentResolver.UNRESOLVED) {
			args[i] = argValue;
		}
		else {
			throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
					"] for @ExceptionHandler method: " + handlerMethod);
		}
	}
	return args;
}
 
Example #3
Source File: WxApiMethodInfo.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
private UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
    CompositeUriComponentsContributor contributor = defaultUriComponentsContributor;
    int paramCount = method.getParameterTypes().length;
    int argCount = args.length;
    if (paramCount != argCount) {
        throw new IllegalArgumentException("方法参数量为" + paramCount + " 与真实参数量不匹配,真实参数量为" + argCount);
    }
    final Map<String, Object> uriVars = new HashMap<>(8);
    for (int i = 0; i < paramCount; i++) {
        MethodParameter param = new SynthesizingMethodParameter(method, i);
        param.initParameterNameDiscovery(parameterNameDiscoverer);
        contributor.contributeMethodArgument(param, args[i], builder, uriVars);
    }
    // We may not have all URI var values, expand only what we have
    return builder.build().expand(name -> uriVars.containsKey(name) ? uriVars.get(name) : UriComponents.UriTemplateVariables.SKIP_VALUE);
}
 
Example #4
Source File: RequestHeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();
	resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	paramContextPath = new SynthesizingMethodParameter(method, 3);
	paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
	paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
	paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
	paramDate = new SynthesizingMethodParameter(method, 7);
	paramInstant = new SynthesizingMethodParameter(method, 8);

	servletRequest = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(webRequest);
}
 
Example #5
Source File: HeaderMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
	@SuppressWarnings("resource")
	GenericApplicationContext cxt = new GenericApplicationContext();
	cxt.refresh();
	this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());

	Method method = getClass().getDeclaredMethod("handleMessage",
			String.class, String.class, String.class, String.class, String.class);
	this.paramRequired = new SynthesizingMethodParameter(method, 0);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramNotAnnotated = new SynthesizingMethodParameter(method, 3);
	this.paramNativeHeader = new SynthesizingMethodParameter(method, 4);

	this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
	GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class);
}
 
Example #6
Source File: RequestHeaderMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();
	resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	paramContextPath = new SynthesizingMethodParameter(method, 3);
	paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
	paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
	paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
	paramDate = new SynthesizingMethodParameter(method, 7);
	paramInstant = new SynthesizingMethodParameter(method, 8);

	servletRequest = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(webRequest);
}
 
Example #7
Source File: ModelAttributeMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	this.request = new ServletWebRequest(new MockHttpServletRequest());
	this.container = new ModelAndViewContainer();
	this.processor = new ModelAttributeMethodProcessor(false);

	Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
			TestBean.class, Errors.class, int.class, TestBean.class,
			TestBean.class, TestBean.class);

	this.paramNamedValidModelAttr = new SynthesizingMethodParameter(method, 0);
	this.paramErrors = new SynthesizingMethodParameter(method, 1);
	this.paramInt = new SynthesizingMethodParameter(method, 2);
	this.paramModelAttr = new SynthesizingMethodParameter(method, 3);
	this.paramBindingDisabledAttr = new SynthesizingMethodParameter(method, 4);
	this.paramNonSimpleType = new SynthesizingMethodParameter(method, 5);

	method = getClass().getDeclaredMethod("annotatedReturnValue");
	this.returnParamNamedModelAttr = new MethodParameter(method, -1);

	method = getClass().getDeclaredMethod("notAnnotatedReturnValue");
	this.returnParamNonSimpleType = new MethodParameter(method, -1);
}
 
Example #8
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}.
 */
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
		NativeWebRequest webRequest, Exception thrownException) throws Exception {

	Class<?>[] paramTypes = handlerMethod.getParameterTypes();
	Object[] args = new Object[paramTypes.length];
	Class<?> handlerType = handler.getClass();
	for (int i = 0; i < args.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(handlerMethod, i);
		GenericTypeResolver.resolveParameterType(methodParam, handlerType);
		Class<?> paramType = methodParam.getParameterType();
		Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException);
		if (argValue != WebArgumentResolver.UNRESOLVED) {
			args[i] = argValue;
		}
		else {
			throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
					"] for @ExceptionHandler method: " + handlerMethod);
		}
	}
	return args;
}
 
Example #9
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}.
 */
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
		NativeWebRequest webRequest, Exception thrownException) throws Exception {

	Class<?>[] paramTypes = handlerMethod.getParameterTypes();
	Object[] args = new Object[paramTypes.length];
	Class<?> handlerType = handler.getClass();
	for (int i = 0; i < args.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(handlerMethod, i);
		GenericTypeResolver.resolveParameterType(methodParam, handlerType);
		Class<?> paramType = methodParam.getParameterType();
		Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException);
		if (argValue != WebArgumentResolver.UNRESOLVED) {
			args[i] = argValue;
		}
		else {
			throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
					"] for @ExceptionHandler method: " + handlerMethod);
		}
	}
	return args;
}
 
Example #10
Source File: ModelAttributeMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	this.request = new ServletWebRequest(new MockHttpServletRequest());
	this.container = new ModelAndViewContainer();
	this.processor = new ModelAttributeMethodProcessor(false);

	Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
			TestBean.class, Errors.class, int.class, TestBean.class,
			TestBean.class, TestBean.class);

	this.paramNamedValidModelAttr = new SynthesizingMethodParameter(method, 0);
	this.paramErrors = new SynthesizingMethodParameter(method, 1);
	this.paramInt = new SynthesizingMethodParameter(method, 2);
	this.paramModelAttr = new SynthesizingMethodParameter(method, 3);
	this.paramBindingDisabledAttr = new SynthesizingMethodParameter(method, 4);
	this.paramNonSimpleType = new SynthesizingMethodParameter(method, 5);

	method = getClass().getDeclaredMethod("annotatedReturnValue");
	this.returnParamNamedModelAttr = new MethodParameter(method, -1);

	method = getClass().getDeclaredMethod("notAnnotatedReturnValue");
	this.returnParamNonSimpleType = new MethodParameter(method, -1);
}
 
Example #11
Source File: SendToMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-14238
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");
}
 
Example #12
Source File: MvcUriComponentsBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static UriComponentsBuilder applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
	CompositeUriComponentsContributor contributor = getUriComponentsContributor();

	int paramCount = method.getParameterCount();
	int argCount = args.length;
	if (paramCount != argCount) {
		throw new IllegalArgumentException("Number of method parameters " + paramCount +
				" does not match number of argument values " + argCount);
	}

	final Map<String, Object> uriVars = new HashMap<>();
	for (int i = 0; i < paramCount; i++) {
		MethodParameter param = new SynthesizingMethodParameter(method, i);
		param.initParameterNameDiscovery(parameterNameDiscoverer);
		contributor.contributeMethodArgument(param, args[i], builder, uriVars);
	}

	// This may not be all the URI variables, supply what we have so far..
	return builder.uriVariables(uriVars);
}
 
Example #13
Source File: MatrixVariablesMapMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.resolver = new MatrixVariableMapMethodArgumentResolver();

	Method method = getClass().getMethod("handle", String.class,
			Map.class, MultiValueMap.class, MultiValueMap.class, Map.class);

	this.paramString = new SynthesizingMethodParameter(method, 0);
	this.paramMap = new SynthesizingMethodParameter(method, 1);
	this.paramMultivalueMap = new SynthesizingMethodParameter(method, 2);
	this.paramMapForPathVar = new SynthesizingMethodParameter(method, 3);
	this.paramMapWithName = new SynthesizingMethodParameter(method, 4);

	this.mavContainer = new ModelAndViewContainer();
	this.request = new MockHttpServletRequest();
	this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());

	Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<String, MultiValueMap<String, String>>();
	this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
}
 
Example #14
Source File: CookieValueMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();

	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	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);
}
 
Example #15
Source File: CookieValueMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();

	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	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);
}
 
Example #16
Source File: MvcUriComponentsBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static UriComponentsBuilder applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
	CompositeUriComponentsContributor contributor = getUriComponentsContributor();

	int paramCount = method.getParameterCount();
	int argCount = args.length;
	if (paramCount != argCount) {
		throw new IllegalArgumentException("Number of method parameters " + paramCount +
				" does not match number of argument values " + argCount);
	}

	final Map<String, Object> uriVars = new HashMap<>();
	for (int i = 0; i < paramCount; i++) {
		MethodParameter param = new SynthesizingMethodParameter(method, i);
		param.initParameterNameDiscovery(parameterNameDiscoverer);
		contributor.contributeMethodArgument(param, args[i], builder, uriVars);
	}

	// This may not be all the URI variables, supply what we have so far..
	return builder.uriVariables(uriVars);
}
 
Example #17
Source File: RequestHeaderMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setUp() throws Exception {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();
	resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());

	Method method = getClass().getMethod("params", String.class, String[].class, String.class, String.class, Map.class);
	paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	paramContextPath = new SynthesizingMethodParameter(method, 3);
	paramNamedValueMap = new SynthesizingMethodParameter(method, 4);

	servletRequest = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(webRequest);
}
 
Example #18
Source File: SendToMethodReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // SPR-14238
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");
}
 
Example #19
Source File: PayloadMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	this.resolver = new PayloadMethodArgumentResolver(new StringMessageConverter(), testValidator());

	Method payloadMethod = PayloadMethodArgumentResolverTests.class.getDeclaredMethod(
			"handleMessage", String.class, String.class, Locale.class,
			String.class, String.class, String.class, String.class);

	this.paramAnnotated = new SynthesizingMethodParameter(payloadMethod, 0);
	this.paramAnnotatedNotRequired = new SynthesizingMethodParameter(payloadMethod, 1);
	this.paramAnnotatedRequired = new SynthesizingMethodParameter(payloadMethod, 2);
	this.paramWithSpelExpression = new SynthesizingMethodParameter(payloadMethod, 3);
	this.paramValidated = new SynthesizingMethodParameter(payloadMethod, 4);
	this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	this.paramValidatedNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 5);
	this.paramNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 6);
}
 
Example #20
Source File: PayloadArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {

	this.resolver = new PayloadArgumentResolver(new StringMessageConverter(), testValidator());

	Method payloadMethod = PayloadArgumentResolverTests.class.getDeclaredMethod(
			"handleMessage", String.class, String.class, Locale.class,
			String.class, String.class, String.class, String.class);

	this.paramAnnotated = new SynthesizingMethodParameter(payloadMethod, 0);
	this.paramAnnotatedNotRequired = new SynthesizingMethodParameter(payloadMethod, 1);
	this.paramAnnotatedRequired = new SynthesizingMethodParameter(payloadMethod, 2);
	this.paramWithSpelExpression = new SynthesizingMethodParameter(payloadMethod, 3);
	this.paramValidated = new SynthesizingMethodParameter(payloadMethod, 4);
	this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	this.paramValidatedNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 5);
	this.paramNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 6);
}
 
Example #21
Source File: HeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() {
	@SuppressWarnings("resource")
	GenericApplicationContext cxt = new GenericApplicationContext();
	cxt.refresh();
	this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());

	Method method = ReflectionUtils.findMethod(getClass(), "handleMessage", (Class<?>[]) null);
	this.paramRequired = new SynthesizingMethodParameter(method, 0);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1);
	this.paramSystemPropertyDefaultValue = new SynthesizingMethodParameter(method, 2);
	this.paramSystemPropertyName = new SynthesizingMethodParameter(method, 3);
	this.paramNotAnnotated = new SynthesizingMethodParameter(method, 4);
	this.paramOptional = new SynthesizingMethodParameter(method, 5);
	this.paramNativeHeader = new SynthesizingMethodParameter(method, 6);

	this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
	GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class);
}
 
Example #22
Source File: RequestHeaderMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example #23
Source File: PathVariableMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	resolver = new PathVariableMethodArgumentResolver();
	mavContainer = new ModelAndViewContainer();
	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request, new MockHttpServletResponse());

	Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
	paramNamedString = new SynthesizingMethodParameter(method, 0);
	paramString = new SynthesizingMethodParameter(method, 1);
	paramNotRequired = new SynthesizingMethodParameter(method, 2);
	paramOptional = new SynthesizingMethodParameter(method, 3);
}
 
Example #24
Source File: CookieValueMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	resolver = new TestCookieValueMethodArgumentResolver();

	Method method = getClass().getMethod("params", Cookie.class, String.class, String.class);
	paramNamedCookie = new SynthesizingMethodParameter(method, 0);
	paramNamedDefaultValueString = new SynthesizingMethodParameter(method, 1);
	paramString = new SynthesizingMethodParameter(method, 2);

	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
 
Example #25
Source File: ServletCookieValueMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	resolver = new ServletCookieValueMethodArgumentResolver(null);
	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request, new MockHttpServletResponse());

	Method method = getClass().getMethod("params", Cookie.class, String.class);
	cookieParameter = new SynthesizingMethodParameter(method, 0);
	cookieStringParameter = new SynthesizingMethodParameter(method, 1);
}
 
Example #26
Source File: RequestHeaderMapMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #27
Source File: PayloadArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.resolver = new PayloadArgumentResolver(new StringMessageConverter(), testValidator());
	this.payloadMethod = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage",
			String.class, String.class, Locale.class, String.class, String.class, String.class, String.class);

	this.paramAnnotated = new SynthesizingMethodParameter(this.payloadMethod, 0);
	this.paramAnnotatedNotRequired = new SynthesizingMethodParameter(this.payloadMethod, 1);
	this.paramAnnotatedRequired = new SynthesizingMethodParameter(payloadMethod, 2);
	this.paramWithSpelExpression = new SynthesizingMethodParameter(payloadMethod, 3);
	this.paramValidated = new SynthesizingMethodParameter(this.payloadMethod, 4);
	this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	this.paramValidatedNotAnnotated = new SynthesizingMethodParameter(this.payloadMethod, 5);
	this.paramNotAnnotated = new SynthesizingMethodParameter(this.payloadMethod, 6);
}
 
Example #28
Source File: CookieValueMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	resolver = new TestCookieValueMethodArgumentResolver();

	Method method = getClass().getMethod("params", Cookie.class, String.class, String.class);
	paramNamedCookie = new SynthesizingMethodParameter(method, 0);
	paramNamedDefaultValueString = new SynthesizingMethodParameter(method, 1);
	paramString = new SynthesizingMethodParameter(method, 2);

	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
 
Example #29
Source File: RequestHeaderMapMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #30
Source File: SpringletsMvcUriComponentsBuilder.java    From springlets with Apache License 2.0 5 votes vote down vote up
private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
	CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor();
	if (contributor == null) {
		logger.debug("Using default CompositeUriComponentsContributor");
		contributor = defaultUriComponentsContributor;
	}

	int paramCount = method.getParameterTypes().length;
	int argCount = args.length;
	if (paramCount != argCount) {
		throw new IllegalArgumentException("Number of method parameters " + paramCount +
				" does not match number of argument values " + argCount);
	}

	final Map<String, Object> uriVars = new HashMap<String, Object>();
	for (int i = 0; i < paramCount; i++) {
		MethodParameter param = new SynthesizingMethodParameter(method, i);
		param.initParameterNameDiscovery(parameterNameDiscoverer);
		contributor.contributeMethodArgument(param, args[i], builder, uriVars);
	}

	// Custom implementation to remove uriVar if the value is null
	removeUriVarsWithNullValue(uriVars);

	// We may not have all URI var values, expand only what we have
	return builder.build().expand(new UriComponents.UriTemplateVariables() {
		@Override
		public Object getValue(String name) {
			return uriVars.containsKey(name) ? uriVars.get(name) : UriComponents.UriTemplateVariables.SKIP_VALUE;
		}
	});
}