Java基础 --- 注解 Annotation

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;
}
  • 构建注解解析
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());
                }

            }
        }
    }
  • 应用注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private  String name;
    @IDAuthenticator(length = 4)
    private String id;

}
  • 测试:
@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);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值