hamcrest / JavaHamcrest

Java (and original) version of Hamcrest

Home Page:http://hamcrest.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GraalVM Native Image support

beckermarc opened this issue · comments

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 in reflect-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 under hamcrest/src/main/resources/META-INF/native-image/org.hamcrest/hamcrest/

[
  {
    "condition": {
      "typeReachable": "org.hamcrest.TypeSafeMatcher"
    },
    "name": "org.hamcrest.TypeSafeMatcher",
    "queryAllDeclaredMethods": true
  },
  {
    "condition": {
      "typeReachable": "org.hamcrest.TypeSafeDiagnosingMatcher"
    },
    "name": "org.hamcrest.TypeSafeDiagnosingMatcher",
    "queryAllDeclaredMethods": true
  },
  {
    "condition": {
      "typeReachable": "org.hamcrest.FeatureMatcher"
    },
    "name": "org.hamcrest.FeatureMatcher",
    "queryAllDeclaredMethods": true
  }
]

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.