Home | History | Annotate | Download | only in annotation
      1 package javax.annotation;
      2 
      3 import java.lang.annotation.Documented;
      4 import java.lang.annotation.Retention;
      5 import java.lang.annotation.RetentionPolicy;
      6 import java.util.regex.Pattern;
      7 import java.util.regex.PatternSyntaxException;
      8 
      9 import javax.annotation.meta.TypeQualifierNickname;
     10 import javax.annotation.meta.TypeQualifierValidator;
     11 import javax.annotation.meta.When;
     12 
     13 /**
     14  * This qualifier is used to denote String values that should be a Regular
     15  * expression.
     16  *
     17  */
     18 @Documented
     19 @Syntax("RegEx")
     20 @TypeQualifierNickname
     21 @Retention(RetentionPolicy.RUNTIME)
     22 public @interface RegEx {
     23     When when() default When.ALWAYS;
     24 
     25     static class Checker implements TypeQualifierValidator<RegEx> {
     26 
     27         public When forConstantValue(RegEx annotation, Object value) {
     28             if (!(value instanceof String))
     29                 return When.NEVER;
     30 
     31             try {
     32                 Pattern.compile((String) value);
     33             } catch (PatternSyntaxException e) {
     34                 return When.NEVER;
     35             }
     36             return When.ALWAYS;
     37 
     38         }
     39 
     40     }
     41 
     42 }
     43