Class getAnnotationsByType() method in Java with Examples
The getAnnotationsByType() method of java.lang.Class class is used to get the annotations of the specified annotation type present in this class. The method returns an array of annotations for the specified annotation type.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public A getAnnotationsByType(Class<T> annotationClass)
Parameter: This method accepts a parameter annotationClass which is the type of the annotation to get.
Return Value: This method returns an array of annotations for the specified annotation type.
Exception: This method throws:
- NullPointerException: if the given annotation class is null.
Below programs demonstrate the getAnnotationsByType() method.
Class represented by myClass: class Test Annotation of myClass of type Annotation: [@Annotation(key=GFG, value=GeeksForGeeks)]
Class represented by myClass: class Test Annotation of myClass of type Deprecated: [@java.lang.Deprecated()]