aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLexManos <LexManos@gmail.com>2021-05-26 10:46:53 -0700
committerGitHub <noreply@github.com>2021-05-26 10:46:53 -0700
commit578f33b206c7325762a956a1f3e3171d5c6d9052 (patch)
tree22e8db707b69b433463446b0709f93a6df65c27f /src
parent12aeb6f4c7c09e749204707475a7f71b16c14133 (diff)
parentc3948938178810000353830671e474abb9d32a2c (diff)
downloadArtifactural-578f33b206c7325762a956a1f3e3171d5c6d9052.tar.gz
Artifactural-578f33b206c7325762a956a1f3e3171d5c6d9052.tar.bz2
Artifactural-578f33b206c7325762a956a1f3e3171d5c6d9052.zip
Remove JDK reflection hacks in favor of `Unsafe`
Merge pull request #4 from sciwhiz12/theUnsafe
Diffstat (limited to 'src')
-rw-r--r--src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java55
-rw-r--r--src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java12
-rw-r--r--src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java57
3 files changed, 8 insertions, 116 deletions
diff --git a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java b/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java
deleted file mode 100644
index fb68144..0000000
--- a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Artifactural
- * Copyright (c) 2018-2021.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation version 2.1
- * of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package net.minecraftforge.artifactural.gradle;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-
-public class ModifierAccess {
- private static Field MODIFIER_ACCESS = null;
- private static boolean accessAttempted = false;
-
- public static synchronized boolean definalize(Field target) {
- if ((target.getModifiers() & Modifier.FINAL) == 0) {
- return true;
- }
-
- if (MODIFIER_ACCESS == null && !accessAttempted) {
- try {
- final Field modifiers = Field.class.getDeclaredField("modifiers");
- modifiers.setAccessible(true);
- MODIFIER_ACCESS = modifiers;
- } catch (NoSuchFieldException e) {
- throw new RuntimeException("Could not access Field.modifiers to definalize reflection object. Use Java 8, current version: " + System.getProperty("java.version"), e);
- }
- accessAttempted = true;
- }
- if (MODIFIER_ACCESS != null) {
- try {
- MODIFIER_ACCESS.setInt(target, target.getModifiers() & ~Modifier.FINAL);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- throw new RuntimeException("Could not definalize field " + target.getDeclaringClass().getName() + "." + target.getName(), e);
- }
- return true;
- }
- return false;
- }
-
-}
diff --git a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java b/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java
index 4e18885..1c8cdac 100644
--- a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java
+++ b/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java
@@ -19,8 +19,11 @@
package net.minecraftforge.artifactural.gradle;
+import net.minecraftforge.fml.unsafe.UnsafeHacks;
+
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.function.UnaryOperator;
public class ReflectionUtils {
@@ -39,7 +42,11 @@ public class ReflectionUtils {
T oldV = (T)f.get(target);
T newV = operator.apply(oldV);
- f.set(target, newV);
+ if (Modifier.isFinal(f.getModifiers())) {
+ UnsafeHacks.setField(f, target, newV);
+ } else {
+ f.set(target, newV);
+ }
if (f.get(target) != newV) {
throw new IllegalStateException("Failed to set new value on " + f.getDeclaringClass().getName() + "." + f.getName());
@@ -68,9 +75,6 @@ public class ReflectionUtils {
for (Field f : clazz.getDeclaredFields()) {
if (f.getName().equals(name)) {
f.setAccessible(true);
- if (!ModifierAccess.definalize(f)) {
- System.out.println("Could not definalize field " + f.getDeclaringClass().getName() + "." + f.getName() + " Exception ate, lets see if it works");
- }
return f;
}
}
diff --git a/src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java b/src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java
deleted file mode 100644
index cf6f65f..0000000
--- a/src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Artifactural
- * Copyright (c) 2018.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation version 2.1
- * of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package net.minecraftforge.artifactural.gradle;
-
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandles.Lookup;
-import java.lang.invoke.VarHandle;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-
-public class ModifierAccess {
- private static VarHandle MODIFIER_ACCESS = null;
- private static boolean accessAttempted = false;
-
- public static synchronized boolean definalize(Field target) {
- if ((target.getModifiers() & Modifier.FINAL) == 0) {
- return true;
- }
-
- if (MODIFIER_ACCESS == null && !accessAttempted) {
- try {
- Lookup lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
- MODIFIER_ACCESS = lookup.findVarHandle(Field.class, "modifiers", int.class);
- } catch (NoSuchFieldException | IllegalAccessException e) {
- throw new RuntimeException("Could not access Field.modifiers to definalize reflection object. Use Java 8, current version: " + System.getProperty("java.version"), e);
- }
- accessAttempted = true;
- }
- if (MODIFIER_ACCESS != null) {
- try {
- MODIFIER_ACCESS.set(target, target.getModifiers() & ~Modifier.FINAL);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException("Could not definalize field " + target.getDeclaringClass().getName() + "." + target.getName(), e);
- }
- return true;
- }
- return false;
- }
-
-}