Target注解介绍:
@Target:注解的作用目标
@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包
声明注解:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface TypeAu{ String name(); } @Target({ElementType.METHOD,}) @Retention(RetentionPolicy.RUNTIME) @interface MethodAu{ String name(); } @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @interface ParamAu{ String name(); } @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface ParamTypeAu{ String name(); }
添加注解:
@TypeAu(name="测试类注解") public class Test002 { @ParamTypeAu(name="测试类变量注解") private String str = ""; @MethodAu(name="测试类方法注解") public String method2(@ParamAu(name="测试类方法参数注解") String p1,String p2){ return null; }
获取注解:
public static void main(String[] args) { // 获取类注解: if(Test002.class.isAnnotationPresent(TypeAu.class)){ System.out.println(Test002.class.getAnnotation(TypeAu.class).name()); } // 获取类变量注解: Field[] fields = Test002.class.getDeclaredFields(); for (Field f : fields) { if(f.isAnnotationPresent(ParamTypeAu.class)){ System.out.println(f.getAnnotation(ParamTypeAu.class).name()); } } // 获取类方法注解: Method[] methods = Test002.class.getDeclaredMethods(); for (Method m : methods) { if(m.isAnnotationPresent(MethodAu.class)){ System.out.println(m.getAnnotation(MethodAu.class).name()); } } // 获取类方法参数注解: Method[] methods2 = Test002.class.getDeclaredMethods(); for (Method m : methods2) { // 获取方法的所有参数 Parameter[] parameters = m.getParameters(); for (Parameter p : parameters) { // 判断是否存在注解 if(p.isAnnotationPresent(ParamAu.class)){ System.out.println(p.getAnnotation(ParamAu.class).name()); } } } }
主要使用的API是Class类中的实现接口AnnotatedElement的方法
isAnnotationPresent --- 检测该元素是否被对应注解修饰
getAnnotation --- 获取注解对象
完整测试代码:

package com.wh.pj1.test001; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Parameter; @TypeAu(name="测试类注解") public class Test002 { @ParamTypeAu(name="测试类变量注解") private String str = ""; @MethodAu(name="测试类方法注解") public String method2(@ParamAu(name="测试类方法参数注解") String p1,String p2){ return null; } public static void main(String[] args) { // 获取类注解: if(Test002.class.isAnnotationPresent(TypeAu.class)){ System.out.println(Test002.class.getAnnotation(TypeAu.class).name()); } // 获取类变量注解: Field[] fields = Test002.class.getDeclaredFields(); for (Field f : fields) { if(f.isAnnotationPresent(ParamTypeAu.class)){ System.out.println(f.getAnnotation(ParamTypeAu.class).name()); } } // 获取类方法注解: Method[] methods = Test002.class.getDeclaredMethods(); for (Method m : methods) { if(m.isAnnotationPresent(MethodAu.class)){ System.out.println(m.getAnnotation(MethodAu.class).name()); } } // 获取类方法参数注解: Method[] methods2 = Test002.class.getDeclaredMethods(); for (Method m : methods2) { // 获取方法的所有参数 Parameter[] parameters = m.getParameters(); for (Parameter p : parameters) { // 判断是否存在注解 if(p.isAnnotationPresent(ParamAu.class)){ System.out.println(p.getAnnotation(ParamAu.class).name()); } } } } } @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface TypeAu{ String name(); } @Target({ElementType.METHOD,}) @Retention(RetentionPolicy.RUNTIME) @interface MethodAu{ String name(); } @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @interface ParamAu{ String name(); } @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface ParamTypeAu{ String name(); }
参考文章: