自定义Java annotation及解析和使用
package annotationTest; import java.lang.annotation.*; @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MethodInfo { String author() default "Jerry"; String version() default "1.0"; String date(); String comment(); }
新建一个类,给其方法添加上刚才创建的注解:
package annotationTest; // Jerry change for demo public class AnnotationExample { @Override @MethodInfo(author = "xxx",version = "1.0",date = "2015/03/26",comment = "override toString()") public String toString() { return "AnnotationExample{}"; } @Deprecated @MethodInfo(comment = "deprecated method", date = "2015/03/26") public static void oldMethod() { System.out.println("old method, don't use it."); } @MethodInfo(author = "Pankaj", comment = "Main method", date = "Nov 17 2012", version = "1.0") public static void genericsTest() { oldMethod(); } }
打印的输出:
@annotationTest.MethodInfo(version="1.0", author="xxx", date="2015/03/26", comment="override toString()") in method:public java.lang.String annotationTest.AnnotationExample.toString() Method with revision no 1.0 = public java.lang.String annotationTest.AnnotationExample.toString() @annotationTest.MethodInfo(version="1.0", author="Pankaj", comment="Main method", date="Nov 17 2012") in method:public static void annotationTest.AnnotationExample.genericsTest() Method with revision no 1.0 = public static void annotationTest.AnnotationExample.genericsTest() @java.lang.Deprecated(forRemoval=false, since="") in method:public static void annotationTest.AnnotationExample.oldMethod() @annotationTest.MethodInfo(version="1.0", author="Jerry", comment="deprecated method", date="2015/03/26") in method:public static void annotationTest.AnnotationExample.oldMethod() Method with revision no 1.0 = public static void annotationTest.AnnotationExample.oldMethod()
同理,新建一个description注解,用来修饰People类:
package annotationTest; import java.lang.annotation.Documented; import java.lang.annotation.Inherited; import java.lang.annotation.*; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Description { String desc(); String author(); int age() default 18; }
package annotationTest; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @Description(author = "Jerry", desc = "Class annotation", age = 35) public class People { @Description(author = "Jerry 2", desc = "method annotation", age = 35) public void hello(){ } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] arg){ try { // 使用类加载器加载类 Class c = Class.forName("annotationTest.People"); // 找到类上面的注解 boolean isExist = c.isAnnotationPresent(Description.class); // 上面的这个方法是用这个类来判断这个类是否存在Description这样的一个注解 if (isExist) { // 拿到注解实例,解析类上面的注解 Description d = (Description) c.getAnnotation(Description.class); System.out.println(d.desc()); } //获取所有的方法 Method[] ms = c.getMethods(); // 遍历所有的方法 for (Method m : ms) { boolean isExist1 = m.isAnnotationPresent(Description.class); if (isExist1) { Description d1=m.getAnnotation(Description.class); System.out.println(d1.desc()); } } //另一种解析方法 for (Method m : ms) { //拿到方法上的所有的注解 Annotation[] as=m.getAnnotations(); for (Annotation a : as) { //用二元操作符判断a是否是Description的实例 if (a instanceof Description) { Description d=(Description) a; System.out.println(d.desc()); } } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }