MinecraftForge / ForgeGradle

Minecraft mod development framework used by Forge and FML for the gradle build system
GNU Lesser General Public License v2.1
508 stars 436 forks source link

Lazily construct values for transient fields in Artifact #860

Closed sciwhiz12 closed 2 years ago

sciwhiz12 commented 2 years ago

This PR fixes an issue from https://github.com/MinecraftForge/ForgeGradle/commit/b6ac471841ef54d4f3e668bf71c8f400824a26a2 by moving the construction and assignment of the transient fields in Artifact from the constructor to getters, fixing any method using these values to work even after deserialization.

As background, transient fields are not serialized by Java's serialization mechanisms (which is used by Gradle), which causes an issue as the equals and hashCode methods of Artifact (introduced by the aforementioned commit) relies on one such field. This causes a NullPointerException if something attempts to call these two methods on a freshly deserialized Artifact instance.

The solution here[^1] is to move creating and assigning the values of these fields to their respective getters (in the case of comparableVersion, a new package-private getter), such that their values are created on-demand and cached for future calls.

This PR also cleans up the formatting of the getters in Artifact from the single-line getter format.

[^1]: In a Discord conversation on the Forge project discord server, this was the chosen option; the other alternative solution were to remove the transient modifier from the fields (which would include data in the serialized form that can already be recreated from the already-serialized fields).