GenericsUtils 反射获取数据

本文深入探讨了Java泛型工具类的实现细节,包括如何使用反射API获取和调用对象的方法,批量设置数据字典信息,以及处理泛型方法和类的技巧。通过具体的代码示例,展示了如何在不指定具体类型的情况下操作对象,实现了代码的复用性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


@Component
public class GenericsUtils {
    private final static Logger logger = LoggerFactory.getLogger(GenericsUtils.class);

    @Autowired
    private OperationPlatformServiceClient operationPlatformServiceClient;
    private static GenericsUtils genericsUtils;

    @PostConstruct
    public void init() {
        genericsUtils = this;
        genericsUtils.operationPlatformServiceClient = this.operationPlatformServiceClient;
    }

    /**
     * 单独设置数据字典信息
     *
     * @param t
     * @param tCls
     * @param enumCode    secode父级码表标示
     * @param fieldName   要对比的字段 class_model-->0001
     * @param setFildName 要替换的子弹
     * @param <T>
     */
    public static <T> void setEnum(T t, Class tCls, String enumCode, String fieldName, String setFildName) {
            if (t != null) {
                //获取属性方法
                Method getMethod = getGetMethod(tCls, fieldName);
                //设置属性方法 setMethodName 方法名称 String.class:参数类型
                Method setNameMethod = getSetMethod(tCls, setFildName, String.class);
                //获取id
                String oldCode = invokeMethod(getMethod,t);

                if (StringUtils.isNotBlank(oldCode)) {
                    //查询指定数据
                    List<EnumCode> enumByCodes = genericsUtils.operationPlatformServiceClient.queryByCodes(enumCode);
                    if (CollectionUtils.isNotEmpty(enumByCodes)) {
                        for (EnumCode enumByCode : enumByCodes) {
                            String seiValue = enumByCode.getSeiValue();
                            String seiName = enumByCode.getSeiName();
                            if (oldCode.equals(seiValue)) {
                                if (StringUtils.isNotBlank(setFildName)) {
                                    invokeSetMethod(setNameMethod,t,seiName);
                                }
                            }
                        }
                    }
                }
            }

    }


    /**
     * 批量设置数据字典信息
     *
     * @param data
     * @param tCls
     * @param enumCode
     * @param fieldName
     * @param setNameFild
     * @param <T>
     */
    public static <T> void setEnumToList(List<T> data, Class tCls, String enumCode, String fieldName, String setNameFild) {
            if (CollectionUtils.isNotEmpty(data)) {
                //获取属性方法
                Method getMethod = getGetMethod(tCls, fieldName);
                //设置属性方法 setMethodName 方法名称 String.class:参数类型
                Method setNameMethod = getSetMethod(tCls, setNameFild, String.class);
                if (StringUtils.isNotBlank(enumCode)) {
                    //查询指定数据
                    List<EnumCode>  enumByCodes = genericsUtils.operationPlatformServiceClient.queryByCodes(enumCode+",''");

                    if (CollectionUtils.isNotEmpty(enumByCodes)) {
                        //设置数据
                        for (int i = 0; i < data.size(); i++) {
                            T t = data.get(i);
                            String oldCode = invokeMethod(getMethod,t);
                            for (EnumCode item : enumByCodes) {
                                String seiName = item.getSeiName();
                                String seiValue = item.getSeiValue();
                                if (!StringUtils.isEmpty(oldCode)&&oldCode.equals(seiValue)) {
                                    if (StringUtils.isNotBlank(setNameFild)) {
                                        invokeSetMethod(setNameMethod,t,seiName);

                                    }
                                }
                            }
                        }
                    }
                }
            }
    }


  /**
     * 获取ids集合
     *
     * @param data
     * @param getMethod
     * @param <T>
     * @return
     * @throws Exception
     */
    private static <T> List getIds(List<T> data, Method getMethod)  {
        List<String> ids = new ArrayList<>();
        Set<String> set = new HashSet<String>();
        for (int i = 0; i < data.size(); i++) {
            T t = data.get(i);
            String itemId =invokeMethod(getMethod,t);
            if (itemId != null) {
                set.add(itemId);
            }
        }
        ids=new ArrayList<>(set);
        return ids;
    }




    /**
     * 获取get方法
     *
     * @param tCls
     * @param fieldName
     * @return
     * @throws Exception
     */
    public static Method getGetMethod(Class tCls, String fieldName)  {
        try {
            String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Method getMethod = tCls.getMethod(getMethodName);
            return getMethod;
        }catch (Exception e){
            logger.error("获取get方法报错:{}",e);
            return null ;
        }
    }

    /**
     * 获取set方法
     *
     * @param tCls
     * @param courseNameFild
     * @return
     * @throws Exception
     */
    public static Method getSetMethod(Class tCls, String courseNameFild, Class argClass)  {
        try {
            String setNameMethodName = "set" + courseNameFild.substring(0, 1).toUpperCase() + courseNameFild.substring(1);
            Method setNameMethod = tCls.getMethod(setNameMethodName, argClass);
            return setNameMethod;
        }catch (Exception e){
            logger.error("获取set方法报错:{}",e);
            return null ;
        }
    }


    /**
     * 获取结果数据
     * @param getMethod
     * @param t
     * @return
     */
    public static <T> String invokeMethod(Method getMethod,T t)  {
        try {
            String invoke = (String) getMethod.invoke(t);
            return invoke;
        }catch (Exception e){
            logger.error("执行get方法报错:{}",e);
            return null ;
        }
    }


    /**
     * 设置数据解雇
     * @param setMethod
     * @param t
     * @param fieldData
     */
    public static <T> void invokeSetMethod(Method setMethod,T t,T fieldData)  {
        try {
            setMethod.invoke(t, fieldData);
        }catch (Exception e){
            logger.error("执行set方法报错:{}",e);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值