From 7a05b84774e8d2b966a045eb51c502253f0dfa2c Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Wed, 12 Jun 2024 03:36:22 +0200 Subject: [PATCH] Make TransformingClassLoader usable in FML tests. --- .../mods/modlauncher/LaunchPluginHandler.java | 16 +++++++++++----- .../TransformationServiceDecorator.java | 4 +++- .../modlauncher/TransformingClassLoader.java | 13 ++++++++++--- .../modlauncher/util/ServiceLoaderUtils.java | 5 +++++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/cpw/mods/modlauncher/LaunchPluginHandler.java b/src/main/java/cpw/mods/modlauncher/LaunchPluginHandler.java index c406977..f395314 100644 --- a/src/main/java/cpw/mods/modlauncher/LaunchPluginHandler.java +++ b/src/main/java/cpw/mods/modlauncher/LaunchPluginHandler.java @@ -26,6 +26,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.VisibleForTesting; import org.objectweb.asm.*; import org.objectweb.asm.tree.*; import cpw.mods.modlauncher.serviceapi.ILaunchPluginService; @@ -33,6 +34,7 @@ import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import static cpw.mods.modlauncher.LogMarkers.*; @@ -41,10 +43,14 @@ public class LaunchPluginHandler { private final Map plugins; public LaunchPluginHandler(final ModuleLayerHandler layerHandler) { - this.plugins = ServiceLoaderUtils.streamServiceLoader(()->ServiceLoader.load(layerHandler.getLayer(IModuleLayerManager.Layer.BOOT).orElseThrow(), ILaunchPluginService.class), - e->LOGGER.fatal(MODLAUNCHER, "Encountered serious error loading launch plugin service. Things will not work well", e)) - .collect(Collectors.toMap(ILaunchPluginService::name, Function.identity())); - final var modlist = plugins.entrySet().stream().map(e->Map.of( + this(ServiceLoaderUtils.streamServiceLoader(()->ServiceLoader.load(layerHandler.getLayer(IModuleLayerManager.Layer.BOOT).orElseThrow(), ILaunchPluginService.class), + e->LOGGER.fatal(MODLAUNCHER, "Encountered serious error loading launch plugin service. Things will not work well", e))); + } + + @VisibleForTesting + public LaunchPluginHandler(Stream plugins) { + this.plugins = plugins.collect(Collectors.toMap(ILaunchPluginService::name, Function.identity())); + final var modlist = this.plugins.entrySet().stream().map(e->Map.of( "name", e.getKey(), "type", "PLUGINSERVICE", "file", ServiceLoaderUtils.fileNameFor(e.getValue().getClass()))) @@ -55,7 +61,7 @@ public LaunchPluginHandler(final ModuleLayerHandler layerHandler) { throw new RuntimeException("The MODLIST isn't set, huh?"); }); } - LOGGER.debug(MODLAUNCHER,"Found launch plugins: [{}]", ()-> String.join(",", plugins.keySet())); + LOGGER.debug(MODLAUNCHER,"Found launch plugins: [{}]", ()-> String.join(",", this.plugins.keySet())); } public Optional get(final String name) { diff --git a/src/main/java/cpw/mods/modlauncher/TransformationServiceDecorator.java b/src/main/java/cpw/mods/modlauncher/TransformationServiceDecorator.java index a4adb14..404a51e 100644 --- a/src/main/java/cpw/mods/modlauncher/TransformationServiceDecorator.java +++ b/src/main/java/cpw/mods/modlauncher/TransformationServiceDecorator.java @@ -21,6 +21,7 @@ import cpw.mods.modlauncher.api.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.VisibleForTesting; import java.util.*; import java.util.stream.*; @@ -35,7 +36,8 @@ public class TransformationServiceDecorator { private final ITransformationService service; private boolean isValid; - TransformationServiceDecorator(ITransformationService service) { + @VisibleForTesting + public TransformationServiceDecorator(ITransformationService service) { this.service = service; } diff --git a/src/main/java/cpw/mods/modlauncher/TransformingClassLoader.java b/src/main/java/cpw/mods/modlauncher/TransformingClassLoader.java index f0156f4..89343a5 100644 --- a/src/main/java/cpw/mods/modlauncher/TransformingClassLoader.java +++ b/src/main/java/cpw/mods/modlauncher/TransformingClassLoader.java @@ -20,6 +20,7 @@ import cpw.mods.cl.ModuleClassLoader; import cpw.mods.modlauncher.api.*; +import org.jetbrains.annotations.VisibleForTesting; import java.lang.module.Configuration; import java.util.*; @@ -33,13 +34,19 @@ public class TransformingClassLoader extends ModuleClassLoader { } private final ClassTransformer classTransformer; - public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, ModuleLayerHandler moduleLayerHandler) { + public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, IModuleLayerManager moduleLayerHandler) { super("TRANSFORMER", moduleLayerHandler.getLayer(IModuleLayerManager.Layer.GAME).orElseThrow().configuration(), List.of(moduleLayerHandler.getLayer(IModuleLayerManager.Layer.SERVICE).orElseThrow())); this.classTransformer = new ClassTransformer(transformStore, pluginHandler, this); } - TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List parentLayers) { - super("TRANSFORMER", configuration, parentLayers); + @VisibleForTesting + public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List parentLayers) { + this(transformStore, pluginHandler, environment, configuration, parentLayers, null); + } + + @VisibleForTesting + public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List parentLayers, ClassLoader parentClassLoader) { + super("TRANSFORMER", configuration, parentLayers, parentClassLoader); TransformerAuditTrail tat = new TransformerAuditTrail(); environment.computePropertyIfAbsent(IEnvironment.Keys.AUDITTRAIL.get(), v->tat); this.classTransformer = new ClassTransformer(transformStore, pluginHandler, this, tat); diff --git a/src/main/java/cpw/mods/modlauncher/util/ServiceLoaderUtils.java b/src/main/java/cpw/mods/modlauncher/util/ServiceLoaderUtils.java index 6b94322..0d01422 100644 --- a/src/main/java/cpw/mods/modlauncher/util/ServiceLoaderUtils.java +++ b/src/main/java/cpw/mods/modlauncher/util/ServiceLoaderUtils.java @@ -45,6 +45,11 @@ public static Stream streamWithErrorHandling(ServiceLoader sl, Consume } public static String fileNameFor(Class clazz) { + // Used in test scenarios where services might come from normal CP + if (clazz.getModule().getLayer() == null) { + return clazz.getProtectionDomain().getCodeSource().getLocation().getFile(); + } + return clazz.getModule().getLayer().configuration() .findModule(clazz.getModule().getName()) .flatMap(rm->rm.reference().location())