Java Reflection Java Java API
java.lang.reflect.Field
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
Returns this field's annotation for the specified type if such an annotation is present, else null.
Note that this method is defined in AnnotatedElement interface.
AnnotatedElement
package com.logicbig.example.field;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.Field;public class GetAnnotationExample { @PrimeNumber private int anInt; public static void main(String... args) throws NoSuchFieldException { Field field = GetAnnotationExample.class.getDeclaredField("anInt"); PrimeNumber annotation = field.getAnnotation(PrimeNumber.class); System.out.println(annotation); } @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) private @interface PrimeNumber {}}
@com.logicbig.example.field.GetAnnotationExample$PrimeNumber()