@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoInherritedAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface IsInheritedAnnotation {
}
@NoInherritedAnnotation
@IsInheritedAnnotation
public interface IInheritedInterface {
}
@NoInherritedAnnotation
@IsInheritedAnnotation
public class InheritedBase {
}
public class MyInheritedClass extends InheritedBase {
}
public class MyInheritedClassUseInterface implements IInheritedInterface{
}
public interface IInheritedInterfaceChild extends IInheritedInterface{
}
public static void main(String[] args) {
{
Annotation[] annotations = IInheritedInterface.class.getAnnotations();
System.out.println("1、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
System.out.println("2、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
{
Annotation[] annotations = MyInheritedClass.class.getAnnotations();
System.out.println("3、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
System.out.println("4、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
// System.out.println(Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
{
Annotation[] annotations = MyInheritedClassUseInterface.class.getAnnotations();
System.out.println("5、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
System.out.println("6、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
{
Annotation[] annotations = IInheritedInterfaceChild.class.getAnnotations();
System.out.println("7、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
System.out.println("8、" + Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
}
}
1、true
2、true
3、true
4、false
5、false
6、false
7、false
8、false