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
* @param outputStatics
* whether to include static fields
* @param excludeNulls
* @param excludeNullValues
* whether to exclude fields whose values are null
* @param reflectUpToClass
* 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>
*
* @see ToStringExclude
* @since 2.1
* @since 3.6
*/
public static <T> String toString(
final T object, final ToStringStyle style, final boolean outputTransients,
final boolean outputStatics, boolean excludeNulls, final Class<? super T> reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNulls)
final boolean outputStatics, boolean excludeNullValues, final Class<? super T> reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics, excludeNullValues)
.toString();
}
@ -565,7 +565,7 @@ public <T> ReflectionToStringBuilder(
* whether to include static fields
* @param excludeNullValues
* whether to exclude fields who value is null
* @since 2.1
* @since 3.6
*/
public <T> ReflectionToStringBuilder(
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
// for primitive types.
final Object fieldValue = this.getValue(field);
if(!excludeNullValues || fieldValue != null){
if (!excludeNullValues || fieldValue != null) {
this.append(fieldName, fieldValue);
}
} catch (final IllegalAccessException ex) {
@ -722,6 +722,7 @@ public boolean isAppendTransients() {
* </p>
*
* @return Whether or not to append fields whose values are null.
* @since 3.6
*/
public boolean isExcludeNullValues() {
return this.excludeNullValues;
@ -773,6 +774,7 @@ public void setAppendTransients(final boolean appendTransients) {
*
* @param excludeNullValues
* Whether or not to append fields whose values are null.
* @since 3.6
*/
public void setExcludeNullValues(final boolean excludeNullValues) {
this.excludeNullValues = excludeNullValues;

View File

@ -17,17 +17,20 @@
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;
public class ReflectionToStringBuilderExcludeNullValuesTest {
class TestFixture{
static class TestFixture {
@SuppressWarnings("unused")
private Integer testIntegerField;
@SuppressWarnings("unused")
private String testStringField;
public TestFixture(Integer a, String b){
public TestFixture(Integer a, String b) {
this.testIntegerField = a;
this.testStringField = b;
}