|
|
@@ -1,147 +0,0 @@
|
|
|
-package cn.tr.module.smart.common.util;
|
|
|
-
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.lang.reflect.Modifier;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author wangzl
|
|
|
- * @description: 基于反射的对象类型转换工具类
|
|
|
- * @date 2025/7/29 16:55
|
|
|
- */
|
|
|
-public class ReflectConverter {
|
|
|
-
|
|
|
- /**
|
|
|
- * 将源对象转换为目标类型的对象
|
|
|
- * @param source 源对象
|
|
|
- * @param targetClass 目标类的Class对象
|
|
|
- * @return 转换后的目标对象
|
|
|
- */
|
|
|
- public static <T> T convert(Object source, Class<T> targetClass) {
|
|
|
- if (source == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- // 创建目标对象实例
|
|
|
- T target = targetClass.getDeclaredConstructor().newInstance();
|
|
|
-
|
|
|
- // 获取源对象和目标对象的所有字段(包括父类)
|
|
|
- List<Field> sourceFields = getAllFields(source.getClass());
|
|
|
- List<Field> targetFields = getAllFields(targetClass);
|
|
|
-
|
|
|
- // 遍历目标字段,从源对象中匹配并赋值
|
|
|
- for (Field targetField : targetFields) {
|
|
|
- // 跳过静态字段和final字段
|
|
|
- if (Modifier.isStatic(targetField.getModifiers()) || Modifier.isFinal(targetField.getModifiers())) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 根据字段名匹配源字段
|
|
|
- Field sourceField = findFieldByName(sourceFields, targetField.getName());
|
|
|
- if (sourceField == null) {
|
|
|
- continue; // 源对象中无对应字段,跳过
|
|
|
- }
|
|
|
-
|
|
|
- // 设置字段可访问
|
|
|
- sourceField.setAccessible(true);
|
|
|
- targetField.setAccessible(true);
|
|
|
-
|
|
|
- // 获取源字段值
|
|
|
- Object value = sourceField.get(source);
|
|
|
- if (value == null) {
|
|
|
- continue; // 源字段值为null,不赋值
|
|
|
- }
|
|
|
-
|
|
|
- // 处理类型转换(此处简化处理,实际可扩展更多类型)
|
|
|
- Object convertedValue = convertValue(value, targetField.getType());
|
|
|
-
|
|
|
- // 赋值到目标字段
|
|
|
- targetField.set(target, convertedValue);
|
|
|
- }
|
|
|
-
|
|
|
- return target;
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("对象转换失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取类及其所有父类的字段
|
|
|
- */
|
|
|
- private static List<Field> getAllFields(Class<?> clazz) {
|
|
|
- List<Field> fields = new ArrayList<>();
|
|
|
- Class<?> currentClass = clazz;
|
|
|
- // 遍历所有父类,直到Object类
|
|
|
- while (currentClass != null && currentClass != Object.class) {
|
|
|
- Field[] declaredFields = currentClass.getDeclaredFields();
|
|
|
- for (Field field : declaredFields) {
|
|
|
- fields.add(field);
|
|
|
- }
|
|
|
- currentClass = currentClass.getSuperclass();
|
|
|
- }
|
|
|
- return fields;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据字段名从字段列表中查找字段
|
|
|
- */
|
|
|
- private static Field findFieldByName(List<Field> fields, String fieldName) {
|
|
|
- for (Field field : fields) {
|
|
|
- if (field.getName().equals(fieldName)) {
|
|
|
- return field;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 简单的字段值类型转换(可根据需求扩展)
|
|
|
- */
|
|
|
- private static Object convertValue(Object value, Class<?> targetType) {
|
|
|
- // 类型相同,直接返回
|
|
|
- if (targetType.isInstance(value)) {
|
|
|
- return value;
|
|
|
- }
|
|
|
-
|
|
|
- // 基本类型与包装类转换(如int与Integer)
|
|
|
- if (targetType.isPrimitive()) {
|
|
|
- targetType = getWrapperClass(targetType);
|
|
|
- }
|
|
|
-
|
|
|
- // 字符串转数字(示例)
|
|
|
- if (targetType == Integer.class && value instanceof String) {
|
|
|
- return Integer.parseInt((String) value);
|
|
|
- }
|
|
|
- if (targetType == Long.class && value instanceof String) {
|
|
|
- return Long.parseLong((String) value);
|
|
|
- }
|
|
|
- if (targetType == Double.class && value instanceof String) {
|
|
|
- return Double.parseDouble((String) value);
|
|
|
- }
|
|
|
-
|
|
|
- // 数字转字符串
|
|
|
- if (targetType == String.class && value instanceof Number) {
|
|
|
- return value.toString();
|
|
|
- }
|
|
|
-
|
|
|
- // 其他类型转换可在此扩展(如日期转换等)
|
|
|
- throw new IllegalArgumentException("不支持的类型转换: " + value.getClass() + " -> " + targetType);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取基本类型对应的包装类
|
|
|
- */
|
|
|
- private static Class<?> getWrapperClass(Class<?> primitiveType) {
|
|
|
- if (primitiveType == int.class) return Integer.class;
|
|
|
- if (primitiveType == long.class) return Long.class;
|
|
|
- if (primitiveType == double.class) return Double.class;
|
|
|
- if (primitiveType == boolean.class) return Boolean.class;
|
|
|
- if (primitiveType == float.class) return Float.class;
|
|
|
- if (primitiveType == short.class) return Short.class;
|
|
|
- if (primitiveType == byte.class) return Byte.class;
|
|
|
- if (primitiveType == char.class) return Character.class;
|
|
|
- return primitiveType;
|
|
|
- }
|
|
|
-}
|