Java tutorial
/** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.daxia.generator.util; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.List; import org.apache.commons.beanutils.BeanUtils; import org.dozer.DozerBeanMapper; import com.google.common.collect.Lists; /** * ??Dozer, ?Bean<->BeanMapper.: * * 1. ?Mapper?. * 2. ?. * 3. ??Collection. * 4. BA?B?. * * @author calvin */ public class BeanMapper { /** * ?Dozer?, ????DozerMapper?. */ private static DozerBeanMapper dozer = new DozerBeanMapper(); /** * Dozer?. */ public static <T> T map(Object source, Class<T> destinationClass) { return dozer.map(source, destinationClass); } /** * Dozer?Collection. */ public static <T> List<T> mapList(@SuppressWarnings("rawtypes") Collection sourceList, Class<T> destinationClass) { List<T> destinationList = Lists.newArrayList(); for (Object sourceObject : sourceList) { T destinationObject = dozer.map(sourceObject, destinationClass); destinationList.add(destinationObject); } return destinationList; } /** * DozerA?B. */ public static void copy(Object source, Object destinationObject) { // dozer.map(source, destinationObject); try { BeanUtils.copyProperties(destinationObject, source); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } }