From 0d4300980df1281e0fc060071461188d74a9f63f Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 9 Oct 2017 16:36:48 +0100 Subject: [PATCH] More internal service refactoring --- .../mojang/MixinServiceLaunchWrapper.java | 38 +++++++++++--- .../launch/platform/MixinPlatformManager.java | 4 +- .../asm/mixin/MixinEnvironment.java | 8 ++- .../asm/mixin/transformer/MixinConfig.java | 4 +- .../mixin/transformer/MixinTransformer.java | 6 +-- .../asm/service/IClassProvider.java | 50 +++++++++++++++++++ .../asm/service/IMixinService.java | 26 +++++----- 7 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/service/IClassProvider.java diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java index 359123f8..e9d7d103 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java @@ -41,6 +41,8 @@ import org.spongepowered.asm.lib.ClassReader; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; +import org.spongepowered.asm.mixin.throwables.MixinException; +import org.spongepowered.asm.service.IClassProvider; import org.spongepowered.asm.service.ILegacyClassTransformer; import org.spongepowered.asm.service.IMixinService; import org.spongepowered.asm.service.ITransformer; @@ -58,7 +60,7 @@ import net.minecraft.launchwrapper.Launch; /** * Mixin service for launchwrapper */ -public class MixinServiceLaunchWrapper implements IMixinService { +public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider { // Blackboard keys public static final String BLACKBOARD_KEY_TWEAKCLASSES = "TweakClasses"; @@ -157,20 +159,29 @@ public class MixinServiceLaunchWrapper implements IMixinService { } /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#getClassLoader() + * @see org.spongepowered.asm.service.IMixinService#getClassProvider() */ @Override - public ClassLoader getClassLoader() { - return Launch.classLoader; + public IClassProvider getClassProvider() { + return this; } /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService - * #getApplicationClassLoader() + * @see org.spongepowered.asm.service.IClassProvider#findClass( + * java.lang.String, boolean) */ @Override - public ClassLoader getApplicationClassLoader() { - return Launch.class.getClassLoader(); + public Class findClass(String name, boolean initialize) throws ClassNotFoundException { + return Class.forName(name, initialize, Launch.classLoader); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IClassProvider#findAgentClass( + * java.lang.String, boolean) + */ + @Override + public Class findAgentClass(String name, boolean initialize) throws ClassNotFoundException { + return Class.forName(name, initialize, Launch.class.getClassLoader()); } /* (non-Javadoc) @@ -181,6 +192,17 @@ public class MixinServiceLaunchWrapper implements IMixinService { Launch.classLoader.registerTransformer(MixinServiceLaunchWrapper.TRANSFORMER_PROXY_CLASS); } + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#checkEnv( + * java.lang.Object) + */ + @Override + public void checkEnv(Object bootSource) { + if (bootSource.getClass().getClassLoader() != Launch.class.getClassLoader()) { + throw new MixinException("Attempted to init the mixin environment in the wrong classloader"); + } + } + /* (non-Javadoc) * @see org.spongepowered.asm.service.IMixinService#getResourceAsStream( * java.lang.String) diff --git a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java index 133428cd..839f57b4 100644 --- a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java @@ -42,6 +42,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.service.MixinService; +import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.mixin.Mixins; //import com.google.common.collect.ImmutableList; @@ -52,7 +53,6 @@ import org.spongepowered.asm.mixin.Mixins; */ public class MixinPlatformManager { - private static final String MFATT_TWEAKER = "TweakClass"; private static final String DEFAULT_MAIN_CLASS = "net.minecraft.client.main.Main"; private static final String MIXIN_TWEAKER_CLASS = "org.spongepowered.asm.launch.MixinTweaker"; @@ -224,7 +224,7 @@ public class MixinPlatformManager { continue; } MainAttributes attributes = MainAttributes.of(uri); - String tweaker = attributes.get(MixinPlatformManager.MFATT_TWEAKER); + String tweaker = attributes.get(Constants.ManifestAttributes.TWEAKER); if (MixinPlatformManager.MIXIN_TWEAKER_CLASS.equals(tweaker)) { MixinPlatformManager.logger.debug("{} contains a mixin tweaker, adding agents", uri); this.addContainer(uri); diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index b2edd010..1df74df9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -881,10 +881,8 @@ public final class MixinEnvironment implements ITokenProvider { throw new MixinException("Environment conflict, mismatched versions or you didn't call MixinBootstrap.init()"); } - // Also sanity check - if (this.getClass().getClassLoader() != this.service.getApplicationClassLoader()) { - throw new MixinException("Attempted to init the mixin environment in the wrong classloader"); - } + // More sanity check + this.service.checkEnv(this); this.options = new boolean[Option.values().length]; for (Option option : Option.values()) { @@ -1002,7 +1000,7 @@ public final class MixinEnvironment implements ITokenProvider { try { @SuppressWarnings("unchecked") Class providerClass = - (Class)Class.forName(providerName, true, this.service.getClassLoader()); + (Class)this.service.getClassProvider().findClass(providerName, true); IEnvironmentTokenProvider provider = providerClass.newInstance(); this.registerTokenProvider(provider); } catch (Throwable th) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index 0bd01359..b29b6d6d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -390,7 +390,7 @@ final class MixinConfig implements Comparable, IMixinConfig { for (String injectionPoint : this.injectorOptions.injectionPoints) { try { - Class injectionPointClass = Class.forName(injectionPoint, true, this.service.getClassLoader()); + Class injectionPointClass = this.service.getClassProvider().findClass(injectionPoint, true); if (InjectionPoint.class.isAssignableFrom(injectionPointClass)) { InjectionPoint.register((Class)injectionPointClass); } else { @@ -438,7 +438,7 @@ final class MixinConfig implements Comparable, IMixinConfig { void onSelect() { if (this.pluginClassName != null) { try { - Class pluginClass = Class.forName(this.pluginClassName, true, this.service.getClassLoader()); + Class pluginClass = this.service.getClassProvider().findClass(this.pluginClassName, true); this.plugin = (IMixinConfigPlugin)pluginClass.newInstance(); if (this.plugin != null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index c57935b6..1a408442 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -282,7 +282,7 @@ public class MixinTransformer extends TreeTransformer { for (String target : unhandled) { try { auditLogger.info("Force-loading class {}", target); - Class.forName(target, true, this.service.getClassLoader()); + this.service.getClassProvider().findClass(target, true); } catch (ClassNotFoundException ex) { auditLogger.error("Could not force-load " + target, ex); } @@ -360,7 +360,7 @@ public class MixinTransformer extends TreeTransformer { printer.add(); try { - Class agent = Class.forName(MixinTransformer.METRONOME_AGENT_CLASS, false, this.service.getApplicationClassLoader()); + Class agent = this.service.getClassProvider().findAgentClass(MixinTransformer.METRONOME_AGENT_CLASS, false); Method mdGetTimes = agent.getDeclaredMethod("getTimes"); @SuppressWarnings("unchecked") @@ -786,7 +786,7 @@ public class MixinTransformer extends TreeTransformer { for (String handlerClassName : Mixins.getErrorHandlerClasses()) { try { MixinTransformer.logger.info("Instancing error handler class {}", handlerClassName); - Class handlerClass = Class.forName(handlerClassName, true, this.service.getClassLoader()); + Class handlerClass = this.service.getClassProvider().findClass(handlerClassName, true); IMixinErrorHandler handler = (IMixinErrorHandler)handlerClass.newInstance(); if (handler != null) { handlers.add(handler); diff --git a/src/main/java/org/spongepowered/asm/service/IClassProvider.java b/src/main/java/org/spongepowered/asm/service/IClassProvider.java new file mode 100644 index 00000000..f02380e7 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/service/IClassProvider.java @@ -0,0 +1,50 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.service; + +/** + * Interface for marshal object which can retrieve classes from the environment + */ +public interface IClassProvider { + + /** + * Marshal a call to Class.forName for a regular class + * + * @param name class name + * @param initialize init flag + * @return Klass + */ + public abstract Class findClass(String name, boolean initialize) throws ClassNotFoundException; + + /** + * Marshal a call to Class.forName for an agent class + * + * @param name agent class name + * @param initialize init flag + * @return Klass + */ + public abstract Class findAgentClass(String name, boolean initialize) throws ClassNotFoundException; + +} diff --git a/src/main/java/org/spongepowered/asm/service/IMixinService.java b/src/main/java/org/spongepowered/asm/service/IMixinService.java index f9c6448d..b567d10c 100644 --- a/src/main/java/org/spongepowered/asm/service/IMixinService.java +++ b/src/main/java/org/spongepowered/asm/service/IMixinService.java @@ -73,6 +73,14 @@ public interface IMixinService { */ public abstract void beginPhase(); + /** + * Check whether the supplied object is a valid boot source for mixin + * environment + * + * @param bootSource boot source + */ + public abstract void checkEnv(Object bootSource); + /** * Get the transformer re-entrance lock for this service, the transformer * uses this lock to track transformer re-entrance when co-operative load @@ -80,21 +88,15 @@ public interface IMixinService { */ public abstract ReEntranceLock getReEntranceLock(); + /** + * Return the class provider for this service + */ + public abstract IClassProvider getClassProvider(); + /** * Get additional platform agents for this service */ public abstract Collection getPlatformAgents(); - - /** - * Get the main classloader for this service - */ - public abstract ClassLoader getClassLoader(); - - /** - * Get the application classloader for this service (usually the system - * classloader) - */ - public abstract ClassLoader getApplicationClassLoader(); /** * Get a resource as a stream from the appropriate classloader, this is @@ -188,5 +190,5 @@ public interface IMixinService { * Get the detected side name for this environment */ public abstract String getSideName(); - + }