From 0944487390685b5a6059472d20f20509350f87e1 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:50:36 -0500 Subject: [PATCH] Code cleanup: UnitOfWorkInvoker and UnitOfWorkInvokerFactory (#98) * Update obsolete javadoc comment in UnitOfWorkInvoker. Dropwizard made UnitOfWorkAspect public a long time ago, in version 1.1.0. * Modify UnitOfWorkInvoker#invoke to use try-with-resources for the Hibernate Session, and wrap the Map#get call with requireNonNull since the containsKey check returned true. Remove redundant RuntimeException type parameter in call to rethrow. * Use var in UnitOfWorkInvokerFactory for the ImmutableMap.Builder to reduce code verbosity * Refactor logic in UnitOfWorkInvokerFactory so that it doesn't reassign a local variable. Instead, if the "unitOfWorkMethods" map is empty, return the rootInvoker. This inverts the logic and is clearer, since re-assigning variables is generally not a good practice. --- .../jakarta/xml/ws/UnitOfWorkInvoker.java | 25 ++++++++++++------- .../xml/ws/UnitOfWorkInvokerFactory.java | 11 +++----- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/UnitOfWorkInvoker.java b/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/UnitOfWorkInvoker.java index 682cfdf..31e5605 100644 --- a/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/UnitOfWorkInvoker.java +++ b/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/UnitOfWorkInvoker.java @@ -1,5 +1,7 @@ package org.kiwiproject.dropwizard.jakarta.xml.ws; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.ImmutableMap; import io.dropwizard.hibernate.UnitOfWork; import org.apache.cxf.message.Exchange; @@ -12,7 +14,15 @@ /** * Wraps underlying invoker in a Hibernate session. Code in this class is based on Dropwizard's UnitOfWorkApplication - * listener and UnitOfWorkAspect. We don't use UnitOfWorkAspect here because it is declared package private. + * listener and UnitOfWorkAspect. + *
+ * Background information:
+ * The javadocs used to state "We don't use UnitOfWorkAspect here because it is declared package private." This
+ * was true up until Dropwizard 1.1.0, which made UnitOfWorkAspect public in
+ * this PR. See
+ * this discussion which
+ * proposes to change this class to use UnitOfWorkAspect directly.
+ *
*
* @see io.dropwizard.hibernate.UnitOfWorkAspect
* @see io.dropwizard.hibernate.UnitOfWorkApplicationListener
@@ -34,14 +44,12 @@ public UnitOfWorkInvoker(Invoker underlying, ImmutableMap