From 9ea47171fdcdbb4bdfd75ad793c26cffbc8f0c33 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 08:50:00 -0700 Subject: [PATCH 01/11] feat(ast): Add support for throwable causes --- .../api/generator/engine/ast/ThrowExpr.java | 18 ++++++ .../engine/writer/ImportWriterVisitor.java | 3 + .../engine/writer/JavaWriterVisitor.java | 7 +++ .../generator/engine/ast/ThrowExprTest.java | 61 +++++++++++++++++++ .../writer/ImportWriterVisitorTest.java | 37 +++++++++++ .../engine/writer/JavaWriterVisitorTest.java | 39 ++++++++++++ 6 files changed, 165 insertions(+) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java b/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java index 805499b074..b96a7abe53 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java @@ -28,6 +28,9 @@ public abstract class ThrowExpr implements Expr { @Nullable public abstract Expr messageExpr(); + @Nullable + public abstract Expr causeExpr(); + @Override public void accept(AstNodeVisitor visitor) { visitor.visit(this); @@ -47,22 +50,37 @@ public Builder setMessageExpr(String message) { public abstract Builder setMessageExpr(Expr expr); + public abstract Builder setCauseExpr(Expr expr); + // Private. abstract TypeNode type(); abstract Expr messageExpr(); + abstract Expr causeExpr(); + abstract ThrowExpr autoBuild(); public ThrowExpr build() { Preconditions.checkState( TypeNode.isExceptionType(type()), String.format("Type %s must be an exception type", type())); + if (messageExpr() != null) { Preconditions.checkState( messageExpr().type().equals(TypeNode.STRING), String.format("Message expression type must be a string for exception %s", type())); } + + if (causeExpr() != null) { + Preconditions.checkState( + ConcreteReference.withClazz(Throwable.class) + .isSupertypeOrEquals(causeExpr().type().reference()), + String.format( + "Cause expression type must be a subclass of Throwable, but found %s", + causeExpr().type())); + } + return autoBuild(); } } diff --git a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java index d54d1f0dfc..259a027402 100644 --- a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java @@ -234,6 +234,9 @@ public void visit(ThrowExpr throwExpr) { if (throwExpr.messageExpr() != null) { throwExpr.messageExpr().accept(this); } + if (throwExpr.causeExpr() != null) { + throwExpr.causeExpr().accept(this); + } } @Override diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 6b4cbc69d1..11ddff441d 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -399,6 +399,13 @@ public void visit(ThrowExpr throwExpr) { if (throwExpr.messageExpr() != null) { throwExpr.messageExpr().accept(this); } + if (throwExpr.causeExpr() != null) { + if (throwExpr.messageExpr() != null) { + buffer.append(COMMA); + space(); + } + throwExpr.causeExpr().accept(this); + } rightParen(); } diff --git a/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java b/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java index 48202e8552..f27d000113 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java @@ -62,4 +62,65 @@ public void createThrowExpr_badMessageExpr() { IllegalStateException.class, () -> ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).build()); } + + @Test + public void createThrowExpr_causeExpr() { + TypeNode npeType = + TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class)); + ThrowExpr.builder() + .setType(npeType) + .setCauseExpr( + NewObjectExpr.builder() + .setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))) + .build()) + .build(); + // Successfully created a ThrowExpr. + } + + @Test + public void createThrowExpr_causeExpr_throwableSubtype() { + TypeNode npeType = + TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class)); + ThrowExpr.builder() + .setType(npeType) + .setCauseExpr( + NewObjectExpr.builder() + .setType(TypeNode.withExceptionClazz(IllegalStateException.class)) + .build()) + .build(); + // Successfully created a ThrowExpr. + } + + @Test + public void createThrowExpr_causeExpr_onThrowableSubtype() { + TypeNode npeType = + TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class)); + assertThrows( + IllegalStateException.class, + () -> + ThrowExpr.builder() + .setType(npeType) + .setCauseExpr(NewObjectExpr.builder().setType(TypeNode.STRING).build()) + .build()); + } + + @Test + public void createThrowExpr_messageAndCauseExpr() { + TypeNode npeType = + TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class)); + Expr messageExpr = + MethodInvocationExpr.builder() + .setMethodName("foobar") + .setReturnType(TypeNode.STRING) + .build(); + ThrowExpr.builder() + .setType(npeType) + .setMessageExpr(messageExpr) + .setCauseExpr( + NewObjectExpr.builder() + .setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))) + .build()) + .build(); + // Successfully created a ThrowExpr. + } } diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 47a63ea6ee..053ea774d5 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -58,6 +58,7 @@ import com.google.common.base.Function; import com.google.common.base.Strings; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -475,6 +476,42 @@ public void writeThrowExprImports_messageExpr() { writerVisitor.write()); } + @Test + public void writeThrowExprImports_messageAndCauseExpr() { + TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class); + Expr messageExpr = + MethodInvocationExpr.builder() + .setStaticReferenceType( + TypeNode.withReference(ConcreteReference.withClazz(IfStatement.class))) + .setMethodName("conditionExpr") + .setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))) + .build(); + + messageExpr = + MethodInvocationExpr.builder() + .setExprReferenceExpr(messageExpr) + .setMethodName("foobar") + .setReturnType(TypeNode.STRING) + .build(); + ThrowExpr throwExpr = + ThrowExpr.builder() + .setType(npeType) + .setMessageExpr(messageExpr) + .setCauseExpr( + NewObjectExpr.builder() + .setType(TypeNode.withExceptionClazz(FileNotFoundException.class)) + .build()) + .build(); + + throwExpr.accept(writerVisitor); + assertEquals( + LineFormatter.lines( + "import com.google.api.generator.engine.ast.Expr;\n", + "import com.google.api.generator.engine.ast.IfStatement;\n", + "import java.io.FileNotFoundException;\n\n"), + writerVisitor.write()); + } + @Test public void writeInstanceofExprImports_basic() { TypeNode exprType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class)); diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index ac1f515169..c07fbd32cf 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1041,6 +1041,22 @@ public void writeThrowExpr_basicWithMessage() { assertEquals("throw new NullPointerException(\"Some message asdf\")", writerVisitor.write()); } + @Test + public void writeThrowExpr_basicWithCause() { + TypeNode npeType = + TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class)); + ThrowExpr throwExpr = + ThrowExpr.builder() + .setType(npeType) + .setCauseExpr( + NewObjectExpr.builder() + .setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))) + .build()) + .build(); + throwExpr.accept(writerVisitor); + assertEquals("throw new NullPointerException(new Throwable())", writerVisitor.write()); + } + @Test public void writeThrowExpr_messageExpr() { TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class); @@ -1055,6 +1071,29 @@ public void writeThrowExpr_messageExpr() { assertEquals("throw new NullPointerException(foobar())", writerVisitor.write()); } + @Test + public void writeThrowExpr_messageAndCauseExpr() { + TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class); + Expr messageExpr = + MethodInvocationExpr.builder() + .setMethodName("foobar") + .setReturnType(TypeNode.STRING) + .build(); + ThrowExpr throwExpr = + ThrowExpr.builder() + .setType(npeType) + .setMessageExpr(messageExpr) + .setCauseExpr( + NewObjectExpr.builder() + .setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))) + .build()) + .build(); + + throwExpr.accept(writerVisitor); + assertEquals( + "throw new NullPointerException(foobar(), new Throwable())", writerVisitor.write()); + } + @Test public void writeInstanceofExpr() { Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build(); From 4ac2d7905e87160f97bd188de2266e47bc7d4c8c Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 10:50:00 -0700 Subject: [PATCH 02/11] fix: call backgroundResources.close() on stub.close() --- .../AbstractServiceStubClassComposer.java | 42 ++++++++++++++++++- .../goldens/GrpcDeprecatedServiceStub.golden | 6 ++- .../composer/grpc/goldens/GrpcEchoStub.golden | 6 ++- .../grpc/goldens/GrpcPublisherStub.golden | 6 ++- .../grpc/goldens/GrpcTestingStub.golden | 6 ++- .../goldens/HttpJsonComplianceStub.golden | 6 ++- .../asset/v1/stub/GrpcAssetServiceStub.java | 6 ++- .../v1/stub/HttpJsonAddressesStub.java | 6 ++- .../v1/stub/HttpJsonRegionOperationsStub.java | 6 ++- .../v1/stub/GrpcIamCredentialsStub.java | 6 ++- .../google/iam/v1/stub/GrpcIAMPolicyStub.java | 6 ++- .../v1/stub/GrpcKeyManagementServiceStub.java | 6 ++- .../v1/stub/GrpcLibraryServiceStub.java | 6 ++- .../v2/stub/GrpcConfigServiceV2Stub.java | 6 ++- .../v2/stub/GrpcLoggingServiceV2Stub.java | 6 ++- .../v2/stub/GrpcMetricsServiceV2Stub.java | 6 ++- .../pubsub/v1/stub/GrpcPublisherStub.java | 6 ++- .../pubsub/v1/stub/GrpcSchemaServiceStub.java | 6 ++- .../pubsub/v1/stub/GrpcSubscriberStub.java | 6 ++- .../v1beta1/stub/GrpcCloudRedisStub.java | 6 ++- 20 files changed, 135 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java index a2ac589a65..da5c06681a 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java @@ -40,6 +40,8 @@ import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.ThisObjectValue; +import com.google.api.generator.engine.ast.ThrowExpr; +import com.google.api.generator.engine.ast.TryCatchStatement; import com.google.api.generator.engine.ast.TypeNode; import com.google.api.generator.engine.ast.ValueExpr; import com.google.api.generator.engine.ast.Variable; @@ -791,6 +793,23 @@ private List createStubOverrideMethods( .build()) .build(); + // Generate the close() method: + // @Override + // public final void close() { + // try { + // backgroundResources.close(); + // } catch (Exception e) { + // throw new IllegalStateException("Failed to close resource", e); + // } + // } + VariableExpr catchExceptionVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setType(TypeNode.withExceptionClazz(Exception.class)) + .setName("e") + .build()) + .build(); List javaMethods = new ArrayList<>(); javaMethods.add( methodMakerStarterFn @@ -799,8 +818,27 @@ private List createStubOverrideMethods( .setReturnType(TypeNode.VOID) .setBody( Arrays.asList( - ExprStatement.withExpr( - MethodInvocationExpr.builder().setMethodName("shutdown").build()))) + TryCatchStatement.builder() + .setTryBody( + Arrays.asList( + ExprStatement.withExpr( + MethodInvocationExpr.builder() + .setExprReferenceExpr(backgroundResourcesVarExpr) + .setMethodName("close") + .build()))) + .setCatchVariableExpr( + catchExceptionVarExpr.toBuilder().setIsDecl(true).build()) + .setCatchBody( + Arrays.asList( + ExprStatement.withExpr( + ThrowExpr.builder() + .setType( + TypeNode.withExceptionClazz( + IllegalStateException.class)) + .setMessageExpr(String.format("Failed to close resource")) + .setCauseExpr(catchExceptionVarExpr) + .build()))) + .build())) .build()); javaMethods.add(voidMethodMakerFn.apply("shutdown")); javaMethods.add(booleanMethodMakerFn.apply("isShutdown")); diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden index 431b6688f5..130e2caf27 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden @@ -125,7 +125,11 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden index d1a4a303bd..de44e89d22 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden @@ -341,7 +341,11 @@ public class GrpcEchoStub extends EchoStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden index 9e221ab5a2..4e81906d39 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden @@ -430,7 +430,11 @@ public class GrpcPublisherStub extends PublisherStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden index ed6f22313a..296d526fc2 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden @@ -382,7 +382,11 @@ public class GrpcTestingStub extends TestingStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden index 1ddcb3bf39..703243fe91 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden @@ -484,7 +484,11 @@ public class HttpJsonComplianceStub extends ComplianceStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java index 712d328088..d9c95603fe 100644 --- a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java +++ b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java @@ -598,7 +598,11 @@ public UnaryCallable deleteFeedCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java index 53a0eb8425..37bd3aea38 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java @@ -403,7 +403,11 @@ public UnaryCallable listPagedCallable( @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java index 0be8cf4d03..d5578176de 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java @@ -167,7 +167,11 @@ public UnaryCallable getCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java index 48731cd53d..bc04cab818 100644 --- a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java +++ b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java @@ -239,7 +239,11 @@ public UnaryCallable signJwtCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java index f40c2bfffe..2c1e96db38 100644 --- a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java +++ b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java @@ -197,7 +197,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java index 19065361ed..13a238d3fc 100644 --- a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java +++ b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java @@ -1119,7 +1119,11 @@ public UnaryCallable getLocationCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java index c912a9b7b3..8e3e2b9c27 100644 --- a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java +++ b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java @@ -450,7 +450,11 @@ public UnaryCallable moveBookCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java index 91ddb14f6a..e2a5c38b45 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java @@ -889,7 +889,11 @@ public UnaryCallable updateCmekSettings @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java index feb0ed3086..48b05cf5a1 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java @@ -328,7 +328,11 @@ public UnaryCallable listLogsPagedCallab @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java index fe15cf6052..da933b1ac4 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java @@ -279,7 +279,11 @@ public UnaryCallable deleteLogMetricCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java index 2b5ce28a06..cff38b2fd6 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java @@ -549,7 +549,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java index 11bac90c7d..094bcea972 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java @@ -412,7 +412,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java index 8580e626f8..20817d2eb9 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java @@ -762,7 +762,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java index 24c34f9b6e..0b73bc8797 100644 --- a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java +++ b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java @@ -505,7 +505,11 @@ public OperationCallable deleteInstanceOperat @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override From 5ea11bf8f7ac502e23929a26416b36a2dd4bcb74 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Fri, 30 Jul 2021 14:52:12 -0700 Subject: [PATCH 03/11] fix: make Throwable a static final in TypeNode --- .../java/com/google/api/generator/engine/ast/ThrowExpr.java | 3 +-- .../java/com/google/api/generator/engine/ast/TypeNode.java | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java b/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java index b96a7abe53..5f86e69d3f 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java @@ -74,8 +74,7 @@ public ThrowExpr build() { if (causeExpr() != null) { Preconditions.checkState( - ConcreteReference.withClazz(Throwable.class) - .isSupertypeOrEquals(causeExpr().type().reference()), + TypeNode.THROWABLE.reference().isSupertypeOrEquals(causeExpr().type().reference()), String.format( "Cause expression type must be a subclass of Throwable, but found %s", causeExpr().type())); diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index 5685efb1f0..99de1a213d 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -81,6 +81,9 @@ public enum TypeKind { public static final TypeNode STRING = withReference(ConcreteReference.withClazz(String.class)); public static final TypeNode VOID_OBJECT = withReference(ConcreteReference.withClazz(Void.class)); + public static final TypeNode THROWABLE = + withReference(ConcreteReference.withClazz(Throwable.class)); + public static final TypeNode DEPRECATED = withReference(ConcreteReference.withClazz(Deprecated.class)); From 7ea5032618cb7d597fe667bea160f1121a35e437 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 11:43:18 -0700 Subject: [PATCH 04/11] fix: update goldens --- .../storage/com/google/storage/v2/stub/GrpcStorageStub.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java index a0ae53ba0e..71c1cdac29 100644 --- a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java +++ b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java @@ -201,7 +201,11 @@ public ClientStreamingCallable writeObj @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override From d5fc5cf234729fab2ff1a8c1fac0286748ef364d Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 13:40:27 -0700 Subject: [PATCH 05/11] feat(ast): support throwing all kinds of expressions --- .../api/generator/engine/ast/ThrowExpr.java | 26 ++++++ .../engine/writer/ImportWriterVisitor.java | 7 ++ .../engine/writer/JavaWriterVisitor.java | 7 ++ .../generator/engine/ast/ThrowExprTest.java | 89 +++++++++++++++++++ .../writer/ImportWriterVisitorTest.java | 20 +++++ .../engine/writer/JavaWriterVisitorTest.java | 15 ++++ 6 files changed, 164 insertions(+) diff --git a/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java b/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java index 5f86e69d3f..5e4808d28a 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java @@ -22,6 +22,9 @@ public abstract class ThrowExpr implements Expr { // TODO(miraleung): Refactor with StringObjectValue and possibly with NewObjectExpr. + @Nullable + public abstract Expr throwExpr(); + @Override public abstract TypeNode type(); @@ -42,6 +45,9 @@ public static Builder builder() { @AutoValue.Builder public abstract static class Builder { + public abstract Builder setThrowExpr(Expr throwExpr); + + // No-op if setThrowExpr is called. public abstract Builder setType(TypeNode type); public Builder setMessageExpr(String message) { @@ -53,6 +59,8 @@ public Builder setMessageExpr(String message) { public abstract Builder setCauseExpr(Expr expr); // Private. + abstract Expr throwExpr(); + abstract TypeNode type(); abstract Expr messageExpr(); @@ -62,6 +70,24 @@ public Builder setMessageExpr(String message) { abstract ThrowExpr autoBuild(); public ThrowExpr build() { + if (throwExpr() != null) { + setType(throwExpr().type()); + Preconditions.checkState( + messageExpr() == null && causeExpr() == null, + "Only one of throwExpr or [messageExpr or causeExpr, inclusive] can be present."); + + if (throwExpr() instanceof VariableExpr) { + Preconditions.checkState( + !((VariableExpr) throwExpr()).isDecl(), "Cannot throw a variable declaration"); + } + + Preconditions.checkState( + TypeNode.isExceptionType(throwExpr().type()), + String.format("Only exception types can be thrown, found %s", throwExpr().type())); + + return autoBuild(); + } + Preconditions.checkState( TypeNode.isExceptionType(type()), String.format("Type %s must be an exception type", type())); diff --git a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java index 259a027402..297500ae7f 100644 --- a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java @@ -231,6 +231,13 @@ public void visit(AnonymousClassExpr anonymousClassExpr) { @Override public void visit(ThrowExpr throwExpr) { throwExpr.type().accept(this); + // If throwExpr is present, then messageExpr and causeExpr will not be present. Relies on AST + // build-time checks. + if (throwExpr.throwExpr() != null) { + throwExpr.throwExpr().accept(this); + return; + } + if (throwExpr.messageExpr() != null) { throwExpr.messageExpr().accept(this); } diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 11ddff441d..9006b0490b 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -392,6 +392,13 @@ public void visit(AnonymousClassExpr anonymousClassExpr) { public void visit(ThrowExpr throwExpr) { buffer.append(THROW); space(); + // If throwExpr is present, then messageExpr and causeExpr will not be present. Relies on AST + // build-time checks. + if (throwExpr.throwExpr() != null) { + throwExpr.throwExpr().accept(this); + return; + } + buffer.append(NEW); space(); throwExpr.type().accept(this); diff --git a/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java b/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java index f27d000113..7bc1589feb 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ThrowExprTest.java @@ -14,6 +14,7 @@ package com.google.api.generator.engine.ast; +import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertThrows; import org.junit.Test; @@ -24,7 +25,24 @@ public void createThrowExpr_basic() { TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class); ThrowExpr.builder().setType(npeType).build(); // No exception thrown, we're good. + } + @Test + public void createThrowExpr_basicExpr() { + TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class); + VariableExpr throwVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setName("e") + .setType(TypeNode.withExceptionClazz(RuntimeException.class)) + .build()) + .build(); + ThrowExpr throwExpr = ThrowExpr.builder().setThrowExpr(throwVarExpr).build(); + assertEquals(throwVarExpr.variable().type(), throwExpr.type()); + // Setting the type doesn't matter. + throwExpr = ThrowExpr.builder().setThrowExpr(throwVarExpr).setType(npeType).build(); + assertEquals(throwVarExpr.variable().type(), throwExpr.type()); } @Test @@ -123,4 +141,75 @@ public void createThrowExpr_messageAndCauseExpr() { .build(); // Successfully created a ThrowExpr. } + + @Test + public void createThrowExpr_cannotThrowVariableDeclaration() { + VariableExpr throwVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setName("e") + .setType(TypeNode.withExceptionClazz(RuntimeException.class)) + .build()) + .build(); + assertThrows( + IllegalStateException.class, + () -> + ThrowExpr.builder() + .setThrowExpr(throwVarExpr.toBuilder().setIsDecl(true).build()) + .build()); + } + + @Test + public void createThrowExpr_cannotThrowNonExceptionTypedExpr() { + VariableExpr throwVarExpr = + VariableExpr.builder() + .setVariable(Variable.builder().setName("str").setType(TypeNode.STRING).build()) + .build(); + assertThrows( + IllegalStateException.class, () -> ThrowExpr.builder().setThrowExpr(throwVarExpr).build()); + } + + @Test + public void createThrowExpr_cannotHaveThrowVariableAndMessageExprPresent() { + Expr messageExpr = + MethodInvocationExpr.builder() + .setMethodName("foobar") + .setReturnType(TypeNode.STRING) + .build(); + VariableExpr throwVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setName("e") + .setType(TypeNode.withExceptionClazz(RuntimeException.class)) + .build()) + .build(); + assertThrows( + IllegalStateException.class, + () -> ThrowExpr.builder().setThrowExpr(throwVarExpr).setMessageExpr(messageExpr).build()); + } + + @Test + public void createThrowExpr_cannotHaveThrowVariableAndCauseExprPresent() { + VariableExpr throwVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setName("e") + .setType(TypeNode.withExceptionClazz(RuntimeException.class)) + .build()) + .build(); + assertThrows( + IllegalStateException.class, + () -> + ThrowExpr.builder() + .setThrowExpr(throwVarExpr) + .setCauseExpr( + NewObjectExpr.builder() + .setType( + TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))) + .build()) + .build()); + } } diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 053ea774d5..6b9d20ff0b 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -449,6 +449,26 @@ public void writeThrowExprImports_basic() { assertEquals("import java.io.IOException;\n\n", writerVisitor.write()); } + @Test + public void writeThrowExprImports_throwExpr() { + Expr exprToThrow = + MethodInvocationExpr.builder() + .setStaticReferenceType( + TypeNode.withReference(ConcreteReference.withClazz(Statement.class))) + .setMethodName("createException") + .setReturnType(TypeNode.withExceptionClazz(Exception.class)) + .build(); + + TypeNode ignoredExceptionType = + TypeNode.withReference(ConcreteReference.withClazz(IOException.class)); + ThrowExpr throwExpr = + ThrowExpr.builder().setType(ignoredExceptionType).setThrowExpr(exprToThrow).build(); + throwExpr.accept(writerVisitor); + assertEquals( + LineFormatter.lines("import com.google.api.generator.engine.ast.Statement;\n\n"), + writerVisitor.write()); + } + @Test public void writeThrowExprImports_messageExpr() { TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class); diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index c07fbd32cf..4be2c916d4 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1031,6 +1031,21 @@ public void writeThrowExpr_basic() { assertEquals("throw new NullPointerException()", writerVisitor.write()); } + @Test + public void writeThrowExpr_basicThrowExpr() { + Expr exprToThrow = + MethodInvocationExpr.builder() + .setStaticReferenceType( + TypeNode.withReference(ConcreteReference.withClazz(Statement.class))) + .setMethodName("createException") + .setReturnType(TypeNode.withExceptionClazz(Exception.class)) + .build(); + + ThrowExpr throwExpr = ThrowExpr.builder().setThrowExpr(exprToThrow).build(); + throwExpr.accept(writerVisitor); + assertEquals("throw Statement.createException()", writerVisitor.write()); + } + @Test public void writeThrowExpr_basicWithMessage() { TypeNode npeType = From 68fb4659cb4ae51a49228746f38e8ee5a4311436 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 10:50:00 -0700 Subject: [PATCH 06/11] fix: call backgroundResources.close() on stub.close() --- .../AbstractServiceStubClassComposer.java | 42 ++++++++++++++++++- .../goldens/GrpcDeprecatedServiceStub.golden | 6 ++- .../composer/grpc/goldens/GrpcEchoStub.golden | 6 ++- .../grpc/goldens/GrpcPublisherStub.golden | 6 ++- .../grpc/goldens/GrpcTestingStub.golden | 6 ++- .../goldens/HttpJsonComplianceStub.golden | 6 ++- .../asset/v1/stub/GrpcAssetServiceStub.java | 6 ++- .../v1/stub/HttpJsonAddressesStub.java | 6 ++- .../v1/stub/HttpJsonRegionOperationsStub.java | 6 ++- .../v1/stub/GrpcIamCredentialsStub.java | 6 ++- .../google/iam/v1/stub/GrpcIAMPolicyStub.java | 6 ++- .../v1/stub/GrpcKeyManagementServiceStub.java | 6 ++- .../v1/stub/GrpcLibraryServiceStub.java | 6 ++- .../v2/stub/GrpcConfigServiceV2Stub.java | 6 ++- .../v2/stub/GrpcLoggingServiceV2Stub.java | 6 ++- .../v2/stub/GrpcMetricsServiceV2Stub.java | 6 ++- .../pubsub/v1/stub/GrpcPublisherStub.java | 6 ++- .../pubsub/v1/stub/GrpcSchemaServiceStub.java | 6 ++- .../pubsub/v1/stub/GrpcSubscriberStub.java | 6 ++- .../v1beta1/stub/GrpcCloudRedisStub.java | 6 ++- 20 files changed, 135 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java index a2ac589a65..da5c06681a 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java @@ -40,6 +40,8 @@ import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.ThisObjectValue; +import com.google.api.generator.engine.ast.ThrowExpr; +import com.google.api.generator.engine.ast.TryCatchStatement; import com.google.api.generator.engine.ast.TypeNode; import com.google.api.generator.engine.ast.ValueExpr; import com.google.api.generator.engine.ast.Variable; @@ -791,6 +793,23 @@ private List createStubOverrideMethods( .build()) .build(); + // Generate the close() method: + // @Override + // public final void close() { + // try { + // backgroundResources.close(); + // } catch (Exception e) { + // throw new IllegalStateException("Failed to close resource", e); + // } + // } + VariableExpr catchExceptionVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setType(TypeNode.withExceptionClazz(Exception.class)) + .setName("e") + .build()) + .build(); List javaMethods = new ArrayList<>(); javaMethods.add( methodMakerStarterFn @@ -799,8 +818,27 @@ private List createStubOverrideMethods( .setReturnType(TypeNode.VOID) .setBody( Arrays.asList( - ExprStatement.withExpr( - MethodInvocationExpr.builder().setMethodName("shutdown").build()))) + TryCatchStatement.builder() + .setTryBody( + Arrays.asList( + ExprStatement.withExpr( + MethodInvocationExpr.builder() + .setExprReferenceExpr(backgroundResourcesVarExpr) + .setMethodName("close") + .build()))) + .setCatchVariableExpr( + catchExceptionVarExpr.toBuilder().setIsDecl(true).build()) + .setCatchBody( + Arrays.asList( + ExprStatement.withExpr( + ThrowExpr.builder() + .setType( + TypeNode.withExceptionClazz( + IllegalStateException.class)) + .setMessageExpr(String.format("Failed to close resource")) + .setCauseExpr(catchExceptionVarExpr) + .build()))) + .build())) .build()); javaMethods.add(voidMethodMakerFn.apply("shutdown")); javaMethods.add(booleanMethodMakerFn.apply("isShutdown")); diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden index 431b6688f5..130e2caf27 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden @@ -125,7 +125,11 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden index d1a4a303bd..de44e89d22 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden @@ -341,7 +341,11 @@ public class GrpcEchoStub extends EchoStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden index 9e221ab5a2..4e81906d39 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden @@ -430,7 +430,11 @@ public class GrpcPublisherStub extends PublisherStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden index ed6f22313a..296d526fc2 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden @@ -382,7 +382,11 @@ public class GrpcTestingStub extends TestingStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden index 1ddcb3bf39..703243fe91 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden @@ -484,7 +484,11 @@ public class HttpJsonComplianceStub extends ComplianceStub { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java index 712d328088..d9c95603fe 100644 --- a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java +++ b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java @@ -598,7 +598,11 @@ public UnaryCallable deleteFeedCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java index 53a0eb8425..37bd3aea38 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java @@ -403,7 +403,11 @@ public UnaryCallable listPagedCallable( @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java index 0be8cf4d03..d5578176de 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java @@ -167,7 +167,11 @@ public UnaryCallable getCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java index 48731cd53d..bc04cab818 100644 --- a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java +++ b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java @@ -239,7 +239,11 @@ public UnaryCallable signJwtCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java index f40c2bfffe..2c1e96db38 100644 --- a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java +++ b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java @@ -197,7 +197,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java index 19065361ed..13a238d3fc 100644 --- a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java +++ b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java @@ -1119,7 +1119,11 @@ public UnaryCallable getLocationCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java index c912a9b7b3..8e3e2b9c27 100644 --- a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java +++ b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java @@ -450,7 +450,11 @@ public UnaryCallable moveBookCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java index 91ddb14f6a..e2a5c38b45 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java @@ -889,7 +889,11 @@ public UnaryCallable updateCmekSettings @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java index feb0ed3086..48b05cf5a1 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java @@ -328,7 +328,11 @@ public UnaryCallable listLogsPagedCallab @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java index fe15cf6052..da933b1ac4 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java @@ -279,7 +279,11 @@ public UnaryCallable deleteLogMetricCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java index 2b5ce28a06..cff38b2fd6 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java @@ -549,7 +549,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java index 11bac90c7d..094bcea972 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java @@ -412,7 +412,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java index 8580e626f8..20817d2eb9 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java @@ -762,7 +762,11 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override diff --git a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java index 24c34f9b6e..0b73bc8797 100644 --- a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java +++ b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java @@ -505,7 +505,11 @@ public OperationCallable deleteInstanceOperat @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override From 6de4e5e85b758b0b9dab2374477481399fb644f7 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 11:43:18 -0700 Subject: [PATCH 07/11] fix: update goldens --- .../storage/com/google/storage/v2/stub/GrpcStorageStub.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java index a0ae53ba0e..71c1cdac29 100644 --- a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java +++ b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java @@ -201,7 +201,11 @@ public ClientStreamingCallable writeObj @Override public final void close() { - shutdown(); + try { + backgroundResources.close(); + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } } @Override From 1c57844a7b7d9f7acbe56bb1d638c163813b7ef3 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 13:52:52 -0700 Subject: [PATCH 08/11] feat(ast): Add support for multi-catch blocks --- .../engine/ast/TryCatchStatement.java | 71 ++++++++++++----- .../engine/writer/ImportWriterVisitor.java | 8 +- .../engine/writer/JavaWriterVisitor.java | 6 +- ...bstractServiceClientTestClassComposer.java | 6 +- .../AbstractServiceStubClassComposer.java | 5 +- .../grpc/ServiceClientTestClassComposer.java | 5 +- .../engine/JavaCodeGeneratorTest.java | 4 +- .../engine/ast/TryCatchStatementTest.java | 45 +++++++++-- .../engine/writer/JavaWriterVisitorTest.java | 77 +++++++++++++++++-- 9 files changed, 179 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/TryCatchStatement.java b/src/main/java/com/google/api/generator/engine/ast/TryCatchStatement.java index 3634374259..1280635e38 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TryCatchStatement.java +++ b/src/main/java/com/google/api/generator/engine/ast/TryCatchStatement.java @@ -17,8 +17,10 @@ import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import javax.annotation.Nonnull; import javax.annotation.Nullable; @AutoValue @@ -26,11 +28,12 @@ public abstract class TryCatchStatement implements Statement { // Required. public abstract ImmutableList tryBody(); + // Optional only if the sample code bit is set (i.e. this is sample code). - @Nullable - public abstract VariableExpr catchVariableExpr(); + public abstract List catchVariableExprs(); // Optional only if the sample code bit is set (i.e. this is sample code). - public abstract ImmutableList catchBody(); + public abstract List> catchBlocks(); + // Optional. @Nullable public abstract AssignmentExpr tryResourceExpr(); @@ -44,8 +47,9 @@ public void accept(AstNodeVisitor visitor) { public static Builder builder() { return new AutoValue_TryCatchStatement.Builder() - .setIsSampleCode(false) - .setCatchBody(Collections.emptyList()); + .setCatchVariableExprs(Collections.emptyList()) + .setCatchBlocks(Collections.emptyList()) + .setIsSampleCode(false); } @AutoValue.Builder @@ -54,32 +58,61 @@ public abstract static class Builder { public abstract Builder setTryBody(List body); - public abstract Builder setCatchVariableExpr(VariableExpr variableExpr); + public abstract Builder setIsSampleCode(boolean isSampleCode); - public abstract Builder setCatchBody(List body); + public Builder addCatch(@Nonnull VariableExpr variableExpr, List body) { + List catchVarExprs = new ArrayList<>(catchVariableExprs()); + catchVarExprs.add(variableExpr); + setCatchVariableExprs(catchVarExprs); - public abstract Builder setIsSampleCode(boolean isSampleCode); + List> blocks = new ArrayList<>(catchBlocks()); + blocks.add(body); + return setCatchBlocks(blocks); + } + + // Private. + abstract Builder setCatchVariableExprs(List variableExpr); + + abstract Builder setCatchBlocks(List> body); + + abstract ImmutableList tryBody(); + + abstract boolean isSampleCode(); + + abstract List catchVariableExprs(); + + abstract List> catchBlocks(); abstract TryCatchStatement autoBuild(); public TryCatchStatement build() { - TryCatchStatement tryCatchStatement = autoBuild(); - NodeValidator.checkNoNullElements(tryCatchStatement.tryBody(), "try body", "try-catch"); - NodeValidator.checkNoNullElements(tryCatchStatement.catchBody(), "catch body", "try-catch"); + NodeValidator.checkNoNullElements(tryBody(), "try body", "try-catch"); + NodeValidator.checkNoNullElements( + catchVariableExprs(), "catch variable expressions", "try-catch"); + catchBlocks() + .forEach(body -> NodeValidator.checkNoNullElements(body, "catch body", "try-catch")); - if (!tryCatchStatement.isSampleCode()) { - Preconditions.checkNotNull( - tryCatchStatement.catchVariableExpr(), + if (!isSampleCode()) { + Preconditions.checkState( + !catchVariableExprs().isEmpty(), "Catch variable expression must be set for real, non-sample try-catch blocks."); Preconditions.checkState( - tryCatchStatement.catchVariableExpr().isDecl(), - "Catch variable expression must be a declaration"); + catchVariableExprs().stream().allMatch(v -> v.isDecl()), + "Catch variable expressions must all be declarations"); Preconditions.checkState( - TypeNode.isExceptionType(tryCatchStatement.catchVariableExpr().variable().type()), - "Catch variable must be an Exception object reference"); + catchVariableExprs().stream() + .allMatch(v -> TypeNode.isExceptionType(v.variable().type())), + "Catch variables must be an Exception object references"); } - return tryCatchStatement; + // Catch any potential future breakage due to changing addCatch above. + Preconditions.checkState( + catchVariableExprs().size() == catchBlocks().size(), + String.format( + "%d catch variables found and %d blocks found, but these numbers must be equal", + catchVariableExprs().size(), catchBlocks().size())); + + return autoBuild(); } } } diff --git a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java index 297500ae7f..58fb2b9aa4 100644 --- a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java @@ -362,11 +362,13 @@ public void visit(TryCatchStatement tryCatchStatement) { statements(tryCatchStatement.tryBody()); Preconditions.checkState( - !tryCatchStatement.isSampleCode() && tryCatchStatement.catchVariableExpr() != null, + !tryCatchStatement.isSampleCode() && !tryCatchStatement.catchVariableExprs().isEmpty(), "Import generation should not be invoked on sample code, but was found when visiting a" + " try-catch block"); - tryCatchStatement.catchVariableExpr().accept(this); - statements(tryCatchStatement.catchBody()); + for (int i = 0; i < tryCatchStatement.catchVariableExprs().size(); i++) { + tryCatchStatement.catchVariableExprs().get(i).accept(this); + statements(tryCatchStatement.catchBlocks().get(i)); + } } @Override diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 9006b0490b..a52b584da1 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -703,17 +703,17 @@ public void visit(TryCatchStatement tryCatchStatement) { statements(tryCatchStatement.tryBody()); rightBrace(); - if (tryCatchStatement.catchVariableExpr() != null) { + for (int i = 0; i < tryCatchStatement.catchVariableExprs().size(); i++) { space(); buffer.append(CATCH); space(); leftParen(); - tryCatchStatement.catchVariableExpr().accept(this); + tryCatchStatement.catchVariableExprs().get(i).accept(this); rightParen(); space(); leftBrace(); newline(); - statements(tryCatchStatement.catchBody()); + statements(tryCatchStatement.catchBlocks().get(i)); rightBrace(); } newline(); diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java index 8333a37f64..7d01cfbca3 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java @@ -536,7 +536,8 @@ private MethodDefinition createRpcTestMethod( TypeNode.withReference( ConcreteReference.builder() .setClazz(List.class) - .setGenerics(Arrays.asList(repeatedPagedResultsField.type().reference())) + .setGenerics( + Arrays.asList(repeatedPagedResultsField.type().reference())) .build())) .setName("resources") .build()); @@ -824,8 +825,7 @@ protected List createRpcExceptionTestStatements( tryBodyExprs.stream() .map(e -> ExprStatement.withExpr(e)) .collect(Collectors.toList())) - .setCatchVariableExpr(catchExceptionVarExpr.toBuilder().setIsDecl(true).build()) - .setCatchBody(catchBody) + .addCatch(catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), catchBody) .build(); return Arrays.asList(EMPTY_LINE_STATEMENT, tryCatchBlock); diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java index da5c06681a..0c8087dab4 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java @@ -826,9 +826,8 @@ private List createStubOverrideMethods( .setExprReferenceExpr(backgroundResourcesVarExpr) .setMethodName("close") .build()))) - .setCatchVariableExpr( - catchExceptionVarExpr.toBuilder().setIsDecl(true).build()) - .setCatchBody( + .addCatch( + catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), Arrays.asList( ExprStatement.withExpr( ThrowExpr.builder() diff --git a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java index 49d7164a55..15b05ee765 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java @@ -1046,8 +1046,9 @@ protected List createStreamingRpcExceptionTestStatements( tryBodyExprs.stream() .map(e -> ExprStatement.withExpr(e)) .collect(Collectors.toList())) - .setCatchVariableExpr(catchExceptionVarExpr.toBuilder().setIsDecl(true).build()) - .setCatchBody(createRpcLroExceptionTestCatchBody(catchExceptionVarExpr, true)) + .addCatch( + catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), + createRpcLroExceptionTestCatchBody(catchExceptionVarExpr, true)) .build(); statements.add(tryCatchBlock); diff --git a/src/test/java/com/google/api/generator/engine/JavaCodeGeneratorTest.java b/src/test/java/com/google/api/generator/engine/JavaCodeGeneratorTest.java index 8ed58c13cb..6f1e80ca70 100644 --- a/src/test/java/com/google/api/generator/engine/JavaCodeGeneratorTest.java +++ b/src/test/java/com/google/api/generator/engine/JavaCodeGeneratorTest.java @@ -606,8 +606,8 @@ private MethodDefinition createPrintShelfListToFile() { loopShelfList, ExprStatement.withExpr(writeToFileWriter), ExprStatement.withExpr(closeFileWriter))) - .setCatchVariableExpr(createVarDeclExpr(ioException)) - .setCatchBody(Arrays.asList(ExprStatement.withExpr(printError))) + .addCatch( + createVarDeclExpr(ioException), Arrays.asList(ExprStatement.withExpr(printError))) .build(); return MethodDefinition.builder() diff --git a/src/test/java/com/google/api/generator/engine/ast/TryCatchStatementTest.java b/src/test/java/com/google/api/generator/engine/ast/TryCatchStatementTest.java index db406aa9eb..f19722f182 100644 --- a/src/test/java/com/google/api/generator/engine/ast/TryCatchStatementTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/TryCatchStatementTest.java @@ -15,9 +15,11 @@ package com.google.api.generator.engine.ast; import static com.google.common.truth.Truth.assertThat; +import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertThrows; import java.util.Arrays; +import java.util.Collections; import org.junit.Test; public class TryCatchStatementTest { @@ -32,9 +34,36 @@ public void validTryCatchStatement_simple() { TryCatchStatement tryCatch = TryCatchStatement.builder() .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) - .setCatchVariableExpr(variableExpr) + .addCatch(variableExpr, Collections.emptyList()) .build(); - assertThat(tryCatch.catchVariableExpr()).isEqualTo(variableExpr); + assertEquals(1, tryCatch.catchVariableExprs().size()); + assertThat(tryCatch.catchVariableExprs().get(0)).isEqualTo(variableExpr); + } + + @Test + public void validTryCatchStatement_simpleMultiBlock() { + VariableExpr firstCatchVarExpr = + VariableExpr.builder() + .setVariable( + createVariable("e", TypeNode.withExceptionClazz(IllegalArgumentException.class))) + .setIsDecl(true) + .build(); + VariableExpr secondCatchVarExpr = + VariableExpr.builder() + .setVariable(createVariable("e", TypeNode.withExceptionClazz(RuntimeException.class))) + .setIsDecl(true) + .build(); + + TryCatchStatement tryCatch = + TryCatchStatement.builder() + .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) + .addCatch(firstCatchVarExpr, Collections.emptyList()) + .addCatch(secondCatchVarExpr, Collections.emptyList()) + .build(); + + assertEquals(2, tryCatch.catchVariableExprs().size()); + assertThat(tryCatch.catchVariableExprs().get(0)).isEqualTo(firstCatchVarExpr); + assertThat(tryCatch.catchVariableExprs().get(1)).isEqualTo(secondCatchVarExpr); } @Test @@ -49,9 +78,9 @@ public void validTryCatchStatement_withResources() { TryCatchStatement.builder() .setTryResourceExpr(assignmentExpr) .setTryBody(Arrays.asList(ExprStatement.withExpr(assignmentExpr))) - .setCatchVariableExpr(variableExpr) + .addCatch(variableExpr, Collections.emptyList()) .build(); - assertThat(tryCatch.catchVariableExpr()).isEqualTo(variableExpr); + assertThat(tryCatch.catchVariableExprs().get(0)).isEqualTo(variableExpr); assertThat(tryCatch.tryResourceExpr()).isEqualTo(assignmentExpr); } @@ -67,7 +96,7 @@ public void validTryCatchStatement_sampleCode() { .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) .setIsSampleCode(true) .build(); - assertThat(tryCatch.catchVariableExpr()).isNull(); + assertThat(tryCatch.catchVariableExprs()).isEmpty(); } @Test @@ -78,7 +107,7 @@ public void invalidTryCatchStatement_missingCatchVariable() { VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build(); assertThrows( - NullPointerException.class, + IllegalStateException.class, () -> { TryCatchStatement.builder() .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) @@ -99,7 +128,7 @@ public void invalidTryCatchStatement_catchVariableNotDecl() { TryCatchStatement tryCatch = TryCatchStatement.builder() .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) - .setCatchVariableExpr(variableExpr) + .addCatch(variableExpr, Collections.emptyList()) .build(); }); } @@ -117,7 +146,7 @@ public void invalidTryCatchStatement_nonExceptionReference() { TryCatchStatement tryCatch = TryCatchStatement.builder() .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) - .setCatchVariableExpr(variableExpr) + .addCatch(variableExpr, Collections.emptyList()) .build(); }); } diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 4be2c916d4..902f3ee4a8 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -73,6 +73,7 @@ import com.google.common.base.Function; import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -1469,7 +1470,7 @@ public void writeTryCatchStatement_simple() { TryCatchStatement.builder() .setTryBody( Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))) - .setCatchVariableExpr(variableExpr) + .addCatch(variableExpr, Collections.emptyList()) .build(); tryCatch.accept(writerVisitor); @@ -1480,6 +1481,72 @@ public void writeTryCatchStatement_simple() { writerVisitor.write()); } + @Test + public void writeTryCatchStatement_simpleMultiCatch() { + VariableExpr firstCatchVarExpr = + VariableExpr.builder() + .setVariable( + createVariable("e", TypeNode.withExceptionClazz(IllegalArgumentException.class))) + .build(); + VariableExpr secondCatchVarExpr = + VariableExpr.builder() + .setVariable(createVariable("e", TypeNode.withExceptionClazz(RuntimeException.class))) + .build(); + + TryCatchStatement tryCatch = + TryCatchStatement.builder() + .setTryBody( + Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))) + .addCatch( + firstCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()) + .addCatch( + secondCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()) + .build(); + + tryCatch.accept(writerVisitor); + assertEquals( + LineFormatter.lines( + "try {\n", + "int x = 3;\n", + "} catch (IllegalArgumentException e) {\n", + "} catch (RuntimeException e) {\n", + "}\n"), + writerVisitor.write()); + } + + @Test + public void writeTryCatchStatement_simpleMultiCatchOrderMatters() { + VariableExpr firstCatchVarExpr = + VariableExpr.builder() + .setVariable( + createVariable("e", TypeNode.withExceptionClazz(IllegalArgumentException.class))) + .build(); + VariableExpr secondCatchVarExpr = + VariableExpr.builder() + .setVariable(createVariable("e", TypeNode.withExceptionClazz(RuntimeException.class))) + .build(); + + TryCatchStatement tryCatch = + TryCatchStatement.builder() + .setTryBody( + Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))) + .addCatch( + secondCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()) + .addCatch( + firstCatchVarExpr.toBuilder().setIsDecl(true).build(), Collections.emptyList()) + .build(); + + tryCatch.accept(writerVisitor); + assertEquals( + LineFormatter.lines( + "try {\n", + "int x = 3;\n", + "} catch (RuntimeException e) {\n", + "} catch (IllegalArgumentException e) {\n", + "}\n"), + writerVisitor.write()); + } + @Test public void writeTryCatchStatement_withResources() { Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class); @@ -1492,8 +1559,8 @@ public void writeTryCatchStatement_withResources() { .setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)) .setTryBody( Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))) - .setCatchVariableExpr(variableExpr) - .setCatchBody( + .addCatch( + variableExpr, Arrays.asList( ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))) .build(); @@ -1541,8 +1608,8 @@ public void writeTryCatchStatement_sampleCodeWithCatch() { .setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)) .setTryBody( Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))) - .setCatchVariableExpr(variableExpr) - .setCatchBody( + .addCatch( + variableExpr, Arrays.asList( ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))) .build(); From 12c88360e524087351d0a4caa4dba7e1477181f4 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 14:10:55 -0700 Subject: [PATCH 09/11] fix: add extra catch block --- .../AbstractServiceStubClassComposer.java | 18 ++++++++++++++++++ .../goldens/GrpcDeprecatedServiceStub.golden | 2 ++ .../composer/grpc/goldens/GrpcEchoStub.golden | 2 ++ .../grpc/goldens/GrpcPublisherStub.golden | 2 ++ .../grpc/goldens/GrpcTestingStub.golden | 2 ++ .../rest/goldens/HttpJsonComplianceStub.golden | 2 ++ .../asset/v1/stub/GrpcAssetServiceStub.java | 2 ++ .../compute/v1/stub/HttpJsonAddressesStub.java | 2 ++ .../v1/stub/HttpJsonRegionOperationsStub.java | 2 ++ .../v1/stub/GrpcIamCredentialsStub.java | 2 ++ .../google/iam/v1/stub/GrpcIAMPolicyStub.java | 2 ++ .../v1/stub/GrpcKeyManagementServiceStub.java | 2 ++ .../v1/stub/GrpcLibraryServiceStub.java | 2 ++ .../v2/stub/GrpcConfigServiceV2Stub.java | 2 ++ .../v2/stub/GrpcLoggingServiceV2Stub.java | 2 ++ .../v2/stub/GrpcMetricsServiceV2Stub.java | 2 ++ .../pubsub/v1/stub/GrpcPublisherStub.java | 2 ++ .../pubsub/v1/stub/GrpcSchemaServiceStub.java | 2 ++ .../pubsub/v1/stub/GrpcSubscriberStub.java | 2 ++ .../redis/v1beta1/stub/GrpcCloudRedisStub.java | 2 ++ .../storage/v2/stub/GrpcStorageStub.java | 2 ++ 21 files changed, 58 insertions(+) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java index 0c8087dab4..034c94c949 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java @@ -798,10 +798,21 @@ private List createStubOverrideMethods( // public final void close() { // try { // backgroundResources.close(); + // } catch (RuntimeException e) { + // throw e; // } catch (Exception e) { // throw new IllegalStateException("Failed to close resource", e); // } // } + + VariableExpr catchRuntimeExceptionVarExpr = + VariableExpr.builder() + .setVariable( + Variable.builder() + .setType(TypeNode.withExceptionClazz(RuntimeException.class)) + .setName("e") + .build()) + .build(); VariableExpr catchExceptionVarExpr = VariableExpr.builder() .setVariable( @@ -826,6 +837,13 @@ private List createStubOverrideMethods( .setExprReferenceExpr(backgroundResourcesVarExpr) .setMethodName("close") .build()))) + .addCatch( + catchRuntimeExceptionVarExpr.toBuilder().setIsDecl(true).build(), + Arrays.asList( + ExprStatement.withExpr( + ThrowExpr.builder() + .setThrowExpr(catchRuntimeExceptionVarExpr) + .build()))) .addCatch( catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), Arrays.asList( diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden index 130e2caf27..b472790622 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden @@ -127,6 +127,8 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden index de44e89d22..940ab7d4c4 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden @@ -343,6 +343,8 @@ public class GrpcEchoStub extends EchoStub { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden index 4e81906d39..84cc56d029 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden @@ -432,6 +432,8 @@ public class GrpcPublisherStub extends PublisherStub { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden index 296d526fc2..b22b976287 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden @@ -384,6 +384,8 @@ public class GrpcTestingStub extends TestingStub { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden index 703243fe91..15b0a124b1 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden @@ -486,6 +486,8 @@ public class HttpJsonComplianceStub extends ComplianceStub { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java index d9c95603fe..082fb7fa95 100644 --- a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java +++ b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java @@ -600,6 +600,8 @@ public UnaryCallable deleteFeedCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java index 37bd3aea38..11ebe42475 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java @@ -405,6 +405,8 @@ public UnaryCallable listPagedCallable( public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java index d5578176de..7e3217b1cb 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java @@ -169,6 +169,8 @@ public UnaryCallable getCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java index bc04cab818..39e6e722c0 100644 --- a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java +++ b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java @@ -241,6 +241,8 @@ public UnaryCallable signJwtCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java index 2c1e96db38..5b5440bd25 100644 --- a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java +++ b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java @@ -199,6 +199,8 @@ public UnaryCallable getIamPolicyCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java index 13a238d3fc..499771b0f6 100644 --- a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java +++ b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java @@ -1121,6 +1121,8 @@ public UnaryCallable getLocationCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java index 8e3e2b9c27..8a5429cefb 100644 --- a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java +++ b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java @@ -452,6 +452,8 @@ public UnaryCallable moveBookCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java index e2a5c38b45..e8e5bf647e 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java @@ -891,6 +891,8 @@ public UnaryCallable updateCmekSettings public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java index 48b05cf5a1..80c6b53aea 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java @@ -330,6 +330,8 @@ public UnaryCallable listLogsPagedCallab public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java index da933b1ac4..6d5eefc13d 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java @@ -281,6 +281,8 @@ public UnaryCallable deleteLogMetricCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java index cff38b2fd6..950b722377 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java @@ -551,6 +551,8 @@ public UnaryCallable getIamPolicyCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java index 094bcea972..0ca41b379b 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java @@ -414,6 +414,8 @@ public UnaryCallable getIamPolicyCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java index 20817d2eb9..878a2ad7d2 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java @@ -764,6 +764,8 @@ public UnaryCallable getIamPolicyCallable() { public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java index 0b73bc8797..4e6c502571 100644 --- a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java +++ b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java @@ -507,6 +507,8 @@ public OperationCallable deleteInstanceOperat public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } diff --git a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java index 71c1cdac29..3097e32c82 100644 --- a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java +++ b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java @@ -203,6 +203,8 @@ public ClientStreamingCallable writeObj public final void close() { try { backgroundResources.close(); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { throw new IllegalStateException("Failed to close resource", e); } From 552af72cecc60799ee87a6a703c534d716d44cf2 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 14:32:15 -0700 Subject: [PATCH 10/11] fix: isolate stub.close change to another PR --- .../AbstractServiceStubClassComposer.java | 41 +------------------ .../goldens/GrpcDeprecatedServiceStub.golden | 6 +-- .../composer/grpc/goldens/GrpcEchoStub.golden | 6 +-- .../grpc/goldens/GrpcPublisherStub.golden | 6 +-- .../grpc/goldens/GrpcTestingStub.golden | 6 +-- .../goldens/HttpJsonComplianceStub.golden | 6 +-- .../asset/v1/stub/GrpcAssetServiceStub.java | 6 +-- .../v1/stub/HttpJsonAddressesStub.java | 6 +-- .../v1/stub/HttpJsonRegionOperationsStub.java | 6 +-- .../v1/stub/GrpcIamCredentialsStub.java | 6 +-- .../google/iam/v1/stub/GrpcIAMPolicyStub.java | 6 +-- .../v1/stub/GrpcKeyManagementServiceStub.java | 6 +-- .../v1/stub/GrpcLibraryServiceStub.java | 6 +-- .../v2/stub/GrpcConfigServiceV2Stub.java | 6 +-- .../v2/stub/GrpcLoggingServiceV2Stub.java | 6 +-- .../v2/stub/GrpcMetricsServiceV2Stub.java | 6 +-- .../pubsub/v1/stub/GrpcPublisherStub.java | 6 +-- .../pubsub/v1/stub/GrpcSchemaServiceStub.java | 6 +-- .../pubsub/v1/stub/GrpcSubscriberStub.java | 6 +-- .../v1beta1/stub/GrpcCloudRedisStub.java | 6 +-- .../storage/v2/stub/GrpcStorageStub.java | 6 +-- 21 files changed, 22 insertions(+), 139 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java index 0c8087dab4..a2ac589a65 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java @@ -40,8 +40,6 @@ import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.ThisObjectValue; -import com.google.api.generator.engine.ast.ThrowExpr; -import com.google.api.generator.engine.ast.TryCatchStatement; import com.google.api.generator.engine.ast.TypeNode; import com.google.api.generator.engine.ast.ValueExpr; import com.google.api.generator.engine.ast.Variable; @@ -793,23 +791,6 @@ private List createStubOverrideMethods( .build()) .build(); - // Generate the close() method: - // @Override - // public final void close() { - // try { - // backgroundResources.close(); - // } catch (Exception e) { - // throw new IllegalStateException("Failed to close resource", e); - // } - // } - VariableExpr catchExceptionVarExpr = - VariableExpr.builder() - .setVariable( - Variable.builder() - .setType(TypeNode.withExceptionClazz(Exception.class)) - .setName("e") - .build()) - .build(); List javaMethods = new ArrayList<>(); javaMethods.add( methodMakerStarterFn @@ -818,26 +799,8 @@ private List createStubOverrideMethods( .setReturnType(TypeNode.VOID) .setBody( Arrays.asList( - TryCatchStatement.builder() - .setTryBody( - Arrays.asList( - ExprStatement.withExpr( - MethodInvocationExpr.builder() - .setExprReferenceExpr(backgroundResourcesVarExpr) - .setMethodName("close") - .build()))) - .addCatch( - catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), - Arrays.asList( - ExprStatement.withExpr( - ThrowExpr.builder() - .setType( - TypeNode.withExceptionClazz( - IllegalStateException.class)) - .setMessageExpr(String.format("Failed to close resource")) - .setCauseExpr(catchExceptionVarExpr) - .build()))) - .build())) + ExprStatement.withExpr( + MethodInvocationExpr.builder().setMethodName("shutdown").build()))) .build()); javaMethods.add(voidMethodMakerFn.apply("shutdown")); javaMethods.add(booleanMethodMakerFn.apply("isShutdown")); diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden index 130e2caf27..431b6688f5 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcDeprecatedServiceStub.golden @@ -125,11 +125,7 @@ public class GrpcDeprecatedServiceStub extends DeprecatedServiceStub { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden index de44e89d22..d1a4a303bd 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden @@ -341,11 +341,7 @@ public class GrpcEchoStub extends EchoStub { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden index 4e81906d39..9e221ab5a2 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcPublisherStub.golden @@ -430,11 +430,7 @@ public class GrpcPublisherStub extends PublisherStub { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden index 296d526fc2..ed6f22313a 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcTestingStub.golden @@ -382,11 +382,7 @@ public class GrpcTestingStub extends TestingStub { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden index 703243fe91..1ddcb3bf39 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonComplianceStub.golden @@ -484,11 +484,7 @@ public class HttpJsonComplianceStub extends ComplianceStub { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java index d9c95603fe..712d328088 100644 --- a/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java +++ b/test/integration/goldens/asset/com/google/cloud/asset/v1/stub/GrpcAssetServiceStub.java @@ -598,11 +598,7 @@ public UnaryCallable deleteFeedCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java index 37bd3aea38..53a0eb8425 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonAddressesStub.java @@ -403,11 +403,7 @@ public UnaryCallable listPagedCallable( @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java index d5578176de..0be8cf4d03 100644 --- a/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java +++ b/test/integration/goldens/compute/com/google/cloud/compute/v1/stub/HttpJsonRegionOperationsStub.java @@ -167,11 +167,7 @@ public UnaryCallable getCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java index bc04cab818..48731cd53d 100644 --- a/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java +++ b/test/integration/goldens/credentials/com/google/cloud/iam/credentials/v1/stub/GrpcIamCredentialsStub.java @@ -239,11 +239,7 @@ public UnaryCallable signJwtCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java index 2c1e96db38..f40c2bfffe 100644 --- a/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java +++ b/test/integration/goldens/iam/com/google/iam/v1/stub/GrpcIAMPolicyStub.java @@ -197,11 +197,7 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java index 13a238d3fc..19065361ed 100644 --- a/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java +++ b/test/integration/goldens/kms/com/google/cloud/kms/v1/stub/GrpcKeyManagementServiceStub.java @@ -1119,11 +1119,7 @@ public UnaryCallable getLocationCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java index 8e3e2b9c27..c912a9b7b3 100644 --- a/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java +++ b/test/integration/goldens/library/com/google/cloud/example/library/v1/stub/GrpcLibraryServiceStub.java @@ -450,11 +450,7 @@ public UnaryCallable moveBookCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java index e2a5c38b45..91ddb14f6a 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcConfigServiceV2Stub.java @@ -889,11 +889,7 @@ public UnaryCallable updateCmekSettings @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java index 48b05cf5a1..feb0ed3086 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcLoggingServiceV2Stub.java @@ -328,11 +328,7 @@ public UnaryCallable listLogsPagedCallab @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java index da933b1ac4..fe15cf6052 100644 --- a/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java +++ b/test/integration/goldens/logging/com/google/cloud/logging/v2/stub/GrpcMetricsServiceV2Stub.java @@ -279,11 +279,7 @@ public UnaryCallable deleteLogMetricCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java index cff38b2fd6..2b5ce28a06 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcPublisherStub.java @@ -549,11 +549,7 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java index 094bcea972..11bac90c7d 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSchemaServiceStub.java @@ -412,11 +412,7 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java index 20817d2eb9..8580e626f8 100644 --- a/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java +++ b/test/integration/goldens/pubsub/com/google/cloud/pubsub/v1/stub/GrpcSubscriberStub.java @@ -762,11 +762,7 @@ public UnaryCallable getIamPolicyCallable() { @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java index 0b73bc8797..24c34f9b6e 100644 --- a/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java +++ b/test/integration/goldens/redis/com/google/cloud/redis/v1beta1/stub/GrpcCloudRedisStub.java @@ -505,11 +505,7 @@ public OperationCallable deleteInstanceOperat @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override diff --git a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java index 71c1cdac29..a0ae53ba0e 100644 --- a/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java +++ b/test/integration/goldens/storage/com/google/storage/v2/stub/GrpcStorageStub.java @@ -201,11 +201,7 @@ public ClientStreamingCallable writeObj @Override public final void close() { - try { - backgroundResources.close(); - } catch (Exception e) { - throw new IllegalStateException("Failed to close resource", e); - } + shutdown(); } @Override From e959ac1cd014bfb71b4d2e415aaf061cfb455d30 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 2 Aug 2021 14:36:50 -0700 Subject: [PATCH 11/11] fix: merge branches --- .../gapic/composer/common/AbstractServiceStubClassComposer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java index a294561e31..034c94c949 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java @@ -40,6 +40,8 @@ import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.ThisObjectValue; +import com.google.api.generator.engine.ast.ThrowExpr; +import com.google.api.generator.engine.ast.TryCatchStatement; import com.google.api.generator.engine.ast.TypeNode; import com.google.api.generator.engine.ast.ValueExpr; import com.google.api.generator.engine.ast.Variable;