diff --git a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java index 45169565..0d779483 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/LaunchClassLoaderUtil.java @@ -29,6 +29,8 @@ import java.util.Collections; import java.util.Map; import java.util.Set; +import org.spongepowered.asm.service.IClassTracker; + import net.minecraft.launchwrapper.LaunchClassLoader; /** @@ -37,7 +39,7 @@ import net.minecraft.launchwrapper.LaunchClassLoader; * them to perform some validation tasks, and insert entries for mixin "classes" * into the invalid classes set. */ -final class LaunchClassLoaderUtil { +final class LaunchClassLoaderUtil implements IClassTracker { private static final String CACHED_CLASSES_FIELD = "cachedClasses"; private static final String INVALID_CLASSES_FIELD = "invalidClasses"; @@ -67,7 +69,7 @@ final class LaunchClassLoaderUtil { this.classLoaderExceptions = LaunchClassLoaderUtil.>getField(classLoader, LaunchClassLoaderUtil.CLASS_LOADER_EXCEPTIONS_FIELD); this.transformerExceptions = LaunchClassLoaderUtil.>getField(classLoader, LaunchClassLoaderUtil.TRANSFORMER_EXCEPTIONS_FIELD); } - + /** * Get the classloader */ @@ -82,10 +84,27 @@ final class LaunchClassLoaderUtil { * @param name class name * @return true if the class name exists in the cache */ - boolean isClassLoaded(String name) { + @Override + public boolean isClassLoaded(String name) { return this.cachedClasses.containsKey(name); } + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getClassRestrictions( + * java.lang.String) + */ + @Override + public String getClassRestrictions(String className) { + String restrictions = ""; + if (this.isClassClassLoaderExcluded(className, null)) { + restrictions = "PACKAGE_CLASSLOADER_EXCLUSION"; + } + if (this.isClassTransformerExcluded(className, null)) { + restrictions = (restrictions.length() > 0 ? restrictions + "," : "") + "PACKAGE_TRANSFORMER_EXCLUSION"; + } + return restrictions; + } + /** * Get whether the specified name or transformedName exist in either of the * exclusion lists @@ -143,7 +162,8 @@ final class LaunchClassLoaderUtil { * * @param name class name */ - void registerInvalidClass(String name) { + @Override + public void registerInvalidClass(String name) { if (this.invalidClasses != null) { this.invalidClasses.add(name); } 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 d043533e..707ec5c5 100644 --- a/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java +++ b/src/launchwrapper/java/org/spongepowered/asm/service/mojang/MixinServiceLaunchWrapper.java @@ -52,8 +52,10 @@ 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.IClassTracker; import org.spongepowered.asm.service.ILegacyClassTransformer; import org.spongepowered.asm.service.ITransformer; +import org.spongepowered.asm.service.ITransformerProvider; import org.spongepowered.asm.service.MixinServiceAbstract; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.perf.Profiler; @@ -72,7 +74,7 @@ import net.minecraft.launchwrapper.Launch; /** * Mixin service for launchwrapper */ -public class MixinServiceLaunchWrapper extends MixinServiceAbstract implements IClassProvider, IClassBytecodeProvider { +public class MixinServiceLaunchWrapper extends MixinServiceAbstract implements IClassProvider, IClassBytecodeProvider, ITransformerProvider { // Blackboard keys public static final Keys BLACKBOARD_KEY_TWEAKCLASSES = Keys.of("TweakClasses"); @@ -259,6 +261,22 @@ public class MixinServiceLaunchWrapper extends MixinServiceAbstract implements I return this; } + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getTransformerProvider() + */ + @Override + public ITransformerProvider getTransformerProvider() { + return this; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getClassTracker() + */ + @Override + public IClassTracker getClassTracker() { + return this.classLoaderUtil; + } + /* (non-Javadoc) * @see org.spongepowered.asm.service.IClassProvider#findClass( * java.lang.String) @@ -315,40 +333,6 @@ public class MixinServiceLaunchWrapper extends MixinServiceAbstract implements I return Launch.classLoader.getResourceAsStream(name); } - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#registerInvalidClass( - * java.lang.String) - */ - @Override - public void registerInvalidClass(String className) { - this.classLoaderUtil.registerInvalidClass(className); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#isClassLoaded( - * java.lang.String) - */ - @Override - public boolean isClassLoaded(String className) { - return this.classLoaderUtil.isClassLoaded(className); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#getClassRestrictions( - * java.lang.String) - */ - @Override - public String getClassRestrictions(String className) { - String restrictions = ""; - if (this.classLoaderUtil.isClassClassLoaderExcluded(className, null)) { - restrictions = "PACKAGE_CLASSLOADER_EXCLUSION"; - } - if (this.classLoaderUtil.isClassTransformerExcluded(className, null)) { - restrictions = (restrictions.length() > 0 ? restrictions + "," : "") + "PACKAGE_TRANSFORMER_EXCLUSION"; - } - return restrictions; - } - /* (non-Javadoc) * @see org.spongepowered.asm.service.IClassProvider#getClassPath() */ diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 2f73ccfa..dc6a75af 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -47,6 +47,7 @@ import org.spongepowered.asm.mixin.transformer.IMixinTransformer; import org.spongepowered.asm.obfuscation.RemapperChain; import org.spongepowered.asm.service.IMixinService; import org.spongepowered.asm.service.ITransformer; +import org.spongepowered.asm.service.ITransformerProvider; import org.spongepowered.asm.service.MixinService; import org.spongepowered.asm.service.MixinServiceAbstract; import org.spongepowered.asm.util.Constants; @@ -1263,7 +1264,8 @@ public final class MixinEnvironment implements ITokenProvider { @Deprecated public List getTransformers() { MixinEnvironment.logger.warn("MixinEnvironment::getTransformers is deprecated!"); - return (List)this.service.getTransformers(); + ITransformerProvider transformers = this.service.getTransformerProvider(); + return transformers != null ? (List)transformers.getTransformers() : Collections.emptyList(); } /** @@ -1275,7 +1277,10 @@ public final class MixinEnvironment implements ITokenProvider { @Deprecated public void addTransformerExclusion(String name) { MixinEnvironment.logger.warn("MixinEnvironment::addTransformerExclusion is deprecated!"); - this.service.addTransformerExclusion(name); + ITransformerProvider transformers = this.service.getTransformerProvider(); + if (transformers != null) { + transformers.addTransformerExclusion(name); + } } /* (non-Javadoc) 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 1ba207f1..ae53c0b9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -64,8 +64,8 @@ import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.mixin.transformer.throwables.MixinReloadException; import org.spongepowered.asm.mixin.transformer.throwables.MixinTargetAlreadyLoadedException; +import org.spongepowered.asm.service.IClassTracker; import org.spongepowered.asm.service.IMixinService; -import org.spongepowered.asm.service.MixinService; import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.asm.ASM; @@ -702,11 +702,6 @@ class MixinInfo implements Comparable, IMixinInfo { } - /** - * Mixin service - */ - private static final IMixinService classLoaderUtil = MixinService.getService(); - /** * Global order of mixin infos, used to determine ordering between mixins * with equivalent priority @@ -843,7 +838,10 @@ class MixinInfo implements Comparable, IMixinInfo { // Inject the mixin class name into the LaunchClassLoader's invalid // classes set so that any classes referencing the mixin directly will // cause the game to crash - MixinInfo.classLoaderUtil.registerInvalidClass(this.className); + IClassTracker tracker = this.service.getClassTracker(); + if (tracker != null) { + tracker.registerInvalidClass(this.className); + } } // Read the class bytes and transform @@ -906,13 +904,14 @@ class MixinInfo implements Comparable, IMixinInfo { throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } + IClassTracker tracker = this.service.getClassTracker(); List declaredTargets = new ArrayList(); for (Object target : this.readTargets(mixin)) { DeclaredTarget declaredTarget = DeclaredTarget.of(target, this); if (declaredTarget == null) { continue; } - if (MixinInfo.classLoaderUtil.isClassLoaded(declaredTarget.name) && !this.isReloading()) { + if (tracker != null && tracker.isClassLoaded(declaredTarget.name) && !this.isReloading()) { String message = String.format("Critical problem: %s target %s was loaded too early.", this, declaredTarget.name); if (this.parent.isRequired()) { throw new MixinTargetAlreadyLoadedException(this, message, declaredTarget.name); @@ -1265,9 +1264,12 @@ class MixinInfo implements Comparable, IMixinInfo { ClassNode classNode = null; try { - String restrictions = this.service.getClassRestrictions(mixinClassName); - if (restrictions.length() > 0) { - this.logger.error("Classloader restrictions [{}] encountered loading {}, name: {}", restrictions, this, mixinClassName); + IClassTracker tracker = this.service.getClassTracker(); + if (tracker != null) { + String restrictions = tracker.getClassRestrictions(mixinClassName); + if (restrictions.length() > 0) { + this.logger.error("Classloader restrictions [{}] encountered loading {}, name: {}", restrictions, this, mixinClassName); + } } classNode = this.service.getBytecodeProvider().getClassNode(mixinClassName, true); } catch (ClassNotFoundException ex) { diff --git a/src/main/java/org/spongepowered/asm/service/IClassTracker.java b/src/main/java/org/spongepowered/asm/service/IClassTracker.java new file mode 100644 index 00000000..6e1e2513 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/service/IClassTracker.java @@ -0,0 +1,62 @@ +/* + * 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; + +/** + * Class trackers are responsible for interacting with the class loading process + * to for the purposes of tracking class load activity and class load + * restrictions. This service component is entirely optional and services can + * elect to return null if the platform does not support this + * functionality. + */ +public interface IClassTracker { + + /** + * Register an invalid class with the service classloader + * + * @param className invalid class name + */ + public abstract void registerInvalidClass(String className); + + /** + * Check whether the specified class was already loaded by the service + * classloader + * + * @param className class name to check + * @return true if the class was already loaded + */ + public abstract boolean isClassLoaded(String className); + + /** + * Check whether the specified class name is subject to any restrictions in + * the context of this service + * + * @param className class name to check + * @return comma-separated list of restrictions, empty string if no + * restrictions apply + */ + public abstract String getClassRestrictions(String className); + +} diff --git a/src/main/java/org/spongepowered/asm/service/IMixinService.java b/src/main/java/org/spongepowered/asm/service/IMixinService.java index 147a34ea..b17f1024 100644 --- a/src/main/java/org/spongepowered/asm/service/IMixinService.java +++ b/src/main/java/org/spongepowered/asm/service/IMixinService.java @@ -97,10 +97,20 @@ public interface IMixinService { */ public abstract IClassBytecodeProvider getBytecodeProvider(); -// /** -// * Return the synthetic class registry for this service -// */ -// public abstract ISyntheticClassRegistry getSyntheticClassRegistry(); + /** + * Return the transformer provider for this service. Note that unlike + * other service components, this component is allowed to be null + * for services which do not support transformers, or don't support + * interacting with them! + */ + public abstract ITransformerProvider getTransformerProvider(); + + /** + * Return the class tracker for this service. Note that unlike + * other service components, this component is allowed to be null + * for services which do not support this functionality! + */ + public abstract IClassTracker getClassTracker(); /** * Get additional platform agents for this service @@ -130,49 +140,6 @@ public interface IMixinService { */ public abstract InputStream getResourceAsStream(String name); - /** - * Register an invalid class with the service classloader - * - * @param className invalid class name - */ - public abstract void registerInvalidClass(String className); - - /** - * Check whether the specified class was already loaded by the service - * classloader - * - * @param className class name to check - * @return true if the class was already loaded - */ - public abstract boolean isClassLoaded(String className); - - /** - * Check whether the specified class name is subject to any restrictions in - * the context of this service - * - * @param className class name to check - * @return comma-separated list of restrictions, empty string if no - * restrictions apply - */ - public abstract String getClassRestrictions(String className); - - /** - * Get currently available transformers in the environment - */ - public abstract Collection getTransformers(); - - /** - * Get currently available transformers in the environment - */ - public abstract Collection getDelegatedTransformers(); - - /** - * Adds a transformer to the transformer exclusions list - * - * @param name Class transformer exclusion to add - */ - public abstract void addTransformerExclusion(String name); - /** * Get the detected side name for this environment */ diff --git a/src/main/java/org/spongepowered/asm/service/ITransformerProvider.java b/src/main/java/org/spongepowered/asm/service/ITransformerProvider.java new file mode 100644 index 00000000..0a5a397d --- /dev/null +++ b/src/main/java/org/spongepowered/asm/service/ITransformerProvider.java @@ -0,0 +1,54 @@ +/* + * 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.util.Collection; + +/** + * Interface for interacting with transforming classloaders. This service + * component is entirely optional and services can elect to return null + * if the platform does not support interacting with the transformer chain (or + * doesn't have a transformer chain!) + */ +public interface ITransformerProvider { + + /** + * Get currently available transformers in the environment + */ + public abstract Collection getTransformers(); + + /** + * Get currently available transformers in the environment + */ + public abstract Collection getDelegatedTransformers(); + + /** + * Adds a transformer to the transformer exclusions list + * + * @param name Class transformer exclusion to add + */ + public abstract void addTransformerExclusion(String name); + +} diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java index 6c287d07..0bf45dd3 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java @@ -34,7 +34,8 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.mixin.transformer.MixinTransformationHandler; import org.spongepowered.asm.service.IClassBytecodeProvider; import org.spongepowered.asm.service.IClassProvider; -import org.spongepowered.asm.service.ITransformer; +import org.spongepowered.asm.service.IClassTracker; +import org.spongepowered.asm.service.ITransformerProvider; import org.spongepowered.asm.service.MixinServiceAbstract; import org.spongepowered.asm.util.IConsumer; @@ -171,6 +172,25 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { } return this.bytecodeProvider; } + + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getTransformerProvider() + */ + @Override + public ITransformerProvider getTransformerProvider() { + return null; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IMixinService#getClassTracker() + */ + @Override + public IClassTracker getClassTracker() { + if (this.classTracker == null) { + this.classTracker = new ModLauncherClassTracker(); + } + return this.classTracker; + } /** * Get (or create) the transformation handler @@ -181,16 +201,6 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { } return this.transformationHandler; } - - /** - * Get (or create) the class tracker - */ - private ModLauncherClassTracker getClassTracker() { - if (this.classTracker == null) { - this.classTracker = new ModLauncherClassTracker(); - } - return this.classTracker; - } /* (non-Javadoc) * @see org.spongepowered.asm.service.IMixinService#getPlatformAgents() @@ -219,59 +229,6 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { return Thread.currentThread().getContextClassLoader().getResourceAsStream(name); } - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#registerInvalidClass( - * java.lang.String) - */ - @Override - public void registerInvalidClass(String className) { - this.getClassTracker().registerInvalidClass(className); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#isClassLoaded( - * java.lang.String) - */ - @Override - public boolean isClassLoaded(String className) { - return this.getClassTracker().isClassLoaded(className); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#getClassRestrictions( - * java.lang.String) - */ - @Override - public String getClassRestrictions(String className) { - return this.getClassTracker().getClassRestrictions(className); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#getTransformers() - */ - @Override - public Collection getTransformers() { - return ImmutableList.of(); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService - * #getDelegatedTransformers() - */ - @Override - public Collection getDelegatedTransformers() { - return ImmutableList.of(); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IMixinService#addTransformerExclusion( - * java.lang.String) - */ - @Override - public void addTransformerExclusion(String name) { - // TODO ? - } - /** * Internal method to retrieve the class processors which will be called by * the launch plugin @@ -279,7 +236,7 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { public Collection getProcessors() { return ImmutableList.of( this.getTransformationHandler(), - this.getClassTracker() + (IClassProcessor)this.getClassTracker() ); } diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java index e9fdc56b..8280d386 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java @@ -32,6 +32,7 @@ import org.objectweb.asm.Type; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.launch.IClassProcessor; import org.spongepowered.asm.launch.Phases; +import org.spongepowered.asm.service.IClassTracker; import cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase; @@ -40,25 +41,40 @@ import cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase; * TCL and class load events so we can report when classes were loaded before * we could transform them */ -public class ModLauncherClassTracker implements IClassProcessor { +public class ModLauncherClassTracker implements IClassProcessor, IClassTracker { private final Set invalidClasses = new HashSet(); private final Set loadedClasses = new HashSet(); - void registerInvalidClass(String className) { + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IClassTracker#registerInvalidClass( + * java.lang.String) + */ + @Override + public void registerInvalidClass(String className) { synchronized (this.invalidClasses) { this.invalidClasses.add(className); } } - boolean isClassLoaded(String className) { + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IClassTracker#isClassLoaded( + * java.lang.String) + */ + @Override + public boolean isClassLoaded(String className) { synchronized (this.loadedClasses) { return this.loadedClasses.contains(className); } } - String getClassRestrictions(String className) { + /* (non-Javadoc) + * @see org.spongepowered.asm.service.IClassTracker#getClassRestrictions( + * java.lang.String) + */ + @Override + public String getClassRestrictions(String className) { return ""; // TODO ? }