Skip to content

Commit 096b05e

Browse files
committed
Make Failable.run(FailableRunnable) null-safe
1 parent 064de49 commit 096b05e

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
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-1757" type="fix" dev="ggregory" due-to="Gary Gregory">Fix NullPointerException in MethodUtils.getMatchingAccessibleMethod((Class, String, Class...)).</action>
6666
<action issue="LANG-1698" type="fix" dev="ggregory" due-to="Jan Arne Sparka, Gary Gregory">Fix StackOverflowError in TypeUtils.typeVariableToString(TypeVariable), TypeUtils.toString(Type) on Java 17 and up.</action>
6767
<action issue="LANG-1511" type="fix" dev="ggregory" due-to="david cogen, Gary Gregory, Bruno P. Kinoshita">SystemUtils is missing important documentation.</action>
68+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.run(FailableRunnable) null-safe.</action>
6869
<!-- ADD -->
6970
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
7071
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>

src/main/java/org/apache/commons/lang3/function/Failable.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,16 @@ public static RuntimeException rethrow(final Throwable throwable) {
419419
/**
420420
* Runs a runnable and rethrows any exception as a {@link RuntimeException}.
421421
*
422-
* @param runnable The runnable to run
423-
* @param <E> the type of checked exception the runnable may throw
422+
* @param runnable The runnable to run, may be null for a noop.
423+
* @param <E> the type of checked exception the runnable may throw.
424424
*/
425425
public static <E extends Throwable> void run(final FailableRunnable<E> runnable) {
426-
try {
427-
runnable.run();
428-
} catch (final Throwable t) {
429-
throw rethrow(t);
426+
if (runnable != null) {
427+
try {
428+
runnable.run();
429+
} catch (final Throwable t) {
430+
throw rethrow(t);
431+
}
430432
}
431433
}
432434

src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,7 @@ public void testRunnable() {
14931493

14941494
// Even invocations, should not throw an exception
14951495
Failable.run(FailureOnOddInvocations::new);
1496+
Failable.run(null);
14961497
}
14971498

14981499
/**

0 commit comments

Comments
 (0)