反射+注解
一:属性注解
1 import java.lang.annotation.*; 2 3 /** 4 * @author 陆伟 5 * 6 */ 7 @Target(ElementType.FIELD) //使用到字段上 8 @Retention(RetentionPolicy.RUNTIME) 9 @Documented 10 public @interface FruitName { 11 String value() default ""; 12 }
类注解
1 import java.lang.annotation.*; 2 3 /** 4 * @author 陆伟 5 * 6 */ 7 @Target(ElementType.TYPE) //类注解 8 @Retention(RetentionPolicy.RUNTIME) //运行时注解 9 public @interface AnnoClass { 10 String value() default ""; 11 }
方法注解
1 import java.lang.annotation.*; 2 3 @Target({ElementType.PARAMETER, ElementType.METHOD}) //方法注解 4 @Retention(RetentionPolicy.RUNTIME) 5 @Documented 6 public @interface AnnoClassParams { 7 Class[] params(); 8 }
二:使用
1 package com.wing.my.cloud.system.modular.system.util.annotation; 2 3 import com.wing.my.cloud.system.modular.system.entity.TestUser; 4 import lombok.*; 5 6 import java.lang.annotation.Annotation; 7 import java.lang.reflect.Field; 8 import java.lang.reflect.Method; 9 import java.util.Arrays; 10 import java.util.List; 11 12 @Data 13 @AllArgsConstructor 14 @NoArgsConstructor 15 @AnnoClass("类注解") 16 public class Apple extends Fruit { 17 @FruitName("苹果") 18 private String appleName; 19 20 @FruitName("红色") 21 public String appleColor; 22 23 @AnnoClassParams(params = {TestUser.class}) 24 public void test() { 25 26 } 27 28 @AnnoClassParams(params = {Thread.class}) 29 private void test1() { 30 31 } 32 33 public static void main(String[] args) { 34 Class<Apple> appleClass = Apple.class; 35 36 Annotation[] annotations = appleClass.getAnnotations(); 37 List<Annotation> annotations1 = Arrays.asList(annotations); 38 annotations1.forEach(annotation -> { 39 System.out.println("【类注解】"+annotation); 40 }); 41 42 //类名 43 System.out.println(appleClass.getName()); 44 //利用反射创建对象 45 try { 46 Apple apple = appleClass.newInstance(); 47 apple.setAppleName("橘子"); 48 System.out.println("【利用反射创建对象】:" + apple); 49 } catch (InstantiationException e) { 50 e.printStackTrace(); 51 } catch (IllegalAccessException e) { 52 e.printStackTrace(); 53 } 54 55 /** 56 * getDeclaredField 仅能获取类本身的属性成员(包括私有、共有、保护) 57 * getDeclaredMethod 得到私有方法 58 */ 59 try { 60 Field appleName = appleClass.getDeclaredField("appleName"); 61 System.out.println("【private 属性】:" + appleName.getName()); 62 FruitName annotation = appleName.getAnnotation(FruitName.class); 63 System.out.println("【private 属性注解】:" + annotation.value()); 64 65 Method declaredMethod = appleClass.getDeclaredMethod("test1"); 66 AnnoClassParams annotation1 = declaredMethod.getAnnotation(AnnoClassParams.class); 67 for (int i = 0; i < annotation1.params().length; i++) { 68 System.out.println("【private 方法】:" + annotation1.params()[i]); 69 } 70 } catch (NoSuchFieldException e) { 71 e.printStackTrace(); 72 } catch (NoSuchMethodException e) { 73 e.printStackTrace(); 74 } 75 76 77 /** 78 * getField 仅能获取类(及其父类可以自己测试) public属性成员 79 * getMethod public方法 80 */ 81 try { 82 Field appleColor = appleClass.getField("appleColor"); 83 FruitName annotation = appleColor.getAnnotation(FruitName.class); 84 System.out.println("【public 属性】" + annotation.value()); 85 Method test1 = appleClass.getMethod("test"); 86 AnnoClassParams annotation1 = test1.getAnnotation(AnnoClassParams.class); 87 for (int i = 0; i < annotation1.params().length; i++) { 88 System.out.println("【public 方法】:" + annotation1.params()[i]); 89 } 90 } catch (NoSuchFieldException e) { 91 e.printStackTrace(); 92 } catch (NoSuchMethodException e) { 93 e.printStackTrace(); 94 } 95 96 97 Field[] fields = appleClass.getDeclaredFields(); 98 List<Field> fieldsList = Arrays.asList(fields); 99 fieldsList.forEach(field -> { 100 System.out.println("【所有的本类的属性】:" + field.getName()); 101 }); 102 103 Field[] fields1 = appleClass.getFields(); 104 List<Field> fields1List = Arrays.asList(fields1); 105 fields1List.forEach(field -> { 106 System.out.println("【所有public属性,包含父类的public:】" + field); 107 }); 108 109 /** 110 * getSuperclass().getDeclaredFields() 111 * 父类属性 112 */ 113 Field[] declaredFields = appleClass.getSuperclass().getDeclaredFields(); 114 List<Field> fieldList = Arrays.asList(declaredFields); 115 fieldList.forEach(field -> { 116 System.out.println("【所有父类的属性】" + field); 117 }); 118 Method[] declaredMethods = appleClass.getDeclaredMethods(); 119 List<Method> methodList = Arrays.asList(declaredMethods); 120 methodList.forEach(method -> { 121 System.out.println("【所有方法】:" + method.getName()); 122 }); 123 } 124 }