parent
5f22e04514
commit
8cacc3b338
|
@ -24,14 +24,11 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.ArraySorter;
|
import org.apache.commons.lang3.ArraySorter;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.commons.lang3.ClassUtils;
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,6 +102,45 @@ import org.apache.commons.lang3.Validate;
|
||||||
*/
|
*/
|
||||||
public class ReflectionToStringBuilder extends ToStringBuilder {
|
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<String> 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<String> list = new ArrayList<>(array.length);
|
||||||
|
for (final Object e : array) {
|
||||||
|
if (e != null) {
|
||||||
|
list.add(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Builds a {@code toString} value using the default {@link ToStringStyle} through reflection.
|
* 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
|
* whether to include transient fields
|
||||||
* @param outputStatics
|
* @param outputStatics
|
||||||
* whether to include static fields
|
* whether to include static fields
|
||||||
|
* @param excludeNullValues
|
||||||
|
* whether to exclude fields whose values are null
|
||||||
* @param reflectUpToClass
|
* @param reflectUpToClass
|
||||||
* the superclass to reflect up to (inclusive), may be {@code null}
|
* the superclass to reflect up to (inclusive), may be {@code null}
|
||||||
* @return the String result
|
* @return the String result
|
||||||
|
@ -307,12 +345,12 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
* @see ToStringSummary
|
* @see ToStringSummary
|
||||||
* @since 2.1
|
* @since 3.6
|
||||||
*/
|
*/
|
||||||
public static <T> String toString(
|
public static <T> String toString(
|
||||||
final T object, final ToStringStyle style, final boolean outputTransients,
|
final T object, final ToStringStyle style, final boolean outputTransients,
|
||||||
final boolean outputStatics, final Class<? super T> reflectUpToClass) {
|
final boolean outputStatics, final boolean excludeNullValues, final Class<? super T> reflectUpToClass) {
|
||||||
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics)
|
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNullValues)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,8 +394,6 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* whether to include transient fields
|
* whether to include transient fields
|
||||||
* @param outputStatics
|
* @param outputStatics
|
||||||
* whether to include static fields
|
* whether to include static fields
|
||||||
* @param excludeNullValues
|
|
||||||
* whether to exclude fields whose values are null
|
|
||||||
* @param reflectUpToClass
|
* @param reflectUpToClass
|
||||||
* the superclass to reflect up to (inclusive), may be {@code null}
|
* the superclass to reflect up to (inclusive), may be {@code null}
|
||||||
* @return the String result
|
* @return the String result
|
||||||
|
@ -366,12 +402,12 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
* @see ToStringSummary
|
* @see ToStringSummary
|
||||||
* @since 3.6
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static <T> String toString(
|
public static <T> String toString(
|
||||||
final T object, final ToStringStyle style, final boolean outputTransients,
|
final T object, final ToStringStyle style, final boolean outputTransients,
|
||||||
final boolean outputStatics, final boolean excludeNullValues, final Class<? super T> reflectUpToClass) {
|
final boolean outputStatics, final Class<? super T> reflectUpToClass) {
|
||||||
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNullValues)
|
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,6 +424,20 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
return toStringExclude(object, toNoNullStringArray(excludeFieldNames));
|
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.
|
* 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();
|
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<String> 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<String> 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.
|
* Whether or not to append static fields.
|
||||||
*/
|
*/
|
||||||
|
@ -506,7 +503,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor.
|
* Constructs a new instance.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -524,7 +521,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor.
|
* Constructs a new instance.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -544,7 +541,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor.
|
* Constructs a new instance.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -569,7 +566,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructs a new instance.
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* the type of the object
|
* the type of the object
|
||||||
|
@ -597,7 +594,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructs a new instance.
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* the type of the object
|
* the type of the object
|
||||||
|
@ -693,17 +690,14 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
final String fieldName = field.getName();
|
final String fieldName = field.getName();
|
||||||
if (this.accept(field)) {
|
if (this.accept(field)) {
|
||||||
try {
|
try {
|
||||||
// Warning: Field.get(Object) creates wrappers objects
|
// Warning: Field.get(Object) creates wrappers objects for primitive types.
|
||||||
// for primitive types.
|
|
||||||
final Object fieldValue = this.getValue(field);
|
final Object fieldValue = this.getValue(field);
|
||||||
if (!excludeNullValues || fieldValue != null) {
|
if (!excludeNullValues || fieldValue != null) {
|
||||||
this.append(fieldName, fieldValue, !field.isAnnotationPresent(ToStringSummary.class));
|
this.append(fieldName, fieldValue, !field.isAnnotationPresent(ToStringSummary.class));
|
||||||
}
|
}
|
||||||
} catch (final IllegalAccessException ex) {
|
} catch (final IllegalAccessException ex) {
|
||||||
//this can't happen. Would get a Security exception
|
// this can't happen. Would get a Security exception instead
|
||||||
// instead
|
// throw a runtime exception in case the impossible happens.
|
||||||
//throw a runtime exception in case the impossible
|
|
||||||
// happens.
|
|
||||||
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
|
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -797,7 +791,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Append to the {@code toString} an {@link Object} array.
|
* Appends to the {@code toString} an {@link Object} array.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param array
|
* @param array
|
||||||
|
@ -834,19 +828,6 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
this.appendTransients = appendTransients;
|
this.appendTransients = appendTransients;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>
|
|
||||||
* Sets whether or not to append fields whose values are null.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @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.
|
* Sets the field names to exclude.
|
||||||
*
|
*
|
||||||
|
@ -864,6 +845,19 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Sets whether or not to append fields whose values are null.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @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.
|
* 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();
|
return super.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that include and exclude names do not intersect.
|
||||||
|
*/
|
||||||
private void validate() {
|
private void validate() {
|
||||||
if (ArrayUtils.containsAny(this.excludeFieldNames, (Object[]) this.includeFieldNames)) {
|
if (ArrayUtils.containsAny(this.excludeFieldNames, (Object[]) this.includeFieldNames)) {
|
||||||
ToStringStyle.unregister(this.getObject());
|
ToStringStyle.unregister(this.getObject());
|
||||||
|
|
Loading…
Reference in New Issue