From b9f15bc007be2bb0ba9633559482396e0352785a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 12 May 2024 15:31:29 -0400 Subject: [PATCH] Make LockVisitor.acceptReadLocked(FailableConsumer) null-safe Make LockVisitor.applyWriteLocked(FailableConsumer) null-safe --- src/changes/changes.xml | 2 ++ .../commons/lang3/concurrent/locks/LockingVisitors.java | 9 ++++++--- .../lang3/concurrent/locks/LockingVisitorsTest.java | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 359100624..11a977dbc 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -126,6 +126,8 @@ The type attribute can be add,update,fix,remove. StringUtils.stripAccents() should handle ligatures, UTF32 math blocks, etc. #1201. TypeUtils.toString(Type) StackOverflowError for an inner class in the inner class parameterized enclosing class #657. Deprecate SystemUtils.getUserName(String) in favor of SystemProperties.getUserName(Supplier). + Make LockVisitor.acceptReadLocked(FailableConsumer) null-safe. + Make LockVisitor.applyWriteLocked(FailableConsumer) null-safe. Bump commons-parent from 64 to 69 #1194. Bump org.codehaus.mojo:exec-maven-plugin from 3.1.1 to 3.2.0 #1175. diff --git a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java index 400eb1a77..d83f52350 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java @@ -26,6 +26,7 @@ import java.util.function.Supplier; import org.apache.commons.lang3.function.Failable; import org.apache.commons.lang3.function.FailableConsumer; import org.apache.commons.lang3.function.FailableFunction; +import org.apache.commons.lang3.function.Suppliers; /** * Combines the monitor and visitor pattern to work with {@link java.util.concurrent.locks.Lock locked objects}. Locked @@ -262,10 +263,12 @@ public class LockingVisitors { * @see #acceptWriteLocked(FailableConsumer) */ protected void lockAcceptUnlock(final Supplier lockSupplier, final FailableConsumer consumer) { - final Lock lock = lockSupplier.get(); + final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock"); lock.lock(); try { - consumer.accept(object); + if (consumer != null) { + consumer.accept(object); + } } catch (final Throwable t) { throw Failable.rethrow(t); } finally { @@ -289,7 +292,7 @@ public class LockingVisitors { * @see #applyWriteLocked(FailableFunction) */ protected T lockApplyUnlock(final Supplier lockSupplier, final FailableFunction function) { - final Lock lock = lockSupplier.get(); + final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock"); lock.lock(); try { return function.apply(object); diff --git a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java index cdf78dae4..74343e891 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java @@ -93,8 +93,10 @@ public class LockingVisitorsTest extends AbstractLangTest { final AtomicInteger res = new AtomicInteger(); final ReadWriteLock rwLock = new ReentrantReadWriteLock(); LockingVisitors.create(res, rwLock).acceptReadLocked(AtomicInteger::incrementAndGet); + LockingVisitors.create(res, rwLock).acceptReadLocked(null); assertEquals(1, res.get()); LockingVisitors.create(res, rwLock).acceptWriteLocked(AtomicInteger::incrementAndGet); + LockingVisitors.create(res, rwLock).acceptWriteLocked(null); assertEquals(2, res.get()); }