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:
parent
0446364ffa
commit
859224ffad
|
@ -342,7 +342,7 @@ 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 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 class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* 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 class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* 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 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
// 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 class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* </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 class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
*
|
*
|
||||||
* @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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue