Fix paperweight changes (#1364)

This commit is contained in:
Pierre Maurice Schwang
2021-10-18 21:38:43 +02:00
committed by GitHub
parent 74486fc8c9
commit c7a490fa03
17 changed files with 94 additions and 53 deletions

View File

@ -1,5 +1,7 @@
package com.fastasyncworldedit.core.util;
import sun.misc.Unsafe;
import javax.annotation.Nonnull;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.AccessibleObject;
@ -12,6 +14,18 @@ import java.util.Comparator;
public class ReflectionUtils {
private static Unsafe UNSAFE;
static {
try {
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
UNSAFE = (Unsafe) unsafeField.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static <T> T as(Class<T> t, Object o) {
return t.isInstance(o) ? t.cast(o) : null;
}
@ -112,4 +126,13 @@ public class ReflectionUtils {
}
}
public static void unsafeSet(Object base, long offset, Object value) {
UNSAFE.putObject(base, offset, value);
}
public static void unsafeSet(Field field, Object base, Object value) {
UNSAFE.putObject(base, UNSAFE.objectFieldOffset(field), value);
}
}