Annotation( 注解 ) 是 JDK5.0 及以后版本引入的一个特性 。 注解是(@interface) Java 的一个新的类型(与接口很相似 ) ,它与类(Class)、接口(interface)、枚举(enum)是在同一个层次。我们可以定义注解、声明注解、获得注解,并且根据获得的注解做相应的处理,许多框架都大量应用了注解
@Override 表示当前方法是覆盖父类的方法。被Override注解的方法必须在父类中存在同样的方法程序才能编译通过
@Deprecated 表示当前元素是不赞成使用的。
@SuppressWarnings 表示关闭一些不当的编译器警告信息。
deprecation :过时的类或方法警告。例如:new Date().toLocal
unchecked :执行了未检查的转换时警告。例如 List list = new ArrayList
fallthrough :当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。
path :在类路径、源文件路径等中有不存在的路径时的警告。
serial :当在可序列化的类上缺少 serialVersionUID 定义时的警告。
finally :任何 finally 子句不能完成时的警告。
all :关于以上所有情况的警告
example
@SuppressWarnings (value={"unchecked"}) public void myMethod() { List wordList = new ArrayList(); wordList.add("foo"); }
注解的定义和接口差不多,只是在interface前面多了一个@
public @interface MyAnnotation { }
/*
* 元注解就是用来对注解类进行注解的注解
* 元注解@Target,@Retention,@Documented,@Inherited
*
* @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
* ElemenetType.CONSTRUCTOR 构造器声明
* ElemenetType.FIELD 域声明(包括 enum 实例)
* ElemenetType.LOCAL_VARIABLE 局部变量声明
* ElemenetType.METHOD 方法声明
* ElemenetType.PACKAGE 包声明
* ElemenetType.PARAMETER 参数声明
* ElemenetType.TYPE 说明该注解只能被声明在类,接口(包括注解类型)或enum声明之前
*
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
* RetentionPolicy.SOURCE 指定注解只保留在一个源文件当中,注解将被编译器丢弃
* RetentionPolicy.CLASS 指定注解只保留在一个 class 文件中,注解在class文件中可用,但会被VM丢弃
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
* @Documented 将此注解包含在 javadoc 中
*
* @Inherited 允许子类继承父类中的注解
*
*/
example
package annotation.Test; import java.lang.annotation.Documented; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.RetentionPolicy; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited /* * 定义注解 Test 注解中含有两个元素 id 和 description description 元素 有默认值 "no description" */ public @interface Test { public int id(); public String description() default "no description"; }
怎样使用注解
package annotation.Test; import java.lang.reflect.Method; public class Test_1 { /* * 被注解的三个方法 */ @Test(id = 1, description = "hello method_1") public void method_1() { } @Test(id = 2) public void method_2() { } @Test(id = 3, description = "last method") public void method_3() { } /* * 解析注解,将Test_1类 所有被注解方法 的信息打印出来 */ public static void main(String[] args) { Method[] methods = Test_1.class.getDeclaredMethods(); for (Method method : methods) { /* * 判断方法中是否有指定注解类型的注解 */ boolean hasAnnotation = method.isAnnotationPresent(Test.class); if (hasAnnotation) { /* * 根据注解类型返回方法的指定类型注解 */ Test annotation = method.getAnnotation(Test.class); System.out.println("Test( method = " + method.getName() + " , id = " + annotation.id() + " , description = " + annotation.description() + " )"); } } } }
output
Test( method = method_1 , id = 1 , description = hello method_1 )
Test( method = method_2 , id = 2 , description = no description )
Test( method = method_3 , id = 3 , description = last method )
另外一篇比较详细的博文
http://my.oschina.net/hongdengyan/blog/179732