com.feingto.cloud.kit.ObjectKit Maven / Gradle / Ivy
package com.feingto.cloud.kit;
import com.feingto.cloud.core.json.JSON;
import com.feingto.cloud.kit.reflection.ReflectionKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* 对象转换工具
*
* @author longfei
*/
@Slf4j
@SuppressWarnings({"unused", "unchecked", "WeakerAccess"})
public class ObjectKit {
private static final String[] IGNORE_FIELD_NAMES = new String[]{"log", "logger", "serialVersionUID"};
private static final String[] IGNORE_FIELD_CLASSES = new String[]{"org.apache.log4j.Logger", "org.slf4j.Logger", "org.slf4j.LoggerFactory", "java.kit.logging.Logger", "java.lang.Object", "java.lang.Class", "java.lang.reflect.AccessibleObject", "java.lang.reflect.Field", "java.lang.reflect.Method", "java.lang.reflect.Constructor"};
/**
* 序列化深度克隆
*
* @param bean Object
* @return
*/
public static T clone(Object bean) {
return copy(bean, (Class) bean.getClass());
}
/**
* 序列化深度复制
*
* @param bean Object
* @param clazz Class
* @return
*/
public static T copy(Object bean, Class clazz) {
return JSON.build().json2pojo(JSON.build().obj2json(bean), clazz);
}
/**
* 序列化深度复制到List
*
* @param bean Object
* @param clazz Class
* @return
*/
public static List copy2list(Object bean, Class clazz) {
return JSON.build().json2list(JSON.build().obj2json(bean), clazz);
}
/**
* 判断对象是否为空
*
* @param object Object
* @return boolean
*/
public static boolean isEmpty(Object object) {
return object == null || object.getClass().isArray() && Array.getLength(object) == 0 ||
object instanceof Collection> && ((Collection>) object).isEmpty() ||
object instanceof Map, ?> && ((Map, ?>) object).isEmpty();
}
/**
* 对象转换集合
*
* @param object Object
* @return boolean
*/
public static Collection objectToCollection(Object object) {
if (object == null || object == "")
return new ArrayList();
Collection collection;
if (object instanceof Collection) {
collection = (Collection) object;
} else if (object instanceof Map) {
collection = ((Map) object).entrySet();
} else if (object.getClass().isArray()) {
collection = new ArrayList();
int length = Array.getLength(object);
for (int i = 0; i < length; i++) {
Object value = Array.get(object, i);
collection.add(value);
}
} else {
collection = new ArrayList();
collection.add(object);
}
return collection;
}
public static Object[] collectionToObject(Collection collection) {
return collection.toArray(new Object[collection.size()]);
}
public static T mapToObject(Class clazz, Map map) {
try {
T m = clazz.newInstance();
BeanUtils.populate(m, map);
return m;
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public static List mapToList(Class clazz, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy