一、前言
基于springframework框架org.springframework.util.ObjectUtils对象处理工具类,可以对象数组间判断isArray、空判断isEmpty、添加对象至数组addObjectToArray、对象数组转换toObjectArray、数据类型是否相等nullSafeEquals等等。
二、源码说明package org.springframework.util;@b@@b@import java.lang.reflect.Array;@b@import java.util.Arrays;@b@@b@public abstract class ObjectUtils@b@{@b@ private static final int INITIAL_HASH = 7;@b@ private static final int MULTIPLIER = 31;@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 = "{}";@b@ private static final String ARRAY_ELEMENT_SEPARATOR = ", ";@b@@b@ public static boolean isCheckedException(Throwable ex)@b@ {@b@ return ((!(ex instanceof RuntimeException)) && (!(ex instanceof Error)));@b@ }@b@@b@ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class>[] declaredExceptions)@b@ {@b@ Class[] arrayOfClass;@b@ int j;@b@ if (!(isCheckedException(ex)))@b@ return true;@b@@b@ if (declaredExceptions != null) {@b@ arrayOfClass = declaredExceptions; int i = arrayOfClass.length; for (j = 0; j [] enumValues, String constant)@b@ {@b@ return containsConstant(enumValues, constant, false);@b@ }@b@@b@ public static boolean containsConstant(Enum>[] enumValues, String constant, boolean caseSensitive)@b@ {@b@ Enum[] arrayOfEnum = enumValues; int i = arrayOfEnum.length; for (int j = 0; j > E caseInsensitiveValueOf(E[] enumValues, String constant)@b@ {@b@ Enum[] arrayOfEnum = enumValues; int i = arrayOfEnum.length; for (int j = 0; j A[] addObjectToArray(A[] array, O obj)@b@ {@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@@b@ Object[] newArr = (Object[])(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@ public static Object[] toObjectArray(Object source)@b@ {@b@ if (source instanceof Object[])@b@ return ((Object[])(Object[])source);@b@@b@ if (source == null)@b@ return new Object[0];@b@@b@ if (!(source.getClass().isArray()))@b@ throw new IllegalArgumentException(new StringBuilder().append("Source is not an array: ").append(source).toString());@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[])(Object[])Array.newInstance(wrapperType, length);@b@ for (int i = 0; i >> 32);@b@ }@b@@b@ public static String identityToString(Object obj)@b@ {@b@ if (obj == null)@b@ return "";@b@@b@ return new StringBuilder().append(obj.getClass().getName()).append("@").append(getIdentityHexString(obj)).toString();@b@ }@b@@b@ public static String getIdentityHexString(Object obj)@b@ {@b@ return Integer.toHexString(System.identityHashCode(obj));@b@ }@b@@b@ public static String getDisplayString(Object obj)@b@ {@b@ if (obj == null)@b@ return "";@b@@b@ return nullSafeToString(obj);@b@ }@b@@b@ public static String nullSafeClassName(Object obj)@b@ {@b@ return ((obj != null) ? obj.getClass().getName() : "null");@b@ }@b@@b@ public static String nullSafeToString(Object obj)@b@ {@b@ if (obj == null)@b@ return "null";@b@@b@ if (obj instanceof String)@b@ return ((String)obj);@b@@b@ if (obj instanceof Object[])@b@ return nullSafeToString((Object[])(Object[])obj);@b@@b@ if (obj instanceof boolean[])@b@ return nullSafeToString((boolean[])(boolean[])obj);@b@@b@ if (obj instanceof byte[])@b@ return nullSafeToString((byte[])(byte[])obj);@b@@b@ if (obj instanceof char[])@b@ return nullSafeToString((char[])(char[])obj);@b@@b@ if (obj instanceof double[])@b@ return nullSafeToString((double[])(double[])obj);@b@@b@ if (obj instanceof float[])@b@ return nullSafeToString((float[])(float[])obj);@b@@b@ if (obj instanceof int[])@b@ return nullSafeToString((int[])(int[])obj);@b@@b@ if (obj instanceof long[])@b@ return nullSafeToString((long[])(long[])obj);@b@@b@ if (obj instanceof short[])@b@ return nullSafeToString((short[])(short[])obj);@b@@b@ String str = obj.toString();@b@ return ((str != null) ? str : "");@b@ }@b@@b@ public static String nullSafeToString(Object[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i