泛型方法+反射,解决重复代码问题

1.泛型方法声明

放在返回结果前,进行修饰,传参List infoList 进行参数的泛型支持

public <T> List<SigmaInstance> generateInstancesByDataInfoList(List<T> infoList,
                                                              Map<Long, GroupInfo> groupMap,
                                                              Map<String, AppInfo> appMap) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
}

2.使用范例

/**
     * 基于传递过来的对象列表,不同对象获取不同的属性写到internalAddress属性中去
     * @param infoList
     * @param groupMap
     * @param appMap
     * @param <T>
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public <T> List<SigmaInstance> generateInstancesByDataInfoList(List<T> infoList,
                                                              Map<Long, GroupInfo> groupMap,
                                                              Map<String, AppInfo> appMap) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<SigmaInstance> sigmaInstances = Lists.newArrayList();
        String getGroupIdMethodName = "getGroupId";
        for (T devopsInstanceInfo:infoList) {
            Method groupMethod = devopsInstanceInfo.getClass().getMethod(getGroupIdMethodName);
            Long groupId = (Long) groupMethod.invoke(devopsInstanceInfo);
            GroupInfo devopsGroupInfo = groupMap.get(groupId);
            if (null == devopsGroupInfo){
                LOG.warn("Get devopsGroupInfo error,devopsInstanceInfo: {}",JsonUtils.toJSONString(devopsInstanceInfo));
                continue;
            }
            AppInfo devopsAppInfo = appMap.get(devopsGroupInfo.getAppEnName());
            if (null == devopsAppInfo){
                LOG.warn("Get devopsAppInfo error,devopsGroupInfo: {}",JsonUtils.toJSONString(devopsGroupInfo));
                continue;
            }
            SigmaInstance sigmaInstance = new SigmaInstance();
            sigmaInstance.setGroupEnName(devopsGroupInfo.getGroupName());
            sigmaInstance.setAppEnName(devopsAppInfo.getAppEnName());
            sigmaInstance.setPlatform(Platform.DEVOPS);
            sigmaInstance.setEnv(generateEnvTypeByDevopsGroup(devopsGroupInfo));
            Method getInternalAddressMethod = devopsInstanceInfo.getClass().getMethod(fieldMethodNameMap.get(devopsInstanceInfo.getClass()));
            sigmaInstance.setInternalAddress((String) getInternalAddressMethod.invoke(devopsInstanceInfo));
            sigmaInstance.setUseType(useTypeMap.get(devopsInstanceInfo.getClass()));
            sigmaInstance.setCreateTime(new Date());
            sigmaInstance.setCreateUser(DevopsProcessor.class.getSimpleName());
            sigmaInstance.setUpdateTime(new Date());
            sigmaInstance.setUpdateUser(DevopsProcessor.class.getSimpleName());
            sigmaInstances.add(sigmaInstance);
        }
        return sigmaInstances;
    }
posted @   SpecialSpeculator  阅读(107)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示