Skip to content

Commit f784369

Browse files
committed
GH-1250 - Register runtime hints for JSONPath internals for native image builds.
Our AOT support generates a JSON file containing application module metadata for runtime usage. That file is evaluated using JSONPath expressions and that evaluation triggers reflection inside the JSONPath library. We now register reflection hints allow the invocation of the constructors of the necessary types. This can be removed as soon as json-path/JsonPath#1042 is fixed.
1 parent 673aed7 commit f784369

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/autoconfigure/ApplicationModulesFileGeneratingProcessor.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
20+
import org.springframework.aot.hint.MemberCategory;
2021
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
2122
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
2223
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2324
import org.springframework.modulith.core.util.ApplicationModulesExporter;
2425
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
2526

27+
import com.jayway.jsonpath.internal.function.PathFunctionFactory;
28+
2629
/**
2730
* Renders the application module description JSON into a resource named
2831
* {@value ApplicationModulesExporter#DEFAULT_LOCATION}.
@@ -51,6 +54,22 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
5154

5255
context.getRuntimeHints().resources().registerPattern(location);
5356
context.getGeneratedFiles().addResourceFile(location, exporter.toJson());
57+
58+
// Register JSONPath internals as available for reflective construction to be able to read the generated files in
59+
// a native image
60+
//
61+
// TODO: Remove once https://github.com/json-path/JsonPath/issues/1042 is fixed
62+
63+
var reflection = context.getRuntimeHints().reflection();
64+
var classLoader = ApplicationModulesFileGeneratingProcessor.class.getClassLoader();
65+
66+
PathFunctionFactory.FUNCTIONS.values().forEach(it -> {
67+
68+
// Tweak name due to shading
69+
var typeName = it.getName().replace("com.jayway.jsonpath", "org.springframework.modulith.runtime.jsonpath");
70+
71+
reflection.registerTypeIfPresent(classLoader, typeName, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
72+
});
5473
};
5574
}
5675
}

0 commit comments

Comments
 (0)