From be0d7e911a9f6f0b07e7328834711fd9d5fb9b83 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Thu, 10 Sep 2015 09:47:17 -0700 Subject: [PATCH] Prevent construction of container classes and reduce API --- .../java/io/grpc/auth/ClientAuthInterceptor.java | 2 +- .../grpc/io/grpc/testing/TestServiceGrpc.java | 2 ++ .../main/grpc/io/grpc/testing/WorkerGrpc.java | 2 ++ compiler/src/java_plugin/cpp/java_generator.cpp | 3 +++ compiler/src/test/golden/TestService.java.txt | 2 ++ compiler/src/test/golden/TestServiceNano.java.txt | 2 ++ .../io/grpc/examples/helloworld/GreeterGrpc.java | 2 ++ .../grpc/examples/routeguide/RouteGuideGrpc.java | 2 ++ .../testing/integration/ReconnectServiceGrpc.java | 2 ++ .../grpc/testing/integration/TestServiceGrpc.java | 2 ++ .../integration/UnimplementedServiceGrpc.java | 2 ++ stub/src/main/java/io/grpc/stub/ClientCalls.java | 2 ++ .../src/main/java/io/grpc/stub/MetadataUtils.java | 15 +++++++++------ 13 files changed, 33 insertions(+), 7 deletions(-) diff --git a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java index d5f5661822..329685a917 100644 --- a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java +++ b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java @@ -58,7 +58,7 @@ import java.util.concurrent.Executor; *

Uses the new and simplified Google auth library: * https://github.com/google/google-auth-library-java */ -public class ClientAuthInterceptor implements ClientInterceptor { +public final class ClientAuthInterceptor implements ClientInterceptor { private final Credentials credentials; diff --git a/benchmarks/src/generated/main/grpc/io/grpc/testing/TestServiceGrpc.java b/benchmarks/src/generated/main/grpc/io/grpc/testing/TestServiceGrpc.java index 0fd865d746..f7b0c15fba 100644 --- a/benchmarks/src/generated/main/grpc/io/grpc/testing/TestServiceGrpc.java +++ b/benchmarks/src/generated/main/grpc/io/grpc/testing/TestServiceGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class TestServiceGrpc { + private TestServiceGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.TestService"; // Static method descriptors that strictly reflect the proto. diff --git a/benchmarks/src/generated/main/grpc/io/grpc/testing/WorkerGrpc.java b/benchmarks/src/generated/main/grpc/io/grpc/testing/WorkerGrpc.java index 1119fbd8b3..69bf37fb2f 100644 --- a/benchmarks/src/generated/main/grpc/io/grpc/testing/WorkerGrpc.java +++ b/benchmarks/src/generated/main/grpc/io/grpc/testing/WorkerGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class WorkerGrpc { + private WorkerGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.Worker"; // Static method descriptors that strictly reflect the proto. diff --git a/compiler/src/java_plugin/cpp/java_generator.cpp b/compiler/src/java_plugin/cpp/java_generator.cpp index f10779843e..0c33201f91 100644 --- a/compiler/src/java_plugin/cpp/java_generator.cpp +++ b/compiler/src/java_plugin/cpp/java_generator.cpp @@ -511,6 +511,9 @@ static void PrintService(const ServiceDescriptor* service, "@$Generated$(\"by gRPC proto compiler\")\n" "public class $service_class_name$ {\n\n"); p->Indent(); + p->Print( + *vars, + "private $service_class_name$() {}\n\n"); p->Print( *vars, diff --git a/compiler/src/test/golden/TestService.java.txt b/compiler/src/test/golden/TestService.java.txt index 7f5f4d4a35..66021a724e 100644 --- a/compiler/src/test/golden/TestService.java.txt +++ b/compiler/src/test/golden/TestService.java.txt @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class TestServiceGrpc { + private TestServiceGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.TestService"; // Static method descriptors that strictly reflect the proto. diff --git a/compiler/src/test/golden/TestServiceNano.java.txt b/compiler/src/test/golden/TestServiceNano.java.txt index 0af54f2ea6..ea6df135f3 100644 --- a/compiler/src/test/golden/TestServiceNano.java.txt +++ b/compiler/src/test/golden/TestServiceNano.java.txt @@ -18,6 +18,8 @@ import java.io.IOException; @javax.annotation.Generated("by gRPC proto compiler") public class TestServiceGrpc { + private TestServiceGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.TestService"; // Static method descriptors that strictly reflect the proto. diff --git a/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java b/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java index 806f12bc0a..8639db1706 100644 --- a/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java +++ b/examples/src/generated/main/grpc/io/grpc/examples/helloworld/GreeterGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class GreeterGrpc { + private GreeterGrpc() {} + public static final String SERVICE_NAME = "helloworld.Greeter"; // Static method descriptors that strictly reflect the proto. diff --git a/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java b/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java index 28c0e86ec1..21f7852bd4 100644 --- a/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java +++ b/examples/src/generated/main/grpc/io/grpc/examples/routeguide/RouteGuideGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class RouteGuideGrpc { + private RouteGuideGrpc() {} + public static final String SERVICE_NAME = "routeguide.RouteGuide"; // Static method descriptors that strictly reflect the proto. diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java index a13667e2bb..372dc54648 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/ReconnectServiceGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class ReconnectServiceGrpc { + private ReconnectServiceGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.ReconnectService"; // Static method descriptors that strictly reflect the proto. diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java index 22aba3ea37..9c096736c2 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/TestServiceGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class TestServiceGrpc { + private TestServiceGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.TestService"; // Static method descriptors that strictly reflect the proto. diff --git a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java index 2e28547da2..4d05ec95c4 100644 --- a/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java +++ b/interop-testing/src/generated/main/grpc/io/grpc/testing/integration/UnimplementedServiceGrpc.java @@ -16,6 +16,8 @@ import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; @javax.annotation.Generated("by gRPC proto compiler") public class UnimplementedServiceGrpc { + private UnimplementedServiceGrpc() {} + public static final String SERVICE_NAME = "grpc.testing.UnimplementedService"; // Static method descriptors that strictly reflect the proto. diff --git a/stub/src/main/java/io/grpc/stub/ClientCalls.java b/stub/src/main/java/io/grpc/stub/ClientCalls.java index ecb0d5dd5e..4a6ef5d8f0 100644 --- a/stub/src/main/java/io/grpc/stub/ClientCalls.java +++ b/stub/src/main/java/io/grpc/stub/ClientCalls.java @@ -55,6 +55,8 @@ import javax.annotation.Nullable; * that the runtime can vary behavior without requiring regeneration of the stub. */ public class ClientCalls { + // Prevent instantiation + private ClientCalls() {} /** * Executes a unary call with a response {@link StreamObserver}. diff --git a/stub/src/main/java/io/grpc/stub/MetadataUtils.java b/stub/src/main/java/io/grpc/stub/MetadataUtils.java index 9962ee4f42..76d2ad327f 100644 --- a/stub/src/main/java/io/grpc/stub/MetadataUtils.java +++ b/stub/src/main/java/io/grpc/stub/MetadataUtils.java @@ -35,6 +35,7 @@ import io.grpc.CallOptions; import io.grpc.Channel; import io.grpc.ClientCall; import io.grpc.ClientInterceptor; +import io.grpc.ExperimentalApi; import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; import io.grpc.Metadata; @@ -47,6 +48,8 @@ import java.util.concurrent.atomic.AtomicReference; * Utility functions for binding and receiving headers. */ public class MetadataUtils { + // Prevent instantiation + private MetadataUtils() {} /** * Attaches a set of request headers to a stub. @@ -55,11 +58,11 @@ public class MetadataUtils { * @param extraHeaders the headers to be passed by each call on the returned stub. * @return an implementation of the stub with {@code extraHeaders} bound to each call. */ - @SuppressWarnings({"unchecked", "rawtypes"}) - public static T attachHeaders( + @ExperimentalApi + public static > T attachHeaders( T stub, final Metadata extraHeaders) { - return (T) stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders)); + return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders)); } /** @@ -94,12 +97,12 @@ public class MetadataUtils { * @param trailersCapture to record the last received trailers * @return an implementation of the stub with {@code extraHeaders} bound to each call. */ - @SuppressWarnings({"unchecked", "rawtypes"}) - public static T captureMetadata( + @ExperimentalApi + public static > T captureMetadata( T stub, AtomicReference headersCapture, AtomicReference trailersCapture) { - return (T) stub.withInterceptors( + return stub.withInterceptors( newCaptureMetadataInterceptor(headersCapture, trailersCapture)); }