前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Eventbus3代码分析(二):注解入门

Eventbus3代码分析(二):注解入门

作者头像
dodo_lihao
发布2018-09-12 10:50:43
3060
发布2018-09-12 10:50:43
举报
文章被收录于专栏:懒人开发懒人开发

注解

我们扯淡到java的知识,最好是先参考别人的文章,或者看官方的 自己能力有限,所以只是简单扯扯,有兴趣,最好自己看官方的解释

我们先看一下java官方的api http://docs.oracle.com/javase/8/docs/api/index.html

对应的注解,在lang包下


具体注解 interface Annotation
  • 我们可以知道是since1.5, 也就是java 5.0 才有的 新特性
    • (其实,现在都java8了, 很多东西我们都很少用, java里面真的有很多好东西,只是不经常用,谁能说自己用了java中 5%的类?)
  • 对应的方法,其他3个都很简单,和Object中的差不多, equals, hashCode,toString,这里就不多扯淡了(建议看 马士兵 的java基础,自己原来也是这样过来的,虽然很啰嗦,但是讲得很细)
  • 重要的是 annotationType()方法,返回 继承Annotation 的泛型类 的Class(其实都是接口,但是都是用的反射中Class的类型)

Annotation 相关类

因为重点是Eventbus,所以简单了解下使用即可 (有时间看自己的能力,再能不能写注解和反射的分析)

这里面,先看几个接口:(都是带@的, 比较好玩)

  • @interface Target:
    • 所有Annotation 子类上面,都有 @Target(ElementType.ANNOTATION_TYPE)
    • 枚举类ElementType,也在包中
代码语言:javascript
复制
public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}
  • 这里类型比较多,上面也有注释,只是说明下经常用到的
    • ElementType.TYPE :用于描述类、接口或enum声明
    • ElementType.FIELD :用于描述实例变量
    • ElementType.METHOD :用于描述方法(eventbus的@interface Subscribe 就是用的这个)
  • @interface Retention :
    • 所有Annotation 子类上面,都有 @Retention(RetentionPolicy.RUNTIME)
    • 枚举类RetentionPolicy,也在包中
代码语言:javascript
复制
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
  • 3中类型也很好猜
  • 例如,我们的@Override
  • 这里@Override 是放在方法上,用于源码阶段
  • @interface Documented
    • 将注解信息添加在java文档
  • @interface Inherited
    • 完全没使用过,略

前面4个是java5开始的,后面2个是java8才有的, 自己也没有怎么试用过(这2个不扯淡了)

  • @interface Repeatable : 转化数组用
  • @interface Native
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.10.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 注解
    • 具体注解 interface Annotation
      • Annotation 相关类
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档