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
Java Book
Runnable examples
Reflection Field:
- Get all fields
- Get all Declared Fields
- Get annotations for a Field
- Get "public static final" field
- Get specific fields
- Get Field value by field name
- Get fields for super class
- Get Inherited Methods and fields
- Get Type of the field or return type of a method.
- Field modifiers: isSynthetic, isEnumConstant
- Set field value
- Set null to a field value
- Set private field value