`
skmbw
  • 浏览: 4428 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

BeanCopierFactory

 
阅读更多
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang.StringUtils;

import com.esotericsoftware.reflectasm.MethodAccess;

import net.sf.cglib.beans.BeanCopier;

public class BeanCopierFactory
{
    private static ConcurrentMap<String, BeanCopier> copierMap = new ConcurrentHashMap<String, BeanCopier>();
    private static ConcurrentMap<Class<? extends Object>, MethodAccess> methodAccessMap = new ConcurrentHashMap<Class<? extends Object>, MethodAccess>();
    private static final BeanCopierFactory instance = new BeanCopierFactory();
   
    private BeanCopierFactory() {
        createBeanCopier("SourceBeanToTargetBean", SourceBean.class, TargetBean.class);
    }
   
    public static BeanCopierFactory getInstance() {
        return instance;
    }
   
    public BeanCopier createBeanCopier(String key, Class<?> sourceClass, Class<?> targetClass) {
        StringBuilder sb = new StringBuilder();
        sb = sb.append(sourceClass.getSimpleName()).append("To").append(targetClass.getSimpleName());
        final String key1 = sb.toString();
        BeanCopier beanCopier = copierMap.get(key1);
        if (beanCopier == null) {
            beanCopier = BeanCopier.create(sourceClass, targetClass, false);
            copierMap.put(key1, beanCopier);
        }
        return beanCopier;
    }
   
    public void beanToMap(Object fromBean, Map<String, Object> toMap)
    {
        MethodAccess methodAccess = methodAccessMap.get(fromBean.getClass());
        if (methodAccess == null){
            methodAccess = MethodAccess.get(fromBean.getClass());
            methodAccessMap.put(fromBean.getClass(), methodAccess);
        }
        try
        {
            String[] methodNames = methodAccess.getMethodNames();
            for (String methodName : methodNames){
                if (methodName.startsWith("get")){
                   
                    Object value = methodAccess.invoke(fromBean, methodName, (Object[])null);
                    toMap.put(StringUtils.uncapitalize(methodName.substring(3)), value);
                }
            }
        }
        catch (Exception e)
        {
          
        }
    }
   
    public void mapToBean(Object toBean, Map<String, Object> fromMap)
    {
        MethodAccess methodAccess = methodAccessMap.get(toBean.getClass());
        if (methodAccess == null){
            methodAccess = MethodAccess.get(toBean.getClass());
            methodAccessMap.put(toBean.getClass(), methodAccess);
        }
        try
        {
            for (Map.Entry<String, Object> entry : fromMap.entrySet()){
                StringBuilder methodName = new StringBuilder("set").append(StringUtils.capitalize(entry.getKey()));
                methodAccess.invoke(toBean, methodName.toString(), entry.getValue());
            }
        }
        catch (Exception e)
        {
          
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics