0

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)
Robert Metzger
  • 4,452
  • 23
  • 50
  • The accepted answer in the dupe suggests running with `--illegal-access=deny` - outright denying it seems like a far better strategy than hoping your unit tests cover all the illegal access cases. – Andy Turner May 12 '20 at 14:10
  • Thanks a lot for taking a look at my question. You are right that we should run with all our tests on CI with that flag to ensure that there are no hidden cases. – Robert Metzger May 14 '20 at 18:49

0 Answers0