Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scala): support scala native image build #1922

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading