Give injector handlers unique names to resolve potential conflicts

This commit is contained in:
Mumfrey committed 2016-02-17 17:45:47 +00:00
1 parent 71b3d2aaaf
commit 083eb425d7
11 files changed
+191 -49

No files matched your search

@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5.2";
public static final String VERSION = "0.5.3";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -291,6 +291,16 @@ public class MixinEnvironment implements ITokenProvider {
*/
DEBUG_INJECTORS(Option.DEBUG_ALL, "countInjections"),
/**
* Disable the injector handler remapper
*/
DEBUG_DISABLE_HANDLER_REMAP(Option.DEBUG_ALL, "disableHandlerRename") {
@Override
boolean getBooleanValue() {
return Booleans.parseBoolean(System.getProperty(this.property), super.getBooleanValue());
}
},
/**
* Dumps the bytecode for the target class to disk when mixin
* application fails
@@ -304,8 +304,6 @@ public class RedirectInjector extends InvokeInjector {
return true;
}
System.err.printf("%s\n", alternateDesc);
throw new InvalidInjectionException(this.info, this.annotationType + " field " + type + " " + this
+ " has an invalid signature. Expected " + desc + " but found " + this.methodNode.desc);
}
@@ -400,15 +400,8 @@ public abstract class InjectionInfo {
}
}
@SuppressWarnings("unchecked")
public static InjectionInfo parse(MixinTargetContext mixin, MethodNode method) {
AnnotationNode annotation = null;
try {
annotation = ASMHelper.getSingleVisibleAnnotation(method, Inject.class, ModifyArg.class, Redirect.class, ModifyVariable.class);
} catch (IllegalArgumentException ex) {
throw new InvalidMixinException(mixin, "Error parsing annotations on " + method.name + " in " + mixin.getClassName() + ": "
+ ex.getMessage());
}
AnnotationNode annotation = InjectionInfo.getInjectorAnnotation(mixin, method);
if (annotation == null) {
return null;
@@ -426,5 +419,30 @@ public abstract class InjectionInfo {
return null;
}
@SuppressWarnings("unchecked")
public static AnnotationNode getInjectorAnnotation(MixinTargetContext mixin, MethodNode method) {
AnnotationNode annotation = null;
try {
annotation = ASMHelper.getSingleVisibleAnnotation(method, Inject.class, ModifyArg.class, Redirect.class, ModifyVariable.class);
} catch (IllegalArgumentException ex) {
throw new InvalidMixinException(mixin, "Error parsing annotations on " + method.name + " in " + mixin.getClassName() + ": "
+ ex.getMessage());
}
return annotation;
}
public static String getInjectorPrefix(AnnotationNode annotation) {
if (annotation != null) {
if (annotation.desc.endsWith(ModifyArg.class.getSimpleName() + ";")) {
return "modify";
} else if (annotation.desc.endsWith(Redirect.class.getSimpleName() + ";")) {
return "redirect";
} else if (annotation.desc.endsWith(ModifyVariable.class.getSimpleName() + ";")) {
return "localvar";
}
}
return "handler";
}
}
@@ -60,6 +60,7 @@ public class ClassInfo extends TreeInfo {
public static final int INCLUDE_PRIVATE = 0x0002;
public static final int INCLUDE_STATIC = 0x0008;
public static final int INCLUDE_ALL = ClassInfo.INCLUDE_PRIVATE | ClassInfo.INCLUDE_STATIC;
/**
* <p>To all intents and purposes, the "real" class hierarchy and the mixin
@@ -193,7 +193,7 @@ public class MixinApplicator {
MixinApplicator(TargetClassContext context) {
this.context = context;
this.targetName = context.getName();
this.targetName = context.getClassName();
this.targetClass = context.getClassNode();
}
@@ -427,7 +427,15 @@ public class MixinApplicator {
if (!this.context.getSessionId().equals(sessionId)) {
throw new ClassFormatError("Invalid @MixinMerged annotation found in" + mixin + " at " + method.name + " in " + this.targetClass.name);
}
if (MixinApplicator.hasFlag(target, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)
&& MixinApplicator.hasFlag(method, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)) {
if (mixin.getEnvironment().getOption(Option.DEBUG_VERBOSE)) {
this.logger.warn("Synthetic bridge method clash for {} in {}", method.name, mixin);
}
return true;
}
String owner = ASMHelper.<String>getAnnotationValue(merged, "mixin");
int priority = ASMHelper.<Integer>getAnnotationValue(merged, "priority");
@@ -47,6 +47,8 @@ import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.RemapperChain;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
@@ -188,13 +190,21 @@ class MixinPreProcessor {
continue;
}
if (this.processMethod(context, mixinMethod, Shadow.class, true, true)) {
if (this.processInjectorMethod(context, mixinMethod)) {
continue;
}
if (this.processMemberMethod(context, mixinMethod, Shadow.class, true, true)) {
iter.remove();
context.addShadowMethod(mixinMethod);
continue;
}
this.processMethod(context, mixinMethod, Overwrite.class, false, false);
if (this.processMemberMethod(context, mixinMethod, Overwrite.class, false, false)) {
continue;
}
this.processMethod(mixinMethod);
}
}
@@ -202,14 +212,28 @@ class MixinPreProcessor {
return true;
}
protected boolean processMethod(MixinTargetContext context, MethodNode mixinMethod, Class<? extends Annotation> annotationType,
protected boolean processInjectorMethod(MixinTargetContext context, MethodNode mixinMethod) {
AnnotationNode annotation = InjectionInfo.getInjectorAnnotation(context, mixinMethod);
boolean surrogate = ASMHelper.getVisibleAnnotation(mixinMethod, Surrogate.class) != null;
if (annotation == null && !surrogate) {
return false;
}
String handlerName = context.getHandlerName(annotation, mixinMethod, surrogate);
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
method.renameTo(handlerName);
mixinMethod.name = handlerName;
return true;
}
protected boolean processMemberMethod(MixinTargetContext context, MethodNode mixinMethod, Class<? extends Annotation> annotationType,
boolean mustExist, boolean mustBePrivate) {
AnnotationNode annotation = ASMHelper.getVisibleAnnotation(mixinMethod, annotationType);
if (annotation == null) {
return false;
}
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_PRIVATE);
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
MethodNode target = MixinPreProcessor.findMethod(context.getTargetClass(), mixinMethod, annotation);
if (target == null) {
@@ -241,6 +265,19 @@ class MixinPreProcessor {
return true;
}
protected void processMethod(MethodNode mixinMethod) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod);
if (method == null) {
return;
}
Method parentMethod = this.mixin.getClassInfo().findMethodInHierarchy(mixinMethod, false);
if (parentMethod != null && parentMethod.isRenamed()) {
mixinMethod.name = parentMethod.getName();
method.renameTo(parentMethod.getName());
}
}
protected void attachFields(MixinTargetContext context) {
for (Iterator<FieldNode> iter = this.classNode.fields.iterator(); iter.hasNext();) {
FieldNode mixinField = iter.next();
@@ -344,13 +381,13 @@ class MixinPreProcessor {
AbstractInsnNode insn = iter.next();
if (insn instanceof MethodInsnNode) {
MethodInsnNode methodNode = (MethodInsnNode)insn;
Method method = this.mixin.getClassInfo().findMethodInHierarchy(methodNode, true, ClassInfo.INCLUDE_PRIVATE);
Method method = ClassInfo.forName(methodNode.owner).findMethodInHierarchy(methodNode, true, ClassInfo.INCLUDE_PRIVATE);
if (method != null && method.isRenamed()) {
methodNode.name = method.getName();
}
} else if (insn instanceof FieldInsnNode) {
FieldInsnNode fieldNode = (FieldInsnNode)insn;
Field field = this.mixin.getClassInfo().findField(fieldNode, ClassInfo.INCLUDE_PRIVATE);
Field field = ClassInfo.forName(fieldNode.owner).findField(fieldNode, ClassInfo.INCLUDE_PRIVATE);
if (field != null && field.isRenamed()) {
fieldNode.name = field.getName();
}
@@ -86,7 +86,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
/**
*
*/
private final ClassNode targetClass;
private final TargetClassContext targetClass;
/**
* Session ID from context
@@ -122,13 +122,12 @@ public class MixinTargetContext implements IReferenceMapperContext {
/**
* Injector groups
*/
protected final InjectorGroupInfo.Map injectorGroups = new InjectorGroupInfo.Map();
private final InjectorGroupInfo.Map injectorGroups = new InjectorGroupInfo.Map();
/**
* Injectors for this target
*/
protected final List<InjectionInfo> injectors = new ArrayList<InjectionInfo>();
private final List<InjectionInfo> injectors = new ArrayList<InjectionInfo>();
/**
* True if this mixin inherits from a mixin at any point in its hierarchy
@@ -146,6 +145,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
*/
private int minRequiredClassVersion = CompatibilityLevel.JAVA_6.classVersion();
/**
* ctor
*
@@ -156,10 +156,10 @@ public class MixinTargetContext implements IReferenceMapperContext {
MixinTargetContext(MixinInfo mixin, ClassNode classNode, TargetClassContext context) {
this.mixin = mixin;
this.classNode = classNode;
this.targetClass = context.getClassNode();
this.targetClassInfo = ClassInfo.forName(this.targetClass.name);
this.targetClass = context;
this.targetClassInfo = ClassInfo.forName(this.targetClass.getName());
this.inheritsFromMixin = mixin.getClassInfo().hasMixinInHierarchy() || this.targetClassInfo.hasMixinTargetInHierarchy();
this.detachedSuper = !this.classNode.superName.equals(this.targetClass.superName);
this.detachedSuper = !this.classNode.superName.equals(this.targetClass.getClassNode().superName);
this.sessionId = context.getSessionId();
this.requireVersion(classNode.version);
}
@@ -244,7 +244,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
* mixins)
*/
public String getTargetClassRef() {
return this.targetClass.name;
return this.targetClass.getName();
}
/**
@@ -253,7 +253,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
* @return the target class
*/
public ClassNode getTargetClass() {
return this.targetClass;
return this.targetClass.getClassNode();
}
/**
@@ -366,7 +366,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
ClassInfo.Method superMethod = this.targetClassInfo.findMethodInHierarchy(method.name, method.desc, false, Traversal.SUPER);
if (superMethod == null || !superMethod.isInjected()) {
throw new InvalidMixinException(this, "Mixin method " + method.name + method.desc + " is tagged with @SoftOverride but no "
+ "valid method was found in superclasses of " + this.targetClass.name);
+ "valid method was found in superclasses of " + this.targetClass.getName());
}
}
}
@@ -383,7 +383,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.transformDescriptor(methodRef);
if (methodRef.getOwner().equals(this.getClassRef())) {
methodRef.setOwner(this.targetClass.name);
methodRef.setOwner(this.targetClass.getName());
} else if ((this.detachedSuper || this.inheritsFromMixin)) {
if (methodRef.getOpcode() == Opcodes.INVOKESPECIAL) {
this.updateStaticBinding(method, methodRef);
@@ -416,18 +416,18 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.transformDescriptor(fieldRef);
if (fieldRef.getOwner().equals(this.getClassRef())) {
fieldRef.setOwner(this.targetClass.name);
fieldRef.setOwner(this.targetClass.getName());
} else {
ClassInfo fieldOwner = ClassInfo.forName(fieldRef.getOwner());
if (fieldOwner.isMixin()) {
ClassInfo actualOwner = this.targetClassInfo.findCorrespondingType(fieldOwner);
fieldRef.setOwner(actualOwner != null ? actualOwner.getName() : this.targetClass.name);
fieldRef.setOwner(actualOwner != null ? actualOwner.getName() : this.targetClass.getName());
}
}
}
private void checkFinal(MethodNode method, Iterator<AbstractInsnNode> iter, FieldInsnNode fieldNode) {
if (!fieldNode.owner.equals(this.targetClass.name)) {
if (!fieldNode.owner.equals(this.targetClass.getName())) {
return;
}
@@ -471,7 +471,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
*/
private void transformTypeNode(MethodNode method, Iterator<AbstractInsnNode> iter, TypeInsnNode typeInsn, AbstractInsnNode lastNode) {
if (typeInsn.getOpcode() == Opcodes.CHECKCAST
&& typeInsn.desc.equals(this.targetClass.name)
&& typeInsn.desc.equals(this.targetClass.getName())
&& lastNode.getOpcode() == Opcodes.ALOAD
&& ((VarInsnNode)lastNode).var == 0) {
iter.remove();
@@ -479,7 +479,7 @@ public class MixinTargetContext implements IReferenceMapperContext {
}
if (typeInsn.desc.equals(this.getClassRef())) {
typeInsn.desc = this.targetClass.name;
typeInsn.desc = this.targetClass.getName();
}
this.transformDescriptor(typeInsn);
@@ -624,7 +624,9 @@ public class MixinTargetContext implements IReferenceMapperContext {
}
private void updateBinding(MethodNode method, MemberRef methodRef, Traversal traversal) {
if (Constants.CTOR.equals(method.name) || methodRef.getOwner().equals(this.targetClass.name) || this.targetClass.name.startsWith("<")) {
if (Constants.CTOR.equals(method.name)
|| methodRef.getOwner().equals(this.targetClass.getName())
|| this.targetClass.getName().startsWith("<")) {
return;
}
@@ -749,14 +751,14 @@ public class MixinTargetContext implements IReferenceMapperContext {
* @return new or existing target handle for the supplied method
*/
public Target getTargetMethod(MethodNode method) {
if (!this.targetClass.methods.contains(method)) {
if (!this.targetClass.getMethods().contains(method)) {
throw new IllegalArgumentException("Invalid target method supplied to getTargetMethod()");
}
String targetName = method.name + method.desc;
Target target = this.targetMethods.get(targetName);
if (target == null) {
target = new Target(this.targetClass, method);
target = new Target(this.targetClass.getClassNode(), method);
this.targetMethods.put(targetName, target);
}
return target;
@@ -906,6 +908,10 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.mixin.postApply(transformedName, targetClass);
}
public String getHandlerName(AnnotationNode annotation, MethodNode method, boolean surrogate) {
return this.targetClass.getHandlerName(annotation, method, surrogate);
}
/**
* Scans the target class for injector methods and prepares discovered
* injectors
@@ -27,6 +27,7 @@ package org.spongepowered.asm.mixin.transformer;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -481,11 +482,17 @@ public class MixinTransformer extends TreeTransformer {
private void init(MixinEnvironment environment) {
this.verboseLoggingLevel = (environment.getOption(Option.DEBUG_VERBOSE)) ? Level.INFO : Level.DEBUG;
this.logger.log(this.verboseLoggingLevel, "Preparing mixins for {}", environment);
long startTime = System.currentTimeMillis();
this.addConfigs(environment);
this.addModules(environment);
this.initConfigs();
this.currentEnvironment = environment;
double elapsedTime = (System.currentTimeMillis() - startTime) * 0.001D;
if (elapsedTime > 0.25D) {
this.logger.log(this.verboseLoggingLevel, "Mixin preparation completed in {} sec", new DecimalFormat("###0.000").format(elapsedTime));
}
}
/**
@@ -501,7 +508,7 @@ public class MixinTransformer extends TreeTransformer {
try {
MixinConfig config = MixinConfig.create(configFile, environment);
if (config != null) {
this.logger.log(this.verboseLoggingLevel, "Adding mixin config {}", config);
this.logger.log(this.verboseLoggingLevel, "Adding config {}", config);
this.pendingConfigs.add(config);
}
} catch (Exception ex) {
@@ -538,6 +545,7 @@ public class MixinTransformer extends TreeTransformer {
private void initConfigs() {
for (MixinConfig config : this.pendingConfigs) {
try {
this.logger.log(this.verboseLoggingLevel, "Preparing {} ({})", config, config.getClasses().size());
config.initialise(this.hotSwapper);
} catch (Exception ex) {
this.logger.error("Error encountered whilst initialising mixin config '" + config.getName() + "': " + ex.getMessage(), ex);
@@ -564,7 +572,7 @@ public class MixinTransformer extends TreeTransformer {
try {
config.postInitialise(this.hotSwapper);
} catch (Exception ex) {
this.logger.error("Error encountered during mixin config postInit setp'" + config.getName() + "': " + ex.getMessage(), ex);
this.logger.error("Error encountered during mixin config postInit step'" + config.getName() + "': " + ex.getMessage(), ex);
}
}
@@ -717,7 +725,7 @@ public class MixinTransformer extends TreeTransformer {
}
private byte[] writeClass(TargetClassContext context) {
return this.writeClass(context.getName(), context.getClassNode());
return this.writeClass(context.getClassName(), context.getClassNode());
}
private byte[] writeClass(String transformedName, ClassNode targetClass) {
@@ -24,14 +24,29 @@
*/
package org.spongepowered.asm.mixin.transformer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import org.spongepowered.asm.lib.tree.AnnotationNode;
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
/**
* Struct for containing target class information during mixin application
*/
class TargetClassContext {
/**
* Mutable integer
*/
static class Counter {
public int value;
}
/**
* Transformer session ID
@@ -41,7 +56,7 @@ class TargetClassContext {
/**
* Target class name
*/
private final String name;
private final String className;
/**
* Target class as tree
@@ -58,6 +73,16 @@ class TargetClassContext {
*/
private final SortedSet<MixinInfo> mixins;
/**
* Injector Method descriptor to ID map
*/
private final Map<String, Counter> injectorMethodIndices = new HashMap<String, Counter>();
/**
* Do not remap injector handler methods (debug option)
*/
private final boolean disableHandlerRemap;
/**
* True once mixins have been applied to this class
*/
@@ -65,10 +90,11 @@ class TargetClassContext {
TargetClassContext(String sessionId, String name, ClassNode classNode, SortedSet<MixinInfo> mixins) {
this.sessionId = sessionId;
this.name = name;
this.className = name;
this.classNode = classNode;
this.classInfo = ClassInfo.fromClassNode(classNode);
this.mixins = mixins;
this.disableHandlerRemap = MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_DISABLE_HANDLER_REMAP);
}
/**
@@ -79,10 +105,17 @@ class TargetClassContext {
}
/**
* Get the class name
* Get the internal class name
*/
public String getName() {
return this.name;
return this.classNode.name;
}
/**
* Get the class name
*/
public String getClassName() {
return this.className;
}
/**
@@ -91,7 +124,14 @@ class TargetClassContext {
public ClassNode getClassNode() {
return this.classNode;
}
/**
* Get the class methods (from the tree)
*/
public List<MethodNode> getMethods() {
return this.classNode.methods;
}
/**
* Get the target class metadata
*/
@@ -106,12 +146,28 @@ class TargetClassContext {
return this.mixins;
}
public String getHandlerName(AnnotationNode annotation, MethodNode method, boolean surrogate) {
if (this.disableHandlerRemap) {
return method.name;
}
String descriptor = String.format("%s%s", method.name, method.desc);
Counter id = this.injectorMethodIndices.get(descriptor);
if (id == null) {
id = new Counter();
this.injectorMethodIndices.put(descriptor, id);
} else if (!surrogate) {
id.value++;
}
return String.format("%s$%s$%d", InjectionInfo.getInjectorPrefix(annotation), method.name, id.value);
}
/**
* Apply mixins to this class
*/
public void applyMixins() {
if (this.applied) {
throw new IllegalStateException("Mixins already applied to target class " + this.name);
throw new IllegalStateException("Mixins already applied to target class " + this.className);
}
this.applied = true;
MixinApplicator applicator = this.createApplicator();