3

BeanValidation 1.1 spec defines API ExecutableValidator.validateParameters to validate all constraints placed on the parameters of the given method.

However the API require it to pass an object instance on which the method to validate is invoked:

/**
 * Validates all constraints placed on the parameters of the given method.
 *
 * @param <T> the type hosting the method to validate
 * @param object the object on which the method to validate is invoked
 * @param method the method for which the parameter constraints is validated
 * @param parameterValues the values provided by the caller for the given method's
 *        parameters
 * @param groups the group or list of groups targeted for validation (defaults to
 *        {@link Default})
 * @return a set with the constraint violations caused by this validation;
 *         will be empty if no error occurs, but never {@code null}
 * @throws IllegalArgumentException if {@code null} is passed for any of the parameters
 *         or if parameters don't match with each other
 * @throws ValidationException if a non recoverable error happens during the
 *         validation process
 */
<T> Set<ConstraintViolation<T>> validateParameters(T object,
                                                   Method method,
                                                   Object[] parameterValues,
                                                   Class<?>... groups);

My question is how can I validate a static method invocation? E.g, the invocation of Foo.bar method defined below:

public class Foo {
   public static void bar(@NotNull String str) {...}
}
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • 1
    Why do you want to validate a static method to begin with? By definition of static, you don't validate the bean anymore. – Tunaki Mar 04 '17 at 23:00
  • I am not validating the bean `Foo`. However I need to validate the parameter `str` in the sample code mentioned – Gelin Luo Mar 05 '17 at 01:43

1 Answers1

3

Bean Validation 1.1 doesn't support static methods. From Requirements on classes to be validated:

Objects hosting constraints and expecting to be validated by Bean Validation providers must fulfill the following requirements:

  • [...]
  • Static fields and static methods are excluded from validation.

Some implementors might support it in the future as an enhancement (e.g. HV-606 for Hibernate Validator), but the specification itself doesn't. It's still explicitly not supported in the 2.0 draft of the specification.

Tunaki
  • 132,869
  • 46
  • 340
  • 423