Skip to content

Commit

Permalink
all: Enable ErrorProne during compilation
Browse files Browse the repository at this point in the history
ErrorProne provides static analysis for common issues, including
misused variables GuardedBy locks.

This increases build time by 60% for parallel builds and 30% for
non-parallel, so I've provided a way to disable the check. It is on by
default though and will be run in our CI environments.
  • Loading branch information
ejona86 authored Feb 24, 2017
1 parent a2f15ae commit 675080b
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ before_install:
- mkdir -p $HOME/.gradle
- echo "checkstyle.ignoreFailures=false" >> $HOME/.gradle/gradle.properties
- echo "failOnWarnings=true" >> $HOME/.gradle/gradle.properties
- echo "errorProne=true" >> $HOME/.gradle/gradle.properties

install:
- ./gradlew assemble generateTestProto install
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
/**
* A {@link Drainable} {@code InputStream} that reads an {@link ByteBuf}.
*/
@SuppressWarnings("InputStreamSlowMultibyteRead") // doesn't matter if slow. It'll throw
public class ByteBufInputStream extends InputStream
implements Drainable, KnownLength {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

package io.grpc.benchmarks;

import com.google.errorprone.annotations.Immutable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
* Verifies whether or not the given {@link SocketAddress} is valid.
*/
@Immutable
public interface SocketAddressValidator {
/**
* Verifier for {@link InetSocketAddress}es.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public void testHistogramToStatsConversion() throws Exception {

Stats.ClientStats stats = loadClient.getStats();

assertEquals(1.0, stats.getLatencies().getMinSeen());
assertEquals(1000.0, stats.getLatencies().getMaxSeen());
assertEquals(10.0, stats.getLatencies().getCount());
assertEquals(1.0, stats.getLatencies().getMinSeen(), 0.0);
assertEquals(1000.0, stats.getLatencies().getMaxSeen(), 0.0);
assertEquals(10.0, stats.getLatencies().getCount(), 0.0);

double base = 0;
double logBase = 1;
Expand Down
19 changes: 16 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ buildscript {
repositories {
mavenCentral()
mavenLocal()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
classpath "ru.vyarus:gradle-animalsniffer-plugin:1.2.0"
classpath 'ru.vyarus:gradle-animalsniffer-plugin:1.2.0'
classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.9'
}
}

Expand All @@ -20,6 +24,9 @@ subprojects {
apply plugin: "com.google.osdetector"
// The plugin only has an effect if a signature is specified
apply plugin: "ru.vyarus.animalsniffer"
if (!rootProject.hasProperty('errorProne') || rootProject.errorProne.toBoolean()) {
apply plugin: "net.ltgt.errorprone"
}

group = "io.grpc"
version = "1.2.0-SNAPSHOT" // CURRENT_GRPC_VERSION
Expand All @@ -33,7 +40,7 @@ subprojects {
}

[compileJava, compileTestJava].each() {
it.options.compilerArgs += ["-Xlint:all", "-Xlint:-options"]
it.options.compilerArgs += ["-Xlint:all", "-Xlint:-options", "-Xlint:-path"]
it.options.encoding = "UTF-8"
if (rootProject.hasProperty('failOnWarnings') && rootProject.failOnWarnings.toBoolean()) {
it.options.compilerArgs += ["-Werror"]
Expand Down Expand Up @@ -136,7 +143,9 @@ subprojects {

[compileJava, compileTestJava].each() {
// Protobuf-generated code produces some warnings.
it.options.compilerArgs += ["-Xlint:-cast"]
// https://github.com/google/protobuf/issues/2718
it.options.compilerArgs += ["-Xlint:-cast", "-Xep:MissingOverride:OFF",
"-Xep:ReferenceEquality:OFF", "-Xep:FunctionalInterfaceClash:OFF"]
}
}

Expand Down Expand Up @@ -198,6 +207,10 @@ subprojects {

// Configuration for modules that use Netty tcnative (for OpenSSL).
tcnative libraries.netty_tcnative

// The ErrorProne plugin defaults to the latest, which would break our
// build if error prone releases a new version with a new check
errorprone 'com.google.errorprone:error_prone_core:2.0.15'
}

signing {
Expand Down
6 changes: 4 additions & 2 deletions compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@ sourceSets {
}

compileTestJava {
options.compilerArgs += ["-Xlint:-cast"]
options.compilerArgs += ["-Xlint:-cast", "-Xep:MissingOverride:OFF",
"-Xep:ReferenceEquality:OFF", "-Xep:FunctionalInterfaceClash:OFF"]
}

compileTestLiteJava {
// Protobuf-generated Lite produces quite a few warnings.
options.compilerArgs += ["-Xlint:-rawtypes", "-Xlint:-unchecked"]
options.compilerArgs += ["-Xlint:-rawtypes", "-Xlint:-unchecked",
"-Xep:MissingOverride:OFF", "-Xep:ReferenceEquality:OFF"]
}

compileTestNanoJava {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/io/grpc/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public enum Code {
UNAUTHENTICATED(16);

private final int value;
@SuppressWarnings("ImmutableEnumChecker") // we make sure the byte[] can't be modified
private final byte[] valueAscii;

private Code(int value) {
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/io/grpc/internal/GrpcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ private static Http2Error[] buildHttp2CodeMap() {
}

private final int code;
// Status is not guaranteed to be deeply immutable. Don't care though, since that's only true
// when there are exceptions in the Status, which is not true here.
@SuppressWarnings("ImmutableEnumChecker")
private final Status status;

Http2Error(int code, Status status) {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/io/grpc/internal/ServerCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public ServerStreamListenerImpl(
this.statsTraceCtx = checkNotNull(statsTraceCtx, "statsTraceCtx");
}

@SuppressWarnings("Finally") // The code avoids suppressing the exception thrown from try
@Override
public void messageRead(final InputStream message) {
Throwable t = null;
Expand Down
3 changes: 2 additions & 1 deletion core/src/test/java/io/grpc/ConnectivityStateInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void equality() {
assertNotEquals(info4, info6);

assertFalse(info1.equals(null));
assertFalse(info1.equals(this));
// Extra cast to avoid ErrorProne EqualsIncompatibleType failure
assertFalse(((Object) info1).equals(this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ private StreamingOutputCallResponse toResponse() {
/**
* Creates a buffer with data read from a file.
*/
@SuppressWarnings("Finally") // Not concerned about suppression; expected to be exceedingly rare
private ByteString createBufferFromFile(String fileClassPath) {
ByteString buffer = ByteString.EMPTY;
InputStream inputStream = getClass().getResourceAsStream(fileClassPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void run() {
}

// server with echo and streaming modes
private class Server implements Runnable {
private static class Server implements Runnable {
private ServerSocket server;
private Socket rcv;
private boolean shutDown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void failInvalidAuthority() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid host or port");

NettyChannelBuilder.forAddress(new InetSocketAddress("invalid_authority", 1234));
Object unused =
NettyChannelBuilder.forAddress(new InetSocketAddress("invalid_authority", 1234));
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion netty/src/test/java/io/grpc/netty/NettyStreamTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

package io.grpc.netty;

import static com.google.common.base.Charsets.US_ASCII;
import static io.grpc.netty.NettyTestUtil.messageFrame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -199,7 +200,7 @@ public void shouldNotBeReadyForDataAfterWritingLargeMessage() throws IOException
}

protected byte[] smallMessage() {
return MESSAGE.getBytes();
return MESSAGE.getBytes(US_ASCII);
}

protected byte[] largeMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void tlsHandler_failsOnNullEngine() throws Exception {
thrown.expect(NullPointerException.class);
thrown.expectMessage("ssl");

ProtocolNegotiators.serverTls(null);
Object unused = ProtocolNegotiators.serverTls(null);
}

@Test
Expand Down Expand Up @@ -264,7 +264,7 @@ public boolean isLoggable(LogRecord record) {
public void tls_failsOnNullSslContext() {
thrown.expect(NullPointerException.class);

ProtocolNegotiators.tls(null, "authority");
Object unused = ProtocolNegotiators.tls(null, "authority");
}

@Test
Expand Down Expand Up @@ -299,13 +299,14 @@ public void tls_invalidHost() throws SSLException {
@Test
public void httpProxy_nullAddressNpe() throws Exception {
thrown.expect(NullPointerException.class);
ProtocolNegotiators.httpProxy(null, "user", "pass", ProtocolNegotiators.plaintext());
Object unused =
ProtocolNegotiators.httpProxy(null, "user", "pass", ProtocolNegotiators.plaintext());
}

@Test
public void httpProxy_nullNegotiatorNpe() throws Exception {
thrown.expect(NullPointerException.class);
ProtocolNegotiators.httpProxy(
Object unused = ProtocolNegotiators.httpProxy(
InetSocketAddress.createUnresolved("localhost", 80), "user", "pass", null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private void writeStreams() {
/**
* Simple status that keeps track of the number of writes performed.
*/
private final class WriteStatus {
private static final class WriteStatus {
int numWrites;

void incrementNumWrites() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,8 @@ void assertClosed() {
}
}

// The wait is safe; nextFrame is called in a loop and can have spurious wakeups
@SuppressWarnings("WaitNotInLoop")
@Override
public boolean nextFrame(Handler handler) throws IOException {
Result result;
Expand Down Expand Up @@ -1692,6 +1694,7 @@ void waitUntilStreamClosed() throws InterruptedException {
}
}

@SuppressWarnings("Finally") // We don't care about suppressed exceptions in the test
static String getContent(InputStream message) {
BufferedReader br = new BufferedReader(new InputStreamReader(message, UTF_8));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private String quotedAV() {
}

// gets hex string attribute value: "#" hexstring
@SuppressWarnings("NarrowingCompoundAssignment")
private String hexAV() {
if (pos + 4 >= length) {
// encoded byte array must be not less then 4 c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ private static Platform findPlatform() {
private static Provider getAppEngineProvider() {
try {
// Forcibly load conscrypt as it is unlikely to be an installed provider on AppEngine
return (Provider) Class.forName("org.conscrypt.OpenSSLProvider").newInstance();
return (Provider) Class.forName("org.conscrypt.OpenSSLProvider")
.getConstructor().newInstance();
} catch (Throwable t) {
throw new RuntimeException("Unable to load conscrypt security provider", t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ private void buildTree() {
}
}

@SuppressWarnings("NarrowingCompoundAssignment")
private void addCode(int sym, int code, byte len) {
Node terminal = new Node(sym, len);

Expand Down
3 changes: 2 additions & 1 deletion protobuf-lite/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ dependencies {

compileTestJava {
// Protobuf-generated Lite produces quite a few warnings.
options.compilerArgs += ["-Xlint:-rawtypes", "-Xlint:-unchecked", "-Xlint:-fallthrough"]
options.compilerArgs += ["-Xlint:-rawtypes", "-Xlint:-unchecked", "-Xlint:-fallthrough",
"-Xep:MissingOverride:OFF", "-Xep:ReferenceEquality:OFF"]
}

protobuf {
Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/java/io/grpc/testing/DeadlineSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void of(Deadline expected) {
* A partially specified proposition about an approximate relationship to a {@code deadline}
* subject using a tolerance.
*/
public abstract class TolerantDeadlineComparison {
public abstract static class TolerantDeadlineComparison {

private TolerantDeadlineComparison() {}

Expand Down
8 changes: 7 additions & 1 deletion thrift/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ project.sourceSets {
}
}

compileTestJava {
// Thrift-generated code produces some warnings.
options.compilerArgs += ["-Xep:MissingOverride:OFF",
"-Xep:NonOverridingEquals:OFF", "-Xep:TypeParameterUnusedInFormals:OFF"]
}

idea {
module {
sourceDirs += file("${projectDir}/src/generated/test/java");
}
}
}
1 change: 1 addition & 0 deletions thrift/src/main/java/io/grpc/thrift/ThriftInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.thrift.TSerializer;

/** InputStream for Thrift. */
@SuppressWarnings("InputStreamSlowMultibyteRead") // TODO(ejona): would be good to fix
final class ThriftInputStream extends InputStream implements Drainable, KnownLength {

/**
Expand Down

0 comments on commit 675080b

Please sign in to comment.