JAVA通过反射获取和设置Bean属性(总结)

获取/设置Bean属性

package utils;

import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class ObjectFiledUtil{
    
    /**
     * get field from T
     * @param obj T
     * @param fieldName field name
     * @return object
     */
    public static <T> Object getObjectField(T obj, String fieldName){
        if(null == obj) return null;
        Object object = null;
        try {
            //get the name of the property get method
            fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Method method = obj.getClass().getMethod("get" + fieldName);
            object = method.invoke(obj);
        } catch (Exception e) {
            log.error("get object field from T error", e);
        }
        return object;
    }

    /**
     * get field from T list
     * @param list list
     * @param fieldName field name
     * @return object list
     */
    public static <T> List<Object> getObjectListField(List<T> list, String fieldName){
        if(null == list || list.size() == 0) return new ArrayList<>();
        List<Object> fieldList = new ArrayList<>();
        try {
            //get the name of the property get method
            fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            for(T t : list){
                Method m = t.getClass().getMethod("get" + fieldName);
                fieldList.add(m.invoke(t));
            }
        } catch (Exception e) {
            log.error("get object list field from T error", e);
        }
        return fieldList;
    }

    /**
     * set a single field value for T
     * @param obj T
     * @param fieldName field name
     * @param value field value
     */
    public static <T> void setObjectField(T obj, String fieldName, T value){
        if(null == obj) return;
        try {
            Field field = obj.getClass().getDeclaredField(fieldName); //get attribute
            if(null != field){
                field.setAccessible(true); //set accessibility
                field.set(obj, value);//set attribute value
            }
        } catch (Exception e) {
            log.error("set object field to T error", e);
        }
    }

    /**
     * set field value for T batch
     * @param obj T
     * @param map key:filed name, value:filed value
     */
    public static <T> void setObjectFieldBatch(T obj, Map<String, Object> map){
        if(null == obj) return;
        try {
            for(String key : map.keySet()){
                Field field = obj.getClass().getDeclaredField(key); //get attribute
                if(null != field){
                    field.setAccessible(true); //set accessibility
                    field.set(obj, map.get(key));//set attribute value
                }
            }
        } catch (Exception e) {
            log.error("set object field to T error", e);
        }
    }

    /**
     * set field value for T list batch
     * @param list list T
     * @param map key:filed name, value:filed value
     */
    public static <T> void setObjectFieldBatch(List<T> list, Map<String, Object> map){
        if(null == list || list.size() == 0) return;
        try {
            for(T t : list){
                setObjectFieldBatch(t, map);
            }
        } catch (Exception e) {
            log.error("set object field to T error", e);
        }
    }

    /**
     * List<Object> to List<String>
     * @param list list
     * @return list
     */
    public static List<String> getStringListByObjectList(List<Object> list){
        if(null == list || list.size() == 0) return new ArrayList<>();
        return list.stream().map(String::valueOf).collect(Collectors.toList());
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

message丶小和尚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值