1package javax.annotation;
2
3import java.lang.annotation.Documented;
4import java.lang.annotation.Retention;
5import java.lang.annotation.RetentionPolicy;
6import java.util.regex.Pattern;
7import java.util.regex.PatternSyntaxException;
8
9import javax.annotation.meta.TypeQualifierNickname;
10import javax.annotation.meta.TypeQualifierValidator;
11import 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)
22public @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