对象字段赋值 - ReflectionUtils

2023-11-09  本文已影响0人  Tinyspot

使用 spring-core 里的 ReflectionUtils 类

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.util.ReflectionUtils;
// ...

public class UpdateField {

    @Test
    public void demo() {
        String extendCondition = "orderCode=1002;orderStatus=100;tradeId=null";
        // 解析
        Map<String, String> params = new HashMap<>();
        if (extendCondition.trim().contains(";")) {
            String[] conditionArray = extendCondition.trim().split(";");
            for (String item : conditionArray) {
                if (StringUtils.isNotEmpty(item) && item.contains("=")) {
                    params.put(item.split("=")[0].trim(), item.split("=")[1].trim());
                }
            }
        }
    
        OrderDTO orderDTO = new OrderDTO("1001", "20230101001");
        updateFields(orderDTO, params);
    
        System.out.println(orderDTO);
    }

    /**
     * 关键字不做解析
     */
    private static final List<String> KEY_WORDS = Arrays.asList("orderId", "orderCode");

    public static void updateFields(Object object, Map<String, String> params) {
        if (object == null || MapUtils.isEmpty(params)) {
            return;
        }

        for (Map.Entry<String, String> entry : params.entrySet()) {
            String key = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(entry.getKey()));
            if (KEY_WORDS.contains(key)) {
                continue;
            }
            String value = params.get(key);
            if (StringUtils.isBlank(value)) {
                continue;
            }
            Field field = ReflectionUtils.findField(object.getClass(), key);
            if (field == null) {
                continue;
            }
            field.setAccessible(true);
            if (StringUtils.equals(value, "null")) {
                ReflectionUtils.setField(field, object, null);
            } else {
                Class<?> fieldType = field.getType();
                if (String.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, value);
                } else if (Integer.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Integer.parseInt(value));
                } else if (Long.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Long.parseLong(value));
                } else if (Date.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, new Date(Long.parseLong(value)));
                } else if (Double.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Double.parseDouble(value));
                } else if (Float.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Float.parseFloat(value));
                } else if (Short.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Short.parseShort(value));
                } else if (Byte.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Byte.parseByte(value));
                } else if (Boolean.class.isAssignableFrom(fieldType)) {
                    ReflectionUtils.setField(field, object, Boolean.valueOf(value));
                }
            }
        }
    }
    
}
@Data
public class OrderDTO {
    private Integer orderId;
    private String orderCode;
    private String tradeId;
    private Integer orderStatus;

    public OrderDTO(String orderCode, String tradeId) {
        this.orderCode = orderCode;
        this.tradeId = tradeId;
    }
}

打印对象:
OrderDTO(orderId=null, orderCode=1001, tradeId=null, orderStatus=100)

上一篇 下一篇

猜你喜欢

热点阅读