下面列出了org.springframework.core.MethodParameter#getParameterIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 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;
}
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 "";
}
}
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;
}
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;
}
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;
}
/**
* 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;
}
/**
* 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());
}
/**
* 判断类对应方法的参数是否有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;
}
/**
* 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;
}
/**
* 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;
}
private String getParameterName(MethodParameter param) {
String paramName = param.getParameterName();
return (paramName != null ? paramName : "Arg " + param.getParameterIndex());
}
protected static String formatArgumentError(MethodParameter param, String message) {
return "Could not resolve parameter [" + param.getParameterIndex() + "] in " +
param.getExecutable().toGenericString() + (StringUtils.hasText(message) ? ": " + message : "");
}
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]));
}
/**
* 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;
}
private String getParameterName(MethodParameter param) {
String paramName = param.getParameterName();
return (paramName != null ? paramName : "Arg " + param.getParameterIndex());
}
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]));
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}