Pessoal tenho uma dúvida com Annotations,Quando Crio uma Anotação
por que devo anota-la com @Retention(RetentionPolicy.RUNTIME)?
Se não anotar a Interface não consigo recuperar as anotações pertencentes
a um campo por exemplo.
Código
/*Interface*/
public @interface Value {
public abstract boolean required();
}
/*Classe*/
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
@Value(required=true)
public class MyClass {
@Value(required=true)//Se Nao anotar nao consigo pegar essa anotacao do campo value.
private String value;
private String name;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
Field[] fields = myClass.getClass().getDeclaredFields();
for(int i = 0; i < fields.length;i++){
Field field = fields[i];
System.out.println("Field:\t" + field.getName());
Annotation[] ant = field.getDeclaredAnnotations();
if(ant.length > 0){
System.out.println("Annotation:\t" + ant[0]);
}
}
}
}