自定义注解的使用

1,781 阅读3分钟

这是我参与8月更文挑战的第25天,活动详情查看:8月更文挑战

1、元注解

使用自定义注解前,先了解相关元注解 java.lang.annotation 提供了四种元注解,专门注解其他的注解(在自定义注解的时候,需要使用到元注解):

  • @Documented – 注解是否将包含在JavaDoc中
  • @Retention – 什么时候使用该注解
  • @Target – 注解用于什么地方
  • @Inherited – 是否允许子类继承该注解

(1). @Retention:定义该注解的生命周期

  • RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
  • RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
  • RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

(2). Target:表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的ElementType 参数包括

  • ElementType.CONSTRUCTOR: 用于描述构造器
  • ElementType.FIELD: 成员变量、对象、属性(包括enum实例)
  • ElementType.LOCAL_VARIABLE: 用于描述局部变量
  • ElementType.METHOD: 用于描述方法
  • ElementType.PACKAGE: 用于描述包
  • ElementType.PARAMETER: 用于描述参数
  • ElementType.TYPE: 用于描述类、接口(包括注解类型) 或enum声明

(3). @Documented:一个简单的Annotations 标记注解,表示是否将注解信息添加在java 文档中。

(4). @Inherited:定义该注释和子类的关系 @Inherited 元注解是一个标记注解,@Inherited 阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited 修饰的annotation 类型被用于一个class,则这个annotation 将被用于该class 的子类。

2、自定义注解类编写的一些规则

  1. Annotation 型定义为@interface, 所有的Annotation 会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口.
  2. 参数成员只能用public 或默认(default) 这两个访问权修饰
  3. 参数成员只能用基本类型byte、short、char、int、long、float、double、boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组.
  4. 要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation 对象,因为你除此之外没有别的获取注解对象的方法
  5. 注解也可以没有定义成员,,不过这样注解就没啥用了 PS:自定义注解需要使用到元注解

3、实例

AnimalName.java

package com.xiaojian.basics.annotation;
/**
 * 动物种类
 */
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalName {
    String value() default "";
}

AnimalColor.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 动物颜色
 */
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalColor {
    /**
     * 颜色枚举
     */
    public enum Color{黑色,白色,灰色,杂色};

    Color animalColor() default Color.白色;
}

AnimalMaster.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 动物主人
 */

@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalMaster {

    int id() default -1;

    String name() default "";

    String address() default "";
}

AnimalInfoUtil.java

package com.xiaojian.basics.annotation;

import java.lang.reflect.Field;

/**
 * 返回动物信息
 */
public class AnimalInfoUtil {

    public static void getInfo(Class<?> clazz){

        StringBuilder strAnimalName = new StringBuilder( "动物种类:");
        StringBuilder strAnimalColor = new StringBuilder( "动物颜色:");
        StringBuilder strAnimalProvider = new StringBuilder( "主人信息:");

        Field[] fields = clazz.getDeclaredFields();

        for(Field field : fields){
            if(field.isAnnotationPresent(AnimalName.class)){
                AnimalName animalAnimal = (AnimalName)field.getAnnotation(AnimalName.class);
                strAnimalName.append(animalAnimal.value());
                System.out.println(strAnimalName);
            } else if(field.isAnnotationPresent(AnimalColor.class)){
                AnimalColor animalColor = (AnimalColor)field.getAnnotation(AnimalColor.class);
                strAnimalColor.append(animalColor.animalColor().toString());
                System.out.println(strAnimalColor);
            } else if(field.isAnnotationPresent(AnimalMaster.class)){
                AnimalMaster animalMaster = (AnimalMaster)field.getAnnotation(AnimalMaster.class);
                strAnimalProvider.append("[编号:" + animalMaster.id() + ",主人名字:" + animalMaster.name() + ",地址:" + animalMaster.address() + "]");
                System.out.println(strAnimalProvider);
            }

        }

    }
}

测试:

package com.xiaojian.basics.annotation;

/**
  * 测试类
  */
public class Test {
    public static void main(String[] args) {
        AnimalInfoUtil.getInfo(Animal.class);
    }

}

控制台显示:

动物种类:狗
动物颜色:黑色
主人信息:[编号:1,主人名字:小明,地址:浙江省杭州市XXXXXXXX]