Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply AutoCloseableMustBeClosed #1009

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 9 additions & 0 deletions changelog/@unreleased/pr-1009.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down