LANG-1167: Add null filter to ReflectionToStringBuilder

Fix/add since javadoc tags, fix checkstyle violations and do other small clean-ups.
This commit is contained in:
pascalschumacher 2017-04-20 19:15:31 +02:00
parent 0446364ffa
commit 859224ffad
2 changed files with 15 additions and 10 deletions

View File

@ -342,7 +342,7 @@ public static <T> String toString(
* 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 excludeNulls * @param excludeNullValues
* whether to exclude fields whose values are null * whether to exclude fields whose values are null
* @param reflectUpToClass * @param reflectUpToClass
* the superclass to reflect up to (inclusive), may be <code>null</code> * the superclass to reflect up to (inclusive), may be <code>null</code>
@ -351,12 +351,12 @@ public static <T> String toString(
* if the Object is <code>null</code> * if the Object is <code>null</code>
* *
* @see ToStringExclude * @see ToStringExclude
* @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, boolean excludeNulls, final Class<? super T> reflectUpToClass) { final boolean outputStatics, boolean excludeNullValues, final Class<? super T> reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNulls) return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNullValues)
.toString(); .toString();
} }
@ -565,7 +565,7 @@ public <T> ReflectionToStringBuilder(
* whether to include static fields * whether to include static fields
* @param excludeNullValues * @param excludeNullValues
* whether to exclude fields who value is null * whether to exclude fields who value is null
* @since 2.1 * @since 3.6
*/ */
public <T> ReflectionToStringBuilder( public <T> ReflectionToStringBuilder(
final T object, final ToStringStyle style, final StringBuffer buffer, final T object, final ToStringStyle style, final StringBuffer buffer,
@ -641,7 +641,7 @@ protected void appendFieldsIn(final Class<?> clazz) {
// 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); this.append(fieldName, fieldValue);
} }
} catch (final IllegalAccessException ex) { } catch (final IllegalAccessException ex) {
@ -722,6 +722,7 @@ public boolean isAppendTransients() {
* </p> * </p>
* *
* @return Whether or not to append fields whose values are null. * @return Whether or not to append fields whose values are null.
* @since 3.6
*/ */
public boolean isExcludeNullValues() { public boolean isExcludeNullValues() {
return this.excludeNullValues; return this.excludeNullValues;
@ -773,6 +774,7 @@ public void setAppendTransients(final boolean appendTransients) {
* *
* @param excludeNullValues * @param excludeNullValues
* Whether or not to append fields whose values are null. * Whether or not to append fields whose values are null.
* @since 3.6
*/ */
public void setExcludeNullValues(final boolean excludeNullValues) { public void setExcludeNullValues(final boolean excludeNullValues) {
this.excludeNullValues = excludeNullValues; this.excludeNullValues = excludeNullValues;

View File

@ -17,17 +17,20 @@
package org.apache.commons.lang3.builder; package org.apache.commons.lang3.builder;
import static org.junit.Assert.*; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
public class ReflectionToStringBuilderExcludeNullValuesTest { public class ReflectionToStringBuilderExcludeNullValuesTest {
class TestFixture{ static class TestFixture {
@SuppressWarnings("unused")
private Integer testIntegerField; private Integer testIntegerField;
@SuppressWarnings("unused")
private String testStringField; private String testStringField;
public TestFixture(Integer a, String b){ public TestFixture(Integer a, String b) {
this.testIntegerField = a; this.testIntegerField = a;
this.testStringField = b; this.testStringField = b;
} }