You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sc...@apache.org on 2009/10/25 20:59:32 UTC
svn commit: r829635 - in /commons/proper/lang/trunk/src:
java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
java/org/apache/commons/lang/builder/ToStringBuilder.java
test/org/apache/commons/lang/builder/ToStringBuilderTest.java
Author: scolebourne
Date: Sun Oct 25 19:59:31 2009
New Revision: 829635
URL: http://svn.apache.org/viewvc?rev=829635&view=rev
Log:
Remove generics as they provide little value and get in the way
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java?rev=829635&r1=829634&r2=829635&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java Sun Oct 25 19:59:31 2009
@@ -95,7 +95,7 @@
* @since 2.0
* @version $Id$
*/
-public class ReflectionToStringBuilder<T> extends ToStringBuilder<T> {
+public class ReflectionToStringBuilder extends ToStringBuilder {
/**
* <p>
@@ -284,9 +284,10 @@
* if the Object is <code>null</code>
* @since 2.1
*/
- public static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics,
- Class<? super T> reflectUpToClass) {
- return new ReflectionToStringBuilder<T>(object, style, null, reflectUpToClass, outputTransients, outputStatics)
+ public static <T> String toString(
+ T object, ToStringStyle style, boolean outputTransients,
+ boolean outputStatics, Class<? super T> reflectUpToClass) {
+ return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics)
.toString();
}
@@ -361,8 +362,8 @@
* The field names to exclude
* @return The toString value.
*/
- public static <T> String toStringExclude(T object, String[] excludeFieldNames) {
- return new ReflectionToStringBuilder<T>(object).setExcludeFieldNames(excludeFieldNames).toString();
+ public static String toStringExclude(Object object, String[] excludeFieldNames) {
+ return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString();
}
/**
@@ -383,7 +384,7 @@
/**
* The last super class to stop appending fields for.
*/
- private Class<? super T> upToClass = null;
+ private Class<?> upToClass = null;
/**
* <p>
@@ -399,7 +400,7 @@
* @throws IllegalArgumentException
* if the Object passed in is <code>null</code>
*/
- public ReflectionToStringBuilder(T object) {
+ public ReflectionToStringBuilder(Object object) {
super(object);
}
@@ -419,7 +420,7 @@
* @throws IllegalArgumentException
* if the Object passed in is <code>null</code>
*/
- public ReflectionToStringBuilder(T object, ToStringStyle style) {
+ public ReflectionToStringBuilder(Object object, ToStringStyle style) {
super(object, style);
}
@@ -445,7 +446,7 @@
* @throws IllegalArgumentException
* if the Object passed in is <code>null</code>
*/
- public ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer) {
+ public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) {
super(object, style, buffer);
}
@@ -466,8 +467,9 @@
* whether to include static fields
* @since 2.1
*/
- public ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class<? super T> reflectUpToClass,
- boolean outputTransients, boolean outputStatics) {
+ public <T> ReflectionToStringBuilder(
+ T object, ToStringStyle style, StringBuffer buffer,
+ Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics) {
super(object, style, buffer);
this.setUpToClass(reflectUpToClass);
this.setAppendTransients(outputTransients);
@@ -616,7 +618,7 @@
* the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> reflectionAppendArray(Object array) {
+ public ReflectionToStringBuilder reflectionAppendArray(Object array) {
this.getStyle().reflectionAppendArrayDetail(this.getStringBuffer(), null, array);
return this;
}
@@ -653,7 +655,7 @@
* The excludeFieldNames to excluding from toString or <code>null</code>.
* @return <code>this</code>
*/
- public ReflectionToStringBuilder<T> setExcludeFieldNames(String[] excludeFieldNamesParam) {
+ public ReflectionToStringBuilder setExcludeFieldNames(String[] excludeFieldNamesParam) {
if (excludeFieldNamesParam == null) {
this.excludeFieldNames = null;
} else {
@@ -671,7 +673,13 @@
* @param clazz
* The last super class to stop appending fields for.
*/
- public void setUpToClass(Class<? super T> 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;
}
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ToStringBuilder.java?rev=829635&r1=829634&r2=829635&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ToStringBuilder.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ToStringBuilder.java Sun Oct 25 19:59:31 2009
@@ -89,7 +89,7 @@
* @since 1.0
* @version $Id$
*/
-public class ToStringBuilder<T> {
+public class ToStringBuilder {
/**
* The default style of output to use.
@@ -190,7 +190,7 @@
/**
* The object being output.
*/
- private final T object;
+ private final Object object;
/**
* The style of output to use.
@@ -207,7 +207,7 @@
* @throws IllegalArgumentException if the Object passed in is
* <code>null</code>
*/
- public ToStringBuilder(T object) {
+ public ToStringBuilder(Object object) {
this(object, getDefaultStyle(), null);
}
@@ -223,7 +223,7 @@
* @throws IllegalArgumentException if the Object passed in is
* <code>null</code>
*/
- public ToStringBuilder(T object, ToStringStyle style) {
+ public ToStringBuilder(Object object, ToStringStyle style) {
this(object, style, null);
}
@@ -240,7 +240,7 @@
* @param buffer the <code>StringBuffer</code> to populate, may be
* <code>null</code>
*/
- 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(boolean value) {
+ public ToStringBuilder append(boolean value) {
style.append(buffer, null, value);
return this;
}
@@ -277,7 +277,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(boolean[] array) {
+ public ToStringBuilder append(boolean[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -291,7 +291,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(byte value) {
+ public ToStringBuilder append(byte value) {
style.append(buffer, null, value);
return this;
}
@@ -305,7 +305,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(byte[] array) {
+ public ToStringBuilder append(byte[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -319,7 +319,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(char value) {
+ public ToStringBuilder append(char value) {
style.append(buffer, null, value);
return this;
}
@@ -333,7 +333,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(char[] array) {
+ public ToStringBuilder append(char[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -347,7 +347,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(double value) {
+ public ToStringBuilder append(double value) {
style.append(buffer, null, value);
return this;
}
@@ -361,7 +361,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(double[] array) {
+ public ToStringBuilder append(double[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -375,7 +375,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(float value) {
+ public ToStringBuilder append(float value) {
style.append(buffer, null, value);
return this;
}
@@ -389,7 +389,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(float[] array) {
+ public ToStringBuilder append(float[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -403,7 +403,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(int value) {
+ public ToStringBuilder append(int value) {
style.append(buffer, null, value);
return this;
}
@@ -417,7 +417,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(int[] array) {
+ public ToStringBuilder append(int[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -431,7 +431,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(long value) {
+ public ToStringBuilder append(long value) {
style.append(buffer, null, value);
return this;
}
@@ -445,7 +445,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(long[] array) {
+ public ToStringBuilder append(long[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -459,7 +459,7 @@
* @param obj the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(Object obj) {
+ public ToStringBuilder append(Object obj) {
style.append(buffer, null, obj, null);
return this;
}
@@ -473,7 +473,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(Object[] array) {
+ public ToStringBuilder append(Object[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -487,7 +487,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(short value) {
+ public ToStringBuilder append(short value) {
style.append(buffer, null, value);
return this;
}
@@ -501,7 +501,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(short[] array) {
+ public ToStringBuilder append(short[] array) {
style.append(buffer, null, array, null);
return this;
}
@@ -514,7 +514,7 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, boolean value) {
+ public ToStringBuilder append(String fieldName, boolean value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -527,7 +527,7 @@
* @param array the array to add to the <code>hashCode</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, boolean[] array) {
+ public ToStringBuilder append(String fieldName, boolean[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -547,7 +547,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, byte value) {
+ public ToStringBuilder append(String fieldName, byte value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -572,7 +572,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, byte[] array) {
+ public ToStringBuilder append(String fieldName, byte[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -592,7 +592,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, char value) {
+ public ToStringBuilder append(String fieldName, char value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -618,7 +618,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, char[] array) {
+ public ToStringBuilder append(String fieldName, char[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -638,7 +638,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, double value) {
+ public ToStringBuilder append(String fieldName, double value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -664,7 +664,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, double[] array) {
+ public ToStringBuilder append(String fieldName, double[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -684,7 +684,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, float value) {
+ public ToStringBuilder append(String fieldName, float value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -710,7 +710,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, float[] array) {
+ public ToStringBuilder append(String fieldName, float[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -730,7 +730,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, int value) {
+ public ToStringBuilder append(String fieldName, int value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -756,7 +756,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, int[] array) {
+ public ToStringBuilder append(String fieldName, int[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -776,7 +776,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, long value) {
+ public ToStringBuilder append(String fieldName, long value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -802,7 +802,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, long[] array) {
+ public ToStringBuilder append(String fieldName, long[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -822,7 +822,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param obj the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, Object obj) {
+ public ToStringBuilder append(String fieldName, Object obj) {
style.append(buffer, fieldName, obj, null);
return this;
}
@@ -850,7 +850,7 @@
* <code>false</code> for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, Object[] array) {
+ public ToStringBuilder append(String fieldName, Object[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -883,7 +883,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @param value the value to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, short value) {
+ public ToStringBuilder append(String fieldName, short value) {
style.append(buffer, fieldName, value);
return this;
}
@@ -909,7 +909,7 @@
* @param array the array to add to the <code>toString</code>
* @return this
*/
- public ToStringBuilder<T> append(String fieldName, short[] array) {
+ public ToStringBuilder append(String fieldName, short[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
@@ -929,7 +929,7 @@
* for summary info
* @return this
*/
- public ToStringBuilder<T> 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 @@
* @return this
* @since 2.0
*/
- public ToStringBuilder<T> appendAsObjectToString(Object object) {
+ public ToStringBuilder appendAsObjectToString(Object object) {
ObjectUtils.identityToString(this.getStringBuffer(), object);
return this;
}
@@ -962,7 +962,7 @@
* @return this
* @since 2.0
*/
- public ToStringBuilder<T> appendSuper(String superToString) {
+ public ToStringBuilder appendSuper(String superToString) {
if (superToString != null) {
style.appendSuper(buffer, superToString);
}
@@ -996,7 +996,7 @@
* @return this
* @since 2.0
*/
- public ToStringBuilder<T> appendToString(String toString) {
+ public ToStringBuilder appendToString(String toString) {
if (toString != null) {
style.appendToString(buffer, toString);
}
@@ -1009,7 +1009,7 @@
* @return The object being output.
* @since 2.0
*/
- public T getObject() {
+ public Object getObject() {
return object;
}
Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java?rev=829635&r1=829634&r2=829635&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/ToStringBuilderTest.java Sun Oct 25 19:59:31 2009
@@ -954,6 +954,29 @@
}
/**
+ * 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.
*/
class ReflectionStaticFieldsFixture {