-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Invoking JSR 303 validations without a bean
PostPosted: Sun Jun 20, 2010 11:51 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.