JSR 349 验证:ExecutableValidator.validateParameters 静态方法

JSR 349 validation: ExecutableValidator.validateParameters on static method

BeanValidation 1.1 规范定义 API ExecutableValidator.validateParameters 以验证对给定方法的参数施加的所有约束。

但是 API 要求它传递一个对象实例,在该实例上调用要验证的方法:

/**
 * 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);

我的问题是如何验证静态方法调用?例如,调用下面定义的 Foo.bar 方法:

public class Foo {
   public static void bar(@NotNull String str) {...}
}

Bean Validation 1.1 不支持静态方法。来自 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.

一些实施者可能会在将来支持它作为增强功能(例如 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