aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-11-01 18:50:11 +0100
committernea <nea@nea.moe>2023-11-01 18:50:11 +0100
commit297cb01f220a617dd08096467978b2fccbc27695 (patch)
treed467f358313ce3648063da41b66361a840fa7864 /src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java
parentd5253dc5c3ae7b2cc1fcb96780e43b929a449eb4 (diff)
downloadModernJavaLauncher-297cb01f220a617dd08096467978b2fccbc27695.tar.gz
ModernJavaLauncher-297cb01f220a617dd08096467978b2fccbc27695.tar.bz2
ModernJavaLauncher-297cb01f220a617dd08096467978b2fccbc27695.zip
Add documentation
Diffstat (limited to 'src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java')
-rw-r--r--src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java b/src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java
new file mode 100644
index 0000000..15f9450
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java
@@ -0,0 +1,44 @@
+package moe.nea.modernjava.launch.util;
+
+import moe.nea.modernjava.launch.transform.TransObjectHolderRef;
+import sun.misc.Unsafe;
+
+import java.lang.reflect.Field;
+
+/**
+ * A companion to my transformations from {@link TransObjectHolderRef} to avoid
+ * having to write all of this out in bytecode.
+ */
+public class ObjectHolderRefCompanion {
+ private static Unsafe unsafe;
+
+ static {
+ try {
+ final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
+ unsafeField.setAccessible(true);
+ unsafe = (Unsafe) unsafeField.get(null);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * A noop to have a jump target for the reflection factories.
+ */
+ public static void makeFieldWritable(Field f) {
+ String s = "Doing nothing. We will use unsafe to set the value instead, if possible";
+ }
+
+ /**
+ * Write a value to a static final field.
+ */
+ public static void doFieldWrite(Field field, Object object) throws IllegalAccessException {
+ if (unsafe == null) {
+ field.set(null, object);
+ } else {
+ Object o = unsafe.staticFieldBase(field);
+ long l = unsafe.staticFieldOffset(field);
+ unsafe.putObject(o, l, object);
+ }
+ }
+}