MinecraftForge / Artifactural

A Gradle artifact processing and management tool
GNU Lesser General Public License v2.1
6 stars 14 forks source link

Remove JDK reflection hacks in favor of `Unsafe` #4

Closed sciwhiz12 closed 3 years ago

sciwhiz12 commented 3 years ago

This PR fixes Artifactural's compatibility with Java 16 due to reflection into JDK internals, by replacing that reflection with the use of sun.misc.Unsafe.

Currently, Artifactural has a hack to modify final fields of classes by reflecting into Field.modifiers. This has two disadvantages:

Because ForgeGradle 5 is planned to exclusively run on Java 16+, an alternative solution had to be found to this. Originally, I attempted to recreate the Gradle objects and substituting our own custom objects as needed, but this was discarded due to being open to the possibility of changes in Gradle's internals which would require that piece of code being fixed up over and over again to work on both old and newer versions of Gradle.

To avoid the JDK module reflection issue, this PR replaces that reflection with the use of sun.misc.Unsafe (accessed through the net.minecraftforge:unsafe project). To summarize, Unsafe allows Java programs to directly modify the JVM's memory, including being able to overwrite values of fields (even final fields) without triggering the module reflection checks.

This eliminates the issue of the JDK reflection preventing Java 16 compatibility (without user intervention), and also provides the opportunity to remove the separately compiled code for Java 8 and Java 9+.

The following changes are also done in this PR:

LexManos commented 3 years ago

Alright so this has been merged, sci was nice enough to do some testing using Unsafe on J16+ that makes it seem like it'll function even with their attempt to lock down reflection. At least the tests work for me. The next thing we need to do is bump this to Gradle 7+ so we get native J16 support. We don't have to worry about this change for G6.9, as that doesn't run on J16 it just can compile for it.

FG5 is targeting J16+ and G7 which is where we will use the 3.0 builds of this.