Skip to content

Commit ba87cda

Browse files
committed
fix(gax): register Conscrypt SSLContext SPI classes for GraalVM reflection
1 parent 671f892 commit ba87cda

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/nativeimage/GoogleJsonClientFeature.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
package com.google.api.gax.nativeimage;
3232

3333
import static com.google.api.gax.nativeimage.NativeImageUtils.registerClassForReflection;
34+
import static com.google.api.gax.nativeimage.NativeImageUtils.registerClassHierarchyForReflection;
3435

3536
import org.graalvm.nativeimage.hosted.Feature;
3637
import org.jspecify.annotations.NullMarked;
@@ -51,9 +52,31 @@ final class GoogleJsonClientFeature implements Feature {
5152
public void beforeAnalysis(BeforeAnalysisAccess access) {
5253
loadApiClient(access);
5354
loadHttpClient(access);
55+
loadConscrypt(access);
5456
loadMiscClasses(access);
5557
}
5658

59+
/**
60+
* Registers Conscrypt SSLContext and Security Provider SPI implementation classes (and their
61+
* nested protocol subclasses) for GraalVM reflection when Conscrypt is present on the classpath.
62+
*
63+
* <p>When Conscrypt is configured as the security provider for HTTP/JSON transports, Java's JCA
64+
* framework reflectively instantiates provider implementation classes (e.g. {@code
65+
* OpenSSLContextImpl$TLSv13}) via String lookup in {@code SSLContext.getInstance("TLS",
66+
* provider)}. In GraalVM Native Image builds, these reflectively looked-up SPI classes are
67+
* stripped by static analysis unless explicitly registered for reflection, leading to {@code
68+
* ClassNotFoundException} / {@code NoSuchAlgorithmException} at runtime.
69+
*/
70+
private void loadConscrypt(BeforeAnalysisAccess access) {
71+
Class<?> conscryptClass = access.findClassByName("org.conscrypt.Conscrypt");
72+
if (conscryptClass != null) {
73+
registerClassHierarchyForReflection(access, "org.conscrypt.OpenSSLContextImpl");
74+
registerClassHierarchyForReflection(access, "org.conscrypt.OpenSSLProvider");
75+
registerClassHierarchyForReflection(access, "org.conscrypt.KeyManagerFactoryImpl");
76+
registerClassHierarchyForReflection(access, "org.conscrypt.TrustManagerFactoryImpl");
77+
}
78+
}
79+
5780
private void loadApiClient(BeforeAnalysisAccess access) {
5881
// For com.google.api-client:google-api-client
5982
Class<?> googleApiClientClass = access.findClassByName(GOOGLE_API_CLIENT_CLASS);

0 commit comments

Comments
 (0)