Annotation(jdk5.0注解)复习(转自http://3w_cnblogs_com/pepcod/)
package annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 定义注解 Test <br> * 为方便测试:注解目标为类 方法,属性及构造方法<br> * 注解中含有三个元素 id ,name和 gid; <br> * id 元素 有默认值 0 <br> */ @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME)
//有四个元注解,一般这两个 就可以搞定了 public @interface TestA { String name() default ""; //不写的话默认为空,不报错 int id() default 0; Class<Long> gid(); }
1 package annotation.test; 2 3 import java.util.HashMap; 4 import java.util.Map; 7 /** 8 * 这个类专门用来测试注解使用 9 */ 10 11 @TestA(name="type",gid=Long.class) 12 // 使用了类注解 13 public class UserAnnotation { 16 @TestA(name="param",id=1,gid=Long.class) // 使用了类成员注解 17 private Integer age; 18 19 @TestA(name="construct",id=2,gid=Long.class)// 使用了构造方法注解 20 public UserAnnotation() { 22 } 23 24 @TestA(name="public method", id=3, gid=Long.class)// 使用了 public 方法注解 25 public void a() { 26 Map<String, String> m = new HashMap<String, String>(0); 27 } 28 29 @TestA(name="protected method", id=4, gid=Long.class)//protected 方法注解 30 protected void b() { 31 Map<String, String> m = new HashMap<String, String>(0); 32 } 33 34 @TestA(name="private method " , id = 5, gid=Long.class) // private 方法注解 35 private void c(){ 36 Map<String, String> m = new HashMap<String, String>(0); 37 } 38 39 public void b(Integer a){ 41 } 42 }
package annotation.test; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class ParseAnnotation { /** * 简单打印出UserAnnotation 类中所使用到的类注解 * 该方法只打印了 Type 类型的注解 * @throws ClassNotFoundException */ public static void parseTypeAnnotation() throws ClassNotFoundException{ Class clazz = Class.forName("annotation.test.UserAnnotation"); Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { TestA testA = (TestA) annotation; System.out.println("type name = "+clazz.getName() + " | id = " + testA.id() + " | name = " + testA.name() + " | gid = " + testA.gid()); } } /** * 简单打印出UserAnnotation 类中所使用到的方法注解 * 该方法只打印了 Method 类型的注解 * @throws ClassNotFoundException */ public static void parseMethodAnnotation() throws ClassNotFoundException{ Method[] methods = UserAnnotation.class.getDeclaredMethods(); for (Method method : methods) { /* * 判断方法中是否有指定注解类型的注解 */ boolean hasAnnotation = method.isAnnotationPresent(TestA.class); if(hasAnnotation){ TestA annotation = method.getAnnotation(TestA.class); System.out.println("method name = " + method.getName() + " | id = " + annotation.id() + " | description = " + annotation.name() + " | gid = " + annotation.gid()); } } } /** * 简单打印出UserAnnotation 类中所使用到的构造方法注解 * 该方法只打印了 构造方法 类型的注解 * @throws ClassNotFoundException */ public static void parseConstructAnnotation() throws ClassNotFoundException{ Constructor[] constructors = UserAnnotation.class.getConstructors(); for (Constructor constructor : constructors) { /* * 判断构造方法中是否有指定注解类型的注解 */ boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class); if(hasAnnotation){ /* * 根据注解类型返回方法的指定类型注解 */ TestA annotation = (TestA) constructor.getAnnotation(TestA.class); System.out.println("constructor = " + constructor.getName() + " | id = " + annotation.id() + " | description = " + annotation.name() + " | gid= "+annotation.gid()); } } } /** * 简单打印出UserAnnotation 类中所使用到的字段注解 * 该方法只打印了 Method 类型的注解 * @throws ClassNotFoundException */ public static void parseFieldAnnotation() throws ClassNotFoundException{ Field[] fields = UserAnnotation.class.getDeclaredFields(); for (Field field : fields) { boolean hasAnnotation = field.isAnnotationPresent(TestA.class); if(hasAnnotation){ TestA annotation = field.getAnnotation(TestA.class); System.out.println("Field = " + field.getName() + " | id = " + annotation.id() + " | description = " + annotation.name() + " | gid= "+annotation.gid()); } } } public static void main(String[] args) throws ClassNotFoundException { System.out.println("------------------------------解析Type注解----------------------------------------------------------"); parseTypeAnnotation(); System.out.println("------------------------------解析Method注解-------------------------------------------------------"); parseMethodAnnotation(); System.out.println("------------------------------解析构造方法(Construct)注解------------------------------------------"); parseConstructAnnotation(); System.out.println("------------------------------解析字段(Field)注解-----------------------------------------------------"); parseFieldAnnotation(); } }
posted on 2015-09-16 18:15 freedom's_blog 阅读(174) 评论(0) 编辑 收藏 举报