自定义一个注解,实现对手机号格式进行检查

import org.hibernate.validator.constraints.CompositionType;
import org.hibernate.validator.constraints.ConstraintComposition;
import org.hibernate.validator.constraints.Length;

import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;
import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@ConstraintComposition(CompositionType.OR)
@Pattern(regexp = "1[3|4|5|6|7|8|9]\\d{9}")
@Null
@Length(min = 0,max = 0)
@Documented
@Constraint(validatedBy = {})
@Target({METHOD,FIELD,CONSTRUCTOR,PARAMETER})
@Retention(RUNTIME)
@ReportAsSingleViolation
public @interface Phone {
    String message() default "手机号格式错误";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

 

注解测试

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.FIELD,ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Hello {
    String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Sex {
    public enum GenderType {
        Male("男"),
        Female("女");
        private String genderStr;
        private GenderType(String agr0){
            this.genderStr = agr0;
        }
        @Override
        public String toString(){
            return genderStr;
        }
    }
    GenderType gender() default GenderType.Male;
}
import com.huixiaoer.ant.api.util.Hello;
import com.huixiaoer.ant.api.util.Sex;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotationTest {

    @Sex(gender = Sex.GenderType.Male)
    public static String sex;

    @Hello("world")
    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        Class cls = AnnotationTest.class;
        Field field = cls.getField("sex");
        Sex sex = field.getAnnotation(Sex.class);
        String sexVal = sex.gender().toString();
        System.out.println(sexVal);

        System.out.println(AnnotationTest.sex);

        Method method = cls.getMethod("main",String[].class);
        Hello hello = method.getAnnotation(Hello.class);
        String value = hello.value();
        //world
        System.out.println(value);
        System.out.println(hello);
        Annotation[] annotations = method.getDeclaredAnnotations();
        for(Annotation annotation:annotations){
            System.out.println(annotation.toString());
        }

    }
}

运行结果:

null
world
@com.huixiaoer.ant.api.util.Hello(value=world)
@com.huixiaoer.ant.api.util.Hello(value=world)

结束