package com.example.util; import com.alibaba.fastjson.JSON; import
org.apache.commons.collections.CollectionUtils; import
org.apache.commons.lang.ArrayUtils; import org.springframework.beans.BeanUtils;
import java.lang.reflect.Constructor; import
java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import
java.util.List; import java.util.Map; /** * @author xiaobu * @version
JDK1.8.0_171 * @date on 2018/11/16 11:06 * @description V1.0 javabean Object conversion tool class
*/ public class EntityUtils { /** * The array collection is converted to the specified object collection *
The specified entity object must contain the constructor of all fields , The order of the elements of the array corresponds to the order and type of the constructor * @param list aggregate * @param clazz c
* @param <T> type * @return List<T> * @description be used for jpa Query customization vo Used */ public
static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) { List<T>
returnList = new ArrayList<>(); if (list.size() == 0) { return returnList; }
Class[] c2 = null; Constructor[] constructors = clazz.getConstructors(); for
(Constructor constructor : constructors) { Class[] tClass =
constructor.getParameterTypes(); if (tClass.length == list.get(0).length) { c2
= tClass; break; } } // Constructor instantiation object for (Object[] o : list) { Constructor<T>
constructor = null; try { constructor = clazz.getConstructor(c2); } catch
(NoSuchMethodException e) { e.printStackTrace(); } try { assert constructor !=
null; returnList.add(constructor.newInstance(o)); } catch
(InstantiationException | IllegalAccessException | InvocationTargetException e)
{ e.printStackTrace(); } } return returnList; } /** * @author xiaobu * @date
2018/11/23 11:54 * @param object The object to be forced to change , entityClass Type after strong turn * @return T *
@descprition Strong conversion of object type * @version 1.0 */ public static <T> T convertBean(Object
object, Class<T> entityClass) { if(null == object) { return null; } return
JSON.parseObject(JSON.toJSONString(object), entityClass); } /** * @author
xiaobu * @date 2018/11/23 11:57 * @param object The object of the conversation * @return
java.util.Map<??> * @descprition Object to map * @version 1.0 */ public static
Map<? ?> objectToMap(Object object){ return convertBean(object, Map.class); }
/** * @author xiaobu * @date 2018/11/23 12:00 * @param map map aggregate , t object *
@return T * @descprition map Transform object * @version 1.0 */ public static <T> T
mapToObject(Map<String, Object> map, Class<T> t) throws InstantiationException,
IllegalAccessException, InvocationTargetException { T instance =
t.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(instance,
map); return instance; } /** * @author xiaobu * @date 2018/11/23 12:30 * @param
source Resource object , target Target object , ignoreProperties assignment new String[]{} * @return T
target object * @descprition Object conversion * @version 1.0 */ public static <T> T copy(Object
source, Class<T> target, String...ignoreProperties){ T targetInstance = null;
try { targetInstance = target.newInstance(); } catch (Exception e) {
e.printStackTrace(); } if(ArrayUtils.isEmpty(ignoreProperties)) { assert
targetInstance != null; BeanUtils.copyProperties(source, targetInstance); }else
{ assert targetInstance != null; BeanUtils.copyProperties(source,
targetInstance, ignoreProperties); } return targetInstance; } /** * @author
xiaobu * @date 2018/11/23 12:32 * @param list, target, ignoreProperties] *
@return java.util.List<T> * @descprition object list transformation * @version 1.0 */ public
static <T, E> List<T> copyList(List<E> list, Class<T> target,
String...ignoreProperties){ List<T> targetList = new ArrayList<>();
if(CollectionUtils.isEmpty(list)) { return targetList; } for(E e : list) {
targetList.add(copy(e, target, ignoreProperties)); } return targetList; } }
 

Technology