La spécification BeanValidation 1.1 définit l'API ExecutableValidator.validateParameters
pour valider toutes les contraintes placées sur les paramètres de la méthode donnée.Validation JSR 349: ExecutableValidator.validateParameters sur la méthode statique
Cependant, l'API ont besoin pour se passer d'une instance d'objet sur lequel la méthode de validation est invoquée:
/**
* 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);
Ma question est comment puis-je valider un appel de méthode statique? Par exemple, l'invocation de Foo.bar
méthode définie ci-dessous:
public class Foo {
public static void bar(@NotNull String str) {...}
}
Pourquoi voulez-vous valider une méthode statique pour commencer? Par définition de static, vous ne validez plus le bean. – Tunaki
Je ne valide pas le haricot 'Foo'. Cependant, j'ai besoin de valider le paramètre 'str' dans l'exemple de code mentionné –