public void validateArgs ( List < Object > args , Object instance , Method m , Command cmd ) { final Set < ConstraintViolation < Object > > set = validator . validateParameters ( instance , m , args . toArray ( ) ) ; final StringBuilder sb = new StringBuilder ( ) ; for ( ConstraintViolation < Object > violation : set ) { final Path path = violation . getPropertyPath ( ) ; final String msg = violation . getMessage ( ) ; String var = path . toString ( ) ; try { int pos = Integer . parseInt ( "" + var . charAt ( var . length ( ) - 1 ) ) ; Argument arg = cmd . getArguments ( ) . get ( pos ) ; sb . append ( arg . getName ( ) ) . append ( " " ) . append ( msg ) . append ( ". " ) ; } catch ( Exception e ) { sb . append ( var ) . append ( " " ) . append ( msg ) . append ( ". " ) ; } } if ( sb . length ( ) > 0 ) { // is ConstraintViolationException more appropriate, // letting user choose their error message? throw new ValidationException ( ARG_VIOLATION_MSG + ": " + sb . toString ( ) ) ; } }
This method will only be called if we know that JSR 303 1 . 1 Bean Validation API and compliant implementation are available on classpath .