From 8cacc3b3380a444face883344ac74c3fd528b67c Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 5 Jul 2022 19:37:14 -0400 Subject: [PATCH] Fix inline comments and sort members Javadoc. --- .../builder/ReflectionToStringBuilder.java | 175 +++++++++--------- 1 file changed, 86 insertions(+), 89 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java index 0f91ed429..bbc52ca2d 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java @@ -24,14 +24,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.apache.commons.lang3.ArraySorter; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; -import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.Validate; /** @@ -105,6 +102,45 @@ import org.apache.commons.lang3.Validate; */ public class ReflectionToStringBuilder extends ToStringBuilder { + private static Object checkNotNull(final Object obj) { + return Validate.notNull(obj, "obj"); + } + + /** + * Converts the given Collection into an array of Strings. The returned array does not contain {@code null} + * entries. Note that {@link Arrays#sort(Object[])} will throw an {@link NullPointerException} if an array element + * is {@code null}. + * + * @param collection + * The collection to convert + * @return A new array of Strings. + */ + static String[] toNoNullStringArray(final Collection collection) { + if (collection == null) { + return ArrayUtils.EMPTY_STRING_ARRAY; + } + return toNoNullStringArray(collection.toArray()); + } + + /** + * Returns a new array of Strings without null elements. Internal method used to normalize exclude lists + * (arrays and collections). Note that {@link Arrays#sort(Object[])} will throw an {@link NullPointerException} + * if an array element is {@code null}. + * + * @param array + * The array to check + * @return The given array or a new array without null. + */ + static String[] toNoNullStringArray(final Object[] array) { + final List list = new ArrayList<>(array.length); + for (final Object e : array) { + if (e != null) { + list.add(e.toString()); + } + } + return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY); + } + /** *

* Builds a {@code toString} value using the default {@link ToStringStyle} through reflection. @@ -299,6 +335,8 @@ public class ReflectionToStringBuilder extends ToStringBuilder { * whether to include transient fields * @param outputStatics * whether to include static fields + * @param excludeNullValues + * whether to exclude fields whose values are null * @param reflectUpToClass * the superclass to reflect up to (inclusive), may be {@code null} * @return the String result @@ -307,12 +345,12 @@ public class ReflectionToStringBuilder extends ToStringBuilder { * * @see ToStringExclude * @see ToStringSummary - * @since 2.1 + * @since 3.6 */ public static String toString( final T object, final ToStringStyle style, final boolean outputTransients, - final boolean outputStatics, final Class reflectUpToClass) { - return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics) + final boolean outputStatics, final boolean excludeNullValues, final Class reflectUpToClass) { + return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNullValues) .toString(); } @@ -356,8 +394,6 @@ public class ReflectionToStringBuilder extends ToStringBuilder { * whether to include transient fields * @param outputStatics * whether to include static fields - * @param excludeNullValues - * whether to exclude fields whose values are null * @param reflectUpToClass * the superclass to reflect up to (inclusive), may be {@code null} * @return the String result @@ -366,12 +402,12 @@ public class ReflectionToStringBuilder extends ToStringBuilder { * * @see ToStringExclude * @see ToStringSummary - * @since 3.6 + * @since 2.1 */ public static String toString( final T object, final ToStringStyle style, final boolean outputTransients, - final boolean outputStatics, final boolean excludeNullValues, final Class reflectUpToClass) { - return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNullValues) + final boolean outputStatics, final Class reflectUpToClass) { + return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics) .toString(); } @@ -388,6 +424,20 @@ public class ReflectionToStringBuilder extends ToStringBuilder { return toStringExclude(object, toNoNullStringArray(excludeFieldNames)); } + /** + * Builds a String for a toString method excluding the given field names. + * + * @param object + * The object to "toString". + * @param excludeFieldNames + * The field names to exclude + * @return The toString value. + */ + public static String toStringExclude(final Object object, final String... excludeFieldNames) { + return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString(); + } + + /** * Builds a String for a toString method including the given field names. * @@ -417,59 +467,6 @@ public class ReflectionToStringBuilder extends ToStringBuilder { return new ReflectionToStringBuilder(object).setIncludeFieldNames(includeFieldNames).toString(); } - /** - * Converts the given Collection into an array of Strings. The returned array does not contain {@code null} - * entries. Note that {@link Arrays#sort(Object[])} will throw an {@link NullPointerException} if an array element - * is {@code null}. - * - * @param collection - * The collection to convert - * @return A new array of Strings. - */ - static String[] toNoNullStringArray(final Collection collection) { - if (collection == null) { - return ArrayUtils.EMPTY_STRING_ARRAY; - } - return toNoNullStringArray(collection.toArray()); - } - - /** - * Returns a new array of Strings without null elements. Internal method used to normalize exclude lists - * (arrays and collections). Note that {@link Arrays#sort(Object[])} will throw an {@link NullPointerException} - * if an array element is {@code null}. - * - * @param array - * The array to check - * @return The given array or a new array without null. - */ - static String[] toNoNullStringArray(final Object[] array) { - final List list = new ArrayList<>(array.length); - for (final Object e : array) { - if (e != null) { - list.add(e.toString()); - } - } - return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY); - } - - - /** - * Builds a String for a toString method excluding the given field names. - * - * @param object - * The object to "toString". - * @param excludeFieldNames - * The field names to exclude - * @return The toString value. - */ - public static String toStringExclude(final Object object, final String... excludeFieldNames) { - return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString(); - } - - private static Object checkNotNull(final Object obj) { - return Validate.notNull(obj, "obj"); - } - /** * Whether or not to append static fields. */ @@ -506,7 +503,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { /** *

- * Constructor. + * Constructs a new instance. *

* *

@@ -524,7 +521,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { /** *

- * Constructor. + * Constructs a new instance. *

* *

@@ -544,7 +541,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { /** *

- * Constructor. + * Constructs a new instance. *

* *

@@ -569,7 +566,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { } /** - * Constructor. + * Constructs a new instance. * * @param * the type of the object @@ -597,7 +594,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { } /** - * Constructor. + * Constructs a new instance. * * @param * the type of the object @@ -693,17 +690,14 @@ public class ReflectionToStringBuilder extends ToStringBuilder { final String fieldName = field.getName(); if (this.accept(field)) { try { - // Warning: Field.get(Object) creates wrappers objects - // for primitive types. + // Warning: Field.get(Object) creates wrappers objects for primitive types. final Object fieldValue = this.getValue(field); if (!excludeNullValues || fieldValue != null) { this.append(fieldName, fieldValue, !field.isAnnotationPresent(ToStringSummary.class)); } } catch (final IllegalAccessException ex) { - //this can't happen. Would get a Security exception - // instead - //throw a runtime exception in case the impossible - // happens. + // this can't happen. Would get a Security exception instead + // throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } @@ -797,7 +791,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { /** *

- * Append to the {@code toString} an {@link Object} array. + * Appends to the {@code toString} an {@link Object} array. *

* * @param array @@ -834,19 +828,6 @@ public class ReflectionToStringBuilder extends ToStringBuilder { this.appendTransients = appendTransients; } - /** - *

- * Sets whether or not to append fields whose values are null. - *

- * - * @param excludeNullValues - * Whether or not to append fields whose values are null. - * @since 3.6 - */ - public void setExcludeNullValues(final boolean excludeNullValues) { - this.excludeNullValues = excludeNullValues; - } - /** * Sets the field names to exclude. * @@ -864,6 +845,19 @@ public class ReflectionToStringBuilder extends ToStringBuilder { return this; } + /** + *

+ * Sets whether or not to append fields whose values are null. + *

+ * + * @param excludeNullValues + * Whether or not to append fields whose values are null. + * @since 3.6 + */ + public void setExcludeNullValues(final boolean excludeNullValues) { + this.excludeNullValues = excludeNullValues; + } + /** * Sets the field names to include. {@code null} or empty means all fields are included. All fields are included by default. This method will override the default behavior. * @@ -924,6 +918,9 @@ public class ReflectionToStringBuilder extends ToStringBuilder { return super.toString(); } + /** + * Validates that include and exclude names do not intersect. + */ private void validate() { if (ArrayUtils.containsAny(this.excludeFieldNames, (Object[]) this.includeFieldNames)) { ToStringStyle.unregister(this.getObject());