Skip to content

Commit 7c8d26d

Browse files
committed
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
1 parent 5bcedcc commit 7c8d26d

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ The <action> type attribute can be add,update,fix,remove.
6565
<action issue="LANG-1793" type="fix" dev="ggregory" due-to="IcoreE">Fix Javadoc description in CharUtils.isAsciiAlphanumeric() #1501.</action>
6666
<action issue="LANG-1794" type="fix" dev="ggregory" due-to="IcoreE">Fix Javadoc for RandomUtils.secure(), it incorrectly mentions securerandom.strongAlgorithms #1503.</action>
6767
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix NullPointerException in ReflectionDiffBuilder.getExcludeFieldNames() when instance created with ReflectionDiffBuilder.ReflectionDiffBuilder(T, T, ToStringStyle).</action>
68+
<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>
6869
<!-- ADD -->
6970
<!-- UPDATE -->
7071
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 92 to 93 #1498.</action>

src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,16 @@ private static String[] toExcludeFieldNames(final String[] excludeFieldNames) {
154154
*/
155155
private String[] excludeFieldNames;
156156

157+
/**
158+
* Constructs a new instance.
159+
*
160+
* @param diffBuilder a non-null DiffBuilder.
161+
* @param excludeFieldNames a non-null String array.
162+
* @throw NullPointerException Thrown on null input.
163+
*/
157164
private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] excludeFieldNames) {
158-
this.diffBuilder = diffBuilder;
159-
this.excludeFieldNames = Objects.requireNonNull(excludeFieldNames);
165+
this.diffBuilder = Objects.requireNonNull(diffBuilder, "diffBuilder");
166+
this.excludeFieldNames = Objects.requireNonNull(excludeFieldNames, "excludeFieldNames");
160167
}
161168

162169
/**
@@ -179,17 +186,9 @@ public ReflectionDiffBuilder(final T left, final T right, final ToStringStyle st
179186
}
180187

181188
private boolean accept(final Field field) {
182-
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
183-
return false;
184-
}
185-
if (Modifier.isTransient(field.getModifiers())) {
186-
return false;
187-
}
188-
if (Modifier.isStatic(field.getModifiers())) {
189-
return false;
190-
}
191-
if (excludeFieldNames != null && Arrays.binarySearch(excludeFieldNames, field.getName()) >= 0) {
192-
// Reject fields from the getExcludeFieldNames list.
189+
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1 || Modifier.isTransient(field.getModifiers())
190+
|| Modifier.isStatic(field.getModifiers()) || Arrays.binarySearch(excludeFieldNames, field.getName()) >= 0) {
191+
// Rejected.
193192
return false;
194193
}
195194
return !field.isAnnotationPresent(DiffExclude.class);

src/test/java/org/apache/commons/lang3/builder/ReflectionDiffBuilderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.math.BigDecimal;
2324
import java.math.BigInteger;
@@ -217,6 +218,11 @@ void testGetExcludeFieldNamesWithNullValuesInExcludedFieldNamesCtor() {
217218
assertNotNull(reflectionDiffBuilder.build());
218219
}
219220

221+
@Test
222+
void testNoDiffBuilderSet() {
223+
assertThrows(NullPointerException.class, () -> ReflectionDiffBuilder.<TypeTestClass>builder().build());
224+
}
225+
220226
@Test
221227
void testNoDifferences() {
222228
final TypeTestClass firstObject = new TypeTestClass();

0 commit comments

Comments
 (0)