From 297cb01f220a617dd08096467978b2fccbc27695 Mon Sep 17 00:00:00 2001 From: nea Date: Wed, 1 Nov 2023 18:50:11 +0100 Subject: Add documentation --- .../launch/util/ClassLoaderManipulations.java | 10 ++++- .../launch/util/ObjectHolderRefCompanion.java | 44 ++++++++++++++++++++++ .../nea/modernjava/launch/util/PropertyNames.java | 16 ++++++++ .../modernjava/launch/util/ReflectionUtils.java | 33 ---------------- .../nea/modernjava/launch/util/TextIoUtils.java | 22 +++++++++++ .../launch/util/WellKnownBlackboard.java | 27 +++++++++++++ 6 files changed, 118 insertions(+), 34 deletions(-) create mode 100644 src/main/java/moe/nea/modernjava/launch/util/ObjectHolderRefCompanion.java create mode 100644 src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java delete mode 100644 src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java create mode 100644 src/main/java/moe/nea/modernjava/launch/util/TextIoUtils.java create mode 100644 src/main/java/moe/nea/modernjava/launch/util/WellKnownBlackboard.java (limited to 'src/main/java/moe/nea/modernjava/launch/util') diff --git a/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java b/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java index 09f2368..6cc21e9 100644 --- a/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java +++ b/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java @@ -1,6 +1,7 @@ package moe.nea.modernjava.launch.util; import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; import java.io.File; import java.lang.reflect.Method; @@ -8,7 +9,10 @@ import java.net.MalformedURLException; import java.net.URL; public class ClassLoaderManipulations { - + /** + * Adds a File to the parent class loader of the launch class loader. Necessary if you want to/have to use + * {@link LaunchClassLoader#addClassLoaderExclusion(String)}. + */ public static void addToParentClassLoader(File file) { try { addToParentClassLoader(file.toURI().toURL()); @@ -17,6 +21,10 @@ public class ClassLoaderManipulations { } } + /** + * Adds a URL to the parent class loader of the launch class loader. Necessary if you want to/have to use + * {@link LaunchClassLoader#addClassLoaderExclusion(String)}. + */ public static void addToParentClassLoader(URL file) { try { Launch.classLoader.addURL(file); 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); + } + } +} diff --git a/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java b/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java new file mode 100644 index 0000000..8540dde --- /dev/null +++ b/src/main/java/moe/nea/modernjava/launch/util/PropertyNames.java @@ -0,0 +1,16 @@ +package moe.nea.modernjava.launch.util; + +public class PropertyNames { + /** + * Property set to indicate whether the java process was launched on the new java version. + */ + public static final String HAS_RELAUNCHED = "modernjava.hasrelaunched"; + /** + * Classpath to load after reloading. + */ + public static final String RELAUNCH_CLASSPATH = "modernjava.relaunchclasspath"; + /** + * Starts a debugger on the given port if present. + */ + public static final String DEBUG_PORT = "modernjava.debugport"; +} diff --git a/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java b/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java deleted file mode 100644 index abc2364..0000000 --- a/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java +++ /dev/null @@ -1,33 +0,0 @@ -package moe.nea.modernjava.launch.util; - -import sun.misc.Unsafe; - -import java.lang.reflect.Field; - -public class ReflectionUtils { - 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(); - } - } - - public static void makeFieldWritable(Field f) { - String s = "Doing nothing. We will use unsafe to set the value instead, if possible"; - } - - 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); - } - } -} diff --git a/src/main/java/moe/nea/modernjava/launch/util/TextIoUtils.java b/src/main/java/moe/nea/modernjava/launch/util/TextIoUtils.java new file mode 100644 index 0000000..b7f82c0 --- /dev/null +++ b/src/main/java/moe/nea/modernjava/launch/util/TextIoUtils.java @@ -0,0 +1,22 @@ +package moe.nea.modernjava.launch.util; + +import java.io.FileDescriptor; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +public class TextIoUtils { + /** + * Flush out the standard IO, without closing it. Works even after {@link System#setOut(PrintStream)} or similar has been called + */ + public static void flushStdIO() { + try { + // These streams should not be closed. We just want to flush them out + //noinspection resource + new FileOutputStream(FileDescriptor.out).flush(); + //noinspection resource + new FileOutputStream(FileDescriptor.err).flush(); + } catch (IOException ignored) { + } + } +} diff --git a/src/main/java/moe/nea/modernjava/launch/util/WellKnownBlackboard.java b/src/main/java/moe/nea/modernjava/launch/util/WellKnownBlackboard.java new file mode 100644 index 0000000..967f531 --- /dev/null +++ b/src/main/java/moe/nea/modernjava/launch/util/WellKnownBlackboard.java @@ -0,0 +1,27 @@ +package moe.nea.modernjava.launch.util; + +import net.minecraft.launchwrapper.Launch; + +import java.util.List; +import java.util.Map; + +/** + * Contains references to the {@link Launch#blackboard black board} in one central spot to avoid misspelling. + */ +@SuppressWarnings("unchecked") +public class WellKnownBlackboard { + /** + * A list of tweaker class names yet to be executed. This does not include tweaker class names present in the current + * round of tweaking. + */ + public static List tweakerNames() { + return (List) Launch.blackboard.get("TweakClasses"); + } + + /** + * A map of arguments in the form of --prefixedKey to value. + */ + public static Map launchArgs() { + return (Map) Launch.blackboard.get("launchArgs"); + } +} -- cgit