From 659b20c0375b574a5d2b9e14f66c975405bfee42 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 12 Nov 2007 22:54:02 +0000 Subject: [PATCH] Applying my second patch from LANG-360 - it seems to do what Stephane/Paul and I are consensing on git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@594336 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ObjectUtils.java | 29 ++++++++++++++++++- .../apache/commons/lang/ObjectUtilsTest.java | 19 ++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/lang/ObjectUtils.java b/src/java/org/apache/commons/lang/ObjectUtils.java index 562a10a15..95adcdfb2 100644 --- a/src/java/org/apache/commons/lang/ObjectUtils.java +++ b/src/java/org/apache/commons/lang/ObjectUtils.java @@ -157,7 +157,33 @@ public class ObjectUtils { if (object == null) { return null; } - return appendIdentityToString(null, object).toString(); + StringBuffer buffer = new StringBuffer(); + identityToString(buffer, object); + return buffer.toString(); + } + + /** + *

Appends the toString that would be produced by Object + * if a class did not override toString itself. null + * will throw a NullPointerException for either of the two parameters.

+ * + *
+     * ObjectUtils.identityToString(buf, "")            = buf.append("java.lang.String@1e23"
+     * ObjectUtils.identityToString(buf, Boolean.TRUE)  = buf.append("java.lang.Boolean@7fa"
+     * ObjectUtils.identityToString(buf, Boolean.TRUE)  = buf.append("java.lang.Boolean@7fa")
+     * 
+ * + * @param buffer the buffer to append to + * @param object the object to create a toString for + * @since 2.4 + */ + public static void identityToString(StringBuffer buffer, Object object) { + if (object == null) { + throw new NullPointerException("Cannot get the toString of a null identity"); + } + buffer.append(object.getClass().getName()) + .append('@') + .append(Integer.toHexString(System.identityHashCode(object))); } /** @@ -177,6 +203,7 @@ public class ObjectUtils { * @return the default toString text, or null if * null passed in * @since 2.0 + * @deprecated The design of this method is bad - see LANG-360. Instead, use identityToString(StringBuffer, Object). */ public static StringBuffer appendIdentityToString(StringBuffer buffer, Object object) { if (object == null) { diff --git a/src/test/org/apache/commons/lang/ObjectUtilsTest.java b/src/test/org/apache/commons/lang/ObjectUtilsTest.java index e022a2d39..a99acfff5 100644 --- a/src/test/org/apache/commons/lang/ObjectUtilsTest.java +++ b/src/test/org/apache/commons/lang/ObjectUtilsTest.java @@ -138,9 +138,22 @@ public class ObjectUtilsTest extends TestCase { "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), ObjectUtils.identityToString(FOO)); Integer i = new Integer(90); - assertEquals( - "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)), - ObjectUtils.identityToString(i)); + String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + assertEquals(expected, ObjectUtils.identityToString(i)); + StringBuffer buffer = new StringBuffer(); + ObjectUtils.identityToString(buffer, i); + assertEquals(expected, buffer.toString()); + + try { + ObjectUtils.identityToString(null, "tmp"); + fail("NullPointerException expected"); + } catch(NullPointerException npe) { + } + try { + ObjectUtils.identityToString(new StringBuffer(), null); + fail("NullPointerException expected"); + } catch(NullPointerException npe) { + } } public void testAppendIdentityToString() {