맵 전환 엔 터 티

7022 단어 IDEAJAVA
 
  

MapToEntryConvertUtils  
package com.rjcloud.common.dal;


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rjcloud.common.util.ConvertFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MapToEntryConvertUtils {

    private static Log log = LogFactory.getLog(MapToEntryConvertUtils.class);

    /**
     *         
     */
    private static Map convertItemCache = new HashMap();

    /**
     * Map   Entry
     * @param 
     * @param valueMap      
     * @param entityClass
     * @param prefix  map    key    prefix + attr
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static  T convert(Map valueMap,Class entityClass,String prefix){
        ConvertEntryItem convertItem = convertItemCache.get(entityClass.getName());
        if(convertItem == null){
            convertItem = ConvertEntryItem.createConvertItem(entityClass);
            convertItemCache.put(entityClass.getName(), convertItem);
        }

        //entityClass        
        List fieldNameList = convertItem.getFieldNameList();
        //       set    
        Map fieldSetMethodMap = convertItem.getFieldSetMethodMap();

        T entity = null;
        try {
            entity = entityClass.newInstance();
        } catch (InstantiationException e) {
            log.error("create "+entityClass.getName()+" instance failed, Do it has a empty constructed function ?", e);
            return null;
        } catch (IllegalAccessException e) {
            log.error("create "+entityClass.getName()+" instance failed, Do it has a empty constructed function ?", e);
            return null;
        }

        Object fieldValue = null;
        Method m;
        Class>[] parameterTypes;
        Object targetValue;
        if(prefix == null) prefix = "";
        //      ,         set  
        for(String fieldName: fieldNameList){
            fieldValue = valueMap.get(prefix+fieldName);
            if(fieldValue == null) continue;
            m = fieldSetMethodMap.get(fieldName);
            if(m == null) continue;

            //set       
            parameterTypes = m.getParameterTypes();
            if(parameterTypes.length != 1) continue;  //        set  

            //                ,     
            if(parameterTypes[0].isAssignableFrom(fieldValue.getClass())){
                targetValue = fieldValue;
            }else{
                //             
                targetValue = ConvertFactory.convert(parameterTypes[0], fieldValue);
            }

            if(targetValue != null){
                try {
                    //  set      
                    m.invoke(entity, targetValue);
                } catch (Exception e) {
                    log.error("set value failed:{method="+m.getName()+",value="+targetValue+"}", e);
                }
            }
        }

        return entity;
    }

    static class ConvertEntryItem{
        private List fieldNameList = new ArrayList();
        private Map fieldSetMethodMap = new HashMap();

        private ConvertEntryItem(){}

        public List getFieldNameList() {
            return fieldNameList;
        }

        public Map getFieldSetMethodMap() {
            return fieldSetMethodMap;
        }

        private void parseEntry(Class> cls){
            Field[] allField = cls.getDeclaredFields();
            Method m = null;
            String methodName;
            for(Field f: allField){
                methodName = f.getName();
                methodName = "set"+methodName.substring(0, 1).toUpperCase()+methodName.substring(1);
                try {
                    //             set  ,             set  
                    m = cls.getDeclaredMethod(methodName, f.getType());
                    if(m != null){
                        Annotation[] annotations = f.getAnnotations();
                        if(annotations!=null && annotations.length>0){
                            if(annotations[0] instanceof com.rjcloud.common.dal.anno.Column) {
                                com.rjcloud.common.dal.anno.Column cco = (com.rjcloud.common.dal.anno.Column) annotations[0];
                                if (cco != null) {
                                    fieldNameList.add(cco.name());
                                    fieldSetMethodMap.put(cco.name(), m);
                                    //continue;
                                }
                            }
                        }
                        fieldNameList.add(f.getName());
                        fieldSetMethodMap.put(f.getName(), m);
                    }
                } catch (SecurityException e) {
                    log.error("parseEntry failed: SecurityException", e);
                } catch (NoSuchMethodException e) {
                    log.info("NoSuchMethod in "+cls.getName()+": "+methodName);
                }
            }

        }

        public static ConvertEntryItem createConvertItem(Class> cls){
            ConvertEntryItem ci = new ConvertEntryItem();
            ci.parseEntry(cls);
            return ci;
        }
    }
}
PageData> pageData = iSourceUnitService.queryDbFrom(sql,args.toArray(),start,size,tableid, unitid);
if (pageData != null) {
    List xzcfEntityList=new ArrayList<>();
    for (Map map : pageData.getData()) {
        XzcfEntity entity = MapToEntryConvertUtils.convert(map, XzcfEntity.class, "");
        xzcfEntityList.add(entity);
    }
    return xzcfEntityList;
}

 
 

좋은 웹페이지 즐겨찾기