package com.hhwy.wdst.common.util;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
* @title: StringUtil工具类
* @description:
* @throws:
*/
public class StringUtil {
public static boolean isNullOrEmpty(Object obj) {
return obj == null || "".equals(obj.toString());
}
public static String toString(Object obj){
if(obj == null) return "null";
return obj.toString();
}
public static String ObjtoString(Object obj){
if(obj == null||"".equals(obj)) return "";
return obj.toString().trim();
}
public static String join(Collection<?> s, String delimiter) {
StringBuffer buffer = new StringBuffer();
Iterator<?> iter = s.iterator();
while (iter.hasNext()) {
buffer.append(iter.next());
if (iter.hasNext()) {
buffer.append(delimiter);
}
}
return buffer.toString();
}
public static Map toMap(Object bean) {
Class<? extends Object> clazz = bean.getClass();
Map<Object, Object> returnMap = new HashMap<Object, Object>();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = null;
result = readMethod.invoke(bean, new Object[0]);
if (null != propertyName) {
propertyName = propertyName.toString();
}
if (null != result) {
result = result.toString();
}
returnMap.put(propertyName, result);
}
}
} catch (IntrospectionException e) {
System.out.println("分析类属性失�?");
} catch (IllegalAccessException e) {
System.out.println("实例�? JavaBean 失败");
} catch (IllegalArgumentException e) {
System.out.println("映射错误");
} catch (InvocationTargetException e) {
System.out.println("调用属�?�的 setter 方法失败");
}
return returnMap;
}
}