com.github.zzlhy.util.Utils Maven / Gradle / Ivy
package com.github.zzlhy.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 空值判断工具类
*/
public class Utils {
/**
* 是否为空
* @param obj obj
* @return boolean
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof String) {
String instance = (String) obj;
if (instance.trim().length() <= 0 || "null".equalsIgnoreCase(instance)) {
return true;
}
} else if (obj instanceof Integer) {
Integer instance = (Integer) obj;
if (instance < 0) {
return true;
}
} else if (obj instanceof List>) {
List> instance = (List>) obj;
if (instance.size() <= 0) {
return true;
}
} else if (obj instanceof Map, ?>) {
Map, ?> instance = (Map, ?>) obj;
if (instance.size() <= 0) {
return true;
}
} else if (obj instanceof Object[]) {
Object[] instance = (Object[]) obj;
if (instance.length <= 0) {
return true;
}
} else if (obj instanceof Long) {
Long instance = (Long) obj;
if (instance < 0) {
return true;
}
}
return false;
}
public static boolean notEmpty(Object obj) {
return !isEmpty(obj);
}
/**
* 对象转Map
* @param obj obj
* @return map
* @throws IllegalAccessException e
*/
public static Map objectToMap(Object obj) throws IllegalAccessException {
if(obj == null){
return null;
}
Map map = new HashMap();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
return map;
}
/**
*
* @param map map
* @param beanClass 实体类
* @return object
* @throws Exception e
*/
public static Object mapToObject(Map map, Class> beanClass) throws Exception {
if (map == null)
return null;
Object obj = beanClass.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
continue;
}
field.setAccessible(true);
field.set(obj, map.get(field.getName()));
}
return obj;
}
}