Java编程思想笔记20.注解

一年又一年,字节跳动 Lark(飞书) 研发团队又双叒叕开始招新生啦!
【内推码】:GTPUVBA
【内推链接】:https://job.toutiao.com/s/JRupWVj
【招生对象】:20年9月后~21年8月前 毕业的同学
【报名时间】:6.16-7.16(提前批简历投递只有一个月抓住机会哦!)
【画重点】:提前批和正式秋招不矛盾!面试成功,提前锁定Offer;若有失利,额外获得一次面试机会,正式秋招开启后还可再次投递。

点击进入我的博客

注解为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便地使用这些数据。

1 注解语法

1.1 Java内置注解

  1. @Override:表示当前的方法定义将覆盖超类中的方法
  2. @Deprecated:表示方法或类已经不再建议使用
  3. @SuppressWarnings:关闭不当的编译器警告信息
@SuppressWarnings的关键词
关键词 用途
all to suppress all warnings
boxing to suppress warnings relative to boxing/unboxing operations
cast to suppress warnings relative to cast operations
dep-ann to suppress warnings relative to deprecated annotation
deprecation to suppress warnings relative to deprecation
fallthrough to suppress warnings relative to missing breaks in switch statements
finally to suppress warnings relative to finally block that don’t return
hiding to suppress warnings relative to locals that hide variable
incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
nls to suppress warnings relative to non-nls string literals
null to suppress warnings relative to null analysis
rawtypes to suppress warnings relative to un-specific types when using generics on class params
restriction to suppress warnings relative to usage of discouraged or forbidden references
serial to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access o suppress warnings relative to incorrect static access
synthetic-access to suppress warnings relative to unoptimized access from inner classes
unchecked to suppress warnings relative to unchecked operations
unqualified-field-access to suppress warnings relative to field access unqualified
unused to suppress warnings relative to unused code

1.2 定义注解

注解的定义和接口类似,只是要加一个@来表示这是一个注解。

@interface MyAnno {
    String value() default "";
}

// 注解嵌套
@interface Anno {
    MyAnno name() default @MyAnno;
}
注解中的元素

注解中的元素只能有以下几种类型:

  1. String
  2. 所有基本类型(不能用基本类型的包装类)
  3. Class
  4. enum
  5. Annotation
  6. 以上所有类型的数组
默认值的限制
  1. 元素不能有不确定的值,要么有默认值,要么使用注解时提供元素的值
  2. 对于非基本类型的元素,不能使用null为其值
注解不能继承

不能使用extends来继承某个@interface。

元注解

元注解负责注解其他的注解,共有四个:

  1. @Target:表示该注解可以用于什么地方,可能的ElementType参数包括:CONSTRUCTOR(构造器的声明)、FIELD(域声明、enum实例)、LOCAL_VARIABLE(局部变量声明)、METHOD(方法声明)、PACKAGE(包声明)、PARAMETER(参数声明)、TYPE(类、接口、注解类型、enum声明)
  2. @Retention:表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:SOURCE(注解将被编译器丢弃)、CLASS(注解在class文件中可用,但会被JVM丢弃)、
    RUNTIME(JVM将在运行也保留注解,因此可以通过反射机制读取注解的信息)
  3. @Document:将此注解包含在Javadoc中
  4. @Inherited:允许子类继承父类中的注解
  5. @Repeatable:可重复注解的注解,允许在同一申明类型(类,属性,或方法)的多次使用同一个注解。在这个注解出现前,一个位置要想注两个相同的注解,是不可能的,编译会出错误。
  6. @Native:仅仅用来标记native的属性,只对属性有效,且只在代码中使用,一般用于给IDE工具做提示用。

2 注解处理器

单纯定义一个注解是没有用的,需要配合注解处理器来处理标注了注解信息。

2.1 从类中提取注解

获取注解的相关操作都在java.lang.reflect包下,即通过反射来获取注解信息。

@MyAnno(value = "Lucas")
public class Main {
    public static void main(String[] args) {
        MyAnno myAnno = Main.class.getAnnotation(MyAnno.class);
        System.out.println(myAnno.value());
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
    String value() default "";
}

2.2 AnnotatedElement接口

该接口提供了获取注解信息的多个方法,Field、Constructor、Method都是其子类。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 6,172评论 0 3
  • 整体Retrofit内容如下: 1、Retrofit解析1之前哨站——理解RESTful 2、Retrofit解析...
    隔壁老李头阅读 12,110评论 4 31
  • 倚立厨房门侧,氤氲漫绕鼻旁。 远嗅锅中鱼煮,垂涎三尺难当。 更喜米新初浸,入鼻余韵悠长。 (钦定词谱何凝体)
    潘蔚阅读 3,486评论 6 16
  • 今天听了笑来老师三个月前的一次关于英语的分享。说说几个收获。 啃一本英语原版书 阅读英文书,其实并不是新鲜事了。我...
    saiyonana阅读 2,563评论 0 1
  • 最短路问题是什么 最短路问题是指:从某顶点出发,沿图的边到达另一顶点所经过的路径中,各边上权值 之和最小的路径。 ...
    周九九丶阅读 1,859评论 0 0