Skip to content

Commit

Permalink
fix(java): Register Spanner classes for reflection to fix Native Imag…
Browse files Browse the repository at this point in the history
…e tests (#760)

* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
mpeddada1 and gcf-owl-bot[bot] committed Mar 16, 2022
1 parent 38deab0 commit da57e9a
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,67 @@
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
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) {
Class<?> spannerTestClass = access.findClassByName(SPANNER_TEST_CLASS);
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(
Expand All @@ -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");
}
}
}

0 comments on commit da57e9a

Please sign in to comment.