Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.apache.commons.lang3.builder;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.mutable.MutableBoolean;

/**
* Works with {@link ToStringBuilder} to create a "deep" {@code toString}.
Expand Down Expand Up @@ -66,16 +68,29 @@ public RecursiveToStringStyle() {
}

/**
* Returns whether or not to recursively format the given {@link Class}.
* By default, this method always returns {@code true}, but may be overwritten by
* subclasses to filter specific classes.
* Tests whether or not to recursively format the given {@link Class}.
* <p>
* By default, this method always filters out the following:
* </p>
* <ul>
* <li><a href="https://docs.oracle.com/javase/specs/jls/se25/html/jls-5.html#jls-5.1.7">Boxed primitives</a>, see {@link ClassUtils#isPrimitiveWrapper(Class)}
* <li>{@link String}</li>
* <li>{@link Number} subclasses</li>
* <li>{@link AtomicBoolean}</li>
* <li>{@link MutableBoolean}</li>
* </ul>
*
* @param clazz
* The class to test.
* @return Whether or not to recursively format the given {@link Class}.
* @param clazz The class to test.
* @return Whether or not to recursively format instances of the given {@link Class}.
*/
protected boolean accept(final Class<?> clazz) {
return true;
// @formatter:off
return !ClassUtils.isPrimitiveWrapper(clazz) &&
!String.class.equals(clazz) &&
!Number.class.isAssignableFrom(clazz) &&
!AtomicBoolean.class.equals(clazz) &&
!MutableBoolean.class.equals(clazz);
// @formatter:on
}

@Override
Expand All @@ -87,10 +102,7 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f

@Override
public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
final Class<? extends Object> clazz = value.getClass();
if (!ClassUtils.isPrimitiveWrapper(clazz) &&
!String.class.equals(clazz) &&
accept(clazz)) {
if (value != null && accept(value.getClass())) {
buffer.append(ReflectionToStringBuilder.toString(value, this));
} else {
super.appendDetail(buffer, fieldName, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.AbstractLangTest;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.mutable.MutableByte;
import org.apache.commons.lang3.mutable.MutableDouble;
import org.apache.commons.lang3.mutable.MutableFloat;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableLong;
import org.apache.commons.lang3.mutable.MutableShort;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,6 +97,30 @@ void testAppendSuper() {
assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
}

@Test
void testAtomicsArray() {
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new AtomicLong[] { new AtomicLong(3), new AtomicLong(6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new AtomicInteger[] { new AtomicInteger(3), new AtomicInteger(6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{true,false}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new AtomicBoolean[] { new AtomicBoolean(true), new AtomicBoolean(false) } }).toString());
}

@Test
void testBigDecimal() {
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new BigDecimal[] { BigDecimal.valueOf(3), BigDecimal.valueOf(6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3.0,6.0}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new BigDecimal[] { BigDecimal.valueOf(3.0), BigDecimal.valueOf(6.0) } }).toString());
}

@Test
void testBigInteger() {
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new BigInteger[] { BigInteger.valueOf(3), BigInteger.valueOf(6) } }).toString());
}

@Test
void testBlank() {
assertEquals(baseStr + "[]", new ToStringBuilder(base).toString());
Expand Down Expand Up @@ -117,6 +153,24 @@ void testLongArrayArray() {
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
}

@Test
void testMutableWrapperArray() {
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableLong[] { new MutableLong(3), new MutableLong(6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableInt[] { new MutableInt(3), new MutableInt(6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableShort[] { new MutableShort(3), new MutableShort(6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3,6}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableByte[] { new MutableByte((byte) 3), new MutableByte((byte) 6) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3.0,6.0}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableFloat[] { new MutableFloat(3f), new MutableFloat(6f) } }).toString());
assertEquals(baseStr + "[{<null>,5,{3.0,6.0}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableDouble[] { new MutableDouble(3d), new MutableDouble(6d) } }).toString());
assertEquals(baseStr + "[{<null>,5,{true,false}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new MutableBoolean[] { new MutableBoolean(true), new MutableBoolean(false) } }).toString());
}

@Test
void testObject() {
final Integer i3 = Integer.valueOf(3);
Expand Down Expand Up @@ -174,5 +228,4 @@ void testPrimitiveWrapperArray() {
assertEquals(baseStr + "[{<null>,5,{true,false}}]",
new ToStringBuilder(base).append(new Object[] { null, base, new Boolean[] { true, false } }).toString());
}

}
Loading