Skip to content

Commit

Permalink
feat(scala): support scala native image build (#1922)
Browse files Browse the repository at this point in the history
## What does this PR do?

This PR supports scala native image build and fix quarkus graalvm build
for java in quarkiverse/quarkus-fury#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 quarkiverse/quarkus-fury#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.
-->
  • Loading branch information
chaokunyang authored Nov 3, 2024
1 parent fa22801 commit 785572d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 19 deletions.
61 changes: 42 additions & 19 deletions java/fury-core/src/main/java/org/apache/fury/type/ScalaTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,64 @@
/** 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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 785572d

Please sign in to comment.