Java Field.getAnnotation(Class <T> annotationClass)
Syntax
Field.getAnnotation(Class <T> annotationClass) has the following syntax.
public <T extends Annotation> T getAnnotation(Class <T> annotationClass)
Example
In the following code shows how to use Field.getAnnotation(Class <T> annotationClass) method.
//from w w w . j a v a 2 s . c o m
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 {
}
The code above generates the following result.