java 获取类,属性变量,方法,方法参数上注解的值等

一:获取类上注解的值

定义注解@Target(ElementType.TYPE)用于类,接口等

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Orange {
    String getName();
    String getValue();
@Orange(getName = "3333",getValue = "4444")
public class ParameterNameTest {
    @Test
    public void main() throws Exception {
        Class<ParameterNameTest> clazz = ParameterNameTest.class;
        if(clazz.isAnnotationPresent(Orange.class)){
            // 获取 "类" 上的注解
            Orange getAnnotation = clazz.getAnnotation(Orange.class);
            System.out.println("\"类\"上的注解值获取到第一个 :"
                    + getAnnotation.getName()+ ",第二个:"+ getAnnotation.getValue());
"类"上的注解值获取到第一个 :3333,第二个:4444

二:获取属性变量上注解的值

定义注解@Target(ElementType.FIELD)用于属性变量

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Banana {
    String length();
    String price();
public class ParameterNameTest {
    @Banana(length = "6666", price = "$888")
    String something = "other information";
    @Test
    public void main() throws Exception {
        Class<ParameterNameTest> clazz = ParameterNameTest.class;
         // 获取 "属性变量" 上的注解的值
        Field[] fields = clazz.getDeclaredFields();
        for(Field field: fields){
            if(field.isAnnotationPresent(Banana.class)){
                Banana bananaAnnotation = field.getAnnotation(Banana.class);
                System.out.println("\"属性变量\"上的注解值获取到第一个 :"
                        + bananaAnnotation.length()+ ",第二个:"+ bananaAnnotation.price());
"属性变量"上的注解值获取到第一个 :6666,第二个:$888

三: 获取方法上注解的值

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Apple {
    String color();
    String number();
public class ParameterNameTest {
    @Apple(color = "红色", number = "5555")
    public void method1(){
        // ...
    @Test
    public void main() throws Exception {
        Class<ParameterNameTest> clazz = ParameterNameTest.class;
        // 获取 "方法"上的注解的值
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method: methods){
            if(method.isAnnotationPresent(Apple.class)){
                Apple appleAnnotation = method.getAnnotation(Apple.class);
                System.out.println("\"方法\"上的注解值获取到第一个 :"
                        + appleAnnotation.color()+ ",第二个:"+ appleAnnotation.number());
"方法"上的注解值获取到第一个 :红色,第二个:5555

三: 获取" 方法参数 " 上注解的值

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cherry {
    String value();
public class ParameterNameTest {
   public void method2(@Cherry("1111") String param1, @Cherry("2222") String param2) {
        System.out.println(param1 + param2);
    @Test
    public void main() throws Exception {
        Class<ParameterNameTest> clazz = ParameterNameTest.class;
        // 获取 "方法参数" 上的注解的值
        Method method = clazz.getDeclaredMethod("method2", String.class, String.class);
        String[] parameterNames = getMethodParameterNamesByAnnotation(method);
        System.out.println("\"方法参数\"上的注解值获取到"+Arrays.toString(parameterNames));
     * 获取给 "方法参数" 进行注解的值
     * @param method 要获取参数名的方法
     * @return 按参数顺序排列的参数名列表
    public static String[] getMethodParameterNamesByAnnotation(Method method) {
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations == null || parameterAnnotations.length == 0) {
            return null;
        String[] parameterNames = new String[parameterAnnotations.length];
        int i = 0;
        for (Annotation[] parameterAnnotation : parameterAnnotations) {
            for (Annotation annotation : parameterAnnotation) {
                if (annotation instanceof Cherry) {
                    Cherry param = (Cherry) annotation;
                    parameterNames[i++] = param.value();
        return parameterNames;
"方法参数"上的注解值获取到[1111, 2222]

小结:主要使用的API是Class类中的实现接口AnnotatedElement的方法

isAnnotationPresent --- 检测该元素是否被对应注解修饰

default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
        return getAnnotation(annotationClass) != null;