首页 > 技术文章 > 初识注解

jzmanu 2016-06-11 09:44 原文

1.认识Annotation


  • 声明:
//java.lang.annotation,为注释提供库支持,JDK1.5开始
public interface Annotation

所有annotation类型一般都会实现该接口。

2.系统内置注解


  • @Override 表示当前的方法将覆盖超类中的方法,编译时进行格式检查。
  • @Deprecated 表示一个类或者是方法不再建议使用,将其标记为过时,但还是可以使用。
  • @SuppressWarnings 表示关闭不当的编译器警告信息。

3.自定义Annocation


(1)注解使用的步骤:
  • 编写注解
  • 在类上应用注解
  • 注解反射操作
(2)自定义注解和使用注解
  • 自定义注解:
package com.manu;
//自定义注解
public @interface MyAnnotation {}
  • 注解的使用:
package com.manu;
//类上使用注解
@MyAnnocation
public class TestAnnotation {
	//属性上使用注解
	@MyAnnocation
	private String name;
	//方法上使用注解
	@MyAnnocation
	public static void MyAnnotation(){
		//...
	}
	public static void main(String[] args) {
	    //...	
	}
}
(3)注解设值及使用:
  • 注解中定义变量:
package com.manu;
//自定义注解
public @interface MyAnnotation {
	//注解中定义变量,注意写法,如果有默认值,则可以不选择设值,也可以选择设值
	public String name() default "name" ;
	public Color Color();
}
//定义枚举类型
enum Color {
	RED,
	BLUE,
	GREEN;
}
  • 注解使用:
package com.manu;
//类上使用注解
@MyAnnocation(name = "class", Color = Color.RED)
public class TestAnnotation {
	//属性上使用注解
	@MyAnnocation(name = "properties", Color = Color.BLUE)
	private String name;
	//方法上使用注解
	@MyAnnocation(Color = Color.GREEN)
	public static void MyAnnotation(){
		//...
	}
	public static void main(String[] args) {
		
	}
}

4. Retention和RetentionPolicy


  • Retentio注释类型是java.lang.annotation中Annotation接口的某一个实现类,用来指示注释类型的注释要保留多久;RetentionPolicy是java.lang.annotation中的枚举类型,是注释保留策略。此枚举类型的常量描述保留注释的不同策略。它们与 Retention 元注释类型一起使用,以指定保留多长的注释。
(1)Retention
  • 指示注释类型的注释要保留多久,如果注释类型声明中不存在Retention注释,则保留策略默认为RetentionPolicy.CLASS
(2)RetentionPolicy
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     * 在编译阶段被编译器丢弃(编译期之后不再有意义,不会写入字节码)
     */
    SOURCE,
    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 此类型注解会记录在class文件中,JVM会忽略,也是默认注解方式(字节码文ji件处理中有用)     
     *
     */
    CLASS,
    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     * 此类注解会记录在class文件中,JVM能够读取,可通过反射机制获得注解信息
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

5.反射与Annotation


package com.manu;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

//类上使用注解
@MyAnnotation(name = "class", color = Color.RED)
public class TestAnnocation {
	//属性上使用注解
	@MyAnnotation(name = "properties", color = Color.BLUE)
	private String name;
	//方法上使用注解
	@MyAnnotation(color = Color.GREEN)
	public static void MyAnnocation(){
		//...
	}
	@MyAnnotation(color = Color.GREEN)
	public static void main(String[] args) throws Exception{
		//获得运行时类的Class对象
		Class<A> c = A.class;
		//根据Class对象创建一个新实例
		Method[] methods = c.getDeclaredMethods();
		for (Method method : methods) {
			Annotation[] anno = method.getDeclaredAnnotations();
			for (Annotation annotation : anno) {
				//获得源代码中底层类的简称
				String name = annotation.annotationType().getSimpleName();
				Method[] m = annotation.annotationType().getDeclaredMethods();
				for (Method met : m) {
					MyAnnotation my = (MyAnnotation)method.getAnnotation(MyAnnotation.class);
					System.out.println("方法名:"+method.getName()+"  name="+my.name()+"  注解名为:"+name+"  注解内定义的变量:"+met.getName());
				}
			}
		}
	}
}
@MyAnnotation(color = Color.BLUE)
class A{
	private String name;
	private Color color;
	public A() {
		super();
	}
	@MyAnnotation(color = Color.GREEN)
	public A(String name) {
		super();
		this.name = name;
	}
	@MyAnnotation(color = Color.BLUE)
	public String getName() {
		return name;
	}
	@MyAnnotation(color = Color.BLUE)
	public Color getColor() {
		return color;
	}
	@MyAnnotation(color = Color.BLUE)
	public void setColor(Color color) {
		this.color = color;
	}
	@MyAnnotation(color = Color.BLUE)
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "newA [name=" + name + "]";
	}
}

下面是运行结果:

方法名:getName  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:name
方法名:getName  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:color
方法名:setName  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:name
方法名:setName  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:color
方法名:setColor  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:name
方法名:setColor  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:color
方法名:getColor  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:name
方法名:getColor  name=VALUE  注解名为:MyAnnotation  注解内定义的变量:color

6. @Target注解


public enum ElementType {//JDK
    FIELD,//字段声明,包括枚举常量
    METHOD,//方法声明
    PARAMETER,//参数声明
    CONSTRUCTOR,//构造方法声明
    LOCAL_VARIABLE,//局部变量声明
    ANNOTATION_TYPE,//注释类型声明(类、接口、枚举、注解)
    PACKAGE,//包声明
    /**
     * @since 1.8
     */
    TYPE_PARAMETER,//参数类型声明
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

7.关于元注解


________________________________________________________________________________________________________

任尔几路来,我自一路去...

推荐阅读