@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MethodParameter)) { return false; } MethodParameter otherParam = (MethodParameter) other; return (this.parameterIndex == otherParam.parameterIndex && getExecutable().equals(otherParam.getExecutable())); }
@Override public int hashCode() { return (getExecutable().hashCode() * 31 + this.parameterIndex); }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MethodParameter)) { return false; } MethodParameter otherParam = (MethodParameter) other; return (this.parameterIndex == otherParam.parameterIndex && getExecutable().equals(otherParam.getExecutable())); }
/** * Return the {@link Parameter} descriptor for method/constructor parameter. * @since 5.0 */ public Parameter getParameter() { if (this.parameterIndex < 0) { throw new IllegalStateException("Cannot retrieve Parameter descriptor for method return type"); } Parameter parameter = this.parameter; if (parameter == null) { parameter = getExecutable().getParameters()[this.parameterIndex]; this.parameter = parameter; } return parameter; }
/** * 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; }
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])); }
private void logArgumentErrorIfNecessary( ServerWebExchange exchange, MethodParameter parameter, Throwable cause) { // Leave stack trace for later, if error is not handled.. String message = cause.getMessage(); if (!message.contains(parameter.getExecutable().toGenericString())) { if (logger.isDebugEnabled()) { logger.debug(exchange.getLogPrefix() + formatArgumentError(parameter, message)); } } }
@Override public String getMessage() { StringBuilder sb = new StringBuilder("Validation failed for argument [") .append(this.parameter.getParameterIndex()).append("] in ") .append(this.parameter.getExecutable().toGenericString()); if (this.bindingResult.getErrorCount() > 1) { sb.append(" with ").append(this.bindingResult.getErrorCount()).append(" errors"); } sb.append(": "); for (ObjectError error : this.bindingResult.getAllErrors()) { sb.append("[").append(error).append("] "); } return sb.toString(); }
/** * 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; }
/** * Returns diagnostic information about the errors held in this object. */ @Override public String getMessage() { MethodParameter parameter = getMethodParameter(); Assert.state(parameter != null, "No MethodParameter"); StringBuilder sb = new StringBuilder("Validation failed for argument at index ") .append(parameter.getParameterIndex()).append(" in method: ") .append(parameter.getExecutable().toGenericString()) .append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): "); for (ObjectError error : this.bindingResult.getAllErrors()) { sb.append("[").append(error).append("] "); } return sb.toString(); }
protected static String formatArgumentError(MethodParameter param, String message) { return "Could not resolve parameter [" + param.getParameterIndex() + "] in " + param.getExecutable().toGenericString() + (StringUtils.hasText(message) ? ": " + message : ""); }
private ServerWebInputException handleMissingBody(MethodParameter parameter) { String paramInfo = parameter.getExecutable().toGenericString(); return new ServerWebInputException("Request body is missing: " + paramInfo, parameter); }
private static String getMethodParameterMessage(MethodParameter parameter) { return "Could not resolve method parameter at index " + parameter.getParameterIndex() + " in " + parameter.getExecutable().toGenericString(); }
public MethodParameterTypeProvider(MethodParameter methodParameter) { this.methodName = (methodParameter.getMethod() != null ? methodParameter.getMethod().getName() : null); this.parameterTypes = methodParameter.getExecutable().getParameterTypes(); this.declaringClass = methodParameter.getDeclaringClass(); this.parameterIndex = methodParameter.getParameterIndex(); this.methodParameter = methodParameter; }
private DestinationHelper getDestinationHelper(MessageHeaders headers, MethodParameter returnType) { SendToUser m1 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendToUser.class); SendTo m2 = AnnotatedElementUtils.findMergedAnnotation(returnType.getExecutable(), SendTo.class); if ((m1 != null && !ObjectUtils.isEmpty(m1.value())) || (m2 != null && !ObjectUtils.isEmpty(m2.value()))) { return new DestinationHelper(headers, m1, m2); } SendToUser c1 = AnnotatedElementUtils.findMergedAnnotation(returnType.getDeclaringClass(), SendToUser.class); SendTo c2 = AnnotatedElementUtils.findMergedAnnotation(returnType.getDeclaringClass(), SendTo.class); if ((c1 != null && !ObjectUtils.isEmpty(c1.value())) || (c2 != null && !ObjectUtils.isEmpty(c2.value()))) { return new DestinationHelper(headers, c1, c2); } return (m1 != null || m2 != null ? new DestinationHelper(headers, m1, m2) : new DestinationHelper(headers, c1, c2)); }
@Override protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException { HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class); Assert.state(servletRequest != null, "No HttpServletRequest"); ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest); Object arg = readWithMessageConverters(inputMessage, parameter, paramType); if (arg == null && checkRequired(parameter)) { throw new HttpMessageNotReadableException("Required request body is missing: " + parameter.getExecutable().toGenericString(), inputMessage); } return arg; }
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; }
/** * Create a new descriptor for a method or constructor parameter. * @param methodParameter the MethodParameter to wrap * @param required whether the dependency is required * @param eager whether this dependency is 'eager' in the sense of * eagerly resolving potential target beans for type matching */ public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) { super(methodParameter); this.declaringClass = methodParameter.getDeclaringClass(); if (methodParameter.getMethod() != null) { this.methodName = methodParameter.getMethod().getName(); } this.parameterTypes = methodParameter.getExecutable().getParameterTypes(); this.parameterIndex = methodParameter.getParameterIndex(); this.containingClass = methodParameter.getContainingClass(); this.required = required; this.eager = eager; }
public MethodParameterTypeProvider(MethodParameter methodParameter) { this.methodName = (methodParameter.getMethod() != null ? methodParameter.getMethod().getName() : null); this.parameterTypes = methodParameter.getExecutable().getParameterTypes(); this.declaringClass = methodParameter.getDeclaringClass(); this.parameterIndex = methodParameter.getParameterIndex(); this.methodParameter = methodParameter; }