这是我的方法级注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Name {
public String myName();
}
这是一个声明注释的虚拟类:
public class Z {
@Name(myName = "George")
public void something() {
}
}
以下是获取价值的方法:
final Method method = Z.class.getMethod("something");
if (method.isAnnotationPresent(Name.class)) {
final Annotation annotation = method.getAnnotation(Name.class);
final Name name = (Name) annotation;
System.out.println(name.myName()); // Prints George
}