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

The following examples show how to use org.springframework.core.MethodParameter#getParameterIndex() . 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: MappingJackson2MessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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
 */
@Nullable
protected Class<?> getSerializationView(@Nullable 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;
}
 
Example 2
Source File: ErrorsMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
private Object getErrors(MethodParameter parameter, BindingContext context) {
	Assert.isTrue(parameter.getParameterIndex() > 0,
			"Errors argument must be declared immediately after a model attribute argument");

	int index = parameter.getParameterIndex() - 1;
	MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());

	Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
			"cannot both be declared with an async type wrapper. " +
			"Either declare the @ModelAttribute without an async wrapper type or " +
			"handle a WebExchangeBindException error signal through the async type.");

	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null && StringUtils.hasText(ann.value()) ?
			ann.value() : Conventions.getVariableNameForParameter(attributeParam));
	Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);

	Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
			"immediately after the @ModelAttribute argument to which it applies. " +
			"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
			"type wrapper and use its onError operators to handle WebExchangeBindException: " +
			parameter.getMethod());

	return errors;
}
 
Example 3
Source File: OpenFeignAutoConfiguration.java    From summerframework with Apache License 2.0 6 votes vote down vote up
public static MethodParameter interfaceMethodParameter(MethodParameter parameter, Class annotationType) {
    if (!parameter.hasParameterAnnotation(annotationType)) {
        for (Class<?> itf : parameter.getDeclaringClass().getInterfaces()) {
            try {
                Method method =
                    itf.getMethod(parameter.getMethod().getName(), parameter.getMethod().getParameterTypes());
                MethodParameter itfParameter = new InterfaceMethodParameter(method, parameter.getParameterIndex());
                if (itfParameter.hasParameterAnnotation(annotationType)) {
                    return itfParameter;
                }
            } catch (NoSuchMethodException e) {
                continue;
            }
        }
    }
    return parameter;
}
 
Example 4
Source File: ErrorsMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private Object getErrors(MethodParameter parameter, BindingContext context) {
	Assert.isTrue(parameter.getParameterIndex() > 0,
			"Errors argument must be declared immediately after a model attribute argument");

	int index = parameter.getParameterIndex() - 1;
	MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());

	Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
			"cannot both be declared with an async type wrapper. " +
			"Either declare the @ModelAttribute without an async wrapper type or " +
			"handle a WebExchangeBindException error signal through the async type.");

	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null && StringUtils.hasText(ann.value()) ?
			ann.value() : Conventions.getVariableNameForParameter(attributeParam));
	Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);

	Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
			"immediately after the @ModelAttribute argument to which it applies. " +
			"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
			"type wrapper and use its onError operators to handle WebExchangeBindException: " +
			parameter.getMethod());

	return errors;
}
 
Example 5
Source File: WxApiExecutor.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
private Object getObjectBody(WxApiMethodInfo wxApiMethodInfo, Object[] args) {
    MethodParameter methodParameter = wxApiMethodInfo.getMethodParameters().stream()
            .filter(p -> !BeanUtils.isSimpleValueType(p.getParameterType()) || p.hasParameterAnnotation(WxApiBody.class))
            .findFirst().orElse(null);
    if (methodParameter == null) {
        throw new WxAppException("没有可处理的参数");
    }
    // 不是简单类型
    if (!BeanUtils.isSimpleValueType(methodParameter.getParameterType())) {
        // 暂时只支持json
        return args[methodParameter.getParameterIndex()];
    }
    if (args[methodParameter.getParameterIndex()] != null) {
        return args[methodParameter.getParameterIndex()].toString();
    } else {
        return "";
    }
}
 
Example 6
Source File: VenusFeignAutoConfig.java    From venus-cloud-feign with Apache License 2.0 6 votes vote down vote up
public static MethodParameter interfaceMethodParameter(MethodParameter parameter, Class annotationType) {
    if (!parameter.hasParameterAnnotation(annotationType)) {
        for (Class<?> itf : parameter.getDeclaringClass().getInterfaces()) {
            try {
                Method method = itf.getMethod(parameter.getMethod().getName(), parameter.getMethod().getParameterTypes());
                MethodParameter itfParameter = new MethodParameter(method, parameter.getParameterIndex());
                if (itfParameter.hasParameterAnnotation(annotationType)) {
                    return itfParameter;
                }
            } catch (NoSuchMethodException e) {
                continue;
            }
        }
    }
    return parameter;
}
 
Example 7
Source File: ModelAttributeMethodProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Whether to raise a fatal bind exception on validation errors.
 * @param parameter the method parameter declaration
 * @return {@code true} if the next method parameter is not of type {@link Errors}
 * @since 5.0
 */
protected boolean isBindExceptionRequired(MethodParameter parameter) {
	int i = parameter.getParameterIndex();
	Class<?>[] paramTypes = parameter.getExecutable().getParameterTypes();
	boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
	return !hasBindingResult;
}
 
Example 8
Source File: TypeDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new type descriptor from a {@link MethodParameter}.
 * <p>Use this constructor when a source or target conversion point is a
 * constructor parameter, method parameter, or method return value.
 * @param methodParameter the method parameter
 */
public TypeDescriptor(MethodParameter methodParameter) {
	this.resolvableType = ResolvableType.forMethodParameter(methodParameter);
	this.type = this.resolvableType.resolve(methodParameter.getParameterType());
	this.annotatedElement = new AnnotatedElementAdapter(methodParameter.getParameterIndex() == -1 ?
			methodParameter.getMethodAnnotations() : methodParameter.getParameterAnnotations());
}
 
Example 9
Source File: RequestBodyArgumentResolver.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 判断类对应方法的参数是否有requestbody注解
 * @param clazz
 * @param refParam
 * @return
 */
private boolean hasRequestBodyAnnotation(Class<?> clazz, MethodParameter refParam) {
    if (ClassUtils.hasMethod(clazz, refParam.getExecutable().getName(), refParam.getExecutable().getParameterTypes())) {
        Method tmpMethod = ClassUtils.getMethod(clazz, refParam.getExecutable().getName(), refParam.getExecutable().getParameterTypes());
        MethodParameter supperParam = new MethodParameter(tmpMethod, refParam.getParameterIndex());
        if (supperParam.hasParameterAnnotation(RequestBody.class)) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: ModelAttributeMethodProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Whether to raise a fatal bind exception on validation errors.
 * @param binder the data binder used to perform data binding
 * @param methodParam the method argument
 * @return {@code true} if the next method argument is not of type {@link Errors}
 */
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter methodParam) {
	int i = methodParam.getParameterIndex();
	Class<?>[] paramTypes = methodParam.getMethod().getParameterTypes();
	boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
	return !hasBindingResult;
}
 
Example 11
Source File: UifDefaultFormMethodArgumentResolver.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Whether to raise a {@link BindException} on validation errors.
 *
 * @param parameter the method argument
 * @return {@code true} if the next method argument is not of type {@link org.springframework.validation.Errors}.
 */
protected boolean isBindExceptionRequired(MethodParameter parameter) {
    int i = parameter.getParameterIndex();
    Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
    boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));

    return !hasBindingResult;
}
 
Example 12
Source File: ModelAttributeMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private boolean hasErrorsArgument(MethodParameter parameter) {
	int i = parameter.getParameterIndex();
	Class<?>[] paramTypes = parameter.getExecutable().getParameterTypes();
	return (paramTypes.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]));
}
 
Example 13
Source File: HandlerMethod.java    From spring-analysis-note with MIT License 4 votes vote down vote up
protected static String formatArgumentError(MethodParameter param, String message) {
	return "Could not resolve parameter [" + param.getParameterIndex() + "] in " +
			param.getExecutable().toGenericString() + (StringUtils.hasText(message) ? ": " + message : "");
}
 
Example 14
Source File: HandlerMethodUtils.java    From spring-webmvc-support with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Find {@link Annotation} from {@link MethodParameter method parameter or method return type}
 *
 * @param methodParameter {@link MethodParameter method parameter or method return type}
 * @param annotationClass {@link Annotation} type
 * @param <A>             {@link Annotation} type
 * @return {@link Annotation} If found , return <code>null</code>
 */
public static <A extends Annotation> A findAnnotation(MethodParameter methodParameter, Class<A> annotationClass) {

    A annotation = null;

    boolean isReturnType = methodParameter.getParameterIndex() < 0;

    if (isReturnType) {

        annotation = methodParameter.getMethodAnnotation(annotationClass);

    } else { // is parameter

        annotation = methodParameter.getParameterAnnotation(annotationClass);

    }


    return annotation;

}
 
Example 15
Source File: PayloadArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
private String getParameterName(MethodParameter param) {
	String paramName = param.getParameterName();
	return (paramName != null ? paramName : "Arg " + param.getParameterIndex());
}
 
Example 16
Source File: PayloadMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private String getParameterName(MethodParameter param) {
	String paramName = param.getParameterName();
	return (paramName != null ? paramName : "Arg " + param.getParameterIndex());
}
 
Example 17
Source File: ModelAttributeMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
private boolean hasErrorsArgument(MethodParameter parameter) {
	int i = parameter.getParameterIndex();
	Class<?>[] paramTypes = parameter.getExecutable().getParameterTypes();
	return (paramTypes.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]));
}
 
Example 18
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Whether to raise a fatal bind exception on validation errors.
 * @param binder the data binder used to perform data binding
 * @param parameter the method parameter descriptor
 * @return {@code true} if the next method argument is not of type {@link Errors}
 * @since 4.1.5
 */
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
	int i = parameter.getParameterIndex();
	Class<?>[] paramTypes = parameter.getExecutable().getParameterTypes();
	boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
	return !hasBindingResult;
}
 
Example 19
Source File: FormModelMethodArgumentResolver.java    From es with Apache License 2.0 3 votes vote down vote up
/**
 * Whether to raise a {@link org.springframework.validation.BindException} on bind or validation errors.
 * The default implementation returns {@code true} if the next method
 * argument is not of type {@link org.springframework.validation.Errors}.
 *
 * @param binder    the data binder used to perform data binding
 * @param parameter the method argument
 */
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
    int i = parameter.getParameterIndex();
    Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
    boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));

    return !hasBindingResult;
}
 
Example 20
Source File: AbstractMessageConverterMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Whether to raise a fatal bind exception on validation errors.
 * @param binder the data binder used to perform data binding
 * @param methodParam the method argument
 * @return {@code true} if the next method argument is not of type {@link Errors}
 * @since 4.1.5
 */
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter methodParam) {
	int i = methodParam.getParameterIndex();
	Class<?>[] paramTypes = methodParam.getMethod().getParameterTypes();
	boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
	return !hasBindingResult;
}