Java元注解
元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种。
1.@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
首
先要明确生命周期长度 SOURCE < CLASS < RUNTIME
,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME
注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用
CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。
2.@Target:定义注解的作用目标
源码为:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
3.@Document:说明该注解将被包含在javadoc中
4.@Inherited:说明子类可以继承父类中的该注解
举例:
// 适用类、接口(包括注解类型)或枚举
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassInfo {
String value();
} // 适用field属性,也包括enum常量
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldInfo {
int[] value();
}
// 适用方法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodInfo {
String name() default "long";
String data();
int age() default 27;
}
这3个注解分别适用于不同的元素,并都带有不同的属性,在使用注解是需要设置这些属性值。
再定义一个测试类来使用这些注解:
@ClassInfo("Test Class")
public class TestRuntimeAnnotation {
@FieldInfo(value = {1, 2})
public String fieldInfo = "FiledInfo";
@FieldInfo(value = {10086})
public int i = 100;
@MethodInfo(name = "BlueBird", data = "Big")
public static String getMethodInfo() {
return TestRuntimeAnnotation.class.getSimpleName();
}
}
在代码中获取注解信息:
private void _testRuntimeAnnotation() {
StringBuffer sb = new StringBuffer();
Class<?> cls = TestRuntimeAnnotation.class;
Constructor<?>[] constructors = cls.getConstructors();
// 获取指定类型的注解
sb.append("Class注解:").append("\n");
ClassInfo classInfo = cls.getAnnotation(ClassInfo.class);
if (classInfo != null) {
sb.append(Modifier.toString(cls.getModifiers())).append(" ")
.append(cls.getSimpleName()).append("\n");
sb.append("注解值: ").append(classInfo.value()).append("\n\n");
}
sb.append("Field注解:").append("\n");
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
FieldInfo fieldInfo = field.getAnnotation(FieldInfo.class);
if (fieldInfo != null) {
sb.append(Modifier.toString(field.getModifiers())).append(" ")
.append(field.getType().getSimpleName()).append(" ")
.append(field.getName()).append("\n");
sb.append("注解值: ").append(Arrays.toString(fieldInfo.value())).append("\n\n");
}
}
sb.append("Method注解:").append("\n");
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
if (methodInfo != null) {
sb.append(Modifier.toString(method.getModifiers())).append(" ")
.append(method.getReturnType().getSimpleName()).append(" ")
.append(method.getName()).append("\n");
sb.append("注解值: ").append("\n");
sb.append("name: ").append(methodInfo.name()).append("\n");
sb.append("data: ").append(methodInfo.data()).append("\n");
sb.append("age: ").append(methodInfo.age()).append("\n");
}
}
System.out.print(sb.toString());
}
Java元注解的更多相关文章
- 使用Java元注解和反射实现简单MVC框架
Springmvc的核心是DispatcherServlet来进行各种请求的拦截,进而进行后续的各种转发处理.流程图如下: 说明:客户端发出一个http请求给web服务器,web服务器对http请求进 ...
- Spring注解与Java元注解小结
注解 Annotation 基于注解的开发,使得代码简洁,可读性高,简化的配置的同时也提高了开发的效率,尤其是SpringBoot的兴起,随着起步依赖和自动配置的完善,更是将基于注解的开发推到了新的高 ...
- Java元注解—— @Retention @Target @Document @Inherited
java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy. ...
- Java元注解@Retention规则
@Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试 1.适用方式: @Retention(RetentionPolicy.RUNTIME) @interf ...
- Java 元注解
元注解@Target,@Retention,@Documented,@Inherited * * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: * Elemen ...
- java元注解(注解在注解上的注解)
//ElementType.TYPE 给类.接口.枚举上使用 @Target(ElementType.TYPE)//给注解进行注解,表示该注解可以用在什么地方 //@Retention(Retenti ...
- java元注解 @Retention注解使用
@Retention定义了该Annotation被保留的时间长短: 1.某些Annotation仅出现在源代码中,而被编译器丢弃: 2.另一些却被编译在class文件中,注解保留在class文件中,在 ...
- java元注解 @Target注解用法
@Target: @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages.types(类.接口.枚举.Annotation类型).类型成员(方法.构造 ...
- Java元注解,简单案例
[注解] 程序中有 注释 和注解 * 注释:给开发人员. * 注解:给计算机看的. 注解使用:学习框架支持注解开发. [JDK提供的注解] @Override :描述方法的重写. @SuppressW ...
随机推荐
- 使用response实现文件下载注意点
创建web工程,使用response实现文件的下载. 在webRoot下创建download文件,里面包含要下载的文件,现在把源码贴上来,然后再说我遇到的问题 public class DownLoa ...
- JAVA基础--线程
sleep和wait的区别: 1. sleep是Thread的方法, wait是object的方法 2. sleep占着CPU睡觉, wait等待CPU,不占用CPU 线程是一个程序内部的顺序控制流 ...
- Xcode之Alcatraz
Alcatraz的安装和使用 转发:http://www.cnblogs.com/wendingding/p/4964661.html 一.简单说明 Alcatraz 是一款 Xcode的插件管理工具 ...
- clearsSelectionOnViewWillAppear
@property (nonatomic) BOOL clearsSelectionOnViewWillAppear NS_AVAILABLE_IOS(3_2); // defaults to YES ...
- Android学习笔记之Intent
Intent是Activity之间的管道 可以用来做Acitivity的跳转或传递数据 protected void onCreate(Bundle savedInstanceState) { sup ...
- Java谜题心得
1,二进制浮点数的运算是对实际算数的一种近似运算. 2,IEEE 754浮点算术保留了一个特殊的值用来表示一个不是数字的数量[IEEE 754].这个值就是NaN(“不是一个数字(Not a Numb ...
- 织梦网站底部的Power by DedeCms怎么去掉?
由于织梦DEDECMS程序6月份的漏洞,很多织梦网站都被黑了,所以大家都在抓紧时间更新系统补丁.但是这次的DEDECMS V5.7版本更新后,在前台网页底部会出现织梦版权信息 “powered by ...
- spring事务的传播性的理解
来自至顶网的文章 http://developer.zdnet.com.cn/2007/0521/402066.shtml
- avi格式详细介绍
百度:http://wenku.baidu.com/link?url=KB7qKc6UG4aeU-i9FtXeV8Uou4JHPceiyz3HNbzCrQw4phY-qRlcp3tTSwYPeIgdx ...
- 通过条件注释<!--[if IE]><!-->判断浏览器
有时我们会在网站头部看到: <!--[if IE 7]> <![endif]--> 或者 <!--[if lt IE 9]> <![endif]--> ...