This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 785572da feat(scala): support scala native image build (#1922)
785572da is described below

commit 785572daf60d166d7f9c884ec57c79b11bb4cad0
Author: Shawn Yang <[email protected]>
AuthorDate: Sun Nov 3 23:31:31 2024 +0800

    feat(scala): support scala native image build (#1922)
    
    ## What does this PR do?
    
    This PR supports scala native image build and fix quarkus graalvm build
    for java in https://github.com/quarkiverse/quarkus-fury/issues/7:
    
    ```
    Error: Class initialization of org.apache.fury.type.ScalaTypes failed. Use 
the option
    
        '--initialize-at-run-time=org.apache.fury.type.ScalaTypes'
    
     to explicitly request initialization of this class at run time.
    com.oracle.svm.core.util.UserError$UserException: Class initialization of 
org.apache.fury.type.ScalaTypes failed. Use the option
    
        '--initialize-at-run-time=org.apache.fury.type.ScalaTypes'
    
     to explicitly request initialization of this class at run time.
            at 
org.graalvm.nativeimage.builder/com.oracle.svm.core.util.UserError.abort(UserError.java:85)
            at 
org.graalvm.nativeimage.builder/com.oracle.svm.hosted.classinitialization.ClassInitializationSupport.ensureClassInitialized(ClassInitializationSupport.java:195)
            at ..................
    
org.graalvm.nativeimage.builder/com.oracle.svm.hosted.classinitialization.ClassInitializationSupport.ensureClassInitialized(ClassInitializationSupport.java:177)
            ... 43 more
    Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: 
scala.collection.Iterable
            at 
org.apache.fury.reflect.ReflectionUtils.loadClass(ReflectionUtils.java:649)
            at org.apache.fury.type.ScalaTypes.<clinit>(ScalaTypes.java:40)
            ... 46 more
    Caused by: java.lang.ClassNotFoundException: scala.collection.Iterable
            at 
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
            at 
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
            at 
org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageClassLoader.loadClass(NativeImageClassLoader.java:637)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
            at 
org.apache.fury.reflect.ReflectionUtils.loadClass(ReflectionUtils.java:646)
            ... 47 more
    ```
    
    ## Related issues
    
    Closes https://github.com/quarkiverse/quarkus-fury/issues/7
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fury/issues/new/choose) describing the
    need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../main/java/org/apache/fury/type/ScalaTypes.java | 61 +++++++++++++++-------
 .../fury-core/native-image.properties              | 35 +++++++++++++
 2 files changed, 77 insertions(+), 19 deletions(-)

diff --git a/java/fury-core/src/main/java/org/apache/fury/type/ScalaTypes.java 
b/java/fury-core/src/main/java/org/apache/fury/type/ScalaTypes.java
index 485776a8..31d43906 100644
--- a/java/fury-core/src/main/java/org/apache/fury/type/ScalaTypes.java
+++ b/java/fury-core/src/main/java/org/apache/fury/type/ScalaTypes.java
@@ -28,41 +28,64 @@ import org.apache.fury.reflect.TypeRef;
 /** Scala types utils using reflection without dependency on scala library. */
 @SuppressWarnings({"unchecked", "rawtypes"})
 public class ScalaTypes {
-  private static final Class<?> SCALA_MAP_TYPE;
-  private static final Class<?> SCALA_SEQ_TYPE;
-  private static final Class<?> SCALA_ITERABLE_TYPE;
-  private static final Class<?> SCALA_ITERATOR_TYPE;
-  private static final java.lang.reflect.Type SCALA_ITERATOR_RETURN_TYPE;
-  private static final java.lang.reflect.Type SCALA_NEXT_RETURN_TYPE;
+  private static volatile Class<?> SCALA_MAP_TYPE;
+  private static volatile Class<?> SCALA_SEQ_TYPE;
+  private static volatile Class<?> SCALA_ITERABLE_TYPE;
+  private static volatile java.lang.reflect.Type SCALA_ITERATOR_RETURN_TYPE;
+  private static volatile java.lang.reflect.Type SCALA_NEXT_RETURN_TYPE;
 
-  static {
-    try {
-      SCALA_ITERABLE_TYPE = 
ReflectionUtils.loadClass("scala.collection.Iterable");
-      SCALA_ITERATOR_TYPE = 
ReflectionUtils.loadClass("scala.collection.Iterator");
+  public static Class<?> getScalaMapType() {
+    if (SCALA_MAP_TYPE == null) {
+      // load scala classes dynamically to make graalvm native build work
+      // see https://github.com/quarkiverse/quarkus-fury/issues/7
       SCALA_MAP_TYPE = ReflectionUtils.loadClass("scala.collection.Map");
-      SCALA_SEQ_TYPE = ReflectionUtils.loadClass("scala.collection.Seq");
-      SCALA_ITERATOR_RETURN_TYPE = 
SCALA_ITERABLE_TYPE.getMethod("iterator").getGenericReturnType();
-      SCALA_NEXT_RETURN_TYPE = 
SCALA_ITERATOR_TYPE.getMethod("next").getGenericReturnType();
-    } catch (NoSuchMethodException e) {
-      throw new RuntimeException(e);
     }
-  }
-
-  public static Class<?> getScalaMapType() {
     return SCALA_MAP_TYPE;
   }
 
   public static Class<?> getScalaSeqType() {
+    if (SCALA_SEQ_TYPE == null) {
+      SCALA_SEQ_TYPE = ReflectionUtils.loadClass("scala.collection.Seq");
+    }
     return SCALA_SEQ_TYPE;
   }
 
   public static Class<?> getScalaIterableType() {
+    if (SCALA_ITERABLE_TYPE == null) {
+      SCALA_ITERABLE_TYPE = 
ReflectionUtils.loadClass("scala.collection.Iterable");
+    }
     return SCALA_ITERABLE_TYPE;
   }
 
   public static TypeRef<?> getElementType(TypeRef typeRef) {
     TypeRef<?> supertype = typeRef.getSupertype(getScalaIterableType());
-    return 
supertype.resolveType(SCALA_ITERATOR_RETURN_TYPE).resolveType(SCALA_NEXT_RETURN_TYPE);
+    return supertype
+        .resolveType(getScalaIteratorReturnType())
+        .resolveType(getScalaNextReturnType());
+  }
+
+  private static Type getScalaIteratorReturnType() {
+    if (SCALA_ITERATOR_RETURN_TYPE == null) {
+      try {
+        SCALA_ITERATOR_RETURN_TYPE =
+            
getScalaIterableType().getMethod("iterator").getGenericReturnType();
+      } catch (NoSuchMethodException e) {
+        throw new RuntimeException(e);
+      }
+    }
+    return SCALA_ITERATOR_RETURN_TYPE;
+  }
+
+  private static Type getScalaNextReturnType() {
+    if (SCALA_NEXT_RETURN_TYPE == null) {
+      Class<?> scalaIteratorType = 
ReflectionUtils.loadClass("scala.collection.Iterator");
+      try {
+        SCALA_NEXT_RETURN_TYPE = 
scalaIteratorType.getMethod("next").getGenericReturnType();
+      } catch (NoSuchMethodException e) {
+        throw new RuntimeException(e);
+      }
+    }
+    return SCALA_NEXT_RETURN_TYPE;
   }
 
   /** Returns key/value type of scala map. */
diff --git 
a/scala/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties
 
b/scala/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties
new file mode 100644
index 00000000..bdc7207e
--- /dev/null
+++ 
b/scala/src/main/resources/META-INF/native-image/org.apache.fury/fury-core/native-image.properties
@@ -0,0 +1,35 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# 
https://www.graalvm.org/latest/reference-manual/native-image/dynamic-features/Reflection/#unsafe-accesses
 :
+# The unsafe offset get on build time may be different from runtime
+Args=--initialize-at-build-time=org.apache.fury.type.ScalaTypes,\
+    org.apache.fury.type.Seq,\
+    org.apache.fury.type.Map,\
+    org.apache.fury.type.Iterable,\
+    scala.collection.Iterator,\
+    org.apache.fury.serializer.scala.ScalaDispatcher,\
+    org.apache.fury.serializer.scala.AbstractScalaCollectionSerializer,\
+    org.apache.fury.serializer.scala.AbstractScalaMapSerializer,\
+    org.apache.fury.serializer.scala.ScalaSortedMapSerializer,\
+    org.apache.fury.serializer.scala.ScalaMapSerializer,\
+    org.apache.fury.serializer.scala.ScalaSortedSetSerializer,\
+    org.apache.fury.serializer.scala.ScalaSeqSerializer,\
+    org.apache.fury.serializer.scala.ScalaCollectionSerializer,\
+    org.apache.fury.serializer.scala.RangeSerializer,\
+    org.apache.fury.serializer.scala.RangeUtils$,\
+    org.apache.fury.serializer.scala.NumericRangeSerializer


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to