Mega Code Archive

 
Categories / Java / Reflection
 

Field with annotations

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