Skip to content

Commit 5bcedcc

Browse files
committed
Fix NullPointerException in ReflectionDiffBuilder.getExcludeFieldNames()
when instance created with ReflectionDiffBuilder.ReflectionDiffBuilder(T, T, ToStringStyle)
1 parent 2bcca49 commit 5bcedcc

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The <action> type attribute can be add,update,fix,remove.
6464
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Apache RAT plugin console warnings.</action>
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>
67+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix NullPointerException in ReflectionDiffBuilder.getExcludeFieldNames() when instance created with ReflectionDiffBuilder.ReflectionDiffBuilder(T, T, ToStringStyle).</action>
6768
<!-- ADD -->
6869
<!-- UPDATE -->
6970
<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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Modifier;
2121
import java.util.Arrays;
22+
import java.util.Objects;
2223

2324
import org.apache.commons.lang3.ArraySorter;
2425
import org.apache.commons.lang3.ArrayUtils;
@@ -155,7 +156,7 @@ private static String[] toExcludeFieldNames(final String[] excludeFieldNames) {
155156

156157
private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] excludeFieldNames) {
157158
this.diffBuilder = diffBuilder;
158-
this.excludeFieldNames = excludeFieldNames;
159+
this.excludeFieldNames = Objects.requireNonNull(excludeFieldNames);
159160
}
160161

161162
/**
@@ -174,7 +175,7 @@ private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] e
174175
*/
175176
@Deprecated
176177
public ReflectionDiffBuilder(final T left, final T right, final ToStringStyle style) {
177-
this(DiffBuilder.<T>builder().setLeft(left).setRight(right).setStyle(style).build(), null);
178+
this(DiffBuilder.<T>builder().setLeft(left).setRight(right).setStyle(style).build(), ArrayUtils.EMPTY_STRING_ARRAY);
178179
}
179180

180181
private boolean accept(final Field field) {
@@ -187,7 +188,7 @@ private boolean accept(final Field field) {
187188
if (Modifier.isStatic(field.getModifiers())) {
188189
return false;
189190
}
190-
if (this.excludeFieldNames != null && Arrays.binarySearch(this.excludeFieldNames, field.getName()) >= 0) {
191+
if (excludeFieldNames != null && Arrays.binarySearch(excludeFieldNames, field.getName()) >= 0) {
191192
// Reject fields from the getExcludeFieldNames list.
192193
return false;
193194
}
@@ -236,7 +237,7 @@ public DiffResult<T> build() {
236237
* @since 3.13.0
237238
*/
238239
public String[] getExcludeFieldNames() {
239-
return this.excludeFieldNames.clone();
240+
return excludeFieldNames.clone();
240241
}
241242

242243
private T getLeft() {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ void testDifferenceInInherited_field() {
146146
assertEquals(1, list.getNumberOfDiffs());
147147
}
148148

149+
@Test
150+
void testGetExcludeFieldNamesEmpty() {
151+
final ReflectionDiffBuilder reflectionDiffBuilder = new ReflectionDiffBuilder(new TypeTestClass(), new TypeTestChildClass(), SHORT_STYLE);
152+
final String[] excludeFieldNames = reflectionDiffBuilder.getExcludeFieldNames();
153+
assertNotNull(excludeFieldNames);
154+
assertEquals(0, excludeFieldNames.length);
155+
}
156+
149157
@Test
150158
void testGetExcludeFieldNamesWithNullExcludedFieldNames() {
151159
// @formatter:off

0 commit comments

Comments
 (0)