[关闭]
@xujun94 2016-07-24T15:09:21.000000Z 字数 5902 阅读 1215

注解使用入门(一)


本篇博客要讲解主要分为以下几个问题

  1. 注解的相关知识点
  2. 基于运行时的注解的例子解析说明

注解的相关知识点

提到注解,大多数人应该都不默认,在我们程序中见到的@Override,@Deprected,@SupressWarnings等等,这些都是注解,只不过是系统自己封装好的,而我们平时比较少去深入理解是怎样实现的?

1)什么是注解(Annotation):

Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法。Annotion(注解)是一个接口,程序可以通过反射来获取指定程序元素的Annotion对象,然后通过Annotion对象来获取注解里面的元数据。

注解的分类:

根据注解参数的个数,我们可以将注解分为三类:
1. 标记注解:一个没有成员定义的Annotation类型被称为标记注解。这种Annotation类型仅使用自身的存在与否来为我们提供信息。比如后面的系统注解@Override;
2. 单值注解
3. 完整注解 

元注解:

元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它annotation类型作说明。Java5.0定义的元注解:
1. @Target,
2. @Retention,
3. @Documented,
4. @Inherited

4)元注解解析说明

根据注解使用方法和用途,我们可以将Annotation分为三类:
1.JDK内置系统注解
2.元注解
3.自定义注解

2)什么是metadata(元数据):

元数据从metadata一词译来,就是“关于数据的数据”的意思。
 
 元数据的功能作用有很多,比如:你可能用过Javadoc的注释自动生成文档。这就是元数据功能的一种。总的来说,元数据可以用来创建文档,跟踪代码的依赖性,执行编译时格式检查,代替已有的配置文件。如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致可分为三类:
1. 编写文档:通过代码里标识的元数据生成文档
2. 代码分析:通过代码里标识的元数据对代码进行分析
3. 编译检查:通过代码里标识的元数据让编译器能实现基本的编译检查

其他知识点暂时不介绍,个人觉得一下子介绍太多概念很难消化。下面让我们一起结合例子来使用它。


下面我们来看一下我们要怎样写一个基于编译时的自定义注解的例子

自定义注解大概可分为以下三个步骤:
1. 自定义一个注解
2. 在其他类使用我们的注解
3. 在运行的时候解析我们的注解

解析运行流程图

1)首先我们我们来看一下我们是怎样自定义一个注解的

这些类型和它们所支持的类在java.lang.annotation包中可以找到。

  1. /*
  2. * 定义注解 MethodInfo
  3. * 为方便测试:注解目标为类 方法,属性及构造方法
  4. * 注解中含有三个元素 id ,name和 gid;
  5. * id 元素 有默认值 0
  6. */
  7. @Documented
  8. @Target({ElementType.TYPE,ElementType.METHOD,
  9. ElementType.FIELD,ElementType.CONSTRUCTOR})
  10. @Retention(RetentionPolicy.RUNTIME)
  11. @Inherited
  12. public @interface MethodInfo {
  13. String name() default "xujunTest";
  14. int id() default 0;
  15. Class<Long> gid();
  16. }

解析说明

  1. String name() default "xujunTest";

2)接着我们来看一下我们要怎样使用我们自定义的注解

  1. /**
  2. * 这个类专门用来测试注解使用
  3. * @author xujun
  4. */
  5. //类成员注解
  6. @TestA(name="type",gid=Long.class)
  7. public class UserAnnotation {
  8. //类成员注解
  9. @TestA(name="param",id=1,gid=Long.class)
  10. private Integer age;
  11. //构造方法注解
  12. @TestA (name="construct",id=2,gid=Long.class)
  13. public UserAnnotation(){
  14. }
  15. //类方法注解
  16. @TestA(name="public method",id=3,gid=Long.class)
  17. public void a(){
  18. Map<String,String> m = new HashMap<String,String>(0);
  19. }
  20. //类方法注解
  21. @TestA(name="protected method",id=4,gid=Long.class)
  22. protected void b(){
  23. Map<String,String> m = new HashMap<String,String>(0);
  24. }
  25. //类方法注解
  26. @TestA(name="private method",id=5,gid=Long.class)
  27. private void c(){
  28. Map<String,String> m = new HashMap<String,String>(0);
  29. }
  30. public void b(Integer a){
  31. }
  32. }

最后我们一起来看一下我们怎样在运行的时候解析我们的Annotation注解

运行时 Annotation 解析

(1) 运行时 Annotation 指 @Retention 为 RUNTIME 的 Annotation,可手动调用下面常用 API 解析

  1. method.getAnnotation(AnnotationName.class);
  2. method.getAnnotations();
  3. method.isAnnotationPresent(AnnotationName.class);

其他 @Target 如 Field,Class 方法类似

  1. /*
  2. * 根据注解类型返回方法的指定类型注解
  3. */
  4. MethodInfo annotation = (MethodInfo) constructor
  5. .getAnnotation(MethodInfo.class);
  1. Annotation[] annotations = clazz.getAnnotations();
  2. for (Annotation annotation : annotations) {
  3. MethodInfo methodInfo = (MethodInfo) annotation
  4. }
  1. /*
  2. * 判断构造方法中是否有指定注解类型的注解
  3. */
  4. boolean hasAnnotation = constructor
  5. .isAnnotationPresent(MethodInfo.class);
  6. if (hasAnnotation) {
  7. /*
  8. * 根据注解类型返回方法的指定类型注解
  9. */
  10. MethodInfo annotation = (MethodInfo) constructor
  11. .getAnnotation(MethodInfo.class);
  12. }

测试代码

  1. public class ParseAnnotation {
  2. static String className="com.xujun.animation.test.UserAnnotation";
  3. /**
  4. * 简单打印出UserAnnotation 类中所使用到的类注解 该方法只打印了 Type 类型的注解
  5. *
  6. * @throws ClassNotFoundException
  7. */
  8. public static void parseTypeAnnotation() throws ClassNotFoundException {
  9. Class clazz = Class.forName(className);
  10. Annotation[] annotations = clazz.getAnnotations();
  11. for (Annotation annotation : annotations) {
  12. MethodInfo testA = (MethodInfo) annotation;
  13. System.out.println("id= \"" + testA.id() + "\"; name= \""
  14. + testA.name() + "\"; gid = " + testA.gid());
  15. }
  16. }
  17. /**
  18. * 简单打印出UserAnnotation 类中所使用到的方法注解 该方法只打印了 Method 类型的注解
  19. *
  20. * @throws ClassNotFoundException
  21. */
  22. public static void parseMethodAnnotation() {
  23. Method[] methods = UserAnnotation.class.getDeclaredMethods();
  24. for (Method method : methods) {
  25. /*
  26. * 判断方法中是否有指定注解类型的注解
  27. */
  28. boolean hasAnnotation = method.isAnnotationPresent(MethodInfo.class);
  29. if (hasAnnotation) {
  30. /*
  31. * 根据注解类型返回方法的指定类型注解
  32. */
  33. MethodInfo annotation = method.getAnnotation(MethodInfo.class);
  34. System.out.println("method = " + method.getName() + " ; id = "
  35. + annotation.id() + " ; description = "
  36. + annotation.name() + "; gid= " + annotation.gid());
  37. }
  38. }
  39. }
  40. /**
  41. * 简单打印出UserAnnotation 类中所使用到的方法注解 该方法只打印了 Method 类型的注解
  42. *
  43. * @throws ClassNotFoundException
  44. */
  45. public static void parseConstructAnnotation() {
  46. Constructor[] constructors = UserAnnotation.class.getConstructors();
  47. for (Constructor constructor : constructors) {
  48. /*
  49. * 判断构造方法中是否有指定注解类型的注解
  50. */
  51. boolean hasAnnotation = constructor
  52. .isAnnotationPresent(MethodInfo.class);
  53. if (hasAnnotation) {
  54. /*
  55. * 根据注解类型返回方法的指定类型注解
  56. */
  57. MethodInfo annotation = (MethodInfo) constructor
  58. .getAnnotation(MethodInfo.class);
  59. System.out.println("constructor = " + constructor.getName()
  60. + " ; id = " + annotation.id() + " ; description = "
  61. + annotation.name() + "; gid= " + annotation.gid());
  62. }
  63. }
  64. }
  65. public static void main(String[] args) throws ClassNotFoundException {
  66. parseTypeAnnotation();
  67. parseMethodAnnotation();
  68. parseConstructAnnotation();
  69. }
  70. }

运行以上测试程序,将可以看到以下输出结果

id= "0"; name= "type"; gid = class java.lang.Long
method = c ; id = 5 ; description = private method; gid= class java.lang.Long
method = b ; id = 4 ; description = protected method; gid= class java.lang.Long
method = a ; id = 3 ; description = public method; gid= class java.lang.Long
constructor = com.xujun.animationdemo.UserAnnotation ; id = 2 ; description = construct; gid= class java.lang.Long


转载请注明原博客地址: http://blog.csdn.net/gdutxiaoxu/article/details/52017033#t3

源码下载地址: http://download.csdn.net/detail/gdutxiaoxu/9582827

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注