java 打印对象属性 工具类_关于java实现任意对象输出字符串的工具类ObjectUtils用户打印日志、接口调试及监控等...
通過該對象工具類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?null?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?null,@b@?*?in?which?case?the?return?value?will?always?be?false)@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?null)@b@?*?@param?obj?the?Object?to?append@b@?*?@return?the?new?array?(of?the?same?component?type;?never?null)@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@?*?
A?null?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?null)@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?true@b@?*?if?both?are?null?or?false?if?only?one?is@b@?*?null.@b@?*?
Compares?arrays?with?Arrays.equals,?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@?*?{@link?Object#hashCode()}.?If?the?object?is?an?array,@b@?*?this?method?will?delegate?to?any?of?the?nullSafeHashCode@b@?*?methods?for?arrays?in?this?class.?If?the?object?is?null,@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?array?is?null,?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?array?is?null,?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?array?is?null,?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?array?is?null,?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?array?is?null,?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?array?is?null,?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?array?is?null,?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?array?is?null,?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?array?is?null,?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?{@link?Boolean#hashCode()}.@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?{@link?Double#hashCode()}.@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?{@link?Float#hashCode()}.@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?{@link?Long#hashCode()}.@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?null)@b@?*?@return?the?object's?identity?as?String?representation,@b@?*?or?an?empty?String?if?the?object?was?null@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?obj?is@b@?*?not?null;?otherwise?returns?an?empty?String.@b@?*?
Differs?from?{@link?#nullSafeToString(Object)}?in?that?it?returns@b@?*?an?empty?String?rather?than?"null"?for?a?null?value.@b@?*?@param?obj?the?object?to?build?a?display?String?for@b@?*?@return?a?display?String?representation?of?obj@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@?*?
Returns?"null"?if?obj?is?null.@b@?*?@param?obj?the?object?to?introspect?(may?be?null)@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@?*?
Builds?a?String?representation?of?the?contents?in?case?of?an?array.@b@?*?Returns?"null"?if?obj?is?null.@b@?*?@param?obj?the?object?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?obj@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@?*?
The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?The?String?representation?consists?of?a?list?of?the?array's?elements,@b@?*?enclosed?in?curly?braces?("{}").?Adjacent?elements?are?separated@b@?*?by?the?characters?",?"?(a?comma?followed?by?a?space).?Returns@b@?*?"null"?if?array?is?null.@b@?*?@param?array?the?array?to?build?a?String?representation?for@b@?*?@return?a?String?representation?of?array@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?轉換不包含繼承的屬性@b@?*?@param?o@b@?*/@b@public?static?void?primitiveWrapperToDefaultValue(Object?o){@b@Field[]?fields?=?o.getClass().getDeclaredFields();@b@for(int?j=0;j=?0){@b@Collection?c?=?(Collection)?o;@b@Object[]?oa?=?c.toArray();@b@sb.append("Collection:"+c.size()+"[\r\n");@b@for(int?i=0;?i0){@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;?i0){@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
總結
以上是生活随笔為你收集整理的java 打印对象属性 工具类_关于java实现任意对象输出字符串的工具类ObjectUtils用户打印日志、接口调试及监控等...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小产权房再建途中被政府叫停,尾因是违章建
- 下一篇: 租的商铺,商场不配合装修怎么投诉