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 7c8d26daa Fail-fast for a null DiffBuilder in
ReflectionDiffBuilder.ReflectionDiffBuilder(DiffBuilder, String[]) instead of
getting a NullPointerException in ReflectionDiffBuilder instance methods
7c8d26daa is described below
commit 7c8d26daac0c08e888249d506e5ef4e655898166
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 26 19:06:12 2025 +0000
Fail-fast for a null DiffBuilder in
ReflectionDiffBuilder.ReflectionDiffBuilder(DiffBuilder, String[])
instead of getting a NullPointerException in ReflectionDiffBuilder
instance methods
- Merge if statements with the same return value
- Remove unnecessary null check
---
src/changes/changes.xml | 1 +
.../lang3/builder/ReflectionDiffBuilder.java | 25 +++++++++++-----------
.../lang3/builder/ReflectionDiffBuilderTest.java | 6 ++++++
3 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e3de10be4..bb10ea25d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,6 +65,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1793" type="fix" dev="ggregory" due-to="IcoreE">Fix
Javadoc description in CharUtils.isAsciiAlphanumeric() #1501.</action>
<action issue="LANG-1794" type="fix" dev="ggregory" due-to="IcoreE">Fix
Javadoc for RandomUtils.secure(), it incorrectly mentions
securerandom.strongAlgorithms #1503.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Fix NullPointerException in
ReflectionDiffBuilder.getExcludeFieldNames() when instance created with
ReflectionDiffBuilder.ReflectionDiffBuilder(T, T, ToStringStyle).</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">Fail-fast for a null DiffBuilder in
ReflectionDiffBuilder.ReflectionDiffBuilder(DiffBuilder, String[]) instead of
getting a NullPointerException in ReflectionDiffBuilder instance
methods.</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.apache.commons:commons-parent from 92 to 93
#1498.</action>
diff --git
a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
index 42e2cc585..ca9da55e8 100644
--- a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
+++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java
@@ -154,9 +154,16 @@ private static String[] toExcludeFieldNames(final String[]
excludeFieldNames) {
*/
private String[] excludeFieldNames;
+ /**
+ * Constructs a new instance.
+ *
+ * @param diffBuilder a non-null DiffBuilder.
+ * @param excludeFieldNames a non-null String array.
+ * @throw NullPointerException Thrown on null input.
+ */
private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final
String[] excludeFieldNames) {
- this.diffBuilder = diffBuilder;
- this.excludeFieldNames = Objects.requireNonNull(excludeFieldNames);
+ this.diffBuilder = Objects.requireNonNull(diffBuilder, "diffBuilder");
+ this.excludeFieldNames = Objects.requireNonNull(excludeFieldNames,
"excludeFieldNames");
}
/**
@@ -179,17 +186,9 @@ public ReflectionDiffBuilder(final T left, final T right,
final ToStringStyle st
}
private boolean accept(final Field field) {
- if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) !=
-1) {
- return false;
- }
- if (Modifier.isTransient(field.getModifiers())) {
- return false;
- }
- if (Modifier.isStatic(field.getModifiers())) {
- return false;
- }
- if (excludeFieldNames != null &&
Arrays.binarySearch(excludeFieldNames, field.getName()) >= 0) {
- // Reject fields from the getExcludeFieldNames list.
+ if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) !=
-1 || Modifier.isTransient(field.getModifiers())
+ || Modifier.isStatic(field.getModifiers()) ||
Arrays.binarySearch(excludeFieldNames, field.getName()) >= 0) {
+ // Rejected.
return false;
}
return !field.isAnnotationPresent(DiffExclude.class);
diff --git
a/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
b/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
index c21764057..deee98b18 100644
---
a/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
+++
b/src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java
@@ -18,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -217,6 +218,11 @@ void
testGetExcludeFieldNamesWithNullValuesInExcludedFieldNamesCtor() {
assertNotNull(reflectionDiffBuilder.build());
}
+ @Test
+ void testNoDiffBuilderSet() {
+ assertThrows(NullPointerException.class, () ->
ReflectionDiffBuilder.<TypeTestClass>builder().build());
+ }
+
@Test
void testNoDifferences() {
final TypeTestClass firstObject = new TypeTestClass();