Java之Annotation

首先可以使用下面四个方法来访问Annotation的信息

  • ** <A extends Annotation> A getAnnotation(Class<A> annotationType) **
    返回该程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null
  • ** Annotation[] getAnnotations() **
    返回该程序元素上存在的所有注解。
  • ** boolean isAnnotationPresent(Class<? extends Annotation> annotationType) **
    判断该程序元素上是否包含指定类型的注解,存在则返回true,否则返回false.
  • Annotation[] getDeclaredAnnotations()
    返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。(如果没有注释直接存在于此元素上,则返回长度为零的一个数组。)该方法的调用者可以随意修改返回的数组;这不会对其他调用者返回的数组产生任何影响。

Java5.0定义了4个标准的meta-annotation类型对 annotation类型作说明

** 1. @Target **: 说明了Annotation所修饰的对象范围取值(ElementType)有:
    1.CONSTRUCTOR:用于描述构造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部变量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述参数
    7.TYPE:用于描述类、接口(包括注解类型) 或enum声明

2. @Retention : 定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)取值(RetentionPoicy)有:
    1.SOURCE:在源文件中有效(即源文件保留)
    2.CLASS:在class文件中有效(即class保留)
    3.RUNTIME:在运行时有效(即运行时保留)

3. @Documented :用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。

4. @Inherited :是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation。当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。

注解可以支持的数据类型

1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
2.String类型
3.Class类型
4.enum类型
5.Annotation类型
6.以上所有类型的数组

Annotation类型里面的参数,方法设定

  • 只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型

  • 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和 String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String;

  • 如果只有一个参数成员,最好把参数名称设为"value",后加小括号.

  • 在每一个注解中每一个方法其实就是一个变量

如下是一个注解使用例子

//定义了一个学生专业的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentMajor {
    public enum Major {计算机, 软件, 经管}//专业的枚举
    Major studentMajor() default Major.经管;//学生的专业
}
//学生名字注解类
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentName {
    String value() default "";
}
//学生信息注解类
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface StudentData {
    public int age() default -1;

    public String address() default "";

    public int height() default -1;
}
//学生类
public class Student {
    @StudentName("luoweidong")
    private String studentName;

    @StudentMajor(studentMajor = StudentMajor.Major.计算机)
    private String studentMajor;

    @StudentData(age = 18, address = "江西", height = 170)
    private String studentData;
}
//注解处理类
public class StudentInfoUtils {
    public static void showData(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {

            if (field.isAnnotationPresent(StudentName.class)) {
                StudentName studentName = field.getAnnotation(StudentName.class);
                Log.d("lwd", studentName.value());
            }

            if (field.isAnnotationPresent(StudentMajor.class)) {
                StudentMajor studentMajor = field.getAnnotation(StudentMajor.class);
                Log.d("lwd", studentMajor.studentMajor() + "");
            }

            if (field.isAnnotationPresent(StudentData.class)) {
                StudentData studentData = field.getAnnotation(StudentData.class);
                Log.d("lwd", studentData.age() + studentData.address() + studentData.height());
            }
        }
    }
}
//测试代码
public class Test {
    public static void main(String[] args) {
        StudentInfoUtils.showData(Student.class);
    }
}
参考博客

深入理解Java:注解(Annotation)1
深入理解Java:注解(Annotation)2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容