I'm working on a software project that currently produces WARNING: Illegal reflective access by Classname
warnings from the Java (9+) process because it does reflective access to classes in java.base
.
Instead of adding opens for java.base
, I want to change the implementation to not rely on reflective access into java.base
anymore.
I would like to guard this implementation change by a JUnit test that fails if an illegal reflective access occurs.
What's the most elegant way to programmatically detect an illegal reflective access in a unit test?
I have considered but rejected the following approaches:
- capturing stdout and stderr in the test (that didn't work)
- starting a separate VM in the test and checking the output (that is pretty involved)