java原生注解@Inherited的使用

java原生注解@Inherited

@Inherited作为java的原生注解之一,表示其标记的注解允许被继承,接口实现除外。

1、自定义两个注解SleepTime和ActiveTime,其中一个有@Inherited标记,另一个没有@Inherited标记

package com.chengya.demo.test;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

@Target({ElementType.TYPE,ElementType.METHOD})
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface SleepTime {
    int max() default 60;

    TimeUnit timeUnit() default TimeUnit.SECONDS;
}

package com.chengya.demo.test;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ActiveTime {
    int min() default 10;

    TimeUnit timeUnit() default TimeUnit.SECONDS;
}

2、自定义接口Animal,并用Cat实现它

@SleepTime(max = 120)
@ActiveTime(min = 5)
public interface Animal {
    
}
public class Cat implements Animal {
    
}

3、自定义父类Dog,并用WhiteDog继承它

@SleepTime(max = 360)
@ActiveTime(min = 60)
public class Dog {
    
}
public class WhiteDog extends Dog {
    
}

4、测试

package com.chengya.demo.test;

import java.lang.annotation.Annotation;

public class ClientTest {
    public static void main(String[] args) throws NoSuchMethodException {
        System.out.println("======Animal Class Annotation======");
        Class animalClass = Animal.class;
        Annotation[] sleepAnnotations = animalClass.getAnnotations();
        for (Annotation annotation : sleepAnnotations) {
            System.out.println(annotation.toString());
        }

        System.out.println("======Cat Class Annotation======");
        Class catClass = Cat.class;
        Annotation[] catAnnotations = catClass.getAnnotations();
        for (Annotation annotation : catAnnotations) {
            System.out.println(annotation.toString());
        }

        System.out.println("======Dog Class Annotation======");
        Class dogClass = Dog.class;
        Annotation[] dogAnnotations = dogClass.getAnnotations();
        for (Annotation annotation : dogAnnotations) {
            System.out.println(annotation.toString());
        }

        System.out.println("======White Dog Class Annotation======");
        Class whiteDogClass = WhiteDog.class;
        Annotation[] whiteDogAnnotations = whiteDogClass.getAnnotations();
        for (Annotation annotation : whiteDogAnnotations) {
            System.out.println(annotation.toString());
        }
    }
}

输出结果:

----------Animal Class Annotation----------
@com.chengya.demo.test.SleepTime(max=120, timeUnit=SECONDS)
@com.chengya.demo.test.ActiveTime(min=5, timeUnit=SECONDS)
----------Cat Class Annotation----------
----------Dog Class Annotation----------
@com.chengya.demo.test.SleepTime(max=360, timeUnit=SECONDS)
@com.chengya.demo.test.ActiveTime(min=60, timeUnit=SECONDS)
----------White Dog Class Annotation----------
@com.chengya.demo.test.SleepTime(max=360, timeUnit=SECONDS)

由此可见,如果父类使用的注解带有@Inherited标注,子类是可以获取到此注解的;如果父类使用的注解不带@Inherited标注,子类则无法获取到此注解。

至于接口,无论接口使用的注解是否带有@Inherited标注,实现类都是无法获取到接口的注解的。

你可能感兴趣的:(java,后端)