From da57e9ac04ebb5af756dc7d34157b8926201b0de Mon Sep 17 00:00:00 2001 From: Mridula <66699525+mpeddada1@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:43:07 -0400 Subject: [PATCH] fix(java): Register Spanner classes for reflection to fix Native Image tests (#760) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(java): register Spanner classes and resource to fix Native Image tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .../features/clients/SpannerFeature.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/java-core/native-image-support/src/main/java/com/google/cloud/nativeimage/features/clients/SpannerFeature.java b/java-core/native-image-support/src/main/java/com/google/cloud/nativeimage/features/clients/SpannerFeature.java index 3e5495840df6..7c9fca142163 100644 --- a/java-core/native-image-support/src/main/java/com/google/cloud/nativeimage/features/clients/SpannerFeature.java +++ b/java-core/native-image-support/src/main/java/com/google/cloud/nativeimage/features/clients/SpannerFeature.java @@ -21,6 +21,7 @@ import com.oracle.svm.core.configure.ResourcesRegistry; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; +import org.graalvm.nativeimage.impl.ConfigurationCondition; /** Registers Spanner library classes for reflection. */ @AutomaticFeature @@ -28,6 +29,24 @@ final class SpannerFeature implements Feature { private static final String SPANNER_CLASS = "com.google.spanner.v1.SpannerGrpc"; private static final String SPANNER_TEST_CLASS = "com.google.cloud.spanner.GceTestEnvConfig"; + private static final String CLIENT_SIDE_IMPL_CLASS = + "com.google.cloud.spanner.connection.ClientSideStatementImpl"; + private static final String CLIENT_SIDE_VALUE_CONVERTER = + "com.google.cloud.spanner.connection.ClientSideStatementValueConverters"; + private static final String CONNECTION_IMPL = + "com.google.cloud.spanner.connection.ConnectionImpl"; + private static final String CLIENT_SIDE_STATEMENTS = + "com.google.cloud.spanner.connection.ClientSideStatements"; + private static final String CONNECTION_STATEMENT_EXECUTOR = + "com.google.cloud.spanner.connection.ConnectionStatementExecutor"; + private static final String CLIENT_SIDE_STATEMENT_NO_PARAM_EXECUTOR = + "com.google.cloud.spanner.connection.ClientSideStatementNoParamExecutor"; + private static final String CLIENT_SIDE_STATEMENT_SET_EXECUTOR = + "com.google.cloud.spanner.connection.ClientSideStatementSetExecutor"; + private static final String ABSTRACT_STATEMENT_PARSER = + "com.google.cloud.spanner.connection.AbstractStatementParser"; + private static final String STATEMENT_PARSER = + "com.google.cloud.spanner.connection.SpannerStatementParser"; @Override public void beforeAnalysis(BeforeAnalysisAccess access) { @@ -35,6 +54,34 @@ public void beforeAnalysis(BeforeAnalysisAccess access) { if (spannerTestClass != null) { NativeImageUtils.registerConstructorsForReflection(access, SPANNER_TEST_CLASS); } + if (access.findClassByName(CLIENT_SIDE_IMPL_CLASS) != null) { + NativeImageUtils.registerClassHierarchyForReflection(access, CLIENT_SIDE_IMPL_CLASS); + } + if (access.findClassByName(CLIENT_SIDE_STATEMENT_NO_PARAM_EXECUTOR) != null) { + NativeImageUtils.registerClassForReflection(access, CLIENT_SIDE_STATEMENT_NO_PARAM_EXECUTOR); + } + if (access.findClassByName(CLIENT_SIDE_STATEMENT_SET_EXECUTOR) != null) { + NativeImageUtils.registerClassForReflection(access, CLIENT_SIDE_STATEMENT_SET_EXECUTOR); + } + if (access.findClassByName(CLIENT_SIDE_VALUE_CONVERTER) != null) { + NativeImageUtils.registerClassHierarchyForReflection(access, CLIENT_SIDE_VALUE_CONVERTER); + } + if (access.findClassByName(CLIENT_SIDE_STATEMENTS) != null) { + NativeImageUtils.registerClassForReflection(access, CLIENT_SIDE_STATEMENTS); + } + if (access.findClassByName(CONNECTION_STATEMENT_EXECUTOR) != null) { + NativeImageUtils.registerClassForReflection(access, CONNECTION_STATEMENT_EXECUTOR); + } + if (access.findClassByName(CONNECTION_IMPL) != null) { + NativeImageUtils.registerClassForReflection(access, CONNECTION_IMPL); + } + if (access.findClassByName(ABSTRACT_STATEMENT_PARSER) != null) { + NativeImageUtils.registerClassHierarchyForReflection(access, ABSTRACT_STATEMENT_PARSER); + } + if (access.findClassByName(STATEMENT_PARSER) != null) { + NativeImageUtils.registerConstructorsForReflection(access, STATEMENT_PARSER); + } + Class spannerClass = access.findClassByName(SPANNER_CLASS); if (spannerClass != null) { NativeImageUtils.registerClassHierarchyForReflection( @@ -47,9 +94,13 @@ public void beforeAnalysis(BeforeAnalysisAccess access) { // Resources ResourcesRegistry resourcesRegistry = ImageSingletons.lookup(ResourcesRegistry.class); resourcesRegistry.addResources( + ConfigurationCondition.alwaysTrue(), "\\Qcom/google/cloud/spanner/connection/ClientSideStatements.json\\E"); resourcesRegistry.addResources( "\\Qcom/google/cloud/spanner/spi/v1/grpc-gcp-apiconfig.json\\E"); + resourcesRegistry.addResources( + ConfigurationCondition.alwaysTrue(), + "\\Qcom/google/cloud/spanner/connection/ITSqlScriptTest_TestQueryOptions.sql\\E"); } } }