@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
int count() default 1;
}
@MyAnnotation(value = "class level")
public class MyClass {
}
public class MyClass {
@MyAnnotation("method level")
public void myMethod() {
}
}
public class MyClass {
@MyAnnotation(value = "field level", count = 2)
private String myField;
}
Class<?> myClass = MyClass.class;
MyAnnotation annotation = myClass.getAnnotation(MyAnnotation.class);
Method myMethod = MyClass.class.getMethod("myMethod");
MyAnnotation annotation = myMethod.getAnnotation(MyAnnotation.class);
Field myField = MyClass.class.getDeclaredField("myField");
MyAnnotation annotation = myField.getAnnotation(MyAnnotation.class);