-
Notifications
You must be signed in to change notification settings - Fork 378
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
GraalVM Native Image support #403
Comments
Thanks for looking into this @beckermarc. I'm looking at creating some new Hamcrest releases in the near future. This looks like doesn't have any backwards compatibilty issues. I'm not a GraalVM user. Is there a sensible way to test this is working correctly? |
Thanks for picking this up.
Yes, although it requires some steps:
|
Thanks @beckermarc. This project is a complete mess right now, suffering from years of neglect. I think a priority is to get the CI infrastructure working again. When I'm doing that, I'll see if there's some way we can pull in a graalvm image to run the tests on. It might be a bit tricky, but I'm sure there's a way. I don't have a massive amount of free time, but I'm planning on plugging away at this project for the forseeable future. I won't get back to this straight away, but I'll get there eventually :-) |
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.startsWith;
@Test
void failNativeTest(){
assertThat("172.30.110.7@-@178924", startsWith("172.30.110.7@-@"));
}
@Test
void successNativeTest(){
assertThat("172.30.110.7@-@178924".startsWith("172.30.110.7@-@"), is(true));
}
[
{
"condition":{"typeReachable":"org.hamcrest.internal.ReflectiveTypeFinder"},
"name":"org.hamcrest.core.StringStartsWith",
"queryAllDeclaredMethods":true
},
{
"condition":{"typeReachable":"org.hamcrest.internal.ReflectiveTypeFinder"},
"name":"org.hamcrest.core.SubstringMatcher",
"queryAllDeclaredMethods":true
}
] |
GraalVM Native Image support and tooling also allows to run tests in Native Image style (e.g. see docs of Spring Boot Native Image support).
As Hamcrest is a popular matcher library and Spring Boot includes Hamcrest for example in it's spring-boot-starter-test it is very likely that Hamcrest is also used in tests executed inside a GraalVM Native Image.
There are some areas where Hamcrest is relying on Java reflection most prominently through
ReflectiveTypeFinder
, which is used in various matcher implementations. Some of these reflection usages - but not all - can be known upfront in the library. GraalVM requires reflection usage to be registered inreflect-config.json
files (see GraalVM docs here and here).A good starting point that should suit many usecases already are reflection hints for the usages of
ReflectiveTypeFinder
.Therefore my suggestion is to place the following
reflect-config.json
file, placed underhamcrest/src/main/resources/META-INF/native-image/org.hamcrest/hamcrest/
As it is preferred to add GraalVM Native Image support to the libraries directly I wanted to ask, if there is willingness to add this configuration to Hamcrest? If not, an alternative solution would be to add the reflection hints to the community maintained GraalVM Reachability Metadata.
The text was updated successfully, but these errors were encountered: