Skip to content

Commit 135f907

Browse files
jvalkealsdeleuze
authored andcommitted
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
1 parent 6902ff7 commit 135f907

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

spring-core/src/main/java/org/springframework/aot/hint/RuntimeHints.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* recorded as well.
2929
*
3030
* @author Stephane Nicoll
31+
* @author Janne Valkealahti
3132
* @since 6.0
3233
*/
3334
public class RuntimeHints {
@@ -40,6 +41,8 @@ public class RuntimeHints {
4041

4142
private final ProxyHints proxies = new ProxyHints();
4243

44+
private final ReflectionHints jni = new ReflectionHints();
45+
4346

4447
/**
4548
* Provide access to reflection-based hints.
@@ -73,4 +76,12 @@ public ProxyHints proxies() {
7376
return this.proxies;
7477
}
7578

79+
/**
80+
* Provide access to jni-based hints.
81+
* @return jni hints
82+
*/
83+
public ReflectionHints jni() {
84+
return this.jni;
85+
}
86+
7687
}

spring-core/src/main/java/org/springframework/aot/nativex/NativeConfigurationWriter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*
3030
* @author Sebastien Deleuze
3131
* @author Stephane Nicoll
32+
* @author Janne Valkealahti
3233
* @since 6.0
3334
* @see <a href="https://www.graalvm.org/22.1/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
3435
*/
@@ -52,6 +53,9 @@ public void write(RuntimeHints hints) {
5253
hints.resources().resourceBundles().findAny().isPresent()) {
5354
writeResourceHints(hints.resources());
5455
}
56+
if (hints.jni().typeHints().findAny().isPresent()) {
57+
writeJniHints(hints.jni());
58+
}
5559
}
5660

5761
/**
@@ -82,4 +86,9 @@ private void writeResourceHints(ResourceHints hints) {
8286
ResourceHintsWriter.INSTANCE.write(writer, hints));
8387
}
8488

89+
private void writeJniHints(ReflectionHints hints) {
90+
writeTo("jni-config.json", writer ->
91+
ReflectionHintsWriter.INSTANCE.write(writer, hints));
92+
}
93+
8594
}

spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@
3434

3535
/**
3636
* Write {@link ReflectionHints} to the JSON output expected by the GraalVM
37-
* {@code native-image} compiler, typically named {@code reflect-config.json}.
37+
* {@code native-image} compiler, typically named {@code reflect-config.json}
38+
* or {@code jni-config.json}.
3839
*
3940
* @author Sebastien Deleuze
4041
* @author Stephane Nicoll
42+
* @author Janne Valkealahti
4143
* @since 6.0
4244
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/Reflection/">Reflection Use in Native Images</a>
45+
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/JNI/">Java Native Interface (JNI) in Native Image</a>
4346
* @see <a href="https://www.graalvm.org/22.0/reference-manual/native-image/BuildConfiguration/">Native Image Build Configuration</a>
4447
*/
4548
class ReflectionHintsWriter {

spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* Tests for {@link FileNativeConfigurationWriter}.
5050
*
5151
* @author Sebastien Deleuze
52+
* @author Janne Valkealahti
5253
*/
5354
public class FileNativeConfigurationWriterTests {
5455

@@ -149,6 +150,23 @@ void reflectionConfig() throws IOException, JSONException {
149150
]""", "reflect-config.json");
150151
}
151152

153+
@Test
154+
void jniConfig() throws IOException, JSONException {
155+
// same format as reflection so just test basic file generation
156+
FileNativeConfigurationWriter generator = new FileNativeConfigurationWriter(tempDir);
157+
RuntimeHints hints = new RuntimeHints();
158+
ReflectionHints jniHints = hints.jni();
159+
jniHints.registerType(StringDecoder.class, builder -> builder.onReachableType(String.class));
160+
generator.write(hints);
161+
assertEquals("""
162+
[
163+
{
164+
"name": "org.springframework.core.codec.StringDecoder",
165+
"condition": { "typeReachable": "java.lang.String" }
166+
}
167+
]""", "jni-config.json");
168+
}
169+
152170
@Test
153171
void resourceConfig() throws IOException, JSONException {
154172
FileNativeConfigurationWriter generator = new FileNativeConfigurationWriter(tempDir);

0 commit comments

Comments
 (0)