Get annotations for a Field


import java.lang.annotation.Annotation;
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 DataBeanTest {

  public static void main(String[] args) {
    Class d = DataBean.class;

    Field fs[] = d.getFields();
    for (Field f : fs) {
      System.out.println(f);

      Annotation a = f.getAnnotation(DataField.class);

      if (a != null) {
        System.out.println(f.getName());
      }
    }
  }
}

class DataBean {
  @DataField
  public String name;

  @DataField
  public String data;

  public String description;
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface DataField {
}
Home 
  Java Book 
    Runnable examples  

Reflection Field:
  1. Get all fields
  2. Get all Declared Fields
  3. Get annotations for a Field
  4. Get "public static final" field
  5. Get specific fields
  6. Get Field value by field name
  7. Get fields for super class
  8. Get Inherited Methods and fields
  9. Get Type of the field or return type of a method.
  10. Field modifiers: isSynthetic, isEnumConstant
  11. Set field value
  12. Set null to a field value
  13. Set private field value