From f1e8a1a29a767f163340f96d7393d6d5c90aed0e Mon Sep 17 00:00:00 2001 From: NotMyFault Date: Mon, 18 Oct 2021 22:01:54 +0200 Subject: [PATCH] refactor: Move `Unsafe` to reflection class --- .../PaperweightPlatformAdapter.java | 8 ++--- .../clipboard/DiskOptimizedClipboard.java | 4 +-- .../core/util/ReflectionUtils.java | 9 ++++++ .../core/util/UnsafeUtility.java | 29 ------------------- 4 files changed, 15 insertions(+), 35 deletions(-) delete mode 100644 worldedit-core/src/main/java/com/fastasyncworldedit/core/util/UnsafeUtility.java diff --git a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java index 6e20e604e..38d54e0ef 100644 --- a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java @@ -8,8 +8,8 @@ import com.fastasyncworldedit.core.FaweCache; import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.math.BitArrayUnstretched; import com.fastasyncworldedit.core.util.MathMan; +import com.fastasyncworldedit.core.util.ReflectionUtils; import com.fastasyncworldedit.core.util.TaskManager; -import com.fastasyncworldedit.core.util.UnsafeUtility; import com.mojang.datafixers.util.Either; import com.sk89q.worldedit.bukkit.adapter.Refraction; import com.sk89q.worldedit.math.BlockVector3; @@ -119,7 +119,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { getVisibleChunkIfPresent.setAccessible(true); methodGetVisibleChunk = MethodHandles.lookup().unreflect(getVisibleChunkIfPresent); - Unsafe unsafe = UnsafeUtility.getUNSAFE(); + Unsafe unsafe = ReflectionUtils.getUnsafe(); fieldLock = PalettedContainer.class.getDeclaredField(Refraction.pickName("lock", "m")); fieldLockOffset = unsafe.objectFieldOffset(fieldLock); @@ -160,7 +160,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { ) { long offset = ((long) layer << CHUNKSECTION_SHIFT) + CHUNKSECTION_BASE; if (layer >= 0 && layer < sections.length) { - return UnsafeUtility.getUNSAFE().compareAndSwapObject(sections, offset, expected, value); + return ReflectionUtils.getUnsafe().compareAndSwapObject(sections, offset, expected, value); } return false; } @@ -169,7 +169,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { //todo there has to be a better way to do this. Maybe using a() in DataPaletteBlock which acquires the lock in NMS? try { synchronized (section) { - Unsafe unsafe = UnsafeUtility.getUNSAFE(); + Unsafe unsafe = ReflectionUtils.getUnsafe(); PalettedContainer blocks = section.getStates(); Semaphore currentLock = (Semaphore) unsafe.getObject(blocks, fieldLockOffset); if (currentLock instanceof DelegateSemaphore) { diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/DiskOptimizedClipboard.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/DiskOptimizedClipboard.java index cf35dc011..30f854b3d 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/DiskOptimizedClipboard.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/DiskOptimizedClipboard.java @@ -6,7 +6,7 @@ import com.fastasyncworldedit.core.internal.exception.FaweClipboardVersionMismat import com.fastasyncworldedit.core.jnbt.streamer.IntValueReader; import com.fastasyncworldedit.core.math.IntTriple; import com.fastasyncworldedit.core.util.MainUtil; -import com.fastasyncworldedit.core.util.UnsafeUtility; +import com.fastasyncworldedit.core.util.ReflectionUtils; import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.IntTag; import com.sk89q.jnbt.Tag; @@ -332,7 +332,7 @@ public class DiskOptimizedClipboard extends LinearClipboard implements Closeable if (cb == null || !cb.isDirect()) { return; } - UnsafeUtility.getUNSAFE().invokeCleaner(cb); + ReflectionUtils.getUnsafe().invokeCleaner(cb); } @Override diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/ReflectionUtils.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/ReflectionUtils.java index eb7a7d9be..87fdf1d90 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/ReflectionUtils.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/ReflectionUtils.java @@ -12,6 +12,9 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Comparator; +/** + * This is an internal class not meant to be used outside the FAWE internals. + */ public class ReflectionUtils { private static Unsafe UNSAFE; @@ -134,5 +137,11 @@ public class ReflectionUtils { UNSAFE.putObject(base, UNSAFE.objectFieldOffset(field), value); } + /** + * @return an instance of {@link Unsafe} + */ + public static Unsafe getUnsafe() { + return UNSAFE; + } } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/UnsafeUtility.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/UnsafeUtility.java deleted file mode 100644 index 34750c533..000000000 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/UnsafeUtility.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fastasyncworldedit.core.util; - -import sun.misc.Unsafe; - -import java.lang.reflect.Field; - -/** - * This is an internal class not meant to be used outside the FAWE internals. - * @hidden - */ -public class UnsafeUtility { - - private static final Unsafe UNSAFE; - - static { - try { - Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); - theUnsafe.setAccessible(true); - UNSAFE = (Unsafe) theUnsafe.get(null); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new ExceptionInInitializerError("Cannot access Unsafe"); - } - } - - public static Unsafe getUNSAFE() { - return UNSAFE; - } - -}