Skip to content
Open
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 @@ -89,6 +89,7 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
if (!ClassUtils.isPrimitiveWrapper(value.getClass()) &&
!String.class.equals(value.getClass()) &&
!value.getClass().isEnum() &&
accept(value.getClass())) {
buffer.append(ReflectionToStringBuilder.toString(value, this));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,28 @@ static class Person {
Job job;
}

static enum Status {
QUEUED,
FINISHED;
};

static class Event {
Status status;
String message;
};

/**
* Making class that has an enum in a recursive position.
* i.e. the enum is a part of the Event object.
*/
static class Thing {
String desc;
Event event;
};

private final Integer base = Integer.valueOf(5);

private final String baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
private final String baseStr = buildClassId(base);

@BeforeEach
public void setUp() {
Expand All @@ -73,6 +92,10 @@ public void tearDown() {
ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
}

private static String buildClassId(Object obj) {
return obj.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(obj));
}

@Test
void testAppendSuper() {
assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
Expand Down Expand Up @@ -154,10 +177,21 @@ void testPerson() {
p.smoker = false;
p.job = new Job();
p.job.title = "Manager";
final String baseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p));
final String jobStr = p.job.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p.job));
assertEquals(baseStr + "[age=33,job=" + jobStr + "[title=Manager],name=John Doe,smoker=false]",
assertEquals(buildClassId(p) + "[age=33,job=" + buildClassId(p.job) + "[title=Manager],name=John Doe,smoker=false]",
new ReflectionToStringBuilder(p, new RecursiveToStringStyle()).toString());
}

@Test
void testEnum() {
Thing thing = new Thing();
thing.desc = "my thing";
Event event = new Event();
event.message = "cool";
event.status = Status.FINISHED;
thing.event = event;

String expected = buildClassId(thing) + "[desc=my thing,event=" + buildClassId(event) + "[message=cool,status=FINISHED]]";
String result = new ReflectionToStringBuilder(thing, new RecursiveToStringStyle()).toString();
assertEquals(expected, result);
}
}