看到很多注解都被@Inherited進(jìn)行了修飾,但是這個(gè)@Inherited有什么作用呢?
查看@Inherited代碼描述:
Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.
Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.
翻譯后:
指示批注類型是自動(dòng)繼承的抒和。如果在注釋類型聲明中存在繼承的元注釋门扇,并且用戶在類聲明上查詢注釋類型歹垫,并且類聲明對該類沒有注釋娶耍,那么該類的超類將自動(dòng)被查詢到注釋類型。這個(gè)過程將被重復(fù)擦耀,直到找到這個(gè)類型的注釋棉圈,或者到達(dá)類層次結(jié)構(gòu)(對象)的頂端。如果沒有超類具有此類的注釋眷蜓,那么查詢將表明該類沒有此類注釋
根據(jù)描述進(jìn)行實(shí)例測試:
定義兩個(gè)注解:@IsInheritedAnnotation 分瘾、@NoInherritedAnnotation,其中@IsInheritedAnnotation加了注解@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface IsInheritedAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoInherritedAnnotation {
}
測試類繼承關(guān)系中@Inherited的作用
@NoInherritedAnnotation
@IsInheritedAnnotation
public class InheritedBase {
}
public class MyInheritedClass extends InheritedBase {
}
測試接口繼承關(guān)系中@Inherited的作用
@NoInherritedAnnotation
@IsInheritedAnnotation
public interface IInheritedInterface {
}
public interface IInheritedInterfaceChild extends IInheritedInterface {
}
測試類實(shí)現(xiàn)接口關(guān)系中@Inherited的作用
public class MyInheritedClassUseInterface implements IInheritedInterface {
}
junit測試代碼
public class MyInheritedClassTest {
@Test
public void testInherited(){
{
Annotation[] annotations = MyInheritedClass.class.getAnnotations();
assertTrue("", Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
{
Annotation[] annotations = MyInheritedClassUseInterface.class.getAnnotations();
assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
{
Annotation[] annotations = IInheritedInterface.class.getAnnotations();
assertTrue("", Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
assertTrue("", Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
{
Annotation[] annotations = IInheritedInterfaceChild.class.getAnnotations();
assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
}
}
測試結(jié)果:
得到以下結(jié)論:
類繼承關(guān)系中@Inherited的作用
類繼承關(guān)系中吁系,子類會(huì)繼承父類使用的注解中被@Inherited修飾的注解
接口繼承關(guān)系中@Inherited的作用
接口繼承關(guān)系中德召,子接口不會(huì)繼承父接口中的任何注解,不管父接口中使用的注解有沒有被@Inherited修飾
類實(shí)現(xiàn)接口關(guān)系中@Inherited的作用
類實(shí)現(xiàn)接口時(shí)不會(huì)繼承任何接口中定義的注解