package com.test; import com.alibaba.fastjson.JSON; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.testng.annotations.Test; import java.lang.annotation.Annotation; import java.lang.reflect.*; import java.util.List; import java.util.Map; /** * @author whcmyx * @date 2021/9/26 * @Content: */ @Slf4j public class TestDemo { @SneakyThrows @Test public void test1() throws ClassNotFoundException { //获取Class对象 Class c1 = Class.forName("com.test.Dept"); //获取类名字 //getName获取的是包名+类名 String name = c1.getName(); //getSimpleName获取的是类名 String simpleName = c1.getSimpleName(); log.info("name:{},simpleName:{}", name, simpleName); //获取类属性 // getDeclaredFields获取所有的属性 Field[] declaredFields = c1.getDeclaredFields(); // getFields获取public属性 Field[] fields = c1.getFields(); log.info("declaredFields:{},fields:{}", JSON.toJSONString(declaredFields), JSON.toJSONString(fields)); //获取指定属性值,获取name属性值 Field name1 = c1.getDeclaredField("name"); log.info("获取name属性值:{}", name1); //获取类方法 // getDeclaredMethods获取本类所有方法 Method[] declaredMethods = c1.getDeclaredMethods(); // getMethods获取本类和父类所有的Public方法 Method[] methods = c1.getMethods(); log.info("declaredMethods:{}", declaredMethods); log.info("methods:{}", methods); //用反射的方式创建对象 //方法一:构造一个对象,调用无参构造器 Dept dept1 = (Dept) c1.newInstance(); log.info("dept1:{}", dept1); // 方法二:通过构造器创建,调用的是无参构造器,所以getDeclaredConstructor不需要传参数 // Constructor declaredConstructor = c1.getDeclaredConstructor(Long.class, String.class); Constructor declaredConstructor = c1.getDeclaredConstructor(); Dept dept2 = (Dept) declaredConstructor.newInstance(); log.info("dept2:{}", dept2); //用反射方式调用方法 //通过反射获取一个方法 Method setName = c1.getDeclaredMethod("setName", String.class); // 调用方法,格式:对象,方法入参值 setName.invoke(dept2, "测试aa"); log.info("重新给dept2中的name重新赋值:{}", dept2); // 用反射方式操作属性 //获取属性 Field id = c1.getDeclaredField("id"); // id为私有属性,不能直接操作。需要关闭安全检测 id.setAccessible(true); id.set(dept2, 10000L); log.info("重新给dept2中的id重新赋值:{}", dept2); } @SneakyThrows @Test public void test2() throws ClassNotFoundException { // // 获取Class对象 // Class c2 = Class.forName("com.test.Dept"); // //获取方法信息 // Method method = c2.getDeclaredMethod("setName", String.class); // Annotation[] annotations = method.getAnnotations(); // log.info("获取注解:{}",JSON.toJSONString(annotations)); // // //获取注解值信息 // OTPDataProvider otpDataProvider = (OTPDataProvider)c2.getAnnotation(OTPDataProvider.class); // String dataFile = otpDataProvider.dataFile(); // log.info("获取注解dataFile:{}",JSON.toJSONString(dataFile)); Class c1 = Class.forName("com.test.Dept"); Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { log.info(annotation.toString()); } //获取指定注解信息 TableDept tableDept = (TableDept) c1.getAnnotation(Dept.class); //获取注解值 String value = tableDept.value(); log.info("注解值:{}", value); //获取类指定注解 //获取id属性信息 Field id = c1.getDeclaredField("id"); //获取id属性信息中的注解信息 FieldDept annotation = id.getAnnotation(FieldDept.class); //获取属性信息中的columnName值 String s = annotation.columnName(); String type = annotation.type(); } public Map<String, Dept> test03(List<Dept> deptList) { return null; } @SneakyThrows @Test public void test04() { // Class c1 = Class.forName("com.Dept"); Method method = TestDemo.class.getDeclaredMethod("test03", List.class); // Method method = c1.getDeclaredMethod("test03",List.class); Type[] genericParameterTypes = method.getGenericParameterTypes(); for (Type genericParameterType : genericParameterTypes) { if (genericParameterType instanceof ParameterizedType) { Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments(); for (Type actualTypeArgument : actualTypeArguments) { log.info("actualTypeArgument:{}", JSON.toJSONString(actualTypeArgument)); } } } } }
反射使用小结
于 2021-09-27 20:33:37 首次发布