From c0060c9e750df77662e04e7ab1b6dcd23a70d8b1 Mon Sep 17 00:00:00 2001 From: David Schlosnagle Date: Sat, 13 Mar 2021 13:24:17 -0500 Subject: [PATCH 1/2] Apply AutoCloseableMustBeClosed ./gradlew compileJava compileTestJava -PerrorProneApply=AutoCloseableMustBeClosed --- build.gradle | 3 ++- .../tritium/metrics/InstrumentedSslServerSocketFactory.java | 4 ++++ .../tritium/metrics/InstrumentedSslSocketFactory.java | 2 ++ .../tritium/metrics/TaggedMetricsExecutorService.java | 2 ++ .../palantir/tritium/metrics/InstrumentedSslContextTest.java | 2 ++ 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index cc9979fb7..282aeb530 100644 --- a/build.gradle +++ b/build.gradle @@ -76,7 +76,8 @@ allprojects { option('NullAway:AnnotatedPackages', 'com.palantir') // warnings not explicitly provided by error-prone - error 'NullAway', + error 'AutoCloseableMustBeClosed', + 'NullAway', 'Slf4jLogsafeArgs', 'PreferCollectionTransform', 'PreferListsPartition', diff --git a/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslServerSocketFactory.java b/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslServerSocketFactory.java index 81aaad95e..55ea46a75 100644 --- a/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslServerSocketFactory.java +++ b/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslServerSocketFactory.java @@ -16,6 +16,7 @@ package com.palantir.tritium.metrics; +import com.google.errorprone.annotations.MustBeClosed; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; @@ -94,6 +95,7 @@ public int hashCode() { return Objects.hash(name, delegate); } + @MustBeClosed private ServerSocket wrap(ServerSocket serverSocket) throws IOException { if (serverSocket instanceof SSLServerSocket) { return new InstrumentedServerSocket((SSLServerSocket) serverSocket, listener); @@ -106,6 +108,7 @@ private static final class InstrumentedServerSocket extends SSLServerSocket { private final SSLServerSocket delegate; private final HandshakeCompletedListener listener; + @MustBeClosed InstrumentedServerSocket(SSLServerSocket delegate, HandshakeCompletedListener listener) throws IOException { this.delegate = delegate; this.listener = listener; @@ -221,6 +224,7 @@ public Socket accept() throws IOException { return wrap(delegate.accept()); } + @MustBeClosed private Socket wrap(Socket socket) { if (socket instanceof SSLSocket && HandshakeInstrumentation.isSocketInstrumentationEnabled()) { ((SSLSocket) socket).addHandshakeCompletedListener(listener); diff --git a/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslSocketFactory.java b/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslSocketFactory.java index 08a639332..e29681d5c 100644 --- a/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslSocketFactory.java +++ b/tritium-metrics/src/main/java/com/palantir/tritium/metrics/InstrumentedSslSocketFactory.java @@ -16,6 +16,7 @@ package com.palantir.tritium.metrics; +import com.google.errorprone.annotations.MustBeClosed; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; @@ -105,6 +106,7 @@ public int hashCode() { return Objects.hash(delegate, name); } + @MustBeClosed private Socket wrap(Socket socket) { if (socket instanceof SSLSocket && HandshakeInstrumentation.isSocketInstrumentationEnabled()) { ((SSLSocket) socket).addHandshakeCompletedListener(listener); diff --git a/tritium-metrics/src/main/java/com/palantir/tritium/metrics/TaggedMetricsExecutorService.java b/tritium-metrics/src/main/java/com/palantir/tritium/metrics/TaggedMetricsExecutorService.java index ab58682d5..6d70b646f 100644 --- a/tritium-metrics/src/main/java/com/palantir/tritium/metrics/TaggedMetricsExecutorService.java +++ b/tritium-metrics/src/main/java/com/palantir/tritium/metrics/TaggedMetricsExecutorService.java @@ -19,6 +19,7 @@ import com.codahale.metrics.Counter; import com.codahale.metrics.Meter; import com.codahale.metrics.Timer; +import com.google.errorprone.annotations.MustBeClosed; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -141,6 +142,7 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE } @Nullable + @MustBeClosed private Timer.Context startQueueTimerIfEnabled() { Timer queuedDurationTimer = queuedDuration; return queuedDurationTimer == null ? null : queuedDurationTimer.time(); diff --git a/tritium-metrics/src/test/java/com/palantir/tritium/metrics/InstrumentedSslContextTest.java b/tritium-metrics/src/test/java/com/palantir/tritium/metrics/InstrumentedSslContextTest.java index bda68c1c8..e590e8995 100644 --- a/tritium-metrics/src/test/java/com/palantir/tritium/metrics/InstrumentedSslContextTest.java +++ b/tritium-metrics/src/test/java/com/palantir/tritium/metrics/InstrumentedSslContextTest.java @@ -20,6 +20,7 @@ import static org.assertj.core.api.Assumptions.assumeThat; import com.google.common.collect.MoreCollectors; +import com.google.errorprone.annotations.MustBeClosed; import com.palantir.tritium.event.InstrumentationProperties; import com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry; import com.palantir.tritium.metrics.registry.MetricName; @@ -258,6 +259,7 @@ void testSslSocketFactory_equality() throws IOException, GeneralSecurityExceptio assertThat(instrumentedFirst.hashCode()).isNotEqualTo(instrumentedThirdDifferentName.hashCode()); } + @MustBeClosed private static Closeable server(SSLContext context) { Undertow server = Undertow.builder() .addHttpsListener(PORT, "0.0.0.0", context) From 92b072fa63001ae6be2cebfcea4da80df678c9e0 Mon Sep 17 00:00:00 2001 From: David Schlosnagle Date: Fri, 5 Mar 2021 23:26:43 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-1009.v2.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 changelog/@unreleased/pr-1009.v2.yml diff --git a/changelog/@unreleased/pr-1009.v2.yml b/changelog/@unreleased/pr-1009.v2.yml new file mode 100644 index 000000000..0ee95f0da --- /dev/null +++ b/changelog/@unreleased/pr-1009.v2.yml @@ -0,0 +1,9 @@ +type: improvement +improvement: + description: |- + Methods returning `AutoCloseable` marked as `@MustBeClosed` + + ./gradlew clean compileJava compileTestJava -PerrorProneApply=AutoCloseableMustBeClosed + ./gradlew format + links: + - https://github.com/palantir/tritium/pull/1009