Add jni-config generation

This commit adds support generating graalvm `jni-config.json` file.

Configuration format for jni/reflection in graalvm is documented
to be exactly same so we can re-use facilities for reflection hints
which should be relatively clean for a user as also graalvm uses same
classes for both jni/reflection.

Closes gh-29007
This commit is contained in:
Janne Valkealahti
2022-09-05 19:07:07 +01:00
committed by Sébastien Deleuze
parent 6902ff77d9
commit 135f9070c5
4 changed files with 42 additions and 1 deletions

View File

@@ -28,6 +28,7 @@ package org.springframework.aot.hint;
* recorded as well.
*
* @author Stephane Nicoll
* @author Janne Valkealahti
* @since 6.0
*/
public class RuntimeHints {
@@ -40,6 +41,8 @@ public class RuntimeHints {
private final ProxyHints proxies = new ProxyHints();
private final ReflectionHints jni = new ReflectionHints();
/**
* Provide access to reflection-based hints.
@@ -73,4 +76,12 @@ public class RuntimeHints {
return this.proxies;
}
/**
* Provide access to jni-based hints.
* @return jni hints
*/
public ReflectionHints jni() {
return this.jni;
}
}

View File

@@ -29,6 +29,7 @@ import org.springframework.aot.hint.SerializationHints;
*
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @author Janne Valkealahti
* @since 6.0
* @see <a href="https://www.graalvm.org/22.1/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
*/
@@ -52,6 +53,9 @@ public abstract class NativeConfigurationWriter {
hints.resources().resourceBundles().findAny().isPresent()) {
writeResourceHints(hints.resources());
}
if (hints.jni().typeHints().findAny().isPresent()) {
writeJniHints(hints.jni());
}
}
/**
@@ -82,4 +86,9 @@ public abstract class NativeConfigurationWriter {
ResourceHintsWriter.INSTANCE.write(writer, hints));
}
private void writeJniHints(ReflectionHints hints) {
writeTo("jni-config.json", writer ->
ReflectionHintsWriter.INSTANCE.write(writer, hints));
}
}

View File

@@ -34,12 +34,15 @@ import org.springframework.lang.Nullable;
/**
* Write {@link ReflectionHints} to the JSON output expected by the GraalVM
* {@code native-image} compiler, typically named {@code reflect-config.json}.
* {@code native-image} compiler, typically named {@code reflect-config.json}
* or {@code jni-config.json}.
*
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @author Janne Valkealahti
* @since 6.0
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/Reflection/">Reflection Use in Native Images</a>
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/JNI/">Java Native Interface (JNI) in Native Image</a>
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
*/
class ReflectionHintsWriter {