反射-注解-Java
1 package com.foreign; 2 3 import java.lang.annotation.Annotation; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method; 6 7 /** 8 * Created with IDEA 9 * author:foreign 10 * Date:2019/9/30 11 * Time:15:33 12 */ 13 public class ReflectionFk { 14 public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException { 15 Class clazz = PersonFk.class; 16 //1 获取类上所有的注解 17 Annotation[] annotations = clazz.getAnnotations(); 18 for (Annotation annotation : annotations) { 19 if (annotation instanceof ClassAnnotation) { 20 ClassAnnotation classAnnotation = (ClassAnnotation) annotation; 21 System.out.println("类注解的name:" + classAnnotation.name()); 22 System.out.println("类注解的value:" + classAnnotation.value()); 23 } 24 } 25 //1.1 指定某个类上具体的注解 26 Annotation annotation = clazz.getAnnotation(ClassAnnotation.class); 27 if (annotation instanceof ClassAnnotation) { 28 ClassAnnotation classAnnotation = (ClassAnnotation) annotation; 29 System.out.println("类注解的name:" + classAnnotation.name()); 30 System.out.println("类注解的value:" + classAnnotation.value()); 31 } 32 33 //2 获取方法上所有的注解 34 Method getName = clazz.getMethod("getName"); 35 Annotation[] annotations1 = getName.getAnnotations(); 36 for (Annotation anno : annotations1) { 37 if (anno instanceof MethodAnnotation) { 38 MethodAnnotation methodAnnotation = (MethodAnnotation) anno; 39 System.out.println("方法注解的name:" + methodAnnotation.name()); 40 System.out.println("方法注解的value:" + methodAnnotation.value()); 41 } 42 } 43 //2.1 指定某个方法上具体的注解 44 Annotation annotation1 = getName.getAnnotation(MethodAnnotation.class); 45 if (annotation1 instanceof MethodAnnotation) { 46 MethodAnnotation methodAnnotation = (MethodAnnotation) annotation1; 47 System.out.println("方法注解的name:" + methodAnnotation.name()); 48 System.out.println("方法注解的value:" + methodAnnotation.value()); 49 } 50 51 //3 参数注解 52 Method method = clazz.getMethod("setName", String.class); 53 Annotation[][] parameterAnnotations = method.getParameterAnnotations(); 54 Class<?>[] parameterTypes = method.getParameterTypes(); 55 int i = 0; 56 for (Annotation[] para : parameterAnnotations) { 57 Class<?> parameterType = parameterTypes[i++]; 58 for (Annotation annotation2 : para) { 59 if(annotation2 instanceof ParaAnnotation) { 60 ParaAnnotation paraAnnotation = (ParaAnnotation) annotation2; 61 System.out.println("方法参数注解的type:" + parameterType); 62 System.out.println("方法参数注解的name:" + paraAnnotation.name()); 63 System.out.println("方法参数注解的value:" + paraAnnotation.value()); 64 } 65 } 66 } 67 68 //4 变量注解 69 Field name = clazz.getDeclaredField("name"); 70 Annotation[] annotations2 = name.getAnnotations(); 71 for (Annotation field : annotations2) { 72 if(field instanceof FieldAnnotation) { 73 FieldAnnotation fieldAnnotation = (FieldAnnotation) field; 74 System.out.println("字段注解的name:" + fieldAnnotation.name()); 75 System.out.println("字段注解的value:" + fieldAnnotation.value()); 76 } 77 } 78 } 79 }
1 package com.foreign; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * Created with IDEA 10 * author:foreign 11 * Date:2019/9/30 12 * Time:15:30 13 */ 14 //表示在运行时允许通过反射访问 15 @Retention(RetentionPolicy.RUNTIME) 16 //表示可以用在类,接口,枚举类型上 17 @Target(ElementType.TYPE) 18 public @interface ClassAnnotation { 19 public String name(); 20 21 public String value(); 22 }
1 package com.foreign; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * Created with IDEA 10 * author:foreign 11 * Date:2019/9/30 12 * Time:16:00 13 */ 14 @Retention(RetentionPolicy.RUNTIME) 15 @Target(ElementType.FIELD) 16 public @interface FieldAnnotation { 17 public String name(); 18 19 public String value(); 20 }
1 package com.foreign; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * Created with IDEA 10 * author:foreign 11 * Date:2019/9/30 12 * Time:15:43 13 */ 14 @Retention(RetentionPolicy.RUNTIME) 15 //表示可以用在方法上 16 @Target(ElementType.METHOD) 17 public @interface MethodAnnotation { 18 public String name(); 19 20 public String value(); 21 }
1 package com.foreign; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * Created with IDEA 10 * author:foreign 11 * Date:2019/9/30 12 * Time:15:54 13 */ 14 @Retention(RetentionPolicy.RUNTIME) 15 @Target(ElementType.PARAMETER) 16 public @interface ParaAnnotation { 17 public String name(); 18 19 public String value(); 20 }
1 package com.foreign; 2 3 /** 4 * Created with IDEA 5 * author:foreign 6 * Date:2019/9/30 7 * Time:15:29 8 */ 9 @ClassAnnotation(name = "foreignClass", value = "foreignAnnotation") 10 public class PersonFk { 11 @FieldAnnotation(name = "foreignField", value = "foreignAnnotation") 12 private String name; 13 private Integer age; 14 private Boolean gender; 15 public String desc; 16 private String departmentId; 17 18 public PersonFk(String name, Integer age, Boolean gender, String desc, String departmentId) { 19 this.name = name; 20 this.age = age; 21 this.gender = gender; 22 this.desc = desc; 23 this.departmentId = departmentId; 24 } 25 26 public PersonFk(String departmentId) { 27 this.departmentId = departmentId; 28 } 29 30 public PersonFk() { 31 } 32 33 @MethodAnnotation(name = "foreignMethod", value = "foreignAnnotation") 34 public String getName() { 35 return name; 36 } 37 38 public void setName(@ParaAnnotation(name = "foreignPara", value = "foreignAnnotation") String name) { 39 this.name = name; 40 } 41 42 public Integer getAge() { 43 return age; 44 } 45 46 public void setAge(Integer age) { 47 this.age = age; 48 } 49 50 public Boolean getGender() { 51 return gender; 52 } 53 54 public void setGender(Boolean gender) { 55 this.gender = gender; 56 } 57 58 public String getDesc() { 59 return desc; 60 } 61 62 public void setDesc(String desc) { 63 this.desc = desc; 64 } 65 66 private String getDepartmentId() { 67 return departmentId; 68 } 69 70 public void setDepartmentId(String departmentId) { 71 this.departmentId = departmentId; 72 } 73 }