Annotation
2016-06-10 00:20
615 查看
概念
Annontation是Java5开始引入的新特征。中文名称一般叫注解。它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。更通俗的意思是为程序的元素(类、方法、成员变量)加上更直观更明了的说明,这些说明信息是与程序的业务逻辑无关,并且是供指定的工具或框架使用的。
Annontation像一种修饰符一样,应用于包、类型、构造方法、方法、成员变量、参数及本地变量的声明语句中。
原理
Annotation其实是一种接口。通过Java的反射机制相关的API来访问annotation信息。相关类(框架或工具中的类)根据这些信息来决定如何使用该程序元素或改变它们的行为。annotation是不会影响程序代码的执行,无论annotation怎么变化,代码都始终如一地执行。
Java语言解释器在工作时会忽略这些annotation,因此在JVM 中这些annotation是“不起作用”的,只能通过配套的工具才能对这些annontaion类型的信息进行访问和处理
基本Annotation
@Override限定重写父类方法。对于子类中被@Override 修饰的方法,如果存在对应的被重写的父类方法,则正确;如果不存在,则报错。@Override 只能作用于方法,不能作用于其他程序元素.
@Deprecated
用于表示某个程序元素(类、方法等)已过时。如果使用被@Deprecated修饰的类或方法等,编译器会发出警告.
@SuppressWarning
抑制编译器警告。指示被@SuppressWarning修饰的程序元素(以及该程序元素中的所有子元素,例如类以及该类中的方法…..)取消显示指定的编译器警告。例如,常见的@SuppressWarning(value=”unchecked”)
写几个演示
@Override
public class Fruit { public void info(){ System.out.println("Fruit's info"); } }
public class Apple extends Fruit{ @Override//必须重载父类的某个方法,否则报错 public void info() { System.out.println("Apple's info"); } }
@Deprecated
public class Fruit { @Deprecated public void info(){ System.out.println("Fruit's info"); } }
public class Test { public static void main(String []args){ new Fruit().info();//会被编译器警告 } }
@SuppressWarning
public class Test { @SuppressWarnings(value = "unchecked") public static void main(String []args){ List<String> myList=new ArrayList();//没有使用泛型限制的集合 } }
自定义Annotation
使用关键字@interfacepublic @interface MyAnno { }
定义了该Annotation之后,就可以在程序任何地方使用该Annotation,可以用来修饰包、类型、构造方法、方法、成员变量、参数及本地变量的声明语句等
public class Fruit { @MyAnno public void info(){ System.out.println("Fruit's info"); } }
Annotation不仅可以是这种简单的Annotation,Annotation还可以携带成员变量,成员变量在Annotation定义中以无参数方法的形式来声明.其方法名和返回值定义了该成员的名字和类型.
public @interface MyAnno { String name(); int age(); }
一旦定义了成员变量之后,使用该Annotation时应该为该Annotation的成员变量指定值.
public class Fruit { @MyAnno(name = "Eric", age = 20) public void info() { System.out.println("Fruit's info"); } }
我们还可以在定义Annotation时使用default指定默认值,指定了默认值的成员变量可以不用为其赋值;也可指定值,默认值会被指定值替代.
public @interface MyAnno { String name() default "Eric"; int age(); }
public class Fruit { @MyAnno(age = 20)//使用默认值 public void info() { System.out.println("Fruit's info"); } @MyAnno(name = "WarEric", age = 20)//使用指定值 public void Color() { System.out.println("I am colorful"); } }
我们可以把Annotation分为如下两类
标记Annotation:不包含成员变量,仅用自身的存在与否来提供信息,例如@Override.
元数据Annotation:包含成员变量的Annotation,因为它们可以接受更多元数据
提取Annotation的信息
这里使用java中的反射来提取Annotation的上的信息,反射的知识不在这里讲,请自己补充.AnnotatedElement接口是所有程序元素(如Class,Method,Constructor)的父接口,所以程序通过反射获取了某个类的AnnotatedElement对象之后,程序就可以调用如下三个方法来访问Annotation信息:
1.getAnnotation(Class<A> annotationClass): 返回该程序元素上存在的,指定类型的注释,如果该类型不存在,则返回NULL; 2.Annotation[] getAnnotations(): 返回该程序元素上的所有注解 3.boolean isAnnotationPresent(Class<? extends Annotation> annotationClass): 判断该程序元素上是否包含指定类型的注释,存在返回true,否则返回false.
下面写个演示,只简单演示一下效果,这里用了很多反射知识.
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD,ElementType.METHOD}) public @interface MyAnno { String name() default ""; int age(); }
public class Cat { @MyA b792 nno(name = "Eric", age =20 ) public void sayName(){ System.out.println("I am a cat"); } }
public class Test { public static void main(String[] args) { try { Annotation[] anno = Class.forName("com.company.eric.Annotation.Cat").getMethod("sayName").getAnnotations(); for (Annotation a : anno) { if(a instanceof MyAnno) { MyAnno my = (MyAnno) a; System.out.println("age:"+my.age()); System.out.println("name:"+my.name()); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
运行结果
age:20 name:Eric
这里要注意使用反射来获得注解所携带的信息方式比较多,我这里只是展示提取信息的效果,更多的方式请参考反射的知识,查阅api.
JDK的元Annotation
java在在java.lang.annotation下提供了四个Meta Annonation(元Annotation),用来修饰其他的Annotation.@Retention
@Retention用来指定一个Annotation可以保留多长时间,@Retention包含一个RetentionPolicy的成员变量.RetentionPolicy.SOURCE:这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面.
RetentionPolicy.CLASS:这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这些信息加载到JVM中。注:默认策略为CLASS类型.
RetentionPolicy.RUNTIME:表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno { String name() default "Eric"; int age(); }
@Target
@Target是用来指定Annotation类型可以用在哪些元素上的,里面包含一个ElementType.TYPE成员变量TYPE(类型)
FIELD(属性)
METHOD(方法)
PARAMETER(参数)
CONSTRUCTOR(构造函数)
LOCAL_VARIABLE(局部变量)
PACKAGE(包)
其中的TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上
@Target({ElementType.METHOD,ElementType.FIELD})
public @interface MyAnno { String name() default "Eric"; int age(); }
@Documented
@Documented目的就是将这一Annotation的信息显示在JAVA API文档上,如果没有增加@Documented的话,JAVA API文档上不会显示相关annotation信息.@Documented
public @interface MyAnno { String name() default "Eric"; int age(); }
@Inherited
@Inherited指定被它修饰的Annotation将有继承性:如果某个类A使用了某个Annotation(使用了@Inherited)修饰,则其子类将自动具有A注释.(用在方法的效果可能不一样,可以自己测试一下)@Inherited
public @interface MyAnno { String name() default "Eric"; int age(); }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序