Java Code Examples for org.springframework.core.MethodParameter#initParameterNameDiscovery()

The following examples show how to use org.springframework.core.MethodParameter#initParameterNameDiscovery() . 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: 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 2
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 3
Source File: ResolvableMethod.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private List<MethodParameter> applyFilters() {
	List<MethodParameter> matches = new ArrayList<>();
	for (int i = 0; i < method.getParameterCount(); i++) {
		MethodParameter param = new SynthesizingMethodParameter(method, i);
		param.initParameterNameDiscovery(nameDiscoverer);
		if (this.filters.stream().allMatch(p -> p.test(param))) {
			matches.add(param);
		}
	}
	return matches;
}
 
Example 4
Source File: InvocableHandlerMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Mono<Object[]> getMethodArgumentValues(
		ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {

	if (ObjectUtils.isEmpty(getMethodParameters())) {
		return EMPTY_ARGS;
	}
	MethodParameter[] parameters = getMethodParameters();
	List<Mono<Object>> argMonos = new ArrayList<>(parameters.length);
	for (MethodParameter parameter : parameters) {
		parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
		Object providedArg = findProvidedArgument(parameter, providedArgs);
		if (providedArg != null) {
			argMonos.add(Mono.just(providedArg));
			continue;
		}
		if (!this.resolvers.supportsParameter(parameter)) {
			return Mono.error(new IllegalStateException(
					formatArgumentError(parameter, "No suitable resolver")));
		}
		try {
			argMonos.add(this.resolvers.resolveArgument(parameter, bindingContext, exchange)
					.defaultIfEmpty(NO_ARG_VALUE)
					.doOnError(cause -> logArgumentErrorIfNecessary(exchange, parameter, cause)));
		}
		catch (Exception ex) {
			logArgumentErrorIfNecessary(exchange, parameter, ex);
			argMonos.add(Mono.error(ex));
		}
	}
	return Mono.zip(argMonos, values ->
			Stream.of(values).map(o -> o != NO_ARG_VALUE ? o : null).toArray());
}
 
Example 5
Source File: ResolvableMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
private List<MethodParameter> applyFilters() {
	List<MethodParameter> matches = new ArrayList<>();
	for (int i = 0; i < method.getParameterCount(); i++) {
		MethodParameter param = new SynthesizingMethodParameter(method, i);
		param.initParameterNameDiscovery(nameDiscoverer);
		if (this.filters.stream().allMatch(p -> p.test(param))) {
			matches.add(param);
		}
	}
	return matches;
}
 
Example 6
Source File: InvocableHandlerMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Get the method argument values for the current message, checking the provided
 * argument values and falling back to the configured argument resolvers.
 * <p>The resulting array will be passed into {@link #doInvoke}.
 * @since 5.1.2
 */
protected Object[] getMethodArgumentValues(Message<?> message, Object... providedArgs) throws Exception {
	if (ObjectUtils.isEmpty(getMethodParameters())) {
		return EMPTY_ARGS;
	}
	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] = findProvidedArgument(parameter, providedArgs);
		if (args[i] != null) {
			continue;
		}
		if (!this.resolvers.supportsParameter(parameter)) {
			throw new MethodArgumentResolutionException(
					message, parameter, formatArgumentError(parameter, "No suitable resolver"));
		}
		try {
			args[i] = this.resolvers.resolveArgument(parameter, message);
		}
		catch (Exception ex) {
			// Leave stack trace for later, exception may actually be resolved and handled..
			if (logger.isDebugEnabled()) {
				String error = ex.getMessage();
				if (error != null && !error.contains(parameter.getExecutable().toGenericString())) {
					logger.debug(formatArgumentError(parameter, error));
				}
			}
			throw ex;
		}
	}
	return args;
}
 
Example 7
Source File: InvocableHandlerMethod.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
		GenericTypeResolver.resolveParameterType(parameter, getBean().getClass());
		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("Error resolving argument", i), ex);
				}
				throw ex;
			}
		}
		if (args[i] == null) {
			String msg = getArgumentResolutionErrorMessage("No suitable resolver for argument", i);
			throw new IllegalStateException(msg);
		}
	}
	return args;
}
 
Example 8
Source File: ResolvableMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
private List<MethodParameter> applyFilters() {
	List<MethodParameter> matches = new ArrayList<>();
	for (int i = 0; i < method.getParameterCount(); i++) {
		MethodParameter param = new SynthesizingMethodParameter(method, i);
		param.initParameterNameDiscovery(nameDiscoverer);
		if (this.filters.stream().allMatch(p -> p.test(param))) {
			matches.add(param);
		}
	}
	return matches;
}
 
Example 9
Source File: InvocableHandlerMethod.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Get the method argument values for the current request, checking the provided
 * argument values and falling back to the configured argument resolvers.
 * <p>The resulting array will be passed into {@link #doInvoke}.
 * @since 5.1.2
 */
protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer,
		Object... providedArgs) throws Exception {

	MethodParameter[] parameters = getMethodParameters();
	if (ObjectUtils.isEmpty(parameters)) {
		return EMPTY_ARGS;
	}

	Object[] args = new Object[parameters.length];
	for (int i = 0; i < parameters.length; i++) {
		MethodParameter parameter = parameters[i];
		parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
		args[i] = findProvidedArgument(parameter, providedArgs);
		if (args[i] != null) {
			continue;
		}
		if (!this.resolvers.supportsParameter(parameter)) {
			throw new IllegalStateException(formatArgumentError(parameter, "No suitable resolver"));
		}
		try {
			args[i] = this.resolvers.resolveArgument(parameter, mavContainer, request, this.dataBinderFactory);
		}
		catch (Exception ex) {
			// Leave stack trace for later, exception may actually be resolved and handled...
			if (logger.isDebugEnabled()) {
				String exMsg = ex.getMessage();
				if (exMsg != null && !exMsg.contains(parameter.getExecutable().toGenericString())) {
					logger.debug(formatArgumentError(parameter, exMsg));
				}
			}
			throw ex;
		}
	}
	return args;
}
 
Example 10
Source File: PojoMethodMapping.java    From netty-websocket-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
private static MethodParameter[] getParameters(Method m) {
    if (m == null) {
        return new MethodParameter[0];
    }
    int count = m.getParameterCount();
    MethodParameter[] result = new MethodParameter[count];
    for (int i = 0; i < count; i++) {
        MethodParameter methodParameter = new MethodParameter(m, i);
        methodParameter.initParameterNameDiscovery(parameterNameDiscoverer);
        result[i] = methodParameter;
    }
    return result;
}
 
Example 11
Source File: AbstractRequestAttributesArgumentResolverTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private MethodParameter initMethodParameter(int parameterIndex) {
	MethodParameter param = new SynthesizingMethodParameter(this.handleMethod, parameterIndex);
	param.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
	GenericTypeResolver.resolveParameterType(param, this.resolver.getClass());
	return param;
}
 
Example 12
Source File: SessionAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private MethodParameter initMethodParameter(int parameterIndex) {
	MethodParameter param = new SynthesizingMethodParameter(this.handleMethod, parameterIndex);
	param.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
	GenericTypeResolver.resolveParameterType(param, this.resolver.getClass());
	return param;
}
 
Example 13
Source File: PathParametersSnippetTest.java    From spring-auto-restdocs with Apache License 2.0 4 votes vote down vote up
private void initParameters(HandlerMethod handlerMethod) {
    for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
        parameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
    }
}
 
Example 14
Source File: AbstractRequestAttributesArgumentResolverTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private MethodParameter initMethodParameter(int parameterIndex) {
	MethodParameter param = new SynthesizingMethodParameter(this.handleMethod, parameterIndex);
	param.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
	GenericTypeResolver.resolveParameterType(param, this.resolver.getClass());
	return param;
}
 
Example 15
Source File: RequestParametersSnippetTest.java    From spring-auto-restdocs with Apache License 2.0 4 votes vote down vote up
private void initParameters(HandlerMethod handlerMethod) {
    for (MethodParameter parameter : handlerMethod.getMethodParameters()) {
        parameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
    }
}
 
Example 16
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod,
		WebDataBinder binder, NativeWebRequest webRequest) throws Exception {

	Class<?>[] initBinderParams = initBinderMethod.getParameterTypes();
	Object[] initBinderArgs = new Object[initBinderParams.length];

	for (int i = 0; i < initBinderArgs.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(initBinderMethod, i);
		methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);
		GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());
		String paramName = null;
		boolean paramRequired = false;
		String paramDefaultValue = null;
		String pathVarName = null;
		Annotation[] paramAnns = methodParam.getParameterAnnotations();

		for (Annotation paramAnn : paramAnns) {
			if (RequestParam.class.isInstance(paramAnn)) {
				RequestParam requestParam = (RequestParam) paramAnn;
				paramName = requestParam.name();
				paramRequired = requestParam.required();
				paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
				break;
			}
			else if (ModelAttribute.class.isInstance(paramAnn)) {
				throw new IllegalStateException(
						"@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod);
			}
			else if (PathVariable.class.isInstance(paramAnn)) {
				PathVariable pathVar = (PathVariable) paramAnn;
				pathVarName = pathVar.value();
			}
		}

		if (paramName == null && pathVarName == null) {
			Object argValue = resolveCommonArgument(methodParam, webRequest);
			if (argValue != WebArgumentResolver.UNRESOLVED) {
				initBinderArgs[i] = argValue;
			}
			else {
				Class<?> paramType = initBinderParams[i];
				if (paramType.isInstance(binder)) {
					initBinderArgs[i] = binder;
				}
				else if (BeanUtils.isSimpleProperty(paramType)) {
					paramName = "";
				}
				else {
					throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
							"] for @InitBinder method: " + initBinderMethod);
				}
			}
		}

		if (paramName != null) {
			initBinderArgs[i] =
					resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null);
		}
		else if (pathVarName != null) {
			initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null);
		}
	}

	return initBinderArgs;
}
 
Example 17
Source File: ClassUtil.java    From mica with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 获取方法参数信息
 *
 * @param constructor    构造器
 * @param parameterIndex 参数序号
 * @return {MethodParameter}
 */
public static MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) {
	MethodParameter methodParameter = new SynthesizingMethodParameter(constructor, parameterIndex);
	methodParameter.initParameterNameDiscovery(PARAMETER_NAME_DISCOVERER);
	return methodParameter;
}
 
Example 18
Source File: ClassUtils.java    From spring-microservice-exam with MIT License 2 votes vote down vote up
/**
 * 获取方法参数信息
 *
 * @param constructor    构造器
 * @param parameterIndex 参数序号
 * @return {MethodParameter}
 */
public static MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) {
    MethodParameter methodParameter = new SynthesizingMethodParameter(constructor, parameterIndex);
    methodParameter.initParameterNameDiscovery(PARAMETERNAMEDISCOVERER);
    return methodParameter;
}
 
Example 19
Source File: ClassUtil.java    From magic-starter with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 获取方法参数信息
 *
 * @param constructor    构造器
 * @param parameterIndex 参数序号
 * @return {MethodParameter}
 */
public static MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) {
	MethodParameter methodParameter = new SynthesizingMethodParameter(constructor, parameterIndex);
	methodParameter.initParameterNameDiscovery(PARAMETER_NAME_DISCOVERER);
	return methodParameter;
}
 
Example 20
Source File: ClassUtil.java    From magic-starter with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 获取方法参数信息
 *
 * @param method         方法
 * @param parameterIndex 参数序号
 * @return {MethodParameter}
 */
public static MethodParameter getMethodParameter(Method method, int parameterIndex) {
	MethodParameter methodParameter = new SynthesizingMethodParameter(method, parameterIndex);
	methodParameter.initParameterNameDiscovery(PARAMETER_NAME_DISCOVERER);
	return methodParameter;
}