@Deprecated
@MethodInfo(comment = "deprecated method", date = "2015/03/26")
public static void oldMethod() {
System.out.println("old method, don't use it.");
}
@SuppressWarnings({ "unchecked", "deprecation" })
@MethodInfo(author = "Pankaj", comment = "Main method", date = "Nov 17 2012", version = "1.0")
public static void genericsTest() {
oldMethod();
}
}使用反射来解析Annotation
注意我们的Annotation的Retention Policy 必须是RUNTIME,否则我们无法在运行时从他里面获得任何数据。
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* Created by Administrator on 2015/3/26.
*/
public class AnnotationParsing {
public static void main(String[] args) {
for (Method method: AnnotationExample.class.getMethods()) {
if (method.isAnnotationPresent(MethodInfo.class)) {
for (Annotation annotation:method.getAnnotations()) {
System.out.println(annotation + " in method:"+ method);
}
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
if ("1.0".equals(methodInfo.version())) {
System.out.println("Method with revision no 1.0 = "
+ method);
}
}
}
}
}