要声明一个注解, 咱们须要元注解, 元注解是指注解的注解,包括@Retention
, @Target
, @Document
, @Inherited
.java
@Retention
注解的保留位置(枚举RetentionPolicy),RetentionPolicy可选值:ide
SOURCE
注解仅存在于源码中,在class字节码文件中不包含, 若是只是作一些检查性的操做,好比 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。CLASS
默认的保留策略,注解在class字节码文件中存在,但运行时没法得到, 若是要在编译时进行一些预处理操做,好比生成一些辅助代码(如 ButterKnife),就用 CLASS注解RUNTIME
注解在class字节码文件中存在,在运行时能够经过反射获取到, 若是须要在运行时去动态获取注解信息,那只能用 RUNTIME 注解@Inherited
说明子类能够继承父类中的该注解函数
@Documented
声明注解可以被javadoc等识别测试
@Target
用来声明注解范围(枚举ElementType),ElementType可选值:code
TYPE
接口、类、枚举、注解FIELD
字段、枚举的常量METHOD
方法PARAMETER
方法参数CONSTRUCTOR
构造函数LOCAL_VARIABLE
局部变量ANNOTATION_TYPE
注解PACKAGE
包@Target(value = ElementType.METHOD) //声明该注解的运行目标: 方法 @Retention(value = RetentionPolicy.RUNTIME) //该注解的生命周期: 运行时 public @interface CanRun { // 经过@interface表示注解类型 String str() default "wow"; // 注解中携带的元数据 }
public class AnnotationRunner { public void method1() { System.out.println("method 1"); } @CanRun(str = "foobar") // 方法2添加了自定义注解的标签同时传入str值 public void method2() { System.out.println("method 2"); } public void method3() { System.out.println("method 3"); } }
public class AnnotationTest { public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { AnnotationRunner runner = new AnnotationRunner(); Method[] methods = runner.getClass().getMethods(); for (Method method : methods){ CanRun annos = method.getAnnotation(CanRun.class); //System.out.println("1"); if (annos != null){ method.invoke(runner); System.out.println(annos.str()); } } } }
运行结果:继承
method 2 foobar