Field: getAnnotation(Class annotationClass) : Field « java.lang.reflect « Java by API






Field: getAnnotation(Class annotationClass)

 

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 Main {

  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 {
}

   
  








Related examples in the same category

1.Field: get(Object obj)
2.Field: getDouble(Object obj)
3.Field: getGenericType()
4.Field: getInt(Object obj)
5.Field: getModifiers()
6.Field: Class getType()
7.Field: isEnumConstant()
8.Field: isSynthetic()
9.Field: setAccessible(boolean flag)
10.Field: setBoolean(Object obj, boolean z)
11.Field: setInt(Object obj, int i)