2017-03-04 408 views
1

BeanValidation 1.1规范定义API ExecutableValidator.validateParameters以验证放置在给定方法参数上的所有约束。JSR 349验证:静态方法上的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); 

我的问题是我怎么能验证一个静态方法调用? E.g,下面定义Foo.bar方法的调用:

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

为什么你想验证一个静态方法开始?按照静态的定义,你不再验证bean。 – Tunaki

+0

我没有验证bean'Foo'。不过,我需要在提到的示例代码中验证参数'str' –

回答

1

Bean验证1.1不支持静态方法。从Requirements on classes to be validated

托管限制和期待的对象由Bean验证提供者必须满足以下要求进行验证:

  • [...]
  • 静态字段和静态方法被排除在验证。

有些实施者可能支持它在将来用作增强(例如HV-606为Hibernate验证),但规范本身不。它在2.0 draft of the specification中仍然不被支持。