package com.logicbig.example.field;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
public class GetDeclaredAnnotationsExample {
@Deprecated
private int anInt;
public static void main(String... args) throws NoSuchFieldException {
Field field = GetDeclaredAnnotationsExample.class.getDeclaredField("anInt");
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
System.out.println(Arrays.toString(declaredAnnotations));
/**
* field.getAnnotations() does the same thing. Field does not override getAnnotations() method
* from AccessibleObject.java, which forwards call to getDeclaredAnnotations()
*/
Annotation[] annotations = field.getAnnotations();
System.out.println(Arrays.toString(annotations));
}
}
Output
[@java.lang.Deprecated(forRemoval=false, since="")]
[@java.lang.Deprecated(forRemoval=false, since="")]