反射技术Get OR Set 多继承模式子类和父类属性,方法
这些都是等一下会调用的工具类,具体入参说明都有注释
import java.lang.reflect.Field;
import org.springframework.util.ReflectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @ClassName ReflectUtils
* @Description 反射方法支持多级继承子父类方法以及属性操作工具类
* @Author liyahui
* @Date 2021-1-14 10:58
* @Version 1.0
*/
public class ReflectUtils {
/**
* 获取本类及其父类的属性的方法
*
* @param obj 当前类对象
* @return 字段数组
*/
private static Field[] getAllFields(Object obj) {
Class clazz = obj.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null) {
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
return fields;
}
/**
* 根据字段名称以及Class对象,查询字段
*
* @param tar 字段名称
* @param clazz 对象
*/
public static Field getFiled(String tar, Class clazz) {
String error = null;
Field field = null;
while (clazz != null) {
try {
field = clazz.getDeclaredField(tar);
error = null;
break;
} catch (Exception e) {
clazz = clazz.getSuperclass();
error = e.getMessage();
}
}
if (error != null || field == null) {
throw new RuntimeException("无法获取源字段:" + tar);
}
return field;
}
/**
* 根据字段名称以及newInstance返回对象以及Class<?> 对象查询值
*
* @param filedName 字段名称
* @param o newInstance返回对象
* @param clazz Class<?> 对象
*/
public static Object getValueByPropName(String filedName, Object o, Class clazz) {
Field field = getFiled(filedName, clazz);
field.setAccessible(true);
return org.springframework.util.ReflectionUtils.getField(field, o);
}
/**
* 根据反射机制set对象属性
*
* @param tar 字段名称
* @param o newInstance返回对象
* @param val set的数据值
* @param clazz Class<?> 对象
*/
public static void setValueByPropName(String tar, Object o, Object val, Class clazz) {
Field field = getFiled(tar, clazz);
field.setAccessible(true);
ReflectionUtils.setField(field, o, val);
}
}
创建3个类,进行互相继承A extends B, B extends C
编写测试类进行测试
package com.example.manage.reflect;
import com.alibaba.fastjson.JSON;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @ClassName ReflectDemo
* @Description 该反射方法支持多级继承子父类方法以及属性操作
* @Author liyahui
* @Date 2021-1-14 10:58
* @Version 1.0
*/
public class ReflectDemo {
public static void main(String[] args) {
reflectParent();
}
public static void reflectParent() {
Object instance = null;
Class<?> c = null;
try {
c = Class.forName("com.example.manage.reflect.A");
instance = c.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
Field[] fields = getAllFields(instance);
for (int i = 0; i < fields.length; i++) {
String type = fields[i].getGenericType().toString();
switch (type) {
case "class java.lang.String":
setValueByPropName(fields[i].getName(), instance, "str", c);
break;
case "class java.lang.Integer":
setValueByPropName(fields[i].getName(), instance, 1, c);
break;
case "class java.lang.Boolean":
setValueByPropName(fields[i].getName(), instance, true, c);
break;
case "class java.lang.Long":
setValueByPropName(fields[i].getName(), instance, 2L, c);
break;
case "class java.util.Date":
setValueByPropName(fields[i].getName(), instance, new Date(), c);
break;
case "class java.lang.Double":
setValueByPropName(fields[i].getName(), instance, 1.0, c);
break;
default:
break;
}
}
System.out.println(JSON.toJSONString((A) instance));
}
/**
* 在这下面可以编写调用数据库操作的一些代码
* */
/************************************************/
// TODO: 2021-1-14 XXXMapper.save((A) instance);
/************************************************/
}