Skip to content

Commit 17b5d80

Browse files
committed
Replace metadata with RuntimeHintsRegistrar.
1 parent 4ad5552 commit 17b5d80

8 files changed

Lines changed: 64 additions & 63 deletions

File tree

graalpy/graalpy-spring-boot-guide/README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,23 @@ This will start the application on port 8080.
472472

473473
The [GraalVM](https://www.graalvm.org/) Native Image compilation requires metadata to properly run code that uses [dynamic proxies](https://www.graalvm.org/latest/reference-manual/native-image/metadata/#dynamic-proxy).
474474

475-
For the case that also a native executable has to be generated, create a proxy configuration file:
475+
For the case that also a native executable has to be generated, add a `RuntimeHintsRegistrar`:
476476

477-
`src/main/resources/META-INF/native-image/proxy-config.json`
478-
```json
479-
[
480-
["com.example.demo.SentimentIntensityAnalyzer"]
481-
]
477+
`src/main/java/com/example/demo/SentimentAnalysisService.java`
478+
```java
479+
// ...
480+
@Service
481+
@ImportRuntimeHints(SentimentAnalysisService.SentimentIntensityAnalyzerRuntimeHints.class)
482+
public class SentimentAnalysisService {
483+
// ...
484+
static class SentimentIntensityAnalyzerRuntimeHints implements RuntimeHintsRegistrar {
485+
@Override
486+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
487+
// Register interface as proxy for Value.as().
488+
hints.proxies().registerJdkProxy(SentimentIntensityAnalyzer.class);
489+
}
490+
}
491+
}
482492
```
483493

484494
### 7.2. Generate a Native Executable with GraalVM

graalpy/graalpy-spring-boot-guide/src/main/java/com/example/demo/SentimentAnalysisService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
package com.example.demo;
88

9+
import org.springframework.aot.hint.RuntimeHints;
10+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
11+
import org.springframework.context.annotation.ImportRuntimeHints;
912
import org.springframework.stereotype.Service;
1013

1114
import java.util.Map;
1215

1316
@Service
17+
@ImportRuntimeHints(SentimentAnalysisService.SentimentIntensityAnalyzerRuntimeHints.class)
1418
public class SentimentAnalysisService {
1519
private final SentimentIntensityAnalyzer sentimentIntensityAnalyzer;
1620

@@ -25,4 +29,12 @@ public SentimentAnalysisService(GraalPyContext context) {
2529
public Map<String, Double> getSentimentScore(String text) {
2630
return sentimentIntensityAnalyzer.polarity_scores(text); // ③
2731
}
32+
33+
static class SentimentIntensityAnalyzerRuntimeHints implements RuntimeHintsRegistrar {
34+
@Override
35+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
36+
// Register interface as proxy for Value.as().
37+
hints.proxies().registerJdkProxy(SentimentIntensityAnalyzer.class);
38+
}
39+
}
2840
}

graalpy/graalpy-spring-boot-guide/src/main/resources/META-INF/native-image/proxy-config.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServiceMixed.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
package com.example.demo;
88

99
import com.example.demo.GraalPyContextConfiguration.GraalPyContext;
10+
import org.springframework.aot.hint.MemberCategory;
11+
import org.springframework.aot.hint.RuntimeHints;
12+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
13+
import org.springframework.context.annotation.ImportRuntimeHints;
1014
import org.springframework.stereotype.Service;
1115

1216
import java.util.List;
1317
import java.util.stream.IntStream;
1418

1519
@Service
20+
@ImportRuntimeHints(PyGalServiceMixed.PyGalServiceMixedRuntimeHints.class)
1621
public class PyGalServiceMixed implements PyGalService {
1722
private final RenderXYFunction renderXYFunction;
1823

@@ -53,4 +58,15 @@ public String renderXYChart() {
5358
);
5459
return renderXYFunction.apply(title, labelDatapointEntries);
5560
}
61+
62+
63+
static class PyGalServiceMixedRuntimeHints implements RuntimeHintsRegistrar {
64+
@Override
65+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
66+
// Register interface as proxy for Value.as().
67+
hints.proxies().registerJdkProxy(RenderXYFunction.class);
68+
// Register methods of Entry for reflection so that they can be called from Python.
69+
hints.reflection().registerType(Entry.class, MemberCategory.INVOKE_DECLARED_METHODS);
70+
}
71+
}
5672
}

graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServicePureJava.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88

99
import com.example.demo.GraalPyContextConfiguration.GraalPyContext;
1010
import org.graalvm.polyglot.Value;
11+
import org.springframework.aot.hint.MemberCategory;
12+
import org.springframework.aot.hint.RuntimeHints;
13+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
14+
import org.springframework.context.annotation.ImportRuntimeHints;
1115
import org.springframework.stereotype.Service;
1216

1317
import java.util.stream.IntStream;
1418

1519

1620
@Service
21+
@ImportRuntimeHints(PyGalServicePureJava.PyGalServicePureJavaRuntimeHints.class)
1722
public class PyGalServicePureJava implements PyGalService {
1823
private final PyGal pyGalModule;
1924

@@ -51,4 +56,17 @@ public String renderXYChart() {
5156
xyChart.add("y = -1", new int[][]{{-5, -1}, {5, -1}});
5257
return xyChart.render().decode();
5358
}
59+
60+
static class PyGalServicePureJavaRuntimeHints implements RuntimeHintsRegistrar {
61+
@Override
62+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
63+
// Register interfaces as proxy for Value.as().
64+
hints.proxies().registerJdkProxy(PyGal.class);
65+
hints.proxies().registerJdkProxy(XY.class);
66+
hints.proxies().registerJdkProxy(BytesIO.class);
67+
68+
// Provide access to title default method.
69+
hints.reflection().registerType(XY.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
70+
}
71+
}
5472
}

graalpy/graalpy-spring-boot-pygal-charts/src/main/resources/META-INF/native-image/com.example/demo/reachability-metadata.json

Lines changed: 0 additions & 47 deletions
This file was deleted.

graalwasm/graalwasm-spring-boot-photon/src/main/java/com/example/demo/PhotonPool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.example.demo;
88

9+
import com.example.demo.Photon.PhotonImage;
910
import jakarta.annotation.PreDestroy;
1011
import org.graalvm.polyglot.Context;
1112
import org.graalvm.polyglot.Engine;
@@ -85,6 +86,7 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
8586
hints.resources()
8687
.registerPattern("photon/*")
8788
.registerPattern("daisies_fuji.jpg");
89+
hints.proxies().registerJdkProxy(PhotonImage.class);
8890
}
8991
}
9092
}

graalwasm/graalwasm-spring-boot-photon/src/main/resources/META-INF/native-image/org.example/demo/proxy-config.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)