Close

Java Reflection - Field.getDeclaredAnnotations() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

public Annotation[] getDeclaredAnnotations()

Returns annotations that are directly present on this element.

Examples


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="")]




See Also