java.lang.annotation 提供了四种元注解,专门注解其余的注解(在自定义注解的时候,须要使用到元注解):
@Documented – 注解是否将包含在JavaDoc中
@Retention – 何时使用该注解
@Target – 注解用于什么地方
@Inherited – 是否容许子类继承该注解html
1.)@Retention – 定义该注解的生命周期
● RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束以后就再也不有任何意义,因此它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
● RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
● RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,所以可使用反射机制读取该注解的信息。咱们自定义的注解一般使用这种方式。java
2.)Target – 表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的ElementType 参数包括
● ElementType.CONSTRUCTOR: 用于描述构造器
● ElementType.FIELD: 成员变量、对象、属性(包括enum实例)
● ElementType.LOCAL_VARIABLE: 用于描述局部变量
● ElementType.METHOD: 用于描述方法
● ElementType.PACKAGE: 用于描述包
● ElementType.PARAMETER: 用于描述参数
● ElementType.TYPE: 用于描述类、接口(包括注解类型) 或enum声明数组
3.)@Documented – 一个简单的Annotations 标记注解,表示是否将注解信息添加在java 文档中。app
4.)@Inherited – 定义该注释和子类的关系
@Inherited 元注解是一个标记注解,@Inherited 阐述了某个被标注的类型是被继承的。若是一个使用了@Inherited 修饰的annotation 类型被用于一个class,则这个annotation 将被用于该class 的子类。ide
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:自定义注解须要使用到元注解测试
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 ""; }
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.javaui
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.javaspa
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]
【参考连接】code
https://www.cnblogs.com/acm-bingzi/p/javaAnnotation.htmlhtm