From 6be34ec2292e2ffb0325fda8cc1b41f7f46444c4 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Thu, 8 Feb 2024 15:28:31 -0500 Subject: [PATCH] Code cleanup: InstrumentedInvokers * Wrap requireNonNull around calls to Map#get after a containsKey check returns true * Replace explicit try/finally with try-with-resources for Timer.Context * Use var instead of "Meter meter" and "Timer timer", etc. * Remove redundant type parameter in call to rethrow --- .../jakarta/xml/ws/InstrumentedInvokers.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/InstrumentedInvokers.java b/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/InstrumentedInvokers.java index 2e8c778..55188d6 100644 --- a/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/InstrumentedInvokers.java +++ b/dropwizard-jakarta-xml-ws/src/main/java/org/kiwiproject/dropwizard/jakarta/xml/ws/InstrumentedInvokers.java @@ -1,5 +1,7 @@ package org.kiwiproject.dropwizard.jakarta.xml.ws; +import static java.util.Objects.requireNonNull; + import com.codahale.metrics.Meter; import com.codahale.metrics.Timer; import com.google.common.collect.ImmutableMap; @@ -33,15 +35,14 @@ public TimedInvoker(Invoker underlying, ImmutableMap timers) { public Object invoke(Exchange exchange, Object o) { Object result; - String methodname = this.getTargetMethod(exchange).getName(); + String methodName = this.getTargetMethod(exchange).getName(); + + if (timers.containsKey(methodName)) { + var timer = requireNonNull(timers.get(methodName)); - if (timers.containsKey(methodname)) { - Timer timer = timers.get(methodname); - final Timer.Context context = timer.time(); - try { + // Timer.Context is AutoCloseable, so this starts and closes (stops) the Timer.Context + try (Timer.Context ignored = timer.time()) { result = this.underlying.invoke(exchange, o); - } finally { - context.stop(); } } else { result = this.underlying.invoke(exchange, o); @@ -66,10 +67,10 @@ public MeteredInvoker(Invoker underlying, ImmutableMap meters) { public Object invoke(Exchange exchange, Object o) { Object result; - String methodname = this.getTargetMethod(exchange).getName(); + String methodName = this.getTargetMethod(exchange).getName(); - if (meters.containsKey(methodname)) { - Meter meter = meters.get(methodname); + if (meters.containsKey(methodName)) { + var meter = requireNonNull(meters.get(methodName)); meter.mark(); } result = this.underlying.invoke(exchange, o); @@ -111,22 +112,23 @@ public ExceptionMeteredInvoker(Invoker underlying, ImmutableMaprethrow(e); // unchecked rethrow + this.rethrow(e); // unchecked rethrow return null; // avoid compiler warning } }