通过该对象工具类ObjectUtils(参考org.springframework.util.ObjectUtils)实现了类集、数组及基本数据类型转换及字符串输出,常用在日志输出打印、接口联调及对象监控等用例场景,常用的方法有ObjectUtils.objToString(obj)、toObjectArray、addObjectToArray、containsElement及isCheckedException等,具体代码如下
import java.lang.reflect.AccessibleObject;@b@import java.lang.reflect.Array;@b@import java.lang.reflect.Field;@b@import java.lang.reflect.Method;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Iterator;@b@@b@ @b@@SuppressWarnings("rawtypes")@b@public abstract class ObjectUtils {@b@ private static final int INITIAL_HASH = 7;@b@ private static final int MULTIPLIER = 31;@b@@b@ private static final String EMPTY_STRING = "";@b@ private static final String NULL_STRING = "null";@b@ private static final String ARRAY_START = "{";@b@ private static final String ARRAY_END = "}";@b@ private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;@b@ private static final String ARRAY_ELEMENT_SEPARATOR = ", ";@b@@b@@b@ /**@b@ * Return whether the given throwable is a checked exception:@b@ * that is, neither a RuntimeException nor an Error.@b@ * @param ex the throwable to check@b@ * @return whether the throwable is a checked exception@b@ * @see java.lang.Exception@b@ * @see java.lang.RuntimeException@b@ * @see java.lang.Error@b@ */@b@ public static boolean isCheckedException(Throwable ex) {@b@ return !(ex instanceof RuntimeException || ex instanceof Error);@b@ }@b@@b@ /**@b@ * Check whether the given exception is compatible with the exceptions@b@ * declared in a throws clause.@b@ * @param ex the exception to checked@b@ * @param declaredExceptions the exceptions declared in the throws clause@b@ * @return whether the given exception is compatible@b@ */@b@ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class<?>[] declaredExceptions) {@b@ if (!isCheckedException(ex)) {@b@ return true;@b@ }@b@ if (declaredExceptions != null) {@b@ int i = 0;@b@ while (i < declaredExceptions.length) {@b@ if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {@b@ return true;@b@ }@b@ i++;@b@ }@b@ }@b@ return false;@b@ }@b@@b@ /**@b@ * Determine whether the given object is an array:@b@ * either an Object array or a primitive array.@b@ * @param obj the object to check@b@ */@b@ public static boolean isArray(Object obj) {@b@ return (obj != null && obj.getClass().isArray());@b@ }@b@@b@ /**@b@ * Determine whether the given array is empty:@b@ * i.e. <code>null</code> or of zero length.@b@ * @param array the array to check@b@ */@b@ public static boolean isEmpty(Object[] array) {@b@ return (array == null || array.length == 0);@b@ }@b@@b@ /**@b@ * Check whether the given array contains the given element.@b@ * @param array the array to check (may be <code>null</code>,@b@ * in which case the return value will always be <code>false</code>)@b@ * @param element the element to check for@b@ * @return whether the element has been found in the given array@b@ */@b@ public static boolean containsElement(Object[] array, Object element) {@b@ if (array == null) {@b@ return false;@b@ }@b@ for (Object arrayEle : array) {@b@ if (nullSafeEquals(arrayEle, element)) {@b@ return true;@b@ }@b@ }@b@ return false;@b@ }@b@@b@ /**@b@ * Append the given Object to the given array, returning a new array@b@ * consisting of the input array contents plus the given Object.@b@ * @param array the array to append to (can be <code>null</code>)@b@ * @param obj the Object to append@b@ * @return the new array (of the same component type; never <code>null</code>)@b@ */@b@ public static Object[] addObjectToArray(Object[] array, Object obj) {@b@ Class<?> compType = Object.class;@b@ if (array != null) {@b@ compType = array.getClass().getComponentType();@b@ }@b@ else if (obj != null) {@b@ compType = obj.getClass();@b@ }@b@ int newArrLength = (array != null ? array.length + 1 : 1);@b@ Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);@b@ if (array != null) {@b@ System.arraycopy(array, 0, newArr, 0, array.length);@b@ }@b@ newArr[newArr.length - 1] = obj;@b@ return newArr;@b@ }@b@@b@ /**@b@ * Convert the given array (which may be a primitive array) to an@b@ * object array (if necessary of primitive wrapper objects).@b@ * <p>A <code>null</code> source value will be converted to an@b@ * empty Object array.@b@ * @param source the (potentially primitive) array@b@ * @return the corresponding object array (never <code>null</code>)@b@ * @throws IllegalArgumentException if the parameter is not an array@b@ */@b@ public static Object[] toObjectArray(Object source) {@b@ if (source instanceof Object[]) {@b@ return (Object[]) source;@b@ }@b@ if (source == null) {@b@ return new Object[0];@b@ }@b@ if (!source.getClass().isArray()) {@b@ throw new IllegalArgumentException("Source is not an array: " + source);@b@ }@b@ int length = Array.getLength(source);@b@ if (length == 0) {@b@ return new Object[0];@b@ }@b@ Class<?> wrapperType = Array.get(source, 0).getClass();@b@ Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);@b@ for (int i = 0; i < length; i++) {@b@ newArray[i] = Array.get(source, i);@b@ }@b@ return newArray;@b@ }@b@@b@ @b@ /**@b@ * Determine if the given objects are equal, returning <code>true</code>@b@ * if both are <code>null</code> or <code>false</code> if only one is@b@ * <code>null</code>.@b@ * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality@b@ * check based on the array elements rather than the array reference.@b@ * @param o1 first Object to compare@b@ * @param o2 second Object to compare@b@ * @return whether the given objects are equal@b@ * @see java.util.Arrays#equals@b@ */@b@ public static boolean nullSafeEquals(Object o1, Object o2) {@b@ if (o1 == o2) {@b@ return true;@b@ }@b@ if (o1 == null || o2 == null) {@b@ return false;@b@ }@b@ if (o1.equals(o2)) {@b@ return true;@b@ }@b@ if (o1.getClass().isArray() && o2.getClass().isArray()) {@b@ if (o1 instanceof Object[] && o2 instanceof Object[]) {@b@ return Arrays.equals((Object[]) o1, (Object[]) o2);@b@ }@b@ if (o1 instanceof boolean[] && o2 instanceof boolean[]) {@b@ return Arrays.equals((boolean[]) o1, (boolean[]) o2);@b@ }@b@ if (o1 instanceof byte[] && o2 instanceof byte[]) {@b@ return Arrays.equals((byte[]) o1, (byte[]) o2);@b@ }@b@ if (o1 instanceof char[] && o2 instanceof char[]) {@b@ return Arrays.equals((char[]) o1, (char[]) o2);@b@ }@b@ if (o1 instanceof double[] && o2 instanceof double[]) {@b@ return Arrays.equals((double[]) o1, (double[]) o2);@b@ }@b@ if (o1 instanceof float[] && o2 instanceof float[]) {@b@ return Arrays.equals((float[]) o1, (float[]) o2);@b@ }@b@ if (o1 instanceof int[] && o2 instanceof int[]) {@b@ return Arrays.equals((int[]) o1, (int[]) o2);@b@ }@b@ if (o1 instanceof long[] && o2 instanceof long[]) {@b@ return Arrays.equals((long[]) o1, (long[]) o2);@b@ }@b@ if (o1 instanceof short[] && o2 instanceof short[]) {@b@ return Arrays.equals((short[]) o1, (short[]) o2);@b@ }@b@ }@b@ return false;@b@ }@b@@b@ /**@b@ * Return as hash code for the given object; typically the value of@b@ * <code>{@link Object#hashCode()}</code>. If the object is an array,@b@ * this method will delegate to any of the <code>nullSafeHashCode</code>@b@ * methods for arrays in this class. If the object is <code>null</code>,@b@ * this method returns 0.@b@ * @see #nullSafeHashCode(Object[])@b@ * @see #nullSafeHashCode(boolean[])@b@ * @see #nullSafeHashCode(byte[])@b@ * @see #nullSafeHashCode(char[])@b@ * @see #nullSafeHashCode(double[])@b@ * @see #nullSafeHashCode(float[])@b@ * @see #nullSafeHashCode(int[])@b@ * @see #nullSafeHashCode(long[])@b@ * @see #nullSafeHashCode(short[])@b@ */@b@ public static int nullSafeHashCode(Object obj) {@b@ if (obj == null) {@b@ return 0;@b@ }@b@ if (obj.getClass().isArray()) {@b@ if (obj instanceof Object[]) {@b@ return nullSafeHashCode((Object[]) obj);@b@ }@b@ if (obj instanceof boolean[]) {@b@ return nullSafeHashCode((boolean[]) obj);@b@ }@b@ if (obj instanceof byte[]) {@b@ return nullSafeHashCode((byte[]) obj);@b@ }@b@ if (obj instanceof char[]) {@b@ return nullSafeHashCode((char[]) obj);@b@ }@b@ if (obj instanceof double[]) {@b@ return nullSafeHashCode((double[]) obj);@b@ }@b@ if (obj instanceof float[]) {@b@ return nullSafeHashCode((float[]) obj);@b@ }@b@ if (obj instanceof int[]) {@b@ return nullSafeHashCode((int[]) obj);@b@ }@b@ if (obj instanceof long[]) {@b@ return nullSafeHashCode((long[]) obj);@b@ }@b@ if (obj instanceof short[]) {@b@ return nullSafeHashCode((short[]) obj);@b@ }@b@ }@b@ return obj.hashCode();@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(Object[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(boolean[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + hashCode(array[i]);@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(byte[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + array[i];@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(char[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + array[i];@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(double[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + hashCode(array[i]);@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(float[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + hashCode(array[i]);@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(int[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + array[i];@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(long[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + hashCode(array[i]);@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return a hash code based on the contents of the specified array.@b@ * If <code>array</code> is <code>null</code>, this method returns 0.@b@ */@b@ public static int nullSafeHashCode(short[] array) {@b@ if (array == null) {@b@ return 0;@b@ }@b@ int hash = INITIAL_HASH;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; i++) {@b@ hash = MULTIPLIER * hash + array[i];@b@ }@b@ return hash;@b@ }@b@@b@ /**@b@ * Return the same value as <code>{@link Boolean#hashCode()}</code>.@b@ * @see Boolean#hashCode()@b@ */@b@ public static int hashCode(boolean bool) {@b@ return bool ? 1231 : 1237;@b@ }@b@@b@ /**@b@ * Return the same value as <code>{@link Double#hashCode()}</code>.@b@ * @see Double#hashCode()@b@ */@b@ public static int hashCode(double dbl) {@b@ long bits = Double.doubleToLongBits(dbl);@b@ return hashCode(bits);@b@ }@b@@b@ /**@b@ * Return the same value as <code>{@link Float#hashCode()}</code>.@b@ * @see Float#hashCode()@b@ */@b@ public static int hashCode(float flt) {@b@ return Float.floatToIntBits(flt);@b@ }@b@@b@ /**@b@ * Return the same value as <code>{@link Long#hashCode()}</code>.@b@ * @see Long#hashCode()@b@ */@b@ public static int hashCode(long lng) {@b@ return (int) (lng ^ (lng >>> 32));@b@ }@b@@b@ @b@ /**@b@ * Return a String representation of an object's overall identity.@b@ * @param obj the object (may be <code>null</code>)@b@ * @return the object's identity as String representation,@b@ * or an empty String if the object was <code>null</code>@b@ */@b@ public static String identityToString(Object obj) {@b@ if (obj == null) {@b@ return EMPTY_STRING;@b@ }@b@ return obj.getClass().getName() + "@" + getIdentityHexString(obj);@b@ }@b@@b@ /**@b@ * Return a hex String form of an object's identity hash code.@b@ * @param obj the object@b@ * @return the object's identity code in hex notation@b@ */@b@ public static String getIdentityHexString(Object obj) {@b@ return Integer.toHexString(System.identityHashCode(obj));@b@ }@b@@b@ /**@b@ * Return a content-based String representation if <code>obj</code> is@b@ * not <code>null</code>; otherwise returns an empty String.@b@ * <p>Differs from {@link #nullSafeToString(Object)} in that it returns@b@ * an empty String rather than "null" for a <code>null</code> value.@b@ * @param obj the object to build a display String for@b@ * @return a display String representation of <code>obj</code>@b@ * @see #nullSafeToString(Object)@b@ */@b@ public static String getDisplayString(Object obj) {@b@ if (obj == null) {@b@ return EMPTY_STRING;@b@ }@b@ return nullSafeToString(obj);@b@ }@b@@b@ /**@b@ * Determine the class name for the given object.@b@ * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.@b@ * @param obj the object to introspect (may be <code>null</code>)@b@ * @return the corresponding class name@b@ */@b@ public static String nullSafeClassName(Object obj) {@b@ return (obj != null ? obj.getClass().getName() : NULL_STRING);@b@ }@b@@b@ /**@b@ * Return a String representation of the specified Object.@b@ * <p>Builds a String representation of the contents in case of an array.@b@ * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.@b@ * @param obj the object to build a String representation for@b@ * @return a String representation of <code>obj</code>@b@ */@b@ public static String nullSafeToString(Object obj) {@b@ if (obj == null) {@b@ return NULL_STRING;@b@ }@b@ if (obj instanceof String) {@b@ return (String) obj;@b@ }@b@ if (obj instanceof Object[]) {@b@ return nullSafeToString((Object[]) obj);@b@ }@b@ if (obj instanceof boolean[]) {@b@ return nullSafeToString((boolean[]) obj);@b@ }@b@ if (obj instanceof byte[]) {@b@ return nullSafeToString((byte[]) obj);@b@ }@b@ if (obj instanceof char[]) {@b@ return nullSafeToString((char[]) obj);@b@ }@b@ if (obj instanceof double[]) {@b@ return nullSafeToString((double[]) obj);@b@ }@b@ if (obj instanceof float[]) {@b@ return nullSafeToString((float[]) obj);@b@ }@b@ if (obj instanceof int[]) {@b@ return nullSafeToString((int[]) obj);@b@ }@b@ if (obj instanceof long[]) {@b@ return nullSafeToString((long[]) obj);@b@ }@b@ if (obj instanceof short[]) {@b@ return nullSafeToString((short[]) obj);@b@ }@b@ String str = obj.toString();@b@ return (str != null ? str : EMPTY_STRING);@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(Object[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@ sb.append(String.valueOf(array[i]));@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(boolean[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(byte[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(char[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@ sb.append("'").append(array[i]).append("'");@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(double[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(float[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(int[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(long[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@@b@ /**@b@ * Return a String representation of the contents of the specified array.@b@ * <p>The String representation consists of a list of the array's elements,@b@ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated@b@ * by the characters <code>", "</code> (a comma followed by a space). Returns@b@ * <code>"null"</code> if <code>array</code> is <code>null</code>.@b@ * @param array the array to build a String representation for@b@ * @return a String representation of <code>array</code>@b@ */@b@ public static String nullSafeToString(short[] array) {@b@ if (array == null) {@b@ return NULL_STRING;@b@ }@b@ int length = array.length;@b@ if (length == 0) {@b@ return EMPTY_ARRAY;@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; i++) {@b@ if (i == 0) {@b@ sb.append(ARRAY_START);@b@ }@b@ else {@b@ sb.append(ARRAY_ELEMENT_SEPARATOR);@b@ }@b@ sb.append(array[i]);@b@ }@b@ sb.append(ARRAY_END);@b@ return sb.toString();@b@ }@b@ @b@ @b@ /**@b@ * 如果一个对象的封装类型为null时,将其设为简单类型的默认值@b@ * 目的:当数据库中字段的值为null时,我们的Bean中的封装类型被设为null,但是,@b@ * 我们可能要对这些封装类型进行计算,如果一个个的判断它们是否为null,将是一件@b@ * 痛苦的事,这时我们希望当它们为null时,能将它们转换为简单类型的默认值@b@ * 比如Integer i = null ,我们希望它为0@b@ * 转换规则:Integer 0,Float 0F,Double 0D@b@ * <b>转换不包含继承的属性<b/>@b@ * @param o@b@ */@b@ public static void primitiveWrapperToDefaultValue(Object o){@b@ Field[] fields = o.getClass().getDeclaredFields();@b@ for(int j=0;j<fields.length;j++){@b@ Field f = fields[j];@b@ Class type = f.getType();@b@ if(!type.isPrimitive())@b@ try{@b@ String mn = StringUtils.capitalize(f.getName());@b@ Method m = null;@b@ try{@b@ m = o.getClass().getDeclaredMethod("get"+mn);@b@ }catch(Exception e){@b@ m = o.getClass().getDeclaredMethod("is"+mn);@b@ }@b@ if(null == m.invoke(o)){@b@ m = o.getClass().getDeclaredMethod("set"+mn,type);@b@ if(Double.class.isAssignableFrom(type)){@b@ m.invoke(o, 0D);@b@ }else if(Float.class.isAssignableFrom(type)){@b@ m.invoke(o, 0F);@b@ }else if(Integer.class.isAssignableFrom(type)){@b@ m.invoke(o, 0);@b@ }else if(Boolean.class.isAssignableFrom(type)){@b@ m.invoke(o, false);@b@ }else if(Character.class.isAssignableFrom(type)){@b@ m.invoke(o, '\u0000' );@b@ }@b@ }@b@ }catch(Exception e){@b@ throw new RuntimeException(e);@b@ }@b@ }@b@ }@b@ @b@ public static String objToString(Object o){@b@ return objToString(o, 0);@b@ }@b@ @b@ /**将一个Object转换为String@b@ * 如果parseColl为false,则不对集合类型的字段作处理@b@ * @param o 要toString的对象@b@ * @param parseColl 是否处理对象集合属性@b@ */@b@ public static String objToString(Object o, int deep){@b@ if(null == o){@b@ return "null";@b@ }@b@ StringBuilder sb = new StringBuilder();@b@ try{@b@ if(Collection.class.isAssignableFrom(o.getClass())){@b@ Class type = ReflectUtils.getSuperClassGenricType(o.getClass(), 0);@b@ if(ClassUtils.isNotDefinedClass(type) || deep >= 0){@b@ Collection c = (Collection) o;@b@ Object[] oa = c.toArray();@b@ sb.append("Collection:"+c.size()+"[\r\n");@b@ for(int i=0; i<oa.length; i++){@b@ if(i>0){@b@ sb.append(",\r\n");@b@ }@b@ sb.append(objToString(oa[i], deep));@b@ }@b@ sb.append("\r\n]");@b@ }else{@b@ sb.append("Collection:[...]");@b@ }@b@ }else if(o.getClass().isArray()){@b@ int len = Array.getLength(o);@b@ sb.append("Array:"+len+"[\r\n");@b@ for(int i=0; i<len; i++){@b@ if(i>0){@b@ sb.append(",\r\n");@b@ }@b@ sb.append(objToString(Array.get(o, i), deep));@b@ }@b@ sb.append("\r\n]");@b@ }else if(o instanceof java.util.Map){@b@ java.util.Map map = (java.util.Map) o;@b@ java.util.Set set = map.keySet();@b@ sb.append("Map:"+set.size()+"{");@b@ Iterator ite = set.iterator();@b@ int idx = 0;@b@ while(ite.hasNext()){@b@ Object key = ite.next();@b@ if(idx++ > 0)sb.append(",\r\n");@b@ sb.append("\""+key+"\": "+objToString(map.get(key), deep));@b@ }@b@ sb.append("}");@b@ }else if(ClassUtils.isNotDefinedClass(o.getClass())){@b@ sb.append(o.toString());@b@ }else{@b@ if(deep >= 0){@b@ sb.append(o.getClass().getCanonicalName()+"{");@b@ appendFieldValue(sb, o, ReflectUtils.getFields(o.getClass(), false), deep);@b@ sb.append("}");@b@ }else{@b@ sb.append(o.getClass().getCanonicalName()+"{...}");@b@ }@b@ }@b@ }catch(Exception e){@b@ throw new RuntimeException(e);@b@ }@b@ return sb.toString();@b@ }@b@ @b@ /*@b@ * 调用传入的对象的方法得到其返回的值@b@ * @param sb@b@ * @param o@b@ * @param fields@b@ * @param isHandleColl@b@ * @throws Exception@b@ */@b@ private static void appendFieldValue(StringBuilder sb, Object o, Field[] fields, int deep) throws Exception{@b@ AccessibleObject.setAccessible(fields, true);@b@ @b@ boolean first = true;@b@ for(int j=0;j<fields.length;j++){@b@ if(first){first = false;}else{sb.append(",");}@b@ Field f = fields[j];@b@ //跳过final或static修饰的字段@b@ if(java.lang.reflect.Modifier.isFinal(f.getModifiers()) || java.lang.reflect.Modifier.isStatic(f.getModifiers())){@b@ continue;@b@ }@b@ try{@b@ sb.append("\""+f.getName()+"\":"+objToString(f.get(o), deep-1 )+"");@b@ }catch(Exception e){@b@ throw new RuntimeException(e);@b@ }@b@ }@b@ }@b@}