diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java index ce98e7cf..fb232b65 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java @@ -154,29 +154,30 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement()); } - String reference = elem.getAnnotation().getValue("method"); - MemberInfo targetMember = MemberInfo.parse(reference); - if (targetMember.name == null) { - return; - } - - try { - targetMember.validate(); - } catch (InvalidMemberDescriptorException ex) { - elem.printMessage(this.ap, Kind.ERROR, ex.getMessage()); - } - - if (targetMember.desc != null) { - this.validateReferencedTarget(elem.getElement(), elem.getAnnotation(), targetMember, elem.toString()); - } - - if (!elem.shouldRemap()) { - return; - } - - for (TypeHandle target : this.mixin.getTargets()) { - if (!this.registerInjector(elem, reference, targetMember, target)) { - break; + for (String reference : elem.getAnnotation().getList("method")) { + MemberInfo targetMember = MemberInfo.parse(reference); + if (targetMember.name == null) { + continue; + } + + try { + targetMember.validate(); + } catch (InvalidMemberDescriptorException ex) { + elem.printMessage(this.ap, Kind.ERROR, ex.getMessage()); + } + + if (targetMember.desc != null) { + this.validateReferencedTarget(elem.getElement(), elem.getAnnotation(), targetMember, elem.toString()); + } + + if (!elem.shouldRemap()) { + continue; + } + + for (TypeHandle target : this.mixin.getTargets()) { + if (!this.registerInjector(elem, reference, targetMember, target)) { + break; + } } } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java index 72b0bb1b..196651ac 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java index e7afecaf..4851332a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java index 533be5d1..f968cc1d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java index 8b4ff503..f046a752 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java index 3a81138f..9f912900 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java index c60ca134..20a640a8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index 0440abde..9feb7251 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -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 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.getValue(this.annotation, "method"); - if (method == null) { + protected Set parseTargets(String type) { + List methods = Annotations.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 members = new LinkedHashSet(); + 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 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 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 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(); + } + } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java index fe930499..88397ddd 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java @@ -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 * */ -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 *