Java Reflection - Class.getAnnotation() Examples

Code Snippets Java Reflection Java 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public <A extends Annotation> A getAnnotation(Class<A> annotationClass)

This method returns this element's annotation for the specified type if such an annotation is present, else null.


Examples


package com.logicbig.example.clazz;

public class GetAnnotationExample {

public static void main(String... args) {
Deprecated annotation = MyClass.class.getAnnotation(Deprecated.class);
System.out.println(annotation);
System.out.println(annotation.annotationType().getSimpleName());
}

@Deprecated
private static class MyClass {
}
}

Output

@java.lang.Deprecated()
Deprecated




See Also