Fix multi-mixin injector arbitration once and for all

This commit is contained in:
Mumfrey committed 2016-02-08 14:05:42 +00:00
1 parent 665908d32e
commit 590c11d6cd
5 files changed
+141 -50

No files matched your search

@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5";
public static final String VERSION = "0.5.1";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -86,11 +86,11 @@ public class InterfaceMixinApplicator extends MixinApplicator {
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
* #applyInjections(
* #prepareInjections(
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
*/
@Override
protected void applyInjections(MixinTargetContext mixin) {
protected void prepareInjections(MixinTargetContext mixin) {
// disabled for interface mixins
for (MethodNode method : this.targetClass.methods) {
try {
@@ -104,5 +104,15 @@ public class InterfaceMixinApplicator extends MixinApplicator {
}
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
* #applyInjections(
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
*/
@Override
protected void applyInjections(MixinTargetContext mixin) {
// Do nothing
}
}
@@ -51,8 +51,6 @@ import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
import org.spongepowered.asm.util.ASMHelper;
@@ -67,6 +65,26 @@ import org.spongepowered.asm.util.InvalidConstraintException;
*/
public class MixinApplicator {
/**
* Passes the mixin applicator applies to each mixin
*/
enum ApplicatorPass {
/**
* Main pass, mix in methods, fields, interfaces etc
*/
MAIN,
/**
* Enumerate injectors and scan for injection points
*/
PREINJECT,
/**
* Apply injectors from previous pass
*/
INJECT
}
/**
* Internal struct for representing a range
*/
@@ -185,9 +203,36 @@ public class MixinApplicator {
* Apply supplied mixins to the target class
*/
void apply(SortedSet<MixinInfo> mixins) {
List<MixinTargetContext> mixinContexts = new ArrayList<MixinTargetContext>();
for (MixinInfo mixin : mixins) {
this.logger.log(mixin.getLoggingLevel(), "Mixing {} from {} into {}", mixin.getName(), mixin.getParent(), this.targetName);
this.applyMixin(mixin.createContextFor(this.targetClass));
mixinContexts.add(mixin.createContextFor(this.targetClass));
}
MixinTargetContext current = null;
try {
for (MixinTargetContext context : mixinContexts) {
current = context;
context.preApply(this.targetName, this.targetClass);
}
for (ApplicatorPass pass : ApplicatorPass.values()) {
for (MixinTargetContext context : mixinContexts) {
current = context;
this.applyMixin(context, pass);
}
}
for (MixinTargetContext context : mixinContexts) {
current = context;
context.postApply(this.targetName, this.targetClass);
}
} catch (InvalidMixinException ex) {
throw ex;
} catch (Exception ex) {
throw new InvalidMixinException(current, "Unexpecteded error whilst applying the mixin class", ex);
}
}
@@ -196,23 +241,28 @@ public class MixinApplicator {
*
* @param mixin Mixin to apply
*/
protected final void applyMixin(MixinTargetContext mixin) {
try {
mixin.preApply(this.targetName, this.targetClass);
this.applyInterfaces(mixin);
this.applyAttributes(mixin);
this.applyAnnotations(mixin);
this.applyFields(mixin);
this.applyMethods(mixin);
this.applyInitialisers(mixin);
this.applyInjections(mixin);
mixin.postApply(this.targetName, this.targetClass);
} catch (InvalidMixinException ex) {
throw ex;
} catch (Exception ex) {
throw new InvalidMixinException(mixin, "Unexpecteded error whilst applying the mixin class", ex);
protected final void applyMixin(MixinTargetContext mixin, ApplicatorPass pass) {
switch (pass) {
case MAIN:
this.applyInterfaces(mixin);
this.applyAttributes(mixin);
this.applyAnnotations(mixin);
this.applyFields(mixin);
this.applyMethods(mixin);
this.applyInitialisers(mixin);
break;
case PREINJECT:
this.prepareInjections(mixin);
break;
case INJECT:
this.applyInjections(mixin);
break;
default:
// wat?
throw new IllegalStateException("Invalid pass specified " + pass);
}
}
@@ -705,34 +755,21 @@ public class MixinApplicator {
}
/**
* Process {@link Inject} annotations and inject callbacks to annotated
* methods
* @param mixin
* Scan for injector methods and injection points
*
* @param mixin Mixin being scanned
*/
protected void prepareInjections(MixinTargetContext mixin) {
mixin.prepareInjections();
}
/**
* Apply all injectors discovered in the previous pass
*
* @param mixin Mixin being applied
*/
protected void applyInjections(MixinTargetContext mixin) {
List<InjectionInfo> injectors = new ArrayList<InjectionInfo>();
for (MethodNode method : this.targetClass.methods) {
InjectionInfo injectInfo = InjectionInfo.parse(mixin, method);
if (injectInfo == null) {
continue;
}
if (injectInfo.isValid()) {
injectInfo.prepare();
injectors.add(injectInfo);
}
method.visibleAnnotations.remove(injectInfo.getAnnotation());
}
for (InjectionInfo injectInfo : injectors) {
injectInfo.inject();
}
for (InjectionInfo injectInfo : injectors) {
injectInfo.postInject();
}
mixin.applyInjections();
}
/**
@@ -49,6 +49,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.InjectionError;
import org.spongepowered.asm.mixin.injection.InjectionValidationException;
import org.spongepowered.asm.mixin.injection.InjectorGroupInfo;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
@@ -112,6 +113,12 @@ public class MixinTargetContext implements IReferenceMapperContext {
*/
protected final InjectorGroupInfo.Map injectorGroups = new InjectorGroupInfo.Map();
/**
* Injectors for this target
*/
protected final List<InjectionInfo> injectors = new ArrayList<InjectionInfo>();
/**
* True if this mixin inherits from a mixin at any point in its hierarchy
*/
@@ -872,4 +879,41 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.mixin.postApply(transformedName, targetClass);
}
/**
* Scans the target class for injector methods and prepares discovered
* injectors
*/
public void prepareInjections() {
this.injectors.clear();
for (MethodNode method : this.targetClass.methods) {
InjectionInfo injectInfo = InjectionInfo.parse(this, method);
if (injectInfo == null) {
continue;
}
if (injectInfo.isValid()) {
injectInfo.prepare();
this.injectors.add(injectInfo);
}
method.visibleAnnotations.remove(injectInfo.getAnnotation());
}
}
/**
* Apply injectors discovered in the {@link #prepareInjections()} pass
*/
public void applyInjections() {
for (InjectionInfo injectInfo : this.injectors) {
injectInfo.inject();
}
for (InjectionInfo injectInfo : this.injectors) {
injectInfo.postInject();
}
this.injectors.clear();
}
}