這個章節主要是學習解析一個類中的註解;包括類上,構造上,屬性上,方法上的註解的信息解析。java
這個自定註解是運行時會被保留的註解,出現的地方沒有限制,主要是方便測試。學習
package anotations; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * 自定義註解 * Created by Administrator on 2016/11/26. */ @Retention(RetentionPolicy.RUNTIME) public @interface Controller { String str(); int id() default 0; Class<Long> gid(); }
這個類主要是給註解賦值,同時對應不一樣的地方。測試
package controller; import anotations.Controller; /** * Created by Administrator on 2016/11/26. */ @Controller(str="class",id=1,gid=Long.class)//在類前面 public class ControllerDemo { @Controller(str="field",id=2,gid=Long.class)//在屬性前面 private String str; @Controller(str="construct",id=3,gid=Long.class)//在構造方法前面 public ControllerDemo(){ } @Controller(str="public-method",id=4,gid=Long.class)//在方法前面 public void say(){ System.out.println("hello world"); } @Controller(str="private-method",id=5,gid=Long.class)//在方法前面 private void say1(){ System.out.println("hello world"); } @Controller(str="protected-method",id=6,gid=Long.class)//在方法前面 void say2(){ System.out.println("hello world"); } }
###解析註解code
package test.anotations; import anotations.Controller; import org.junit.Test; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * Created by Administrator on 2016/11/29. */ public class ControllerDemoTest { @Test public void test() throws Exception { Class clazz = Class.forName("controller.ControllerDemo"); printMethod(clazz); printField(clazz); printClass(clazz); //打印構造器上的註解和方法、屬性相似。 } /** * 打印方法中的註解 */ private void printMethod(Class clazz) { Method[] methods = clazz.getDeclaredMethods();//獲取全部方法 for (Method method : methods) { //判斷方法是否有註解 boolean hasAnnotation = method.isAnnotationPresent(Controller.class); if(hasAnnotation){ Controller controller = method.getAnnotation(Controller.class); System.out.print("method:"+method.getName()); System.out.print(" str:"+controller.str()); System.out.print(" id:"+controller.id()); System.out.print(" gid:"+controller.gid()); System.out.println(); } } } /** * 打印全部屬性註解 * @param clazz */ private void printField(Class clazz){ Field[] fields = clazz.getDeclaredFields();//獲取全部屬性 for(Field field : fields){ boolean hasAnnotation = field.isAnnotationPresent(Controller.class); if(hasAnnotation){ Controller controller = field.getAnnotation(Controller.class); System.out.print("field:"+field.getName()); System.out.print(" str:"+controller.str()); System.out.print(" id:"+controller.id()); System.out.print(" gid:"+controller.gid()); System.out.println(); } } } /** * 打印類上的註解 * @param clazz */ private void printClass(Class clazz){ Annotation[] annotations = clazz.getAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof Controller){ Controller controller = (Controller)annotation; System.out.print("class:"+clazz.getName()); System.out.print(" str:"+controller.str()); System.out.print(" id:"+controller.id()); System.out.print(" gid:"+controller.gid()); System.out.println(); } } } }