To define an annotation type called Meta:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Meta {
String data() default "fast";
}
Using an annotation
To use that annotation:
...
@Meta(data = "slow")
public void foo(){
...
}
...
Using the annotation meta data ("calm down dear, it's only an example").
To print out the meta data that the annotation defines for the example above:
public static void main(String[] args) {
for (Method method : MyClass.class.getMethods()) {
Annotation[] annotations = method.getAnnotations();
int num = annotations.length;
if(num==1){
System.out.println(((Meta)annotations[0]).data());
}
}
}
No comments:
Post a Comment