• Java基础 --- 注解


    Java基础 --- 注解 Annotation

    Java注解

    • Java注解它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。

    Java自带的标准注解

    • @Override: 限定重写父类方法, 该注解只能用于方法
    • @Deprecated: 用于表示某个程序元素(类, 方法等)已过时
    • @SuppressWaring: 作用是屏蔽一些无关紧要的警告。使开发者能看到一些他们真正关心的警告。从而提高开发者的效率
    • @SuppressWarnings(“unchecked”)
      告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。
    • @SuppressWarnings(“serial”)
      如果编译器出现这样的警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long,使用这个注释将警告信息去掉。
    • @SuppressWarnings(“deprecation”)
      如果使用了使用@Deprecated注释的方法,编译器将出现警告信息。使用这个注释将警告信息去掉

    自定义注解

    元注解: 自定义注解时需要元注解

    • @Retention
      用来定义该注解在哪一个阶段可用,包括 在源代码中(RetentionPolicy.SOURCE)、类文件中(RetentionPolicy.CLASS)或者运行时(RetentionPolicy.RUNTIME)
    • @Documented
      生成文档信息的时候保留注解,对类作辅助说明
    • @Target:
      注解可以修饰什么
      在这里插入图片描述
    • @ Inherited
      说明子类可以继承父类中的该注解. 如果某个类使用了被@Inherited 修饰的Annotation, 则其子类将自动具有该注解

    构建自定义注解

    • 定义一个注解
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface IDAuthenticator {
        //属性类型, 属性名称, 默认值
        int length() default 8;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 构建注解解析
    public void check(Student student) throws IllegalAccessException {
    	//使用Java反射得到属性
        for(Field field:student.getClass().getDeclaredFields()){
        	//如果属性被IDAuthenticator修饰
            if(field.isAnnotationPresent(IDAuthenticator.class)){
            	//拿到IDAuthenticator 对象
                IDAuthenticator idAuthenticator = field.getAnnotation(IDAuthenticator.class);
                field.setAccessible(true);
                //拿到student类中对应的属性, 也就是id属性
                Object value=field.get(student);
                //判断属性长度
                if(value instanceof String){
                    String id=(String) value;
                    //idAuthenticator.length()就是注解中的属性参数
                    if(id.length()!=idAuthenticator.length()){
                        throw  new  IllegalArgumentException("the length of "+field.getName()+" should be "+idAuthenticator.length());
                    }
    
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 应用注解
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Student {
        private  String name;
        @IDAuthenticator(length = 4)
        private String id;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 测试:
    @Test
    public void useAnnotation(){
        Student student01 = new Student("小明", "20210122");
        Student student02 = new Student("小军", "2021");
        Student student03 = new Student("小花", "20210121");
        
        for(Student student:new Student[]{student01,student02,student03}){
            try{
                check(student);
                System.out.println(" Student "+student+" checks ok ");
            } catch (IllegalArgumentException | IllegalAccessException e) {
                System.out.println(" Student "+student+" checks failed "+e);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    kubernetes集群编排——k8s存储(volumes,持久卷,statefulset控制器)
    第四季度净利润扭亏为盈,迪士尼的流媒体终于成功了?
    三面拼多多都没过,惨败在(java中间件、数据库与spring框架)后悔没有早点知道这些
    Raspberry Pi 3 Model B+ (树莓派3B+)快速上手
    Android开发中集合之Collection和Collections集合
    在不能升级版本的情况下,解决k8s证书到期且续约只有1年的问题
    微信小程序开发实战-setData字段特别多时怎么办,试试动态遍历字段并提取赋值
    JUC并发编程——阻塞队列(基于狂神说的学习笔记)
    mysql-通过binlog日志复制主从同步
    实现实时美颜:主播直播美颜SDK的技术细节
  • 原文地址:https://blog.csdn.net/weixin_38803409/article/details/126863709