Java自定义注解(Annotation)的使用

从 jdk5开始,Java增加了对元数据的支持,也就是Annotation,Annotation其实就是对代码的一种特殊标记,这些标记可以在编译,类加载和运行时被读取,并执行相应的处理。当然刚刚说了,Annotation只是一种标记,所以要是在代码里面不用这些标记也是能完成相应的工作的,只是有时候用注解能简化很多代码,看起来非常的简洁。
常见的注解(Annotation)
● @Override——限定重写父类方法
● @Deprecated——标示已过时
● @SuppressWarning——抑制编译器警告
JAVA的元注解
除了上面的注解,还有元注解。元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种。
1.@Retention 这个是决定你Annotation存活的时间的,它包含一个RetationPolicy的value成员变量,用于指定它所修饰的Annotation保留时间,一般有:

 1. Retationpolicy.CLASS:编译器将把注解记录在Class文件中,
    不过当java程序执行的时候,JVM将抛弃它。不过当java程序执行的时候,JVM将抛弃它。
    
 2. Retationpolicy.SOURCE : 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得。
 
 4. Retationpolicy.RUNTIME : 在Retationpolicy.CLASS的基础上,JVM执行的时候也不会抛弃它,
    所以我们一般在程序中可以通过反射来获得这个注解,然后进行处理。
    

首 先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。

2.@Target 这个注解一般用来指定被修饰的注解修饰哪些元素,如下:

  ○ ElementType.ANNOTATION_TYPE : //注解

  ○ ElementType.CONSTRUCTOR: //构造函数

  ○ ElementType.FIELD: //字段、枚举的常量

  ○ ElementType.LOCAL_VARIABLE: //局部变量

  ○ ElementType.METHOD: //方法

  ○ ElementType.PACKAGE: //包   

  ○ ElementType.PARAMETER://方法参数

  ○ ElementType.TYPE: //接口、类、枚举、注解
  1. @Document 这个注解修饰的Annotation类可以被javadoc工具提取成文档

  2. @Inherited 被他修饰的注解具有继承性,说明子类可以继承父类中的该注解

例子
自定义注解MyClassAnnotation

@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.TYPE)  
public @interface MyClassAnnotation {

	String value();
}

自定义注解MyFieldAnnotation

@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.FIELD)  
public @interface MyFieldAnnotation {

	public String name() default "fieldName";
}

自定义注解MyMethodAnnotation

@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.METHOD)  
public @interface MyMethodAnnotation {

	      String name();
	      int age();
}

在实例中使用TestRuntimeAnnotation 来测试注解情况:

@MyClassAnnotation(value = "test Class")
public class TestRuntimeAnnotation {

	    @MyFieldAnnotation
	    public String fieldInfo = "FiledInfo";  
	 
	 
	    @MyMethodAnnotation(age = 0, name = "zhangsan")
	    public static String getMethodInfo() {  
	        return TestRuntimeAnnotation.class.getSimpleName();  
	    }  
	    
	    public static void main(String[]args) {
	        StringBuffer sb = new StringBuffer();  
	        Class cls = TestRuntimeAnnotation.class;  
	        sb.append("Class注解:").append("\n");  
	        MyClassAnnotation myClassAnnotation = cls.getAnnotation(MyClassAnnotation.class);  
	        if (myClassAnnotation != null) {  
	            sb.append(Modifier.toString(cls.getModifiers())).append(" ")  
	                    .append(cls.getSimpleName()).append("\n");  
	            sb.append("注解值: ").append(myClassAnnotation.value()).append("\n\n");  
	        }  
	     
	        sb.append("Field注解:").append("\n");  
	        Field[] fields = cls.getDeclaredFields();  
	        for (Field field : fields) {  
	        	MyFieldAnnotation fieldInfo = field.getAnnotation(MyFieldAnnotation.class);  
	            if (fieldInfo != null) {  
	                sb.append(Modifier.toString(field.getModifiers())).append(" ")  
	                        .append(field.getType().getSimpleName()).append(" ")  
	                        .append(field.getName()).append("\n");  
	                sb.append("注解值: ").append(fieldInfo.name()).append("\n\n");  
	            }  
	        }  
	     
	        sb.append("Method注解:").append("\n");  
	        Method[] methods = cls.getDeclaredMethods();  
	        for (Method method : methods) {  
	        	MyMethodAnnotation methodInfo = method.getAnnotation(MyMethodAnnotation.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("age: ").append(methodInfo.age()).append("\n");  
	            }  
	        }  
	     
	        System.out.print(sb.toString());  
	    }
}

测试结果如下:

Class注解:
public TestRuntimeAnnotation
注解值: test Class

Field注解:
public String fieldInfo
注解值: fieldName

Method注解:
public static String getMethodInfo
注解值: 
name: zhangsan
age: 0

你可能感兴趣的:(IT)