Making reflection fully work on Java 9 and later

Jim Jerald Burton
2 min readNov 24, 2021

--

Due to the strong encapsulation introduced starting from jdk 16 some non-public classes, methods and fields of the java.* package are no longer available via reflection, so how to get the legacy code to work on these new versions of JDK?

In this situation, the member handlers of Burningwave Core library comes to our aid, by giving us the ability to retrieve any field, method, or constructor of any class. These components are able to accomplish their task thanks to a special driver which requires no parameters to be passed to the jvm executable.

The members handlers use to cache all members for faster access, and now let’s now see how to use these components.
To start we need to add the following dependency to our pom.xml:

For fields handling we are going to use Fields component:

For methods handling we are going to use Methods component and we can accomplish the task in several ways:

  • by direct method invocation:
  • by direct method handle invocation:
  • by retrieving and invoking a method:
  • by retrieving and invoking a method handle:

Another way to use reflection is to export all modules to all modules at runtime through Modules component: in this case it will be possible to use reflection in the standard style without resorting to the reflection components of Burnignwave Core.

From here you can download/clone the tutorial shared on GitHub.

--

--