From 93a3b6b251429030ee0393a8483d2a225a7b0db2 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 25 Oct 2009 19:59:31 +0000 Subject: [PATCH] Remove generics as they provide little value and get in the way git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@829635 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/ReflectionToStringBuilder.java | 38 +++--- .../commons/lang/builder/ToStringBuilder.java | 110 +++++++++--------- .../lang/builder/ToStringBuilderTest.java | 23 ++++ 3 files changed, 101 insertions(+), 70 deletions(-) diff --git a/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java index 0658586c7..9ad01a44b 100644 --- a/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java +++ b/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java @@ -95,7 +95,7 @@ * @since 2.0 * @version $Id$ */ -public class ReflectionToStringBuilder extends ToStringBuilder { +public class ReflectionToStringBuilder extends ToStringBuilder { /** *

@@ -284,9 +284,10 @@ public static String toString(Object object, ToStringStyle style, boolean output * if the Object is null * @since 2.1 */ - public static String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, - Class reflectUpToClass) { - return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics) + public static String toString( + T object, ToStringStyle style, boolean outputTransients, + boolean outputStatics, Class reflectUpToClass) { + return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics) .toString(); } @@ -361,8 +362,8 @@ static String[] toNoNullStringArray(Object[] array) { * The field names to exclude * @return The toString value. */ - public static String toStringExclude(T object, String[] excludeFieldNames) { - return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString(); + public static String toStringExclude(Object object, String[] excludeFieldNames) { + return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString(); } /** @@ -383,7 +384,7 @@ public static String toStringExclude(T object, String[] excludeFieldNames) { /** * The last super class to stop appending fields for. */ - private Class upToClass = null; + private Class upToClass = null; /** *

@@ -399,7 +400,7 @@ public static String toStringExclude(T object, String[] excludeFieldNames) { * @throws IllegalArgumentException * if the Object passed in is null */ - public ReflectionToStringBuilder(T object) { + public ReflectionToStringBuilder(Object object) { super(object); } @@ -419,7 +420,7 @@ public ReflectionToStringBuilder(T object) { * @throws IllegalArgumentException * if the Object passed in is null */ - public ReflectionToStringBuilder(T object, ToStringStyle style) { + public ReflectionToStringBuilder(Object object, ToStringStyle style) { super(object, style); } @@ -445,7 +446,7 @@ public ReflectionToStringBuilder(T object, ToStringStyle style) { * @throws IllegalArgumentException * if the Object passed in is null */ - public ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer) { + public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { super(object, style, buffer); } @@ -466,8 +467,9 @@ public ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buf * whether to include static fields * @since 2.1 */ - public ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class reflectUpToClass, - boolean outputTransients, boolean outputStatics) { + public ReflectionToStringBuilder( + T object, ToStringStyle style, StringBuffer buffer, + Class reflectUpToClass, boolean outputTransients, boolean outputStatics) { super(object, style, buffer); this.setUpToClass(reflectUpToClass); this.setAppendTransients(outputTransients); @@ -616,7 +618,7 @@ public boolean isAppendTransients() { * the array to add to the toString * @return this */ - public ToStringBuilder reflectionAppendArray(Object array) { + public ReflectionToStringBuilder reflectionAppendArray(Object array) { this.getStyle().reflectionAppendArrayDetail(this.getStringBuffer(), null, array); return this; } @@ -653,7 +655,7 @@ public void setAppendTransients(boolean appendTransients) { * The excludeFieldNames to excluding from toString or null. * @return this */ - public ReflectionToStringBuilder setExcludeFieldNames(String[] excludeFieldNamesParam) { + public ReflectionToStringBuilder setExcludeFieldNames(String[] excludeFieldNamesParam) { if (excludeFieldNamesParam == null) { this.excludeFieldNames = null; } else { @@ -671,7 +673,13 @@ public ReflectionToStringBuilder setExcludeFieldNames(String[] excludeFieldNa * @param clazz * The last super class to stop appending fields for. */ - public void setUpToClass(Class clazz) { + public void setUpToClass(Class clazz) { + if (clazz != null) { + Object object = getObject(); + if (object != null && clazz.isInstance(object) == false) { + throw new IllegalArgumentException("Specified class is not a superclass of the object"); + } + } this.upToClass = clazz; } diff --git a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java index ceb957b08..ee2abd43a 100644 --- a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java +++ b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java @@ -89,7 +89,7 @@ * @since 1.0 * @version $Id$ */ -public class ToStringBuilder { +public class ToStringBuilder { /** * The default style of output to use. @@ -190,7 +190,7 @@ public static void setDefaultStyle(ToStringStyle style) { /** * The object being output. */ - private final T object; + private final Object object; /** * The style of output to use. @@ -207,7 +207,7 @@ public static void setDefaultStyle(ToStringStyle style) { * @throws IllegalArgumentException if the Object passed in is * null */ - public ToStringBuilder(T object) { + public ToStringBuilder(Object object) { this(object, getDefaultStyle(), null); } @@ -223,7 +223,7 @@ public ToStringBuilder(T object) { * @throws IllegalArgumentException if the Object passed in is * null */ - public ToStringBuilder(T object, ToStringStyle style) { + public ToStringBuilder(Object object, ToStringStyle style) { this(object, style, null); } @@ -240,7 +240,7 @@ public ToStringBuilder(T object, ToStringStyle style) { * @param buffer the StringBuffer to populate, may be * null */ - public ToStringBuilder(T object, ToStringStyle style, StringBuffer buffer) { + public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { if (style == null) { style = getDefaultStyle(); } @@ -263,7 +263,7 @@ public ToStringBuilder(T object, ToStringStyle style, StringBuffer buffer) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(boolean value) { + public ToStringBuilder append(boolean value) { style.append(buffer, null, value); return this; } @@ -277,7 +277,7 @@ public ToStringBuilder append(boolean value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(boolean[] array) { + public ToStringBuilder append(boolean[] array) { style.append(buffer, null, array, null); return this; } @@ -291,7 +291,7 @@ public ToStringBuilder append(boolean[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(byte value) { + public ToStringBuilder append(byte value) { style.append(buffer, null, value); return this; } @@ -305,7 +305,7 @@ public ToStringBuilder append(byte value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(byte[] array) { + public ToStringBuilder append(byte[] array) { style.append(buffer, null, array, null); return this; } @@ -319,7 +319,7 @@ public ToStringBuilder append(byte[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(char value) { + public ToStringBuilder append(char value) { style.append(buffer, null, value); return this; } @@ -333,7 +333,7 @@ public ToStringBuilder append(char value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(char[] array) { + public ToStringBuilder append(char[] array) { style.append(buffer, null, array, null); return this; } @@ -347,7 +347,7 @@ public ToStringBuilder append(char[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(double value) { + public ToStringBuilder append(double value) { style.append(buffer, null, value); return this; } @@ -361,7 +361,7 @@ public ToStringBuilder append(double value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(double[] array) { + public ToStringBuilder append(double[] array) { style.append(buffer, null, array, null); return this; } @@ -375,7 +375,7 @@ public ToStringBuilder append(double[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(float value) { + public ToStringBuilder append(float value) { style.append(buffer, null, value); return this; } @@ -389,7 +389,7 @@ public ToStringBuilder append(float value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(float[] array) { + public ToStringBuilder append(float[] array) { style.append(buffer, null, array, null); return this; } @@ -403,7 +403,7 @@ public ToStringBuilder append(float[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(int value) { + public ToStringBuilder append(int value) { style.append(buffer, null, value); return this; } @@ -417,7 +417,7 @@ public ToStringBuilder append(int value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(int[] array) { + public ToStringBuilder append(int[] array) { style.append(buffer, null, array, null); return this; } @@ -431,7 +431,7 @@ public ToStringBuilder append(int[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(long value) { + public ToStringBuilder append(long value) { style.append(buffer, null, value); return this; } @@ -445,7 +445,7 @@ public ToStringBuilder append(long value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(long[] array) { + public ToStringBuilder append(long[] array) { style.append(buffer, null, array, null); return this; } @@ -459,7 +459,7 @@ public ToStringBuilder append(long[] array) { * @param obj the value to add to the toString * @return this */ - public ToStringBuilder append(Object obj) { + public ToStringBuilder append(Object obj) { style.append(buffer, null, obj, null); return this; } @@ -473,7 +473,7 @@ public ToStringBuilder append(Object obj) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(Object[] array) { + public ToStringBuilder append(Object[] array) { style.append(buffer, null, array, null); return this; } @@ -487,7 +487,7 @@ public ToStringBuilder append(Object[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(short value) { + public ToStringBuilder append(short value) { style.append(buffer, null, value); return this; } @@ -501,7 +501,7 @@ public ToStringBuilder append(short value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(short[] array) { + public ToStringBuilder append(short[] array) { style.append(buffer, null, array, null); return this; } @@ -514,7 +514,7 @@ public ToStringBuilder append(short[] array) { * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, boolean value) { + public ToStringBuilder append(String fieldName, boolean value) { style.append(buffer, fieldName, value); return this; } @@ -527,7 +527,7 @@ public ToStringBuilder append(String fieldName, boolean value) { * @param array the array to add to the hashCode * @return this */ - public ToStringBuilder append(String fieldName, boolean[] array) { + public ToStringBuilder append(String fieldName, boolean[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -547,7 +547,7 @@ public ToStringBuilder append(String fieldName, boolean[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -560,7 +560,7 @@ public ToStringBuilder append(String fieldName, boolean[] array, boolean full * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, byte value) { + public ToStringBuilder append(String fieldName, byte value) { style.append(buffer, fieldName, value); return this; } @@ -572,7 +572,7 @@ public ToStringBuilder append(String fieldName, byte value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, byte[] array) { + public ToStringBuilder append(String fieldName, byte[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -592,7 +592,7 @@ public ToStringBuilder append(String fieldName, byte[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -605,7 +605,7 @@ public ToStringBuilder append(String fieldName, byte[] array, boolean fullDet * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, char value) { + public ToStringBuilder append(String fieldName, char value) { style.append(buffer, fieldName, value); return this; } @@ -618,7 +618,7 @@ public ToStringBuilder append(String fieldName, char value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, char[] array) { + public ToStringBuilder append(String fieldName, char[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -638,7 +638,7 @@ public ToStringBuilder append(String fieldName, char[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -651,7 +651,7 @@ public ToStringBuilder append(String fieldName, char[] array, boolean fullDet * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, double value) { + public ToStringBuilder append(String fieldName, double value) { style.append(buffer, fieldName, value); return this; } @@ -664,7 +664,7 @@ public ToStringBuilder append(String fieldName, double value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, double[] array) { + public ToStringBuilder append(String fieldName, double[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -684,7 +684,7 @@ public ToStringBuilder append(String fieldName, double[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -697,7 +697,7 @@ public ToStringBuilder append(String fieldName, double[] array, boolean fullD * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, float value) { + public ToStringBuilder append(String fieldName, float value) { style.append(buffer, fieldName, value); return this; } @@ -710,7 +710,7 @@ public ToStringBuilder append(String fieldName, float value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, float[] array) { + public ToStringBuilder append(String fieldName, float[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -730,7 +730,7 @@ public ToStringBuilder append(String fieldName, float[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -743,7 +743,7 @@ public ToStringBuilder append(String fieldName, float[] array, boolean fullDe * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, int value) { + public ToStringBuilder append(String fieldName, int value) { style.append(buffer, fieldName, value); return this; } @@ -756,7 +756,7 @@ public ToStringBuilder append(String fieldName, int value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, int[] array) { + public ToStringBuilder append(String fieldName, int[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -776,7 +776,7 @@ public ToStringBuilder append(String fieldName, int[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -789,7 +789,7 @@ public ToStringBuilder append(String fieldName, int[] array, boolean fullDeta * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, long value) { + public ToStringBuilder append(String fieldName, long value) { style.append(buffer, fieldName, value); return this; } @@ -802,7 +802,7 @@ public ToStringBuilder append(String fieldName, long value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, long[] array) { + public ToStringBuilder append(String fieldName, long[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -822,7 +822,7 @@ public ToStringBuilder append(String fieldName, long[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -835,7 +835,7 @@ public ToStringBuilder append(String fieldName, long[] array, boolean fullDet * @param obj the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, Object obj) { + public ToStringBuilder append(String fieldName, Object obj) { style.append(buffer, fieldName, obj, null); return this; } @@ -850,7 +850,7 @@ public ToStringBuilder append(String fieldName, Object obj) { * false for summary info * @return this */ - public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail) { + public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail) { style.append(buffer, fieldName, obj, Boolean.valueOf(fullDetail)); return this; } @@ -863,7 +863,7 @@ public ToStringBuilder append(String fieldName, Object obj, boolean fullDetai * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, Object[] array) { + public ToStringBuilder append(String fieldName, Object[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -883,7 +883,7 @@ public ToStringBuilder append(String fieldName, Object[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -896,7 +896,7 @@ public ToStringBuilder append(String fieldName, Object[] array, boolean fullD * @param value the value to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, short value) { + public ToStringBuilder append(String fieldName, short value) { style.append(buffer, fieldName, value); return this; } @@ -909,7 +909,7 @@ public ToStringBuilder append(String fieldName, short value) { * @param array the array to add to the toString * @return this */ - public ToStringBuilder append(String fieldName, short[] array) { + public ToStringBuilder append(String fieldName, short[] array) { style.append(buffer, fieldName, array, null); return this; } @@ -929,7 +929,7 @@ public ToStringBuilder append(String fieldName, short[] array) { * for summary info * @return this */ - public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) { + public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); return this; } @@ -943,7 +943,7 @@ public ToStringBuilder append(String fieldName, short[] array, boolean fullDe * @return this * @since 2.0 */ - public ToStringBuilder appendAsObjectToString(Object object) { + public ToStringBuilder appendAsObjectToString(Object object) { ObjectUtils.identityToString(this.getStringBuffer(), object); return this; } @@ -962,7 +962,7 @@ public ToStringBuilder appendAsObjectToString(Object object) { * @return this * @since 2.0 */ - public ToStringBuilder appendSuper(String superToString) { + public ToStringBuilder appendSuper(String superToString) { if (superToString != null) { style.appendSuper(buffer, superToString); } @@ -996,7 +996,7 @@ public ToStringBuilder appendSuper(String superToString) { * @return this * @since 2.0 */ - public ToStringBuilder appendToString(String toString) { + public ToStringBuilder appendToString(String toString) { if (toString != null) { style.appendToString(buffer, toString); } @@ -1009,7 +1009,7 @@ public ToStringBuilder appendToString(String toString) { * @return The object being output. * @since 2.0 */ - public T getObject() { + public Object getObject() { return object; } diff --git a/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java b/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java index a771e0a87..57963bb3f 100644 --- a/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java +++ b/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java @@ -953,6 +953,29 @@ public Object toStringWithStatics(Object object, ToStringStyle style, Class refl return ReflectionToStringBuilder.toString(object, style, false, true, reflectUpToClass); } + /** + * Tests ReflectionToStringBuilder setUpToClass(). + */ + public void test_setUpToClass_valid() { + Integer val = new Integer(5); + ReflectionToStringBuilder test = new ReflectionToStringBuilder(val); + test.setUpToClass(Number.class); + } + + /** + * Tests ReflectionToStringBuilder setUpToClass(). + */ + public void test_setUpToClass_invalid() { + Integer val = new Integer(5); + ReflectionToStringBuilder test = new ReflectionToStringBuilder(val); + try { + test.setUpToClass(String.class); + fail(); + } catch (IllegalArgumentException ex) { + // expected + } + } + /** * Tests ReflectionToStringBuilder.toString() for statics. */