1 Reply Latest reply: Jul 1, 2009 1:06 PM by 800387 RSS

    Validation with custom annotation

    843793
      Hello

      here is my code:
      @Target(ElementType.METHOD)
      @Retention(RetentionPolicy.RUNTIME)
      public @interface SomeAnnotation {
        public int number() default -1;
      }
      and here is method for what i need this annotation :
      @SomeAnnotation
        public int calcSomething(int number) {
                if(number<0)
                     throw new IllegalArgumentException(Constants.NEGATIVE_NUMBER_ERROR);
           
                return number;
           }
      can anyone tell me how to do so that the validation would be with this annotation and i wont have to throw exception and/or chek if number is negative or not how to use custom annotation for this ? Is this possible? If yes please show me how to do it
        • 1. Re: Validation with custom annotation
          800387
          I'm not clear what you are trying to accomplish here. If you want to validate the data coming in, you can use an existing validator framework (such as Spring Modules or Struts Validator) which already has annotations that you can use. If you instead want to check preconditions before a method is fired, you can consider AOP or design by contract API's.

          - Saish