Hi all,
I've writing a spring AOP wrapper that invokes JSR 303 validations for method arguments in our service tier. Currently, I have it working for beans. Whenever a developer adds an annotation we've created "@Check" to the argument, the validator is invoked before the function is called. This works great, but we're still having to manually invoke checks for primitive types. To work around this, I've added before advice to any annotation that comes from the packages javax.validation.contraints or org.hibernate.validator. My advice is getting executed, but I can't seem to explicity invoke the validator for a single property. I need something like the following.
Code:
public void validateJSRInternal(JoinPoint jp) throws Throwable {
logger
.debug(
"Invoking before advice for JSR-303 annotation. Target object is {} on method {}",
jp.getTarget(), jp.getSignature());
MethodSignature sig = (MethodSignature) jp.getSignature();
Object[] args = jp.getArgs();
Method signatureMethod = sig.getMethod();
// get the matching signature from the target
Method targetMethod = jp.getTarget().getClass().getMethod(
signatureMethod.getName(), signatureMethod.getParameterTypes());
Annotation[][] annotations = targetMethod.getParameterAnnotations();
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>();
for (int i = 0; i < annotations.length; i++) {
// no annotations on this arg, continue
if (annotations[i].length == 0) {
continue;
}
// if we get here we have annotations, process them to see if one of
// them is a valid input argument
for (int j = 0; j < annotations[i].length; j++) {
Class<? extends Annotation> annotation = annotations[i][j].annotationType();
//TODO see if we have validators for this annotation and validate the args
logger.warn("I should be validating....");
}
}
if (!violations.isEmpty()) {
throw new InputValidationException(violations);
}
}
I've tried creating a new instance of the ConstraintHelper to get all contraints for a given annotation, but it isn't working. Any ideas how to get around this?
Thanks,
Todd