1 Reply Latest reply: Apr 24, 2010 2:27 AM by 800389 RSS

    Constraint Annotation

    843793
      Hi,

      I have a NotNull annotation defined here which uses the javax.validation.Constraint.

        
      @Documented  
      @Constraint(validatedBy=NotNullValidator.class)    
      @Target( { METHOD, FIELD, PARAMETER })   
      @Retention(RUNTIME)   
      public @interface NotNull {   
        
          String message() default "{validator.notNull}";   
             
          Class<?>[] groups() default {};     
        
          Class<? extends Payload>[] payload() default {};        
             
          String getValidationErrorCode() default "test_tanuja";   
      }   
        
        
      Configuration<?> configuration = Validation.byDefaultProvider().configure();   
      ValidatorFactory factory = configuration.buildValidatorFactory();   
      Validator validator = factory.getValidator();   
        
        
      Set<ConstraintViolation<ValidatableObject>> constraintViolations = validator.validate(o);   
      for (ConstraintViolation<ValidatableObject> value : constraintViolations) {   
        
        Annotation[]  a = annotation.annotationType().getDeclaredAnnotations();   
        for (Annotation a1 : a){   
          if (a1.annotationType().isAnnotationPresent(javax.validation.Constraint.class)){   
          //do something   
          }   
          if (a1.annotationType().isAnnotationPresent(Target.class)){   
          //do something   
          }   
        
        }   
        Assert.assertEquals("is mandatory", value.getMessage());   
        
      }   
      a1.annotationType().isAnnotationPresent(javax.validation.Constraint.class) returns false though it is present.
      a1.annotationType().isAnnotationPresent(javax.validation.Constraint.class) returns true

      Is it possible to get hold of @Constraint and the value of validatedBy?

      Thanks in Advance,
      Tanuja