Reflection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | package com.tenpay.risk.aml.cdd.batch.apisvr.core.basic; import lombok.Data; import org.junit.jupiter.api.Test; import java.io.Serializable; import java.lang.annotation.*; import java.lang.reflect.*; import java.util.ArrayList; import java.util.List; /** * 1、获取指定的构造函数,并创建新的实例 * 2、获取私有成员方法并调用 * 3、获取静态方法并调用 * 4、访问私有字段 * 5、访问注解Annotation * 6、访问成员变量的泛型、方法参数的泛型、方法返回值的泛型 method.getGenericReturnType,listField.getGenericType(),setListMethod.getGenericParameterTypes() */ @Data public class ReflectionTest { @Test void test() throws Exception{ /**获取类的Class*/ Class<Person> personClass = Person. class ; System.out.println(personClass); System.out.println(personClass.getName()); System.out.println(personClass.getSimpleName()); System.out.println(personClass.getModifiers()); /**获取所有构造函数 Declared*/ Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors(); for (Constructor<?> declaredConstructor : declaredConstructors) { System.out.println(declaredConstructor); } /**指定具体的构造函数创建实例*/ Constructor<Person> declaredConstructor = personClass.getDeclaredConstructor(Integer. class , String. class ); Person binfire = declaredConstructor.newInstance( 1 , "binfire" ); System.out.println(binfire); /**访问私有方法*/ Method sayHello = personClass.getDeclaredMethod( "sayHello" ); sayHello.setAccessible( true ); sayHello.invoke(binfire); /**访问静态方法*/ Method sayHelloMessage = personClass.getDeclaredMethod( "sayHelloMessage" ,String. class ,Integer. class ,String. class ); sayHelloMessage.setAccessible( true ); sayHelloMessage.invoke( null , "binfire" , 30 , "您好" ); /**访问字段*/ Field[] declaredFields = personClass.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField); } Field name = personClass.getDeclaredField( "name" ); name.setAccessible( true ); name.set(binfire, "中国" ); Object o = name.get(binfire); System.out.println(o); Field age = personClass.getDeclaredField( "age" ); age.setAccessible( true ); age.setInt(binfire, 123 ); int anInt = age.getInt(binfire); System.out.println(anInt); /**Annotation注解*/ if (name.isAnnotationPresent(MyAnnotation. class )){ MyAnnotation declaredAnnotation = name.getDeclaredAnnotation(MyAnnotation. class ); if (declaredAnnotation!= null ){ System.out.println(declaredAnnotation.testA()+ ":" +declaredAnnotation.testB()); } } } @Test void testGenerialInfo() throws NoSuchMethodException { getMethodReturnValueGeneric(); } /** * 获取方法返回值的泛型类型信息 * * @throws NoSuchMethodException */ public void getMethodReturnValueGeneric() throws NoSuchMethodException { // 获取名为"getList"的方法,在MyClass类中 Method getListMethod = MyClass. class .getMethod( "getList" ); // 获取返回值类型,getGenericReturnType()会返回值带有泛型的返回值类型 Type genericReturnType = getListMethod.getGenericReturnType(); // 但我们实际上需要获取返回值类型中的泛型信息,所以要进一步判断,即判断获取的返回值类型是否是参数化类型ParameterizedType if (genericReturnType instanceof ParameterizedType) { // 如果要使用ParameterizedType中的方法,必须先强制向下转型 ParameterizedType type = (ParameterizedType) genericReturnType; // 获取返回值类型中的泛型类型,因为可能有多个泛型类型,所以返回一个数组 Type[] actualTypeArguments = type.getActualTypeArguments(); // 循环数组,遍历每一个泛型类型 for (Type actualTypeArgument : actualTypeArguments) { Class typeArgClass = (Class) actualTypeArgument; System.out.println( "成员方法返回值的泛型信息:" + typeArgClass); } } } /** * 获取类中成员变量的泛型类型信息 * * @throws NoSuchFieldException */ public static void getMemberVariablesGeneric() throws NoSuchFieldException { // 获取MyTestClass类中名为"list"的字段 Field listField = MyClass. class .getField( "list" ); // 获取该字段的类型信息,getGenericType()方法能够获取带有泛型的类型信息 Type genericType = listField.getGenericType(); // 但我们实际上需要获取返回值类型中的泛型信息,所以要进一步判断,即判断获取的返回值类型是否是参数化类型ParameterizedType if (genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; // 获取成员变量的泛型类型信息 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); for (Type actualTypeArgument : actualTypeArguments) { Class fieldArgClass = (Class) actualTypeArgument; System.out.println( "成员变量的泛型信息:" + fieldArgClass); } } } /** * 获取方法参数的泛型类型信息 * * @throws NoSuchMethodException */ public static void getMethodParametricGeneric() throws NoSuchMethodException { // 获取MyTestClass类中名为"setList"的方法 Method setListMethod = MyClass. class .getMethod( "setList" , List. class ); // 获取该方法的参数类型信息(带有泛型) Type[] genericParameterTypes = setListMethod.getGenericParameterTypes(); // 但我们实际上需要获取返回值类型中的泛型信息,所以要进一步判断,即判断获取的返回值类型是否是参数化类型ParameterizedType for (Type genericParameterType : genericParameterTypes) { ParameterizedType parameterizedType = (ParameterizedType) genericParameterType; // 获取成员方法参数的泛型类型信息 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); for (Type actualTypeArgument : actualTypeArguments) { Class realType = (Class) actualTypeArgument; System.out.println( "成员方法参数的泛型信息:" + realType); } } } } class MyClass { // 带有泛型的成员变量 public List<Object> list = new ArrayList<>(); // 方法返回值中带有泛型 public List<Object> getList() { return list; } // 方法参数带有泛型 public void setList(List<Object> list) { this .list = list; } } @Data class Person implements Cloneable, Serializable { /** * default VersionUID */ private static final long serialVersionUID = 1L; private int age; @MyAnnotation (testA = "123" ,testB = "456" ) private String name; private Person(){ this .age= 0 ; this .name= null ; } public Person(Integer age, String name) { this .setAge(age); this .setName(name); } private void sayHello(){ System.out.println( "say hello" ); } private static void sayHelloMessage(String name,Integer age,String message){ System.out.printf( "%s;%d;%s;%n" ,name,age,message); } public int getResult( int [] array){ int sum= 0 ; for ( int e:array){ sum=e+sum; } return sum; } @Override protected Object clone() throws CloneNotSupportedException { return super .clone(); } @Override public String toString() { return String.format( "%d:%s" , this .getAge(), this .getName()); } } /** * 注解 @interface开头 * 元注解, 标记在注解上的注解 * @Retention,@Documented,@Target,@Inherited,@Repeatable * @Retention: 保留期, * * RetentionPolicy.CLASS (默认) * 注解被编译器在编译阶段写入字节码文件,但它并不会在运行时被加载到 JVM 中 *RetentionPolicy.RUNTIME * 注解被编译器在编译阶段写入字节码文件,而且会在运行时加载到 JVM 中,所以在程序运行时可以获取到它们。 * * @Inherited * 继承的子类同样继承 * * 注解成员变量:无参的构造函数 * */ @Target ({ElementType.METHOD,ElementType.FIELD}) @Retention (RetentionPolicy.RUNTIME) @interface MyAnnotation { String testA() default "" ; String testB() default "" ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix