您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

Java元注解Retention怎么声明

2024/4/16 13:47:55发布4次查看
1.注解声明:通过@interface就可以声明一个注解。
@target(elementtype.field)@retention(retentionpolicy.runtime)public @interface bindview { int value();}
@target(elementtype.method)@retention(retentionpolicy.runtime)public @interface get { string value() default "";}
@target(elementtype.parameter)@retention(retentionpolicy.runtime)public @interface queue { string value() ;}
2. @target 元注解,注解的注解,它的取值定义在elementtype枚举类中。
@target注解 用来定义我们自定义注解代码的什么位置。
@documented@retention(retentionpolicy.runtime)@target({elementtype.annotation_type})public @interface target { elementtype[] value();}
1)elementtype.field 使用在成员变量上。
2)elementtype.method 使用在成员方法上。
3)elementtype.parameter 使用在方法参数上。
4)elementtype.type 使用在类、接口上。
5)elementtype.annotation_type 使用在注解上。
3.@retention 元注解,取值定义在retentionpolicy枚举类中。
用来定义注解生效的阶段:
1)source:注解只在源码阶段有效,不会编译到字节码中。
2)class:注解在源码、字节码阶段有效,运行阶段不存在。
3)runtime:注解在源码、字节码、运行阶段有效,也是最长用的。
@documented@retention(retentionpolicy.runtime)@target({elementtype.annotation_type})public @interface retention { retentionpolicy value();}
public enum retentionpolicy { source, class, runtime; private retentionpolicy() { }}
2.注解的使用
@bindview(r.id.start_activity) textview starttextview;
@get("http://www.baidu.com") call getperson(@queue("name") string name,@queue("200")int price); @get("http://www.baidu.com") call getperson();
注解的使用很简单。
注解单独存在没有任何意义,必须配合其他技术。
应用:
1)注解+apt注解处理器,生产java代码 ,databinding、butterknife、dagger2 hilt
2)注解+代码埋点
3)注解+反射+动态代理 retrofit xutils lifecycle
以上就是java元注解retention怎么声明的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录