From e863dcb2e7a654af6b8eb5865d8d1d8eee0a6d28 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 10 Nov 2017 12:25:39 -0700 Subject: [PATCH] [LANG-1367] ObjectUtils.identityToString(Object) and friends should allocate builders and buffers with a size --- src/changes/changes.xml | 4 +++ .../org/apache/commons/lang3/ObjectUtils.java | 33 ++++++++++++++----- .../apache/commons/lang3/ObjectUtilsTest.java | 6 ++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7d102003a..07461bedf 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -45,6 +45,10 @@ The type attribute can be add,update,fix,remove. + + ObjectUtils.identityToString(Object) and friends should allocate builders and buffers with a size + + Fix tests DateUtilsTest for Java 9 with en_GB locale Fix NullPointerException in isJavaVersionAtLeast on Java 10, add SystemUtils.IS_JAVA_10, add JavaVersion.JAVA_10 diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 1bcf72d4c..6b93a7d65 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -47,7 +47,7 @@ import org.apache.commons.lang3.text.StrBuilder; public class ObjectUtils { private static final char AT_SIGN = '@'; - + /** *

Singleton used as a {@code null} placeholder where * {@code null} has another meaning.

@@ -333,8 +333,14 @@ public class ObjectUtils { if (object == null) { return null; } - final StringBuilder builder = new StringBuilder(); - identityToString(builder, object); + final String name = object.getClass().getName(); + final String hexString = Integer.toHexString(System.identityHashCode(object)); + final StringBuilder builder = new StringBuilder(name.length() + 1 + hexString.length()); + // @formatter:off + builder.append(name) + .append(AT_SIGN) + .append(hexString); + // @formatter:off return builder.toString(); } @@ -381,9 +387,12 @@ public class ObjectUtils { @Deprecated public static void identityToString(final StrBuilder builder, final Object object) { Validate.notNull(object, "Cannot get the toString of a null object"); - builder.append(object.getClass().getName()) + final String name = object.getClass().getName(); + final String hexString = Integer.toHexString(System.identityHashCode(object)); + builder.ensureCapacity(builder.length() + name.length() + 1 + hexString.length()); + builder.append(name) .append(AT_SIGN) - .append(Integer.toHexString(System.identityHashCode(object))); + .append(hexString); } /** @@ -403,9 +412,12 @@ public class ObjectUtils { */ public static void identityToString(final StringBuffer buffer, final Object object) { Validate.notNull(object, "Cannot get the toString of a null object"); - buffer.append(object.getClass().getName()) + final String name = object.getClass().getName(); + final String hexString = Integer.toHexString(System.identityHashCode(object)); + buffer.ensureCapacity(buffer.length() + name.length() + 1 + hexString.length()); + buffer.append(name) .append(AT_SIGN) - .append(Integer.toHexString(System.identityHashCode(object))); + .append(hexString); } /** @@ -425,9 +437,12 @@ public class ObjectUtils { */ public static void identityToString(final StringBuilder builder, final Object object) { Validate.notNull(object, "Cannot get the toString of a null object"); - builder.append(object.getClass().getName()) + final String name = object.getClass().getName(); + final String hexString = Integer.toHexString(System.identityHashCode(object)); + builder.ensureCapacity(builder.length() + name.length() + 1 + hexString.length()); + builder.append(name) .append(AT_SIGN) - .append(Integer.toHexString(System.identityHashCode(object))); + .append(hexString); } // ToString diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 06b97e2a0..661722dac 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -195,7 +195,7 @@ public class ObjectUtilsTest { public void testIdentityToStringObjectNull() { assertNull(ObjectUtils.identityToString(null)); } - + @Test public void testIdentityToStringInteger() { final Integer i = Integer.valueOf(90); @@ -203,14 +203,14 @@ public class ObjectUtilsTest { assertEquals(expected, ObjectUtils.identityToString(i)); } - + @Test public void testIdentityToStringString() { assertEquals( "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), ObjectUtils.identityToString(FOO)); } - + @Test public void testIdentityToStringStringBuilder() { final Integer i = Integer.valueOf(90);