[lang] ReflectionToStringBuilder.toString(null) throws exception by design
ReflectionToStringBuilder.toString is now null-safe and returns the style's nullText.
ToStringBuilder constructors are now null-safe. A new ToStringBuilder on a null followed by a call to toString returns "".


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137857 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2004-06-30 18:22:22 +00:00
parent 6977758a3a
commit be6e4802f4
3 changed files with 28 additions and 54 deletions

View File

@ -87,7 +87,7 @@
* @author Gary Gregory
* @author Pete Gieser
* @since 1.0
* @version $Id: ToStringBuilder.java,v 1.32 2004/02/18 22:53:24 ggregory Exp $
* @version $Id: ToStringBuilder.java,v 1.33 2004/06/30 18:21:49 ggregory Exp $
*/
public class ToStringBuilder {
@ -144,7 +144,7 @@ public static String reflectionToString(Object object, ToStringStyle style, bool
/**
* <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
*
* @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,Class)
* @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)
* @since 2.0
*/
public static String reflectionToString(
@ -228,14 +228,8 @@ public ToStringBuilder(Object object, ToStringStyle style) {
* may be <code>null</code>
* @param buffer the <code>StringBuffer</code> to populate, may be
* <code>null</code>
* @throws IllegalArgumentException if the Object passed in is
* <code>null</code>
*/
public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) {
super();
if (object == null) {
throw new IllegalArgumentException("The object to create a toString for must not be null");
}
if (style == null) {
style = getDefaultStyle();
}
@ -1038,8 +1032,8 @@ public ToStringStyle getStyle() {
* @return the String <code>toString</code>
*/
public String toString() {
style.appendEnd(buffer, object);
return buffer.toString();
style.appendEnd(this.getStringBuffer(), this.getObject());
return this.getStringBuffer().toString();
}
}

View File

@ -48,7 +48,7 @@
* @author Gary Gregory
* @author Pete Gieser
* @since 1.0
* @version $Id: ToStringStyle.java,v 1.29 2004/02/18 22:53:24 ggregory Exp $
* @version $Id: ToStringStyle.java,v 1.30 2004/06/30 18:21:49 ggregory Exp $
*/
public abstract class ToStringStyle implements Serializable {
@ -234,11 +234,13 @@ public void appendToString(StringBuffer buffer, String toString) {
* <code>toString</code> for, must not be <code>null</code>
*/
public void appendStart(StringBuffer buffer, Object object) {
appendClassName(buffer, object);
appendIdentityHashCode(buffer, object);
appendContentStart(buffer);
if (fieldSeparatorAtStart) {
appendFieldSeparator(buffer);
if (object != null) {
appendClassName(buffer, object);
appendIdentityHashCode(buffer, object);
appendContentStart(buffer);
if (fieldSeparatorAtStart) {
appendFieldSeparator(buffer);
}
}
}
@ -247,9 +249,12 @@ public void appendStart(StringBuffer buffer, Object object) {
*
* @param buffer the <code>StringBuffer</code> to populate
* @param object the <code>Object</code> to build a
* <code>toString</code> for, must not be <code>null</code>
* <code>toString</code> for.
*/
public void appendEnd(StringBuffer buffer, Object object) {
if (object == null){
return;
}
if (fieldSeparatorAtEnd == false) {
removeLastFieldSeparator(buffer);
}
@ -1282,7 +1287,7 @@ protected void appendSummary(StringBuffer buffer, String fieldName, boolean[] ar
* @param object the <code>Object</code> whose name to output
*/
protected void appendClassName(StringBuffer buffer, Object object) {
if (useClassName) {
if (useClassName && object != null) {
if (useShortClassName) {
buffer.append(getShortClassName(object.getClass()));
} else {
@ -1298,7 +1303,7 @@ protected void appendClassName(StringBuffer buffer, Object object) {
* @param object the <code>Object</code> whose id to output
*/
protected void appendIdentityHashCode(StringBuffer buffer, Object object) {
if (useIdentityHashCode) {
if (this.isUseIdentityHashCode() && object!=null) {
buffer.append('@');
buffer.append(Integer.toHexString(System.identityHashCode(object)));
}

View File

@ -30,7 +30,7 @@
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author <a href="mailto:alex@apache.org">Alex Chaffee</a>
* @version $Id: ToStringBuilderTest.java,v 1.13 2004/02/18 23:00:51 ggregory Exp $
* @version $Id: ToStringBuilderTest.java,v 1.14 2004/06/30 18:22:22 ggregory Exp $
*/
public class ToStringBuilderTest extends TestCase {
@ -62,46 +62,18 @@ protected void tearDown() throws Exception {
//-----------------------------------------------------------------------
public void testConstructorEx1() {
try {
new ToStringBuilder(null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
assertEquals("", new ToStringBuilder(null).toString());
}
public void testConstructorEx2() {
try {
new ToStringBuilder(null, null);
} catch (IllegalArgumentException ex) {
try {
new ToStringBuilder(base, null);
} catch (Exception ex2) {
fail();
}
return;
}
fail();
assertEquals("", new ToStringBuilder(null, null).toString());
new ToStringBuilder(this.base, null).toString();
}
public void testConstructorEx3() {
try {
new ToStringBuilder(null, null, null);
} catch (IllegalArgumentException ex) {
try {
new ToStringBuilder(base, null, null);
new ToStringBuilder(base, ToStringStyle.DEFAULT_STYLE, null);
} catch (Exception ex2) {
fail();
}
return;
}
fail();
assertEquals("", new ToStringBuilder(null, null, null).toString());
new ToStringBuilder(this.base, null, null);
new ToStringBuilder(this.base, ToStringStyle.DEFAULT_STYLE, null);
}
public void testGetSetDefault() {
@ -915,4 +887,7 @@ class InheritedReflectionStaticFieldsFixture extends SimpleReflectionStaticField
static final int staticInt2 = 67890;
}
public void testReflectionNull() {
assertEquals("<null>", ReflectionToStringBuilder.toString(null));
}
}