/** * Return {@code true} if the parameter has at least one annotation, * {@code false} if it has none. * @see #getParameterAnnotations() */ public boolean hasParameterAnnotations() { return (getParameterAnnotations().length != 0); }
/** * Return {@code true} if the parameter has at least one annotation, * {@code false} if it has none. * @see #getParameterAnnotations() */ public boolean hasParameterAnnotations() { return (getParameterAnnotations().length != 0); }
/** * Check whether this method parameter is annotated with any variant of a * {@code Nullable} annotation, e.g. {@code javax.annotation.Nullable} or * {@code edu.umd.cs.findbugs.annotations.Nullable}. */ private boolean hasNullableAnnotation() { for (Annotation ann : getParameterAnnotations()) { if ("Nullable".equals(ann.annotationType().getSimpleName())) { return true; } } return false; }
/** * Return the parameter annotation of the given type, if available. * @param annotationType the annotation type to look for * @return the annotation object, or {@code null} if not found */ @SuppressWarnings("unchecked") @Nullable public <A extends Annotation> A getParameterAnnotation(Class<A> annotationType) { Annotation[] anns = getParameterAnnotations(); for (Annotation ann : anns) { if (annotationType.isInstance(ann)) { return (A) ann; } } return null; }
/** * Obtain the annotations associated with the wrapped field or method/constructor parameter. */ public Annotation[] getAnnotations() { if (this.field != null) { Annotation[] fieldAnnotations = this.fieldAnnotations; if (fieldAnnotations == null) { fieldAnnotations = this.field.getAnnotations(); this.fieldAnnotations = fieldAnnotations; } return fieldAnnotations; } else { return obtainMethodParameter().getParameterAnnotations(); } }
/** * Check whether this method parameter is annotated with any variant of a * {@code Nullable} annotation, e.g. {@code javax.annotation.Nullable} or * {@code edu.umd.cs.findbugs.annotations.Nullable}. */ private boolean hasNullableAnnotation() { for (Annotation ann : getParameterAnnotations()) { if ("Nullable".equals(ann.annotationType().getSimpleName())) { return true; } } return false; }
/** * Return the parameter annotation of the given type, if available. * @param annotationType the annotation type to look for * @return the annotation object, or {@code null} if not found */ @SuppressWarnings("unchecked") @Nullable public <A extends Annotation> A getParameterAnnotation(Class<A> annotationType) { Annotation[] anns = getParameterAnnotations(); for (Annotation ann : anns) { if (annotationType.isInstance(ann)) { return (A) ann; } } return null; }
Annotation[] anns = this.combinedAnnotations; if (anns == null) { anns = super.getParameterAnnotations(); try { Field field = getDeclaringClass().getDeclaredField(this.parameterName);
/** * Check if the given MethodParameter requires validation and if so return * a (possibly empty) Object[] with validation hints. A return value of * {@code null} indicates that validation is not required. */ @Nullable private Object[] extractValidationHints(MethodParameter parameter) { Annotation[] annotations = parameter.getParameterAnnotations(); for (Annotation ann : annotations) { Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) { Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann)); return (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints}); } } return null; }
/** * Filter on method arguments that don't have the given annotations. * @param annotationTypes the annotation types */ @SafeVarargs public final ArgResolver annotNotPresent(Class<? extends Annotation>... annotationTypes) { this.filters.add(param -> (annotationTypes.length > 0 ? Arrays.stream(annotationTypes).noneMatch(param::hasParameterAnnotation) : param.getParameterAnnotations().length == 0)); return this; }
/** * Filter on method arguments that don't have the given annotations. * @param annotationTypes the annotation types */ @SafeVarargs public final ArgResolver annotNotPresent(Class<? extends Annotation>... annotationTypes) { this.filters.add(param -> (annotationTypes.length > 0 ? Arrays.stream(annotationTypes).noneMatch(param::hasParameterAnnotation) : param.getParameterAnnotations().length == 0)); return this; }
/** * Obtain the annotations associated with the wrapped field or method/constructor parameter. */ public Annotation[] getAnnotations() { if (this.field != null) { Annotation[] fieldAnnotations = this.fieldAnnotations; if (fieldAnnotations == null) { fieldAnnotations = this.field.getAnnotations(); this.fieldAnnotations = fieldAnnotations; } return fieldAnnotations; } else { return obtainMethodParameter().getParameterAnnotations(); } }
/** * Validate the model attribute if applicable. * <p>The default implementation checks for {@code @javax.validation.Valid}, * Spring's {@link org.springframework.validation.annotation.Validated}, * and custom annotations whose name starts with "Valid". * @param binder the DataBinder to be used * @param parameter the method parameter declaration * @see WebDataBinder#validate(Object...) * @see SmartValidator#validate(Object, Errors, Object...) */ protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) { for (Annotation ann : parameter.getParameterAnnotations()) { Object[] validationHints = determineValidationHints(ann); if (validationHints != null) { binder.validate(validationHints); break; } } }
private void validateIfApplicable(WebExchangeDataBinder binder, MethodParameter parameter) { for (Annotation ann : parameter.getParameterAnnotations()) { Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) { Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann)); if (hints != null) { Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints}); binder.validate(validationHints); } else { binder.validate(); } } } }
/** * Validate the binding target if applicable. * <p>The default implementation checks for {@code @javax.validation.Valid}, * Spring's {@link org.springframework.validation.annotation.Validated}, * and custom annotations whose name starts with "Valid". * @param binder the DataBinder to be used * @param parameter the method parameter descriptor * @since 4.1.5 * @see #isBindExceptionRequired */ protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) { Annotation[] annotations = parameter.getParameterAnnotations(); for (Annotation ann : annotations) { Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) { Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann)); Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints}); binder.validate(validationHints); break; } } }
/** * Validate the model attribute if applicable. * <p>The default implementation checks for {@code @javax.validation.Valid}, * Spring's {@link org.springframework.validation.annotation.Validated}, * and custom annotations whose name starts with "Valid". * @param binder the DataBinder to be used * @param parameter the method parameter declaration * @see WebDataBinder#validate(Object...) * @see SmartValidator#validate(Object, Errors, Object...) */ protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) { for (Annotation ann : parameter.getParameterAnnotations()) { Object[] validationHints = determineValidationHints(ann); if (validationHints != null) { binder.validate(validationHints); break; } } }
Class<?> targetType, String fieldName, @Nullable Object value) { for (Annotation ann : parameter.getParameterAnnotations()) { Object[] validationHints = determineValidationHints(ann); if (validationHints != null) {
/** * 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.getNestedParameterType()); this.annotatedElement = new AnnotatedElementAdapter(methodParameter.getParameterIndex() == -1 ? methodParameter.getMethodAnnotations() : methodParameter.getParameterAnnotations()); }
return; for (Annotation ann : parameter.getParameterAnnotations()) { Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
/** * 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.getNestedParameterType()); this.annotatedElement = new AnnotatedElementAdapter(methodParameter.getParameterIndex() == -1 ? methodParameter.getMethodAnnotations() : methodParameter.getParameterAnnotations()); }