The method returns
this class's annotation(s) for the specified type if such annotations are either directly present or indirectly
present.
package com.logicbig.example.clazz;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
public class GetDeclaredAnnotationsByTypeExample {
public static void main(String... args) {
System.out.println("-- annotations by type--");
Deprecated[] d = MyClass.class.getAnnotationsByType(Deprecated.class);
System.out.println(Arrays.toString(d));
MyAnnotation[] m = MyClass.class.getAnnotationsByType(MyAnnotation.class);
System.out.println(Arrays.toString(m));
}
@Deprecated
public static class MyClass extends MySuperClass {
}
@MyAnnotation
public static class MySuperClass {
}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
private static @interface MyAnnotation {
}
}
Output
-- annotations by type--
[@java.lang.Deprecated(forRemoval=false, since="")]
[@com.logicbig.example.clazz.GetDeclaredAnnotationsByTypeExample$MyAnnotation()]