Refactor some elements of mixin service into new smaller components

The main goal here is to reduce the amount of boilerplate required when
providing out simple implementations of the mixin service. Some mixin
end users are using extremely simple environments and this reduces the
boilerplate code in those implementations.
This commit is contained in:
Mumfrey committed 2019-10-16 17:17:43 +01:00
1 parent 22909dcfa2
commit 03e8ad59f0
9 files changed
+235 -168

No files matched your search

@@ -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<ITransformer> getTransformers() {
MixinEnvironment.logger.warn("MixinEnvironment::getTransformers is deprecated!");
return (List<ITransformer>)this.service.getTransformers();
ITransformerProvider transformers = this.service.getTransformerProvider();
return transformers != null ? (List<ITransformer>)transformers.getTransformers() : Collections.<ITransformer>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)
@@ -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<MixinInfo>, 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<MixinInfo>, 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<MixinInfo>, IMixinInfo {
throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className));
}
IClassTracker tracker = this.service.getClassTracker();
List<DeclaredTarget> declaredTargets = new ArrayList<DeclaredTarget>();
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<MixinInfo>, 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) {
@@ -0,0 +1,62 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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 <tt>null</tt> 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);
}
@@ -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. <b>Note that unlike
* other service components, this component is allowed to be <tt>null</tt>
* for services which do not support transformers, or don't support
* interacting with them</b>!
*/
public abstract ITransformerProvider getTransformerProvider();
/**
* Return the class tracker for this service. <b>Note that unlike
* other service components, this component is allowed to be <tt>null</tt>
* for services which do not support this functionality</b>!
*/
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<ITransformer> getTransformers();
/**
* Get currently available transformers in the environment
*/
public abstract Collection<ITransformer> 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
*/
@@ -0,0 +1,54 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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 <tt>null</tt>
* if the platform does not support interacting with the transformer chain (or
* doesn't <em>have</em> a transformer chain!)
*/
public interface ITransformerProvider {
/**
* Get currently available transformers in the environment
*/
public abstract Collection<ITransformer> getTransformers();
/**
* Get currently available transformers in the environment
*/
public abstract Collection<ITransformer> getDelegatedTransformers();
/**
* Adds a transformer to the transformer exclusions list
*
* @param name Class transformer exclusion to add
*/
public abstract void addTransformerExclusion(String name);
}