Add compatibility with GraalVM 22.1 (#1215)

* do not init java net http classes at build time
This commit is contained in:
Michiel Borkent 2022-03-22 16:41:26 +01:00 committed by GitHub
parent 6d35314096
commit 6305f7450d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 104 deletions

View file

@ -54,6 +54,7 @@ args=("-jar" "$BABASHKA_JAR"
"--verbose" "--verbose"
"--no-fallback" "--no-fallback"
"--native-image-info" "--native-image-info"
# --trace-class-initialization=jdk.internal.net.http.common.DebugLogger,jdk.internal.net.http.websocket.WebSocketImpl,jdk.internal.net.http.common.Utils
"$BABASHKA_XMX") "$BABASHKA_XMX")
BABASHKA_STATIC=${BABASHKA_STATIC:-} BABASHKA_STATIC=${BABASHKA_STATIC:-}

View file

@ -113,7 +113,7 @@
java.util.Iterator java.util.Iterator
{:methods [{:name "hasNext"} {:methods [{:name "hasNext"}
{:name "next"}]} {:name "next"}]}
}) })
(def custom-map (def custom-map
(cond-> (cond->
@ -126,6 +126,52 @@
{:methods [{:name "getBundle" {:methods [{:name "getBundle"
:parameterTypes ["java.lang.String","java.util.Locale","java.lang.ClassLoader"]}]}))) :parameterTypes ["java.lang.String","java.util.Locale","java.lang.ClassLoader"]}]})))
(def java-net-http-classes
"These classes must be initialized at run time since GraalVM 22.1"
'[java.net.Authenticator
java.net.CookieHandler
java.net.CookieManager
java.net.CookieStore
java.net.CookiePolicy
java.net.HttpCookie
java.net.PasswordAuthentication
java.net.ProxySelector
java.net.http.HttpClient
java.net.http.HttpClient$Builder
java.net.http.HttpClient$Redirect
java.net.http.HttpClient$Version
java.net.http.HttpHeaders
java.net.http.HttpRequest
java.net.http.HttpRequest$BodyPublisher
java.net.http.HttpRequest$BodyPublishers
java.net.http.HttpRequest$Builder
java.net.http.HttpResponse
java.net.http.HttpResponse$BodyHandler
java.net.http.HttpResponse$BodyHandlers
java.net.http.HttpTimeoutException
java.net.http.WebSocket
java.net.http.WebSocket$Builder
java.net.http.WebSocket$Listener
java.security.cert.X509Certificate
javax.crypto.Mac
javax.crypto.spec.SecretKeySpec
javax.net.ssl.HostnameVerifier ;; clj-http-lite
javax.net.ssl.HttpsURLConnection ;; clj-http-lite
javax.net.ssl.KeyManagerFactory
javax.net.ssl.SSLContext
javax.net.ssl.SSLParameters
javax.net.ssl.SSLSession ;; clj-http-lite
javax.net.ssl.TrustManager
javax.net.ssl.TrustManagerFactory
javax.net.ssl.X509TrustManager
jdk.internal.net.http.HttpClientBuilderImpl
jdk.internal.net.http.HttpClientFacade
jdk.internal.net.http.HttpRequestBuilderImpl
jdk.internal.net.http.HttpResponseImpl
jdk.internal.net.http.common.MinimalFuture
jdk.internal.net.http.websocket.BuilderImpl
jdk.internal.net.http.websocket.WebSocketImpl])
(def classes (def classes
`{:all [clojure.lang.ArityException `{:all [clojure.lang.ArityException
clojure.lang.BigInt clojure.lang.BigInt
@ -225,51 +271,6 @@
;; java.net.URL, see below ;; java.net.URL, see below
java.net.URLEncoder java.net.URLEncoder
java.net.URLDecoder java.net.URLDecoder
;; java.net.http
~@(when features/java-net-http?
'[java.net.Authenticator
java.net.CookieHandler
java.net.CookieManager
java.net.CookieStore
java.net.CookiePolicy
java.net.HttpCookie
java.net.PasswordAuthentication
java.net.ProxySelector
java.net.http.HttpClient
java.net.http.HttpClient$Builder
java.net.http.HttpClient$Redirect
java.net.http.HttpClient$Version
java.net.http.HttpHeaders
java.net.http.HttpRequest
java.net.http.HttpRequest$BodyPublisher
java.net.http.HttpRequest$BodyPublishers
java.net.http.HttpRequest$Builder
java.net.http.HttpResponse
java.net.http.HttpResponse$BodyHandler
java.net.http.HttpResponse$BodyHandlers
java.net.http.HttpTimeoutException
java.net.http.WebSocket
java.net.http.WebSocket$Builder
java.net.http.WebSocket$Listener
java.security.cert.X509Certificate
javax.crypto.Mac
javax.crypto.spec.SecretKeySpec
javax.net.ssl.HostnameVerifier ;; clj-http-lite
javax.net.ssl.HttpsURLConnection ;; clj-http-lite
javax.net.ssl.KeyManagerFactory
javax.net.ssl.SSLContext
javax.net.ssl.SSLParameters
javax.net.ssl.SSLSession ;; clj-http-lite
javax.net.ssl.TrustManager
javax.net.ssl.TrustManagerFactory
javax.net.ssl.X509TrustManager
jdk.internal.net.http.HttpClientBuilderImpl
jdk.internal.net.http.HttpClientFacade
jdk.internal.net.http.HttpRequestBuilderImpl
jdk.internal.net.http.HttpResponseImpl
jdk.internal.net.http.common.MinimalFuture
jdk.internal.net.http.websocket.BuilderImpl
jdk.internal.net.http.websocket.WebSocketImpl])
~@(when features/java-nio? ~@(when features/java-nio?
'[java.nio.ByteBuffer '[java.nio.ByteBuffer
java.nio.ByteOrder java.nio.ByteOrder
@ -499,63 +500,74 @@
m (apply hash-map m (apply hash-map
(for [c classes (for [c classes
c [(list 'quote c) c]] c [(list 'quote c) c]]
c))] c))
(assoc m :public-class m (assoc m :public-class
(fn [v] (fn [v]
(cond (instance? java.lang.Process v) (cond (instance? java.lang.Process v)
java.lang.Process java.lang.Process
(instance? java.lang.ProcessHandle v) (instance? java.lang.ProcessHandle v)
java.lang.ProcessHandle java.lang.ProcessHandle
(instance? java.lang.ProcessHandle$Info v) (instance? java.lang.ProcessHandle$Info v)
java.lang.ProcessHandle$Info java.lang.ProcessHandle$Info
;; added for calling .put on .environment from ProcessBuilder ;; added for calling .put on .environment from ProcessBuilder
(instance? java.util.Map v) (instance? java.util.Map v)
java.util.Map java.util.Map
;; added for issue #239 regarding clj-http-lite ;; added for issue #239 regarding clj-http-lite
;; can potentially be removed due to fix for #1061 ;; can potentially be removed due to fix for #1061
(instance? java.io.ByteArrayOutputStream v) (instance? java.io.ByteArrayOutputStream v)
java.io.ByteArrayOutputStream java.io.ByteArrayOutputStream
(instance? java.security.MessageDigest v) (instance? java.security.MessageDigest v)
java.security.MessageDigest java.security.MessageDigest
;; streams ;; streams
(instance? java.io.InputStream v) (instance? java.io.InputStream v)
java.io.InputStream java.io.InputStream
(instance? java.io.OutputStream v) (instance? java.io.OutputStream v)
java.io.OutputStream java.io.OutputStream
;; java nio ;; java nio
(instance? java.nio.file.Path v) (instance? java.nio.file.Path v)
java.nio.file.Path java.nio.file.Path
(instance? java.nio.file.FileSystem v) (instance? java.nio.file.FileSystem v)
java.nio.file.FileSystem java.nio.file.FileSystem
(instance? java.nio.file.PathMatcher v) (instance? java.nio.file.PathMatcher v)
java.nio.file.PathMatcher java.nio.file.PathMatcher
(instance? java.util.stream.BaseStream v) (instance? java.util.stream.BaseStream v)
java.util.stream.BaseStream java.util.stream.BaseStream
(instance? java.nio.ByteBuffer v) (instance? java.nio.ByteBuffer v)
java.nio.ByteBuffer java.nio.ByteBuffer
(instance? java.nio.charset.Charset v) (instance? java.nio.charset.Charset v)
java.nio.charset.Charset java.nio.charset.Charset
(instance? java.nio.charset.CharsetEncoder v) (instance? java.nio.charset.CharsetEncoder v)
java.nio.charset.CharsetEncoder java.nio.charset.CharsetEncoder
(instance? java.nio.CharBuffer v) (instance? java.nio.CharBuffer v)
java.nio.CharBuffer java.nio.CharBuffer
(instance? java.nio.channels.FileChannel v) (instance? java.nio.channels.FileChannel v)
java.nio.channels.FileChannel java.nio.channels.FileChannel
(instance? java.net.CookieStore v) (instance? java.net.CookieStore v)
java.net.CookieStore java.net.CookieStore
;; this makes interop on reified classes work ;; this makes interop on reified classes work
;; see java_net_http_test/interop-test ;; see java_net_http_test/interop-test
(instance? sci.impl.types.IReified v) (instance? sci.impl.types.IReified v)
(first (t/getInterfaces v)) (first (t/getInterfaces v))
;; fix for #1061 ;; fix for #1061
(instance? java.io.Closeable v) (instance? java.io.Closeable v)
java.io.Closeable java.io.Closeable
(instance? java.nio.file.attribute.BasicFileAttributes v) (instance? java.nio.file.attribute.BasicFileAttributes v)
java.nio.file.attribute.BasicFileAttributes java.nio.file.attribute.BasicFileAttributes
;; keep commas for merge friendliness ;; keep commas for merge friendliness
,,,))))) ,,,)))]
m))
(def class-map (gen-class-map))
(def class-map*
"This contains mapping of symbol to class of all classes that are allowed to be initialized at build time."
(gen-class-map))
(def class-map
"A delay to delay initialization of java-net-http classes to run time, since GraalVM 22.1"
(delay (persistent! (reduce (fn [acc c]
(assoc! acc c (Class/forName (str c))))
(transient class-map*) (when features/java-net-http?
java-net-http-classes)))))
(def imports (def imports
'{Appendable java.lang.Appendable '{Appendable java.lang.Appendable
@ -602,7 +614,9 @@
}) })
(defn reflection-file-entries [] (defn reflection-file-entries []
(let [entries (vec (for [c (sort (:all classes)) (let [entries (vec (for [c (sort (concat (:all classes)
(when features/java-net-http?
java-net-http-classes)))
:let [class-name (str c)]] :let [class-name (str c)]]
{:name class-name {:name class-name
:allPublicMethods true :allPublicMethods true

View file

@ -777,7 +777,7 @@ Use bb run --help to show this help output.
'load-file (sci-namespaces/core-var 'load-file load-file*)))) 'load-file (sci-namespaces/core-var 'load-file load-file*))))
:env env :env env
:features #{:bb :clj} :features #{:bb :clj}
:classes classes/class-map :classes @classes/class-map
:imports classes/imports :imports classes/imports
:load-fn load-fn :load-fn load-fn
:uberscript uberscript :uberscript uberscript