This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 15ff8f999 [LANG-1710] ReflectionToStringBuilder changes in version 
3.13.0 has broken the logic for overriding classes
15ff8f999 is described below

commit 15ff8f999780b566d21b53550a6c3e0c5cbe8994
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Aug 27 07:44:42 2023 -0400

    [LANG-1710] ReflectionToStringBuilder changes in version 3.13.0 has
    broken the logic for overriding classes
---
 src/changes/changes.xml                            |  1 +
 .../lang3/builder/ReflectionToStringBuilder.java   | 15 ++++--
 ...ionToStringBuilderCustomImplementationTest.java | 55 ++++++++++++++++++++++
 3 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index aad9a5fa0..a03eecff6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" 
due-to="ljacqu">[Javadoc] Point to right getShortClassName flavor in Javadoc 
for relevant notes #1097.</action>
     <action                   type="fix" dev="ggregory" 
due-to="hduelme">Improve performance of StringUtils.isMixedCase() 
#1096.</action>
     <action issue="LANG-1706" type="fix" dev="ggregory" due-to="Alberto 
Fernández">ThreadUtils find methods should not return null items #1098.</action>
+    <action issue="LANG-1710" type="fix" dev="ggregory" due-to="Shashank 
Sharma, Gary Gregory, Oksana">ReflectionToStringBuilder changes in version 
3.13.0 has broken the logic for overriding classes.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Rob Spoor, 
Gary Gregory">Add Functions#function(Function).</action>
     <action                   type="add" dev="ggregory" due-to="Rob Spoor, 
Gary Gregory">Add FailableFunction#function(FailableFunction).</action>
diff --git 
a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java 
b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java
index d6413681b..c505a5187 100644
--- 
a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java
+++ 
b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java
@@ -657,10 +657,17 @@ public class ReflectionToStringBuilder extends 
ToStringBuilder {
         for (final Field field : fields) {
             final String fieldName = field.getName();
             if (this.accept(field)) {
-                // Warning: Field.get(Object) creates wrappers objects for 
primitive types.
-                final Object fieldValue = Reflection.getUnchecked(field, 
getObject());
-                if (!excludeNullValues || fieldValue != null) {
-                    this.append(fieldName, fieldValue, 
!field.isAnnotationPresent(ToStringSummary.class));
+                try {
+                    // Warning: Field.get(Object) creates wrappers objects
+                    // for primitive types.
+                    final Object fieldValue = this.getValue(field);
+                    if (!excludeNullValues || fieldValue != null) {
+                        this.append(fieldName, fieldValue, 
!field.isAnnotationPresent(ToStringSummary.class));
+                    }
+                } catch (final IllegalAccessException e) {
+                    // this can't happen. Would get a Security exception 
instead throw a runtime exception in case the
+                    // impossible happens.
+                    throw new IllegalStateException(e);
                 }
             }
         }
diff --git 
a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderCustomImplementationTest.java
 
b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderCustomImplementationTest.java
new file mode 100644
index 000000000..61e9ee635
--- /dev/null
+++ 
b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderCustomImplementationTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.builder;
+
+import org.apache.commons.lang3.AbstractLangTest;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests {@link ReflectionToStringBuilder} always uses {@link 
ReflectionToStringBuilder#getValue(Field)} to get the
+ * value of every field in the class.
+ */
+public class ReflectionToStringBuilderCustomImplementationTest extends 
AbstractLangTest {
+
+    public static class CustomReflectionToStringBuilder extends 
ReflectionToStringBuilder {
+
+        private static final String CUSTOM_PREFIX = "prefix:";
+
+        public CustomReflectionToStringBuilder(Object object, ToStringStyle 
toStringStyle) {
+            super(object, toStringStyle);
+        }
+
+        @Override
+        protected Object getValue(Field field) throws IllegalAccessException {
+            return CUSTOM_PREFIX + super.getValue(field);
+        }
+    }
+
+    @SuppressWarnings("unused") // Used indirectly by ReflectionToStringBuilder
+    private final String stringField = "string";
+
+    @Test
+    public void testBuild() {
+        assertEquals("[stringField=prefix:string]",
+                new CustomReflectionToStringBuilder(this, 
ToStringStyle.NO_CLASS_NAME_STYLE).build());
+    }
+
+}
\ No newline at end of file

Reply via email to