GetSetUtil

package com.jpush;

import cn.jpush.statistics.excel.model.AdSettlementInfoModel;
import cn.jpush.statistics.model.entity.AdSettlementInfo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class GetSetUtil {
    public static void main(String[] args) {
       setMethod(AdSettlementInfoModel.class,AdSettlementInfo.class,"model","info");

    }
    //            hiveData.setCpc(result.get("cpc"));
    private static void mapToPojo(Class clazz,String target , String mapGet){
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            char first = name.charAt(0);
            String substring = name.substring(1);
            String pre = Character.toUpperCase(first)+substring;
            System.out.println(target+pre+"("+mapGet+"(\""+name+"\"));");
//            System.out.println("hiveData.set"+pre+"(result.get(\""+name+"\"));");
        }
    }

    
    private static void setMethod(Class sourceClass,Class targetClass,String sourceName,String targeName){
        Method[] sourceMethods = sourceClass.getMethods();
        Method[] targetMethods = targetClass.getMethods();

        Map<String,Method> sourceMethodMap = new HashMap<>();
        Map<String,Method> targetMethodMap = new HashMap<>();
        for (Method sourceMethod : sourceMethods) {
            sourceMethodMap.put(sourceMethod.getName(),sourceMethod);
        }

        for (Method targetMethod : targetMethods) {
            targetMethodMap.put(targetMethod.getName(),targetMethod);
        }


        for (Map.Entry<String, Method> stringMethodEntry : sourceMethodMap.entrySet()) {
            Method method = targetMethodMap.get(stringMethodEntry.getKey());
            if(method == null){
                continue;
            }
            String name = method.getName();
            if(name.startsWith("get") && !"getClass".equals(name)){
                String setName = name.substring(3);
                System.out.println(targeName+".set"+setName+"("+sourceName+"."+name+"());");
            }
        }
        
        
    }
}

 

posted @ 2020-10-23 18:08  冬马党  阅读(104)  评论(0编辑  收藏  举报