From 9f5d5390213b03edf9bd332926f081e321b653e8 Mon Sep 17 00:00:00 2001 From: Ken Southerland Date: Sun, 30 Nov 2025 18:34:09 -0800 Subject: [PATCH 1/2] Allows enum in recursive string style --- .../org/apache/commons/lang3/builder/RecursiveToStringStyle.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/apache/commons/lang3/builder/RecursiveToStringStyle.java b/src/main/java/org/apache/commons/lang3/builder/RecursiveToStringStyle.java index c8bf3fc6375..7ea1c91b86d 100644 --- a/src/main/java/org/apache/commons/lang3/builder/RecursiveToStringStyle.java +++ b/src/main/java/org/apache/commons/lang3/builder/RecursiveToStringStyle.java @@ -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 { From 4cf612e5777527c34f28c02b0cb93dfcd8574737 Mon Sep 17 00:00:00 2001 From: Ken Southerland Date: Sun, 30 Nov 2025 21:21:24 -0800 Subject: [PATCH 2/2] test to go with recursive enum fix --- .../builder/RecursiveToStringStyleTest.java | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/builder/RecursiveToStringStyleTest.java b/src/test/java/org/apache/commons/lang3/builder/RecursiveToStringStyleTest.java index cbb1407e38e..6bff2de5aee 100644 --- a/src/test/java/org/apache/commons/lang3/builder/RecursiveToStringStyleTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/RecursiveToStringStyleTest.java @@ -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() { @@ -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()); @@ -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); + } }