diff --git a/src/agent/java/org/spongepowered/tools/agent/MixinAgent.java b/src/agent/java/org/spongepowered/tools/agent/MixinAgent.java index f20ebc7f..736aafb8 100644 --- a/src/agent/java/org/spongepowered/tools/agent/MixinAgent.java +++ b/src/agent/java/org/spongepowered/tools/agent/MixinAgent.java @@ -105,7 +105,7 @@ public class MixinAgent implements IHotSwap { String targetName = target.replace('/', '.'); MixinAgent.logger.debug("Re-transforming target class {}", target); try { - Class targetClass = service.findClass(targetName); + Class targetClass = service.getClassProvider().findClass(targetName); byte[] targetBytecode = MixinAgent.classLoader.getOriginalTargetBytecode(targetName); if (targetBytecode == null) { MixinAgent.logger.error("Target class {} bytecode is not registered", targetName); 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 e9d7d103..ccf6e379 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java @@ -42,6 +42,7 @@ 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.IClassBytecodeProvider; import org.spongepowered.asm.service.IClassProvider; import org.spongepowered.asm.service.ILegacyClassTransformer; import org.spongepowered.asm.service.IMixinService; @@ -60,7 +61,7 @@ import net.minecraft.launchwrapper.Launch; /** * Mixin service for launchwrapper */ -public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider { +public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider, IClassBytecodeProvider { // Blackboard keys public static final String BLACKBOARD_KEY_TWEAKCLASSES = "TweakClasses"; @@ -77,8 +78,15 @@ public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider */ private static final Logger logger = LogManager.getLogger("mixin"); + /** + * Utility for reflecting into Launch ClassLoader + */ private final LaunchClassLoaderUtil classLoaderUtil = new LaunchClassLoaderUtil(Launch.classLoader); + /** + * Transformer re-entrance lock, shared between the mixin transformer and + * the metadata service + */ private final ReEntranceLock lock = new ReEntranceLock(1); /** @@ -166,6 +174,23 @@ public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider return this; } + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getBytecodeProvider() + */ + @Override + public IClassBytecodeProvider getBytecodeProvider() { + return this; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IClassProvider#findClass( + * java.lang.String) + */ + @Override + public Class findClass(String name) throws ClassNotFoundException { + return Launch.classLoader.findClass(name); + } + /* (non-Javadoc) * @see org.spongepowered.asm.service.IClassProvider#findClass( * java.lang.String, boolean) @@ -212,15 +237,6 @@ public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider return Launch.classLoader.getResourceAsStream(name); } - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#findClass( - * java.lang.String) - */ - @Override - public Class findClass(String name) throws ClassNotFoundException { - return Launch.classLoader.findClass(name); - } - /* (non-Javadoc) * @see org.spongepowered.asm.service.IMixinService#registerInvalidClass( * java.lang.String) @@ -240,16 +256,7 @@ public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider } /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#isClassExcluded( - * java.lang.String, java.lang.String) - */ - @Override - public boolean isClassExcluded(String name, String transformedName) { - return this.classLoaderUtil.isClassExcluded(name, transformedName); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#getClassPath() + * @see org.spongepowered.asm.service.IClassProvider#getClassPath() */ @Override public URL[] getClassPath() { @@ -348,7 +355,7 @@ public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider * except the excluded transformers */ private byte[] applyTransformers(String name, String transformedName, byte[] basicClass, Profiler profiler) { - if (this.isClassExcluded(name, transformedName)) { + if (this.classLoaderUtil.isClassExcluded(name, transformedName)) { return basicClass; } @@ -428,8 +435,8 @@ public class MixinServiceLaunchWrapper implements IMixinService, IClassProvider */ @Override public final String getSideName() { - // Using this method first prevents us from accidentally loading FML classes - // too early when using the tweaker in dev + // Using this method first prevents us from accidentally loading FML + // classes too early when using the tweaker in dev for (ITweaker tweaker : GlobalProperties.>get(MixinServiceLaunchWrapper.BLACKBOARD_KEY_TWEAKS)) { if (tweaker.getClass().getName().endsWith(".common.launcher.FMLServerTweaker")) { return "SERVER"; 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 839f57b4..6a96bb1d 100644 --- a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java @@ -212,7 +212,7 @@ public class MixinPlatformManager { * mixin tweaker in their manifest) and add agents for them */ private void scanClasspath() { - URL[] sources = MixinService.getService().getClassPath(); + URL[] sources = MixinService.getService().getClassProvider().getClassPath(); for (URL url : sources) { try { URI uri = url.toURI(); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index b5c27232..342360b5 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -1673,7 +1673,7 @@ public final class ClassInfo { ClassInfo info = ClassInfo.cache.get(className); if (info == null) { try { - ClassNode classNode = MixinService.getService().getClassNode(className); + ClassNode classNode = MixinService.getService().getBytecodeProvider().getClassNode(className); info = new ClassInfo(classNode); } catch (Exception ex) { ClassInfo.logger.catching(Level.TRACE, ex); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InnerClassGenerator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InnerClassGenerator.java index 34b0dc4d..8f0e1d74 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/InnerClassGenerator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InnerClassGenerator.java @@ -120,7 +120,7 @@ final class InnerClassGenerator implements IClassGenerator { } byte[] getClassBytes() throws ClassNotFoundException, IOException { - return MixinService.getService().getClassBytes(this.originalName, true); + return MixinService.getService().getBytecodeProvider().getClassBytes(this.originalName, true); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index f6287efb..c189dae1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -1158,7 +1158,7 @@ class MixinInfo implements Comparable, IMixinInfo { byte[] mixinBytes = null; try { - mixinBytes = this.service.getClassBytes(mixinClassName, runTransformers); + mixinBytes = this.service.getBytecodeProvider().getClassBytes(mixinClassName, runTransformers); } catch (ClassNotFoundException ex) { throw new ClassNotFoundException(String.format("The specified mixin '%s' was not found", mixinClassName)); } catch (IOException ex) { diff --git a/src/main/java/org/spongepowered/asm/service/IClassBytecodeProvider.java b/src/main/java/org/spongepowered/asm/service/IClassBytecodeProvider.java new file mode 100644 index 00000000..b8144338 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/service/IClassBytecodeProvider.java @@ -0,0 +1,70 @@ +/* + * 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; + +import java.io.IOException; + +import org.spongepowered.asm.lib.tree.ClassNode; + +/** + * Interface for object which can provide class bytecode + */ +public interface IClassBytecodeProvider { + + /** + * Retrieve class bytes using available classloaders, does not transform the + * class + * + * @param name class name + * @param transformedName transformed class name + * @return class bytes or null if not found + * @throws IOException propagated + */ + public abstract byte[] getClassBytes(String name, String transformedName) throws IOException; + + /** + * Retrieve transformed class bytes by using available classloaders and + * running transformer delegation chain on the result if the runTransformers + * option is enabled + * + * @param name full class name + * @param runTransformers true to run transformers on the loaded bytecode + * @return transformed bytes + * @throws ClassNotFoundException if class not found + * @throws IOException propagated + */ + public abstract byte[] getClassBytes(String name, boolean runTransformers) throws ClassNotFoundException, IOException; + + /** + * Retrieve transformed class as an ASM tree + * + * @param name full class name + * @return tree + * @throws ClassNotFoundException if class not found + * @throws IOException propagated + */ + public abstract ClassNode getClassNode(String name) throws ClassNotFoundException, IOException; + +} diff --git a/src/main/java/org/spongepowered/asm/service/IClassProvider.java b/src/main/java/org/spongepowered/asm/service/IClassProvider.java index f02380e7..7cd1b8d3 100644 --- a/src/main/java/org/spongepowered/asm/service/IClassProvider.java +++ b/src/main/java/org/spongepowered/asm/service/IClassProvider.java @@ -24,10 +24,26 @@ */ package org.spongepowered.asm.service; +import java.net.URL; + /** * Interface for marshal object which can retrieve classes from the environment */ public interface IClassProvider { + + /** + * Get the current classpath from the service classloader + */ + public abstract URL[] getClassPath(); + + /** + * Find a class in the service classloader + * + * @param name class name + * @return resultant class + * @throws ClassNotFoundException if the class was not found + */ + public abstract Class findClass(final String name) throws ClassNotFoundException; /** * Marshal a call to Class.forName for a regular class diff --git a/src/main/java/org/spongepowered/asm/service/IMixinService.java b/src/main/java/org/spongepowered/asm/service/IMixinService.java index b567d10c..24c9be09 100644 --- a/src/main/java/org/spongepowered/asm/service/IMixinService.java +++ b/src/main/java/org/spongepowered/asm/service/IMixinService.java @@ -24,12 +24,9 @@ */ package org.spongepowered.asm.service; -import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.util.Collection; -import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.util.ReEntranceLock; @@ -93,6 +90,11 @@ public interface IMixinService { */ public abstract IClassProvider getClassProvider(); + /** + * Return the class bytecode provider for this service + */ + public abstract IClassBytecodeProvider getBytecodeProvider(); + /** * Get additional platform agents for this service */ @@ -108,15 +110,6 @@ public interface IMixinService { */ public abstract InputStream getResourceAsStream(String name); - /** - * Find a class in the service classloader - * - * @param name class name - * @return resultant class - * @throws ClassNotFoundException if the class was not found - */ - public abstract Class findClass(final String name) throws ClassNotFoundException; - /** * Register an invalid class with the service classloader * @@ -132,60 +125,12 @@ public interface IMixinService { * @return true if the class was already loaded */ public abstract boolean isClassLoaded(String className); - - /** - * Check whether the specified class is excluded in the service classloader - * - * @param name class name to check - * @param transformedName transformed name to check - * @return true if the class is excluded in the service classloader - */ - public abstract boolean isClassExcluded(String name, String transformedName); - - /** - * Get the current classpath from the service classloader - */ - public abstract URL[] getClassPath(); /** * Get currently available transformers in the environment */ public abstract Collection getTransformers(); - /** - * Retrieve class bytes using available classloaders, does not transform the - * class - * - * @param name class name - * @param transformedName transformed class name - * @return class bytes or null if not found - * @throws IOException propagated - */ - public abstract byte[] getClassBytes(String name, String transformedName) throws IOException; - - /** - * Retrieve transformed class bytes by using available classloaders and - * running transformer delegation chain on the result if the runTransformers - * option is enabled - * - * @param name full class name - * @param runTransformers true to run transformers on the loaded bytecode - * @return transformed bytes - * @throws ClassNotFoundException if class not found - * @throws IOException propagated - */ - public abstract byte[] getClassBytes(String name, boolean runTransformers) throws ClassNotFoundException, IOException; - - /** - * Retrieve transformed class as an ASM tree - * - * @param name full class name - * @return tree - * @throws ClassNotFoundException if class not found - * @throws IOException propagated - */ - public abstract ClassNode getClassNode(String name) throws ClassNotFoundException, IOException; - /** * Get the detected side name for this environment */