You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2007/11/12 23:54:02 UTC
svn commit: r594336 - in /commons/proper/lang/trunk/src:
java/org/apache/commons/lang/ObjectUtils.java
test/org/apache/commons/lang/ObjectUtilsTest.java
Author: bayard
Date: Mon Nov 12 14:54:02 2007
New Revision: 594336
URL: http://svn.apache.org/viewvc?rev=594336&view=rev
Log:
Applying my second patch from LANG-360 - it seems to do what Stephane/Paul and I are consensing on
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java?rev=594336&r1=594335&r2=594336&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java Mon Nov 12 14:54:02 2007
@@ -157,7 +157,33 @@
if (object == null) {
return null;
}
- return appendIdentityToString(null, object).toString();
+ StringBuffer buffer = new StringBuffer();
+ identityToString(buffer, object);
+ return buffer.toString();
+ }
+
+ /**
+ * <p>Appends the toString that would be produced by <code>Object</code>
+ * if a class did not override toString itself. <code>null</code>
+ * will throw a NullPointerException for either of the two parameters. </p>
+ *
+ * <pre>
+ * 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")
+ * </pre>
+ *
+ * @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 @@
* @return the default toString text, or <code>null</code> if
* <code>null</code> 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) {
Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java?rev=594336&r1=594335&r2=594336&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java Mon Nov 12 14:54:02 2007
@@ -138,9 +138,22 @@
"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() {