Java Field .getDeclaredAnnotations ()

Syntax

Field.getDeclaredAnnotations() has the following syntax.

public Annotation [] getDeclaredAnnotations()

Example

In the following code shows how to use Field.getDeclaredAnnotations() method.


//from w  w  w . j  a va2 s.c  o  m
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

enum Spy {
  @Deprecated
  BLACK, WHITE
}

public class Main {

  public static void main(String... args)throws Exception {
      Class<?> c = Class.forName("Spy");
      Field[] flds = c.getFields();
      for (Field f : flds) {
        Annotation[] annos = f.getDeclaredAnnotations();
        for(Annotation anno:annos){
          System.out.println(anno.toString());
        }
          
        
      }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable