Allow injectors to specify multiple explicit target methods

This commit is contained in:
Mumfrey committed 2017-07-18 16:56:55 +01:00
1 parent 6aee1957e4
commit 217bf321ba
9 files changed
+139 -81

No files matched your search

@@ -55,13 +55,13 @@ public @interface Inject {
public String id() default "";
/**
* String representation of a
* String representation of one or more
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
* MemberInfo} which identify the target methods.
*
* @return target method for this injector
* @return target method(s) for this injector
*/
public String method();
public String[] method();
/**
* Array of {@link Slice} annotations which describe the method bisections
@@ -73,13 +73,13 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
public @interface ModifyArg {
/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
* String representation of one or more
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identify the target methods.
*
* @return target method
* @return target method(s) for this injector
*/
public String method();
public String[] method();
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -78,13 +78,13 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
public @interface ModifyArgs {
/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
* String representation of one or more
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identify the target methods.
*
* @return target method
* @return target method(s) for this injector
*/
public String method();
public String[] method();
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -60,13 +60,13 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
public @interface ModifyConstant {
/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
* String representation of one or more
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identify the target methods.
*
* @return method to inject into
* @return target method(s) for this injector
*/
public String method();
public String[] method();
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -68,13 +68,13 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
public @interface ModifyVariable {
/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
* String representation of one or more
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identify the target methods.
*
* @return target method
* @return target method(s) for this injector
*/
public String method();
public String[] method();
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -227,13 +227,13 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
public @interface Redirect {
/**
* String representation of a
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identifies the target method.
* String representation of one or more
* {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo
* MemberInfo} which identify the target methods.
*
* @return method to inject into
* @return target method(s) for this injector
*/
public String method();
public String[] method();
/**
* A {@link Slice} annotation which describes the method bisection used in
@@ -29,9 +29,11 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.lib.tree.AnnotationNode;
@@ -160,29 +162,33 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
String type = "@" + Bytecode.getSimpleName(this.annotation);
List<AnnotationNode> injectionPoints = this.readInjectionPoints(type);
this.findMethods(this.parseTarget(type), type);
this.findMethods(this.parseTargets(type), type);
this.parseInjectionPoints(injectionPoints);
this.parseRequirements();
this.injector = this.parseInjector(this.annotation);
}
protected MemberInfo parseTarget(String type) {
String method = Annotations.<String>getValue(this.annotation, "method");
if (method == null) {
protected Set<MemberInfo> parseTargets(String type) {
List<String> methods = Annotations.<String>getValue(this.annotation, "method", false);
if (methods == null) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing method name");
}
try {
MemberInfo targetMember = MemberInfo.parseAndValidate(method, this.mixin);
if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '"
+ targetMember.owner + "', which is not supported");
Set<MemberInfo> members = new LinkedHashSet<MemberInfo>();
for (String method : methods) {
try {
MemberInfo targetMember = MemberInfo.parseAndValidate(method, this.mixin);
if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '"
+ targetMember.owner + "', which is not supported");
}
members.add(targetMember);
} catch (InvalidMemberDescriptorException ex) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + ", has invalid target descriptor: \""
+ method + "\"");
}
return targetMember;
} catch (InvalidMemberDescriptorException ex) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + ", has invalid target descriptor: \""
+ method + "\"");
}
return members;
}
protected List<AnnotationNode> readInjectionPoints(String type) {
@@ -353,29 +359,32 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
/**
* Finds methods in the target class which match searchFor
*
* @param searchFor member info to search for
* @param searchFor members to search for
* @param type annotation type
*/
private void findMethods(MemberInfo searchFor, String type) {
private void findMethods(Set<MemberInfo> searchFor, String type) {
this.targets.clear();
int ordinal = 0;
for (MethodNode target : this.classNode.methods) {
if (searchFor.matches(target.name, target.desc, ordinal)) {
boolean isMixinMethod = Annotations.getVisible(target, MixinMerged.class) != null;
if (searchFor.matchAll && (Bytecode.methodIsStatic(target) != this.isStatic || target == this.method || isMixinMethod)) {
continue;
for (MemberInfo member : searchFor) {
int ordinal = 0;
for (MethodNode target : this.classNode.methods) {
if (member.matches(target.name, target.desc, ordinal)) {
boolean isMixinMethod = Annotations.getVisible(target, MixinMerged.class) != null;
if (member.matchAll && (Bytecode.methodIsStatic(target) != this.isStatic || target == this.method || isMixinMethod)) {
continue;
}
this.checkTarget(target);
this.targets.add(target);
ordinal++;
}
this.checkTarget(target);
this.targets.add(target);
ordinal++;
}
}
if (this.targets.size() == 0) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " could not find '" + searchFor.name + "'");
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " could not find any targets matching "
+ InjectionInfo.namesOf(searchFor));
}
}
@@ -487,4 +496,27 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
return String.format("%s->@%s::%s%s", mixin.toString(), Bytecode.getSimpleName(annotation), method.name, method.desc);
}
/**
* Print the names of the specified members as a human-readable list
*
* @param members members to print
* @return human-readable list of member names
*/
private static String namesOf(Collection<MemberInfo> members) {
int index = 0, count = members.size();
StringBuilder sb = new StringBuilder();
for (MemberInfo member : members) {
if (index > 0) {
if (index == (count - 1)) {
sb.append(" or ");
} else {
sb.append(", ");
}
}
sb.append('\'').append(member.name).append('\'');
index++;
}
return sb.toString();
}
}
@@ -37,6 +37,7 @@ import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.SignaturePrinter;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
/**
@@ -79,7 +80,7 @@ import com.google.common.base.Strings;
* foo.bar.Baz.func_1234_a(DDD)V</pre>
* </blockquote>
*/
public class MemberInfo {
public final class MemberInfo {
/**
* Member owner in internal form but without L;, can be null
@@ -555,6 +556,30 @@ public class MemberInfo {
&& (ordinal == 0 || this.matchAll);
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != MemberInfo.class) {
return false;
}
MemberInfo other = (MemberInfo)obj;
return this.matchAll == other.matchAll && this.forceField == other.forceField
&& Objects.equal(this.owner, other.owner)
&& Objects.equal(this.name, other.name)
&& Objects.equal(this.desc, other.desc);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hashCode(this.matchAll, this.owner, this.name, this.desc);
}
/**
* Create a new version of this member with a different owner
*