diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java index e85b9a20..52672d85 100644 --- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java @@ -39,6 +39,7 @@ import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.gen.throwables.InvalidAccessorException; +import org.spongepowered.asm.mixin.injection.selectors.ElementNode; import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.selectors.TargetSelector; @@ -49,7 +50,6 @@ import org.spongepowered.asm.mixin.struct.SpecialMethodInfo; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Bytecode; -import org.spongepowered.asm.util.asm.ElementNode; import com.google.common.base.Joiner; import com.google.common.base.Strings; diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/InvokerInfo.java b/src/main/java/org/spongepowered/asm/mixin/gen/InvokerInfo.java index ed2b4b68..b65d0c15 100644 --- a/src/main/java/org/spongepowered/asm/mixin/gen/InvokerInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/gen/InvokerInfo.java @@ -27,6 +27,7 @@ package org.spongepowered.asm.mixin.gen; import org.objectweb.asm.Type; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.gen.throwables.InvalidAccessorException; +import org.spongepowered.asm.mixin.injection.selectors.ElementNode; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector.Configure; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; @@ -36,7 +37,6 @@ import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.Constants; -import org.spongepowered.asm.util.asm.ElementNode; /** * Information about an invoker diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java b/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java index 50fd27d1..97ec046b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Desc.java @@ -111,6 +111,12 @@ public @interface Desc { */ public Class[] args() default { }; + /** + * The next elements of this descriptor path, evaluated in order for each + * recurse point. + */ + // public Next[] next() default { }; + /** * The minimum number of times this selector should match. By default the * selector is allowed to match no targets. When selecting fields or methods diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 4d976bd8..ea8802ff 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -457,7 +457,7 @@ public class CallbackInjector extends Injector { MethodNode callbackMethod = this.methodNode; if (!callback.checkDescriptor(this.methodNode.desc)) { - if (this.info.getTargets().size() > 1) { + if (this.info.getTargetCount() > 1) { return; // Look for a match in other targets before failing } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java index 23c053c7..9347f3a7 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/InjectorTarget.java @@ -32,6 +32,7 @@ import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfig; import org.spongepowered.asm.mixin.injection.InjectionPoint; +import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.util.Annotations; @@ -50,15 +51,26 @@ public class InjectorTarget { /** * Cache of slices */ - private final Map cache = new HashMap(); + private final Map cache = new HashMap(); /** * Target method data */ private final Target target; + /** + * Selector which selected this method + */ + private final ITargetSelector selector; + + /** + * Name of the mixin which merged the target, if any + */ private final String mergedBy; + /** + * Priority of the mixin which merged the target, if any + */ private final int mergedPriority; /** @@ -67,9 +79,10 @@ public class InjectorTarget { * @param context owner * @param target target */ - public InjectorTarget(ISliceContext context, Target target) { + public InjectorTarget(ISliceContext context, Target target, ITargetSelector selector) { this.context = context; this.target = target; + this.selector = selector; AnnotationNode merged = Annotations.getVisible(target.method, MixinMerged.class); this.mergedBy = Annotations.getValue(merged, "mixin"); @@ -95,6 +108,13 @@ public class InjectorTarget { return this.target.method; } + /** + * Get the selector which selected this target + */ + public ITargetSelector getSelector() { + return this.selector; + } + /** * Get whether this target method was merged by another mixin */ @@ -125,14 +145,14 @@ public class InjectorTarget { * @return insn slice */ public InsnList getSlice(String id) { - ReadOnlyInsnList slice = this.cache.get(id); + InsnListReadOnly slice = this.cache.get(id); if (slice == null) { MethodSlice sliceInfo = this.context.getSlice(id); if (sliceInfo != null) { slice = sliceInfo.getSlice(this.target.method); } else { // No slice exists so just wrap the method insns - slice = new ReadOnlyInsnList(this.target.method.instructions); + slice = new InsnListReadOnly(this.target.method.instructions); } this.cache.put(id, slice); } @@ -154,7 +174,7 @@ public class InjectorTarget { * Dispose all cached instruction lists */ public void dispose() { - for (ReadOnlyInsnList insns : this.cache.values()) { + for (InsnListReadOnly insns : this.cache.values()) { insns.dispose(); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/ReadOnlyInsnList.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListReadOnly.java similarity index 98% rename from src/main/java/org/spongepowered/asm/mixin/injection/code/ReadOnlyInsnList.java rename to src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListReadOnly.java index 528ebac6..70c86574 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/ReadOnlyInsnList.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListReadOnly.java @@ -34,11 +34,11 @@ import org.objectweb.asm.tree.InsnList; * instances so that custom InjectionPoint implementations cannot modify the * insn list whilst inspecting it. */ -class ReadOnlyInsnList extends InsnList { +class InsnListReadOnly extends InsnList { private InsnList insnList; - public ReadOnlyInsnList(InsnList insns) { + public InsnListReadOnly(InsnList insns) { this.insnList = insns; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java index 4de644ca..77b28703 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java @@ -57,7 +57,7 @@ public final class MethodSlice { * identified by start and end to be accessed. In essence * this class provides a view of the underlying InsnList. */ - static final class InsnListSlice extends ReadOnlyInsnList { + static final class InsnListSlice extends InsnListReadOnly { /** * ListIterator for the slice view, wraps an iterator returned by the @@ -359,7 +359,7 @@ public final class MethodSlice { * @param method method to slice * @return read only slice */ - public ReadOnlyInsnList getSlice(MethodNode method) { + public InsnListReadOnly getSlice(MethodNode method) { int max = method.instructions.size() - 1; int start = this.find(method, this.from, 0, 0, this.name + "(from)"); int end = this.find(method, this.to, max, start, this.name + "(to)"); @@ -373,7 +373,7 @@ public final class MethodSlice { } if (start == 0 && end == max) { - return new ReadOnlyInsnList(method.instructions); + return new InsnListReadOnly(method.instructions); } return new InsnListSlice(method.instructions, start, end); @@ -398,7 +398,7 @@ public final class MethodSlice { } Deque nodes = new LinkedList(); - ReadOnlyInsnList insns = new ReadOnlyInsnList(method.instructions); + InsnListReadOnly insns = new InsnListReadOnly(method.instructions); boolean result = injectionPoint.find(method.desc, insns, nodes); Selector select = injectionPoint.getSelector(); if (nodes.size() != 1 && select == Selector.ONE) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ElementNode.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ElementNode.java new file mode 100644 index 00000000..52ac2036 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ElementNode.java @@ -0,0 +1,682 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.mixin.injection.selectors; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.objectweb.asm.Handle; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.InsnList; +import org.objectweb.asm.tree.InvokeDynamicInsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.util.Handles; + +import com.google.common.base.Strings; + +/** + * Wrapper for all node types supported by {@link ITargetSelector target + * selectors} ({@link FieldNode}, {@link MethodNode}, {@link FieldInsnNode}, + * {@link MethodInsnNode} and {@link InvokeDynamicInsnNode}) which allows access + * to common properties of things which are basically "arbitrary node with + * owner, name and descriptor" + * + * @param node type + */ +public abstract class ElementNode { + + /** + * ElementNode for MethodNode + */ + static class ElementNodeMethod extends ElementNode { + + private final ClassNode owner; + + private final MethodNode method; + + ElementNodeMethod(ClassNode owner, MethodNode method) { + this.owner = owner; + this.method = method; + } + + @Override + public boolean isMethod() { + return true; + } + + @Override + public MethodNode getMethod() { + return this.method; + } + + @Override + public String getOwner() { + return this.owner != null ? this.owner.name : null; + } + + @Override + public String getName() { + return this.method.name; + } + + @Override + public String getDesc() { + return this.method.desc; + } + + @Override + public String getSignature() { + return this.method.signature; + } + + @Override + public MethodNode get() { + return this.method; + } + + @Override + public boolean equals(Object obj) { + return this.method.equals(obj); + } + + @Override + public int hashCode() { + return this.method.hashCode(); + } + + } + + /** + * ElementNode for FieldNode + */ + static class ElementNodeField extends ElementNode { + + private final ClassNode owner; + + private final FieldNode field; + + ElementNodeField(ClassNode owner, FieldNode field) { + this.owner = owner; + this.field = field; + } + + @Override + public boolean isField() { + return true; + } + + @Override + public FieldNode getField() { + return this.field; + } + + @Override + public String getOwner() { + return this.owner != null ? this.owner.name : null; + } + + @Override + public String getName() { + return this.field.name; + } + + @Override + public String getDesc() { + return this.field.desc; + } + + @Override + public String getSignature() { + return this.field.signature; + } + + @Override + public FieldNode get() { + return this.field; + } + + @Override + public boolean equals(Object obj) { + return this.field.equals(obj); + } + + @Override + public int hashCode() { + return this.field.hashCode(); + } + + } + + /** + * ElementNode for MethodInsnNode + */ + static class ElementNodeMethodInsn extends ElementNode { + + private MethodInsnNode insn; + + ElementNodeMethodInsn(MethodInsnNode method) { + this.insn = method; + } + + @Override + public boolean isMethod() { + return true; + } + + @Override + public AbstractInsnNode getInsn() { + return this.insn; + } + + @Override + public String getOwner() { + return this.insn.owner; + } + + @Override + public String getName() { + return this.insn.name; + } + + @Override + public String getDesc() { + return this.insn.desc; + } + + @Override + public String getSignature() { + return null; + } + + @Override + public MethodInsnNode get() { + return this.insn; + } + + @Override + public boolean equals(Object obj) { + return this.insn.equals(obj); + } + + @Override + public int hashCode() { + return this.insn.hashCode(); + } + + } + + /** + * ElementNode for InvokeDynamicInsnNode + */ + static class ElementNodeInvokeDynamicInsn extends ElementNode { + + private InvokeDynamicInsnNode insn; + + private Type samMethodType; + + private Handle implMethod; + + private Type instantiatedMethodType; + + ElementNodeInvokeDynamicInsn(InvokeDynamicInsnNode invokeDynamic) { + this.insn = invokeDynamic; + + if (invokeDynamic.bsmArgs != null && invokeDynamic.bsmArgs.length > 1) { + Object samMethodType = invokeDynamic.bsmArgs[0]; + Object implMethod = invokeDynamic.bsmArgs[1]; + Object instantiatedMethodType = invokeDynamic.bsmArgs[2]; + if (samMethodType instanceof Type && implMethod instanceof Handle && instantiatedMethodType instanceof Type) { + this.samMethodType = (Type)samMethodType; + this.implMethod = (Handle)implMethod; + this.instantiatedMethodType = (Type)instantiatedMethodType; + } + } + } + + @Override + public boolean isMethod() { + return this.implMethod == null || !Handles.isField(this.implMethod); + } + + @Override + public AbstractInsnNode getInsn() { + return this.insn; + } + + @Override + public String getOwner() { + return this.implMethod != null ? this.implMethod.getOwner() : this.insn.name; + } + + @Override + public String getName() { + return this.insn.name; + } + + @Override + public String getSyntheticName() { + return this.implMethod != null ? this.implMethod.getName() : this.insn.name; + } + + @Override + public String getDesc() { + return this.implMethod != null ? this.implMethod.getDesc() : this.insn.desc; + } + + @Override + public String getDelegateDesc() { + return this.samMethodType != null ? this.samMethodType.getDescriptor() : this.getDesc(); + } + + @Override + public String getImplDesc() { + return this.instantiatedMethodType != null ? this.instantiatedMethodType.getDescriptor() : this.getDesc(); + } + + @Override + public String getSignature() { + return null; + } + + @Override + public InvokeDynamicInsnNode get() { + return this.insn; + } + + @Override + public boolean equals(Object obj) { + return this.insn.equals(obj); + } + + @Override + public int hashCode() { + return this.insn.hashCode(); + } + + } + + /** + * ElementNode for FieldInsnNode + */ + static class ElementNodeFieldInsn extends ElementNode { + + private FieldInsnNode insn; + + ElementNodeFieldInsn(FieldInsnNode field) { + this.insn = field; + } + + @Override + public boolean isField() { + return true; + } + + @Override + public AbstractInsnNode getInsn() { + return this.insn; + } + + @Override + public String getOwner() { + return this.insn.owner; + } + + @Override + public String getName() { + return this.insn.name; + } + + @Override + public String getDesc() { + return this.insn.desc; + } + + @Override + public String getSignature() { + return null; + } + + @Override + public FieldInsnNode get() { + return this.insn; + } + + @Override + public boolean equals(Object obj) { + return this.insn.equals(obj); + } + + @Override + public int hashCode() { + return this.insn.hashCode(); + } + + } + + /** + * Wrapper iterator for method insns + */ + static class ElementNodeIterator implements Iterator> { + + private final Iterator iter; + + private final boolean filterDynamic; + + ElementNodeIterator(Iterator iter, boolean filterDynamic) { + this.iter = iter; + this.filterDynamic = filterDynamic; + } + + @Override + public boolean hasNext() { + return this.iter.hasNext(); + } + + @Override + public ElementNode next() { + AbstractInsnNode elem = this.iter.next(); + return !this.filterDynamic || (elem != null && elem.getOpcode() == Opcodes.INVOKEDYNAMIC) ? ElementNode.of(elem) : null; + } + + } + + /** + * Wrapper for InsnList which returns node iterator + */ + static class ElementNodeIterable implements Iterable> { + + private final Iterable iterable; + + private final boolean filterDynamic; + + public ElementNodeIterable(Iterable iterable, boolean filterDynamic) { + this.iterable = iterable; + this.filterDynamic = filterDynamic; +} + + @Override + public Iterator> iterator() { + return new ElementNodeIterator(this.iterable.iterator(), this.filterDynamic); + } + + } + + /** + * Get whether this element is a method + */ + public boolean isMethod() { + return false; + } + + /** + * get whether this element is a field + */ + public boolean isField() { + return false; + } + + /** + * get whether this element is an instruction + */ + public boolean isInsn() { + return false; + } + + /** + * Get the {@link MethodNode} if this member is a method, otherwise returns + * null + */ + public MethodNode getMethod() { + return null; + } + + /** + * Get the {@link FieldNode} if this member is a field, otherwise returns + * null + */ + public FieldNode getField() { + return null; + } + + /** + * Get the {@link AbstractInsnNode instruction} if this member is an insn, + * otherwise returns null + */ + public AbstractInsnNode getInsn() { + return null; + } + + /** + * Get the element owner's name, if this element has an owner, otherwise + * returns null + */ + public abstract String getOwner(); + + /** + * Get the element name + */ + public abstract String getName(); + + /** + * Get the synthetic element name. For INVOKEDYNAMIC elements this is the + * real name of the lambda method implementing the delegate. + */ + public String getSyntheticName() { + return this.getName(); + } + + /** + * Get the element descriptor. For INVOKEDYNAMIC this is the full descriptor + * of the lambda (including prepended captures). + */ + public abstract String getDesc(); + + /** + * For INVOKEDYNAMIC elements, returns original descriptor of the delegate. + */ + public String getDelegateDesc() { + return this.getDesc(); + } + + /** + * For INVOKEDYNAMIC elements, returns specialised descriptor of the + * delegate (lambda descriptor without prepended captures), can be the same + * as the delegate descriptor or more specialised. + */ + public String getImplDesc() { + return this.getDesc(); + } + + /** + * Get the element signature, can be null + */ + public abstract String getSignature(); + + /** + * Returns the node with horrible duck typing + */ + public abstract TNode get(); + + @Override + public String toString() { + String desc = Strings.nullToEmpty(this.getDesc()); + if (!desc.isEmpty() && this.isField()) { + desc = ":" + desc; + } + String owner = Strings.nullToEmpty(this.getOwner()); + if (!owner.isEmpty()) { + owner = "L" + owner + ";"; + } + return String.format("%s%%s", owner, Strings.nullToEmpty(this.getName()), desc); + } + + /** + * Create an ElementNode wrapper for the supplied method node + * + * @param owner class which owns the method or null + * @param method Method node to wrap + * @return ElementNode + */ + public static ElementNode of(ClassNode owner, MethodNode method) { + return new ElementNodeMethod(owner, method); + } + + /** + * Create an ElementNode wrapper for the supplied field node + * + * @param owner class which owns the field or null + * @param field Field node to wrap + * @return ElementNode + */ + public static ElementNode of(ClassNode owner, FieldNode field) { + return new ElementNodeField(owner, field); + } + + /** + * Create an ElementNode wrapper for the supplied node object + * + * @param owner class which owns the node or null + * @param node Node to wrap + * @param Node type + * @return ElementNode + * @throws IllegalArgumentException if the supplied argument is not a + * {@link MethodNode} or {@link FieldNode} + */ + @SuppressWarnings("unchecked") + public static ElementNode of(ClassNode owner, TNode node) { + if (node instanceof ElementNode) { + return (ElementNode)node; + } else if (node instanceof MethodNode) { + return (ElementNode)new ElementNodeMethod(owner, (MethodNode)node); + } else if (node instanceof FieldNode) { + return (ElementNode)new ElementNodeField(owner, (FieldNode)node); + } else if (node instanceof MethodInsnNode) { + return (ElementNode)new ElementNodeMethodInsn((MethodInsnNode)node); + } else if (node instanceof InvokeDynamicInsnNode) { + return (ElementNode)new ElementNodeInvokeDynamicInsn((InvokeDynamicInsnNode)node); + } else if (node instanceof FieldInsnNode) { + return (ElementNode)new ElementNodeFieldInsn((FieldInsnNode)node); + } + throw new IllegalArgumentException("Could not create ElementNode for unknown node type: " + node.getClass().getName()); + } + + /** + * Create an ElementNode wrapper for the supplied node object + * + * @param node Node to wrap + * @param Node type + * @return ElementNode + * @throws IllegalArgumentException if the supplied argument is not a + * {@link MethodNode} or {@link FieldNode} + */ + @SuppressWarnings("unchecked") + public static ElementNode of(TNode node) { + if (node instanceof MethodInsnNode) { + return (ElementNode)new ElementNodeMethodInsn((MethodInsnNode)node); + } else if (node instanceof InvokeDynamicInsnNode) { + return (ElementNode)new ElementNodeInvokeDynamicInsn((InvokeDynamicInsnNode)node); + } else if (node instanceof FieldInsnNode) { + return (ElementNode)new ElementNodeFieldInsn((FieldInsnNode)node); + } + return null; + } + + /** + * Convert the supplied list of nodes to a list of wrapped ElementNodes + * + * @param owner Owner of the supplied nodes, can be null + * @param list List of nodes + * @param Node type + * @return List of wrapped nodes + */ + public static List> listOf(ClassNode owner, List list) { + List> nodes = new ArrayList>(); + for (TNode node : list) { + nodes.add(ElementNode.of(owner, node)); + } + return nodes; + } + + /** + * Get a list of wrapped ElementNodes for the fields of the supplied owner + * class + * + * @param owner Class to get fields, must not be null + * @return List of wrapped nodes + */ + public static List> fieldList(ClassNode owner) { + List> fields = new ArrayList>(); + for (FieldNode field : owner.fields) { + fields.add(new ElementNodeField(owner, field)); + } + return fields; + } + + /** + * Get a list of wrapped ElementNodes for the methods of the supplied owner + * class + * + * @param owner Class to get methods, must not be null + * @return List of wrapped nodes + */ + public static List> methodList(ClassNode owner) { + List> methods = new ArrayList>(); + for (MethodNode method : owner.methods) { + methods.add(new ElementNodeMethod(owner, method)); + } + return methods; + } + + /** + * Get a wrapped version of the supplied insn list which returns element + * nodes for each (supported) instruction (FieldInsnNode, MethodInsnNode and + * InvokeDynamicInsnNode). + * + * @param insns Insn list to wrap + * @return Wrapper for insn list + */ + public static Iterable> insnList(InsnList insns) { + return new ElementNodeIterable(insns, false); + } + + /** + * Get a wrapped version of the supplied insn list which returns element + * nodes for every INVOKEDYNAMIC instruction only + * + * @param insns Insn list to wrap + * @return Wrapper for insn list + */ + public static Iterable> dynamicInsnList(InsnList insns) { + return new ElementNodeIterable(insns, true); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ITargetSelector.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ITargetSelector.java index 642a41ab..00e62ead 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ITargetSelector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/ITargetSelector.java @@ -28,7 +28,6 @@ import org.objectweb.asm.tree.AbstractInsnNode; import org.spongepowered.asm.mixin.injection.Desc; import org.spongepowered.asm.mixin.injection.selectors.dynamic.DynamicSelectorDesc; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; -import org.spongepowered.asm.util.asm.ElementNode; /** * Target Selectors are used to identify target members in a class, with the diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/InvalidSelector.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/InvalidSelector.java index 42feb510..33238a66 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/InvalidSelector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/InvalidSelector.java @@ -25,7 +25,6 @@ package org.spongepowered.asm.mixin.injection.selectors; import org.objectweb.asm.tree.AbstractInsnNode; -import org.spongepowered.asm.util.asm.ElementNode; /** * Since the contract of {@link TargetSelector#parse} prohibits returing diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/MemberMatcher.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/MemberMatcher.java index f3d77f03..25f331de 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/MemberMatcher.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/MemberMatcher.java @@ -31,7 +31,6 @@ import java.util.regex.PatternSyntaxException; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.FieldInsnNode; import org.objectweb.asm.tree.MethodInsnNode; -import org.spongepowered.asm.util.asm.ElementNode; /** * A {@link ITargetSelector Target Selector} which matches an element of @@ -231,7 +230,7 @@ public final class MemberMatcher implements ITargetSelector { */ @Override public MatchResult match(ElementNode node) { - return node == null ? MatchResult.NONE : this.matches(node.getOwnerName(), node.getName(), node.getDesc()); + return node == null ? MatchResult.NONE : this.matches(node.getOwner(), node.getName(), node.getDesc()); } /* (non-Javadoc) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java index f5a6815c..2d7e61ce 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/TargetSelector.java @@ -29,6 +29,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -50,7 +51,6 @@ import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.mixin.throwables.MixinError; import org.spongepowered.asm.mixin.throwables.MixinException; import org.spongepowered.asm.util.Annotations; -import org.spongepowered.asm.util.asm.ElementNode; import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.asm.util.logging.MessageRouter; @@ -71,14 +71,14 @@ public final class TargetSelector { /** * Any exact match returned by the query */ - public final TNode exactMatch; + public final ElementNode exactMatch; /** * All candidates returned by the query */ - public final List candidates; + public final List> candidates; - Result(TNode exactMatch, List candidates) { + Result(ElementNode exactMatch, List> candidates) { this.exactMatch = exactMatch; this.candidates = candidates; } @@ -98,10 +98,10 @@ public final class TargetSelector { public TNode getSingleResult(boolean strict) { int resultCount = this.candidates.size(); if (this.exactMatch != null) { - return this.exactMatch; + return this.exactMatch.get(); } if (resultCount == 1 || !strict) { - return this.candidates.get(0); + return this.candidates.get(0).get(); } throw new IllegalStateException((resultCount == 0 ? "No" : "Multiple") + " candidates were found"); } @@ -456,9 +456,9 @@ public final class TargetSelector { * @param Node type * @return query result */ - public static Result run(ITargetSelector selector, List> nodes) { - List candidates = new ArrayList(); - TNode exactMatch = TargetSelector.runSelector(selector, nodes, candidates); + public static Result run(ITargetSelector selector, Iterable> nodes) { + List> candidates = new ArrayList>(); + ElementNode exactMatch = TargetSelector.runSelector(selector, nodes, candidates); return new Result(exactMatch, candidates); } @@ -470,12 +470,12 @@ public final class TargetSelector { * @param Node type * @return query result */ - public static Result run(Iterable selector, List> nodes) { - TNode exactMatch = null; - List candidates = new ArrayList(); + public static Result run(Iterable selector, Iterable> nodes) { + ElementNode exactMatch = null; + List> candidates = new ArrayList>(); for (ITargetSelector target : selector) { - TNode selectorExactMatch = TargetSelector.runSelector(target, nodes, candidates); + ElementNode selectorExactMatch = TargetSelector.runSelector(target, nodes, candidates); if (exactMatch == null) { exactMatch = selectorExactMatch; } @@ -484,22 +484,23 @@ public final class TargetSelector { return new Result(exactMatch, candidates); } - private static TNode runSelector(ITargetSelector selector, List> nodes, List candidates) { + private static ElementNode runSelector(ITargetSelector selector, Iterable> nodes, + List> candidates) { int matchCount = 0; - TNode exactMatch = null; - for (ElementNode element : nodes) { + ElementNode exactMatch = null; + for (Iterator> iterator = nodes.iterator(); iterator.hasNext();) { + ElementNode element = iterator.next(); MatchResult match = selector.match(element); if (match.isMatch()) { matchCount++; if (matchCount > selector.getMaxMatchCount()) { break; } - TNode node = element.get(); - if (!candidates.contains(node)) { - candidates.add(node); + if (!candidates.contains(element)) { + candidates.add(element); } if (exactMatch == null && match.isExactMatch()) { - exactMatch = node; + exactMatch = element; } } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java index 8257c664..cd8ff753 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DescriptorResolver.java @@ -121,7 +121,10 @@ public final class DescriptorResolver { return Type.VOID_TYPE; } Type ownerClass = this.desc.getTypeValue("owner"); - return ownerClass == Type.VOID_TYPE ? Type.getObjectType(this.context.getMixin().getTargetClassRef()) : ownerClass; + if (ownerClass != Type.VOID_TYPE) { + return ownerClass; + } + return this.context != null ? Type.getObjectType(this.context.getMixin().getTargetClassRef()) : ownerClass; } @Override @@ -164,6 +167,11 @@ public final class DescriptorResolver { return new Quantifier(min, max != null ? (max > 0 ? max : Integer.MAX_VALUE) : -1); } + @Override + public List getNext() { + return this.desc != null ? this.desc.getAnnotationList("next") : Collections.emptyList(); + } + } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java index 7175cab2..e3f48c9b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/DynamicSelectorDesc.java @@ -24,6 +24,8 @@ */ package org.spongepowered.asm.mixin.injection.selectors.dynamic; +import java.util.List; + import org.objectweb.asm.Type; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.FieldInsnNode; @@ -32,6 +34,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Desc; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Slice; +import org.spongepowered.asm.mixin.injection.selectors.ElementNode; import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; @@ -43,7 +46,6 @@ import org.spongepowered.asm.mixin.injection.selectors.MatchResult; import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.Quantifier; import org.spongepowered.asm.util.SignaturePrinter; -import org.spongepowered.asm.util.asm.ElementNode; import org.spongepowered.asm.util.asm.IAnnotationHandle; import com.google.common.base.Strings; @@ -168,7 +170,26 @@ import com.google.common.base.Strings; */ @SelectorId("Desc") @SelectorAnnotation(Desc.class) -public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITargetSelectorByName { +public class DynamicSelectorDesc implements ITargetSelectorDynamic, ITargetSelectorByName { + + /** + * Next selector + */ + final class Next extends DynamicSelectorDesc { + + private final int index; + + Next(int index, IResolvedDescriptor next) { + super(null, null, next.getOwner(), next.getName(), next.getArgs(), next.getReturnType(), next.getMatches(), null); + this.index = index; + } + + @Override + public ITargetSelector next() { + return DynamicSelectorDesc.this.next(this.index + 1); + } + + } /** * Parser/resolver error mesage, only stored if the descriptor is invalid so @@ -216,28 +237,40 @@ public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITarge */ private final Quantifier matches; + /** + * Annotation for next, parsed on demand + */ + private final List next; + private DynamicSelectorDesc(IResolvedDescriptor desc) { - this(null, desc.getId(), desc.getOwner(), desc.getName(), desc.getArgs(), desc.getReturnType(), desc.getMatches()); + this(null, desc.getId(), desc.getOwner(), desc.getName(), desc.getArgs(), desc.getReturnType(), desc.getMatches(), + desc.getNext()); } private DynamicSelectorDesc(DynamicSelectorDesc desc, Quantifier quantifier) { - this(desc.parseException, desc.id, desc.owner, desc.name, desc.args, desc.returnType, quantifier); + this(desc.parseException, desc.id, desc.owner, desc.name, desc.args, desc.returnType, quantifier, desc.next); + } + + private DynamicSelectorDesc(DynamicSelectorDesc desc, Type owner) { + this(desc.parseException, desc.id, owner, desc.name, desc.args, desc.returnType, desc.matches, desc.next); } private DynamicSelectorDesc(InvalidSelectorException ex) { - this(ex, null, null, null, null, null, Quantifier.NONE); + this(ex, null, null, null, null, null, Quantifier.NONE, null); } - private DynamicSelectorDesc(InvalidSelectorException ex, String id, Type owner, String name, Type[] args, Type returnType, Quantifier matches) { + protected DynamicSelectorDesc(InvalidSelectorException ex, String id, Type owner, String name, Type[] args, Type returnType, Quantifier matches, + List then) { this.parseException = ex; this.id = id; this.owner = owner; - this.name = name; + this.name = Strings.emptyToNull(name); this.args = args; this.returnType = returnType; this.methodDesc = returnType != null ? Bytecode.getDescriptor(returnType, args) : null; this.matches = matches; + this.next = then; } /** @@ -333,7 +366,9 @@ public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITarge if (started) { sb.append(", "); } - sb.append("name = \"").append(this.name).append("\""); + if (this.name != null) { + sb.append("value = \"").append(this.name).append("\""); + } if (this.args.length > 0) { sb.append(", args = { "); @@ -396,9 +431,19 @@ public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITarge @Override public ITargetSelector next() { - return this; // Recurse + return this.next(0); } + protected ITargetSelector next(int index) { + if (index >= 0 && index < this.next.size()) { + IAnnotationHandle nextAnnotation = this.next.get(index); + IResolvedDescriptor descriptor = DescriptorResolver.resolve(nextAnnotation, null); + return new Next(index, descriptor); + } + + return null; + } + @Override public ITargetSelector configure(Configure request, String... args) { request.checkArgs(args); @@ -414,12 +459,10 @@ public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITarge } break; case MOVE: - return new DynamicSelectorDesc(this.parseException, this.id, Type.getObjectType(args[0]), this.name, this.args, this.returnType, - this.matches); + return new DynamicSelectorDesc(this, Type.getObjectType(args[0])); case CLEAR_LIMITS: if (this.getMinMatchCount() != 0 || this.getMaxMatchCount() < Integer.MAX_VALUE) { - return new DynamicSelectorDesc(this.parseException, this.id, Type.getObjectType(args[0]), this.name, this.args, this.returnType, - Quantifier.ANY); + return new DynamicSelectorDesc(this, Quantifier.ANY); } break; default: @@ -457,9 +500,9 @@ public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITarge if (node == null) { return MatchResult.NONE; } else if (node.isMethod()) { - return this.matches(node.getOwnerName(), node.getName(), node.getDesc(), this.methodDesc); + return this.matches(node.getOwner(), node.getName(), node.getDesc(), this.methodDesc); } else { - return this.matches(node.getOwnerName(), node.getName(), node.getDesc(), this.returnType.getInternalName()); + return this.matches(node.getOwner(), node.getName(), node.getDesc(), this.returnType.getInternalName()); } } @@ -482,12 +525,14 @@ public final class DynamicSelectorDesc implements ITargetSelectorDynamic, ITarge private MatchResult matches(String owner, String name, String desc, String compareWithDesc) { if (!compareWithDesc.equals(desc)) { return MatchResult.NONE; - } else if (!this.owner.getInternalName().equals(owner)) { + } else if (this.owner != Type.VOID_TYPE && !this.owner.getInternalName().equals(owner)) { return MatchResult.NONE; - } else if (this.name.equals(name)) { + } else if (this.name != null && this.name.equals(name)) { return MatchResult.EXACT_MATCH; - } else if (this.name.equalsIgnoreCase(name)) { + } else if (this.name != null && this.name.equalsIgnoreCase(name)) { return MatchResult.MATCH; + } else if (this.name == null) { + return MatchResult.EXACT_MATCH; } return MatchResult.NONE; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/IResolvedDescriptor.java b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/IResolvedDescriptor.java index c6a99687..39a0860f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/IResolvedDescriptor.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/selectors/dynamic/IResolvedDescriptor.java @@ -24,6 +24,8 @@ */ package org.spongepowered.asm.mixin.injection.selectors.dynamic; +import java.util.List; + import org.objectweb.asm.Type; import org.spongepowered.asm.util.Quantifier; import org.spongepowered.asm.util.asm.IAnnotationHandle; @@ -33,7 +35,7 @@ import org.spongepowered.asm.util.asm.IAnnotationHandle; * descriptor was not resolved then {@link #isResolved} returns false. */ public interface IResolvedDescriptor { - + /** * Get whether the descriptor was successfully resolved */ @@ -83,5 +85,10 @@ public interface IResolvedDescriptor { * Get the specified matches values from the resolved descriptor. */ public abstract Quantifier getMatches(); + + /** + * Get the values specified for next, if any + */ + public abstract List getNext(); } 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 428d436e..8e233442 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 @@ -30,10 +30,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; -import java.util.Deque; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -58,6 +56,7 @@ import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.code.InjectorTarget; import org.spongepowered.asm.mixin.injection.code.MethodSlice; import org.spongepowered.asm.mixin.injection.code.MethodSlices; +import org.spongepowered.asm.mixin.injection.selectors.ElementNode; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector.Configure; import org.spongepowered.asm.mixin.injection.selectors.InvalidSelectorException; @@ -77,7 +76,6 @@ import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.asm.ASM; -import org.spongepowered.asm.util.asm.ElementNode; import org.spongepowered.asm.util.asm.MethodNodeEx; import org.spongepowered.asm.util.logging.MessageRouter; @@ -162,6 +160,33 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } } + /** + * Selected target, paired with the selector which identified it + */ + static class SelectedTarget { + + private final ITargetSelector root; + + final ITargetSelector selector; + + final MethodNode method; + + SelectedTarget(ITargetSelector root, ITargetSelector selector, MethodNode method) { + this.root = root; + this.selector = selector; + this.method = method; + } + + SelectedTarget(ITargetSelector selector, MethodNode method) { + this(null, selector, method); + } + + ITargetSelector getRoot() { + return this.root != null ? this.root : this.selector; + } + + } + /** * Default conform prefix for handler methods */ @@ -194,10 +219,15 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC */ protected final boolean isStatic; + /** + * Target selector(s) + */ + protected final Set selectors = new LinkedHashSet(); + /** * Target method(s) */ - protected final Deque targets = new ArrayDeque(); + protected final List targets = new ArrayList(); /** * Method slice descriptors parsed from the annotation @@ -296,12 +326,13 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC List injectionPoints = this.readInjectionPoints(); this.parseRequirements(); - this.findMethods(this.parseTargets()); + this.parseSelectors(); + this.findTargets(); this.parseInjectionPoints(injectionPoints); this.injector = this.parseInjector(this.annotation); } - protected Set parseTargets() { + protected void parseSelectors() { Set selectors = new LinkedHashSet(); TargetSelector.parse(Annotations.getValue(this.annotation, "method", false), this, selectors); TargetSelector.parse(Annotations.getValue(this.annotation, "target", false), this, selectors); @@ -312,12 +343,10 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC this.annotationType, this.methodName)); } - Set attachedSelectors = new LinkedHashSet(); - // Validate and attach the parsed selectors for (ITargetSelector selector : selectors) { try { - attachedSelectors.add(selector.validate().attach(this)); + this.selectors.add(selector.validate().attach(this)); } catch (InvalidMemberDescriptorException ex) { throw new InvalidInjectionException(this, String.format("%s annotation on %s, has invalid target descriptor: %s. %s", this.annotationType, this.methodName, ex.getMessage(), this.mixin.getReferenceMapper().getStatus())); @@ -331,8 +360,6 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC ex.getMessage())); } } - - return attachedSelectors; } protected List readInjectionPoints() { @@ -387,9 +414,9 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC */ public void prepare() { this.targetNodes.clear(); - for (MethodNode targetMethod : this.targets) { - Target target = this.mixin.getTargetMethod(targetMethod); - InjectorTarget injectorTarget = new InjectorTarget(this, target); + for (SelectedTarget targetMethod : this.targets) { + Target target = this.mixin.getTargetMethod(targetMethod.method); + InjectorTarget injectorTarget = new InjectorTarget(this, target, targetMethod.selector); try { this.targetNodes.put(target, this.injector.find(injectorTarget, this.injectionPoints)); } catch (SelectorException ex) { @@ -459,12 +486,12 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } /** - * Get methods being injected into + * Get number of methods being injected into * - * @return methods being injected into + * @return count of methods being injected into */ - public Collection getTargets() { - return this.targets; + public int getTargetCount() { + return this.targets.size(); } /** @@ -543,20 +570,26 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } /** - * Finds methods in the target class which match searchFor - * - * @param selectors members to search for + * Find methods in the target class which match the parsed selectors */ - private void findMethods(Set selectors) { + protected void findTargets() { this.targets.clear(); + this.findRootTargets(); + this.validateTargets(); + } + /** + * Evaluate the root selectors parsed from this injector, find the root + * targets and store them in the {@link #targets} collection. + */ + private void findRootTargets() { // When remapping refmap is enabled this implies we are in a development // environment. In certain circumstances including the descriptor for // the method may actually fail, so we will do a second pass without the // descriptor if this happens. int passes = this.mixin.getOption(Option.REFMAP_REMAP) ? 2 : 1; - for (ITargetSelector selector : selectors) { + for (ITargetSelector selector : this.selectors) { selector = selector.configure(Configure.SELECT_MEMBER); int matchCount = 0; @@ -564,12 +597,9 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC // Second pass ignores descriptor ITargetSelector permissiveSelector = selector.configure(Configure.PERMISSIVE); - if (permissiveSelector == selector) { - // Selector is already permissive, so skip the second pass - passes = 1; - } + int selectorPasses = (permissiveSelector == selector) ? 1 : passes; - scan: for (int pass = 0; pass < passes && matchCount < 1; pass++) { + scan: for (int pass = 0; pass < selectorPasses && matchCount < 1; pass++) { ITargetSelector passSelector = pass == 0 ? selector : permissiveSelector; for (MethodNode target : this.classNode.methods) { if (passSelector.match(ElementNode.of(this.classNode, target)).isExactMatch()) { @@ -578,7 +608,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC boolean isMixinMethod = Annotations.getVisible(target, MixinMerged.class) != null; if (maxCount <= 1 || ((this.isStatic || !Bytecode.isStatic(target)) && target != this.method && !isMixinMethod)) { this.checkTarget(target); - this.targets.add(target); + this.targets.add(new SelectedTarget(passSelector, target)); } if (matchCount >= maxCount) { @@ -595,7 +625,13 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC this.mixin.getReferenceMapper().getStatus(), this.getDynamicInfo()))); } } - + } + + /** + * Post-search validation that some targets were found, we can fail-fast if + * no targets were actually identified + */ + protected void validateTargets() { this.targetCount = this.targets.size(); if (this.targetCount > 0) { return; @@ -604,17 +640,17 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC if ((this.mixin.getOption(Option.DEBUG_INJECTORS) && this.expectedCallbackCount > 0)) { throw new InvalidInjectionException(this, String.format("Injection validation failed: %s annotation on %s could not find any targets matching %s in %s. %s%s", - this.annotationType, this.methodName, InjectionInfo.namesOf(selectors), this.mixin.getTarget(), + this.annotationType, this.methodName, InjectionInfo.namesOf(this.selectors), this.mixin.getTarget(), this.mixin.getReferenceMapper().getStatus(), this.getDynamicInfo())); } else if (this.requiredCallbackCount > 0) { throw new InvalidInjectionException(this, String.format("Critical injection failure: %s annotation on %s could not find any targets matching %s in %s. %s%s", - this.annotationType, this.methodName, InjectionInfo.namesOf(selectors), this.mixin.getTarget(), + this.annotationType, this.methodName, InjectionInfo.namesOf(this.selectors), this.mixin.getTarget(), this.mixin.getReferenceMapper().getStatus(), this.getDynamicInfo())); } } - private void checkTarget(MethodNode target) { + protected void checkTarget(MethodNode target) { AnnotationNode merged = Annotations.getVisible(target, MixinMerged.class); if (merged == null) { return; @@ -641,7 +677,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } return description.length() > 0 ? String.format(" Method is @Dynamic(%s).", description) : ""; } - + /** * Parse an injector from the specified method (if an injector annotation is * present). If no injector annotation is present then null is @@ -733,7 +769,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } return sb.toString(); } - + /** * Register an injector info class. The supplied class must be decorated * with an {@link AnnotationType} annotation for registration purposes. 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 36de13e2..ee629a16 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 @@ -28,6 +28,7 @@ import org.objectweb.asm.Type; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.FieldInsnNode; import org.objectweb.asm.tree.MethodInsnNode; +import org.spongepowered.asm.mixin.injection.selectors.ElementNode; import org.spongepowered.asm.mixin.injection.selectors.ISelectorContext; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelector; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; @@ -43,7 +44,6 @@ import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.Quantifier; import org.spongepowered.asm.util.SignaturePrinter; -import org.spongepowered.asm.util.asm.ElementNode; import com.google.common.base.Objects; import com.google.common.base.Strings; @@ -216,6 +216,11 @@ import com.google.common.base.Strings; */ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelectorConstructor { + /** + * Separator for elements in the path + */ + private static final String ARROW = "->"; + /** * Member owner in internal form but without L;, can be null */ @@ -246,6 +251,11 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec */ private final String input; + /** + * The actual String value passed into the {@link #parse} method + */ + private final String tail; + /** * ctor * @@ -253,18 +263,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec * @param matches Quantifier specifying the number of matches required */ public MemberInfo(String name, Quantifier matches) { - this(name, null, null, matches, null); - } - - /** - * ctor - * - * @param name Member name, must not be null - * @param minMatches Min number of matches required - * @param maxMatches Max number of matches allowed - */ - public MemberInfo(String name, int minMatches, int maxMatches) { - this(name, null, null, new Quantifier(minMatches, maxMatches), null); + this(name, null, null, matches, null, null); } /** @@ -276,20 +275,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec * @param matches Quantifier specifying the number of matches required */ public MemberInfo(String name, String owner, Quantifier matches) { - this(name, owner, null, matches, null); - } - - /** - * ctor - * - * @param name Member name, must not be null - * @param owner Member owner, can be null otherwise must be in internal form - * without L; - * @param minMatches Min number of matches required - * @param maxMatches Max number of matches allowed - */ - public MemberInfo(String name, String owner, int minMatches, int maxMatches) { - this(name, owner, null, new Quantifier(minMatches, maxMatches), null); + this(name, owner, null, matches, null, null); } /** @@ -301,7 +287,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec * @param desc Member descriptor, can be null */ public MemberInfo(String name, String owner, String desc) { - this(name, owner, desc, Quantifier.DEFAULT, null); + this(name, owner, desc, Quantifier.DEFAULT, null, null); } /** @@ -314,35 +300,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec * @param matches Quantifier specifying the number of matches required */ public MemberInfo(String name, String owner, String desc, Quantifier matches) { - this(name, owner, desc, matches, null); - } - - /** - * ctor - * - * @param name Member name, must not be null - * @param owner Member owner, can be null otherwise must be in internal form - * without L; - * @param desc Member descriptor, can be null - * @param minMatches Min number of matches required - * @param maxMatches Max number of matches allowed - */ - public MemberInfo(String name, String owner, String desc, int minMatches, int maxMatches) { - this(name, owner, desc, new Quantifier(minMatches, maxMatches), null); - } - - /** - * ctor - * - * @param name Member name, must not be null - * @param owner Member owner, can be null otherwise must be in internal form - * without L; - * @param desc Member descriptor, can be null - * @param minMatches Min number of matches required - * @param maxMatches Max number of matches allowed - */ - public MemberInfo(String name, String owner, String desc, int minMatches, int maxMatches, String input) { - this(name, owner, desc, new Quantifier(minMatches, maxMatches), input); + this(name, owner, desc, matches, null, null); } /** @@ -354,7 +312,20 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec * @param desc Member descriptor, can be null * @param matches Quantifier specifying the number of matches required */ - public MemberInfo(String name, String owner, String desc, Quantifier matches, String input) { + public MemberInfo(String name, String owner, String desc, Quantifier matches, String tail) { + this(name, owner, desc, matches, tail, null); + } + + /** + * ctor + * + * @param name Member name, must not be null + * @param owner Member owner, can be null otherwise must be in internal form + * without L; + * @param desc Member descriptor, can be null + * @param matches Quantifier specifying the number of matches required + */ + public MemberInfo(String name, String owner, String desc, Quantifier matches, String tail, String input) { if (owner != null && owner.contains(".")) { throw new IllegalArgumentException("Attempt to instance a MemberInfo with an invalid owner format"); } @@ -364,6 +335,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec this.desc = desc; this.matches = matches; this.forceField = false; + this.tail = tail; this.input = input; } @@ -377,6 +349,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec this.matches = Quantifier.DEFAULT; this.forceField = false; this.input = null; + this.tail = null; if (insn instanceof MethodInsnNode) { MethodInsnNode methodNode = (MethodInsnNode) insn; @@ -404,6 +377,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec this.desc = mapping.getDesc(); this.matches = Quantifier.SINGLE; this.forceField = mapping.getType() == IMapping.Type.FIELD; + this.tail = null; this.input = null; } @@ -418,6 +392,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec this.desc = method.getDesc(); this.matches = remapped.matches; this.forceField = false; + this.tail = null; this.input = null; } @@ -433,12 +408,13 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec this.desc = original.desc; this.matches = original.matches; this.forceField = original.forceField; + this.tail = original.tail; this.input = null; } @Override public ITargetSelector next() { - return null; // Not supported yet + return Strings.isNullOrEmpty(this.tail) ? null : MemberInfo.parse(this.tail, null); } @Override @@ -476,7 +452,8 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec String quantifier = this.matches.toString(); String desc = this.desc != null ? this.desc : ""; String separator = desc.startsWith("(") ? "" : (this.desc != null ? ":" : ""); - return owner + name + quantifier + separator + desc; + String tail = this.tail != null ? " " + MemberInfo.ARROW + " " + this.tail : ""; + return owner + name + quantifier + separator + desc + tail; } /** @@ -718,7 +695,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec */ @Override public MatchResult match(ElementNode node) { - return node == null ? MatchResult.NONE : this.matches(node.getOwnerName(), node.getName(), node.getDesc()); + return node == null ? MatchResult.NONE : this.matches(node.getOwner(), node.getName(), node.getDesc()); } /* (non-Javadoc) @@ -808,12 +785,12 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec switch (request) { case SELECT_MEMBER: if (this.matches.isDefault()) { - return new MemberInfo(this.name, this.owner, this.desc, Quantifier.SINGLE); + return new MemberInfo(this.name, this.owner, this.desc, Quantifier.SINGLE, this.tail); } break; case SELECT_INSTRUCTION: if (this.matches.isDefault()) { - return new MemberInfo(this.name, this.owner, this.desc, Quantifier.ANY); + return new MemberInfo(this.name, this.owner, this.desc, Quantifier.ANY, this.tail); } break; case MOVE: @@ -826,7 +803,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec return this.transform(null); case CLEAR_LIMITS: if (this.matches.getMin() != 0 || this.matches.getMax() < Integer.MAX_VALUE) { - return new MemberInfo(this.name, this.owner, this.desc, 0, Integer.MAX_VALUE); + return new MemberInfo(this.name, this.owner, this.desc, Quantifier.ANY, this.tail); } break; default: @@ -896,6 +873,13 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec String desc = null; String owner = null; String name = Strings.nullToEmpty(input).replaceAll("\\s", ""); + String tail = null; + + int arrowPos = name.indexOf(MemberInfo.ARROW); + if (arrowPos > -1) { + tail = name.substring(arrowPos + 2); + name = name.substring(0, arrowPos); + } if (context != null) { name = context.remap(name); @@ -959,7 +943,7 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec name = null; } - return new MemberInfo(name, owner, desc, quantifier, input); + return new MemberInfo(name, owner, desc, quantifier, tail, input); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/struct/MemberRef.java b/src/main/java/org/spongepowered/asm/mixin/struct/MemberRef.java index b8baec32..f2461fb7 100644 --- a/src/main/java/org/spongepowered/asm/mixin/struct/MemberRef.java +++ b/src/main/java/org/spongepowered/asm/mixin/struct/MemberRef.java @@ -29,12 +29,13 @@ import org.objectweb.asm.tree.FieldInsnNode; import org.objectweb.asm.tree.MethodInsnNode; import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError; import org.spongepowered.asm.util.Bytecode; +import org.spongepowered.asm.util.Handles; /** * Reference to a field or method that also includes invocation instructions. * - *

To instances are defined to be equal if they both refer to the same method - * and have the same invocation instructions.

+ *

Two instances are defined to be equal if they both refer to the same + * member and have the same invocation instructions.

*/ public abstract class MemberRef { @@ -209,26 +210,12 @@ public abstract class MemberRef { @Override public boolean isField() { - switch (this.handle.getTag()) { - case Opcodes.H_INVOKEVIRTUAL: - case Opcodes.H_INVOKESTATIC: - case Opcodes.H_INVOKEINTERFACE: - case Opcodes.H_INVOKESPECIAL: - case Opcodes.H_NEWINVOKESPECIAL: - return false; - case Opcodes.H_GETFIELD: - case Opcodes.H_GETSTATIC: - case Opcodes.H_PUTFIELD: - case Opcodes.H_PUTSTATIC: - return true; - default: - throw new MixinTransformerError("Invalid tag " + this.handle.getTag() + " for method handle " + this.handle + "."); - } + return Handles.isField(this.handle); } @Override public int getOpcode() { - int opcode = MemberRef.opcodeFromTag(this.handle.getTag()); + int opcode = Handles.opcodeFromTag(this.handle.getTag()); if (opcode == 0) { throw new MixinTransformerError("Invalid tag " + this.handle.getTag() + " for method handle " + this.handle + "."); } @@ -237,12 +224,11 @@ public abstract class MemberRef { @Override public void setOpcode(int opcode) { - int tag = MemberRef.tagFromOpcode(opcode); + int tag = Handles.tagFromOpcode(opcode); if (tag == 0) { throw new MixinTransformerError("Invalid opcode " + Bytecode.getOpcodeName(opcode) + " for method handle " + this.handle + "."); } - boolean itf = tag == Opcodes.H_INVOKEINTERFACE; - this.handle = new org.objectweb.asm.Handle(tag, this.handle.getOwner(), this.handle.getName(), this.handle.getDesc(), itf); + this.setHandle(tag, this.handle.getOwner(), this.handle.getName(), this.handle.getDesc(), this.handle.isInterface()); } @Override @@ -252,8 +238,7 @@ public abstract class MemberRef { @Override public void setOwner(String owner) { - boolean itf = this.handle.getTag() == Opcodes.H_INVOKEINTERFACE; - this.handle = new org.objectweb.asm.Handle(this.handle.getTag(), owner, this.handle.getName(), this.handle.getDesc(), itf); + this.setHandle(this.handle.getTag(), owner, this.handle.getName(), this.handle.getDesc(), this.handle.isInterface()); } @Override @@ -263,8 +248,7 @@ public abstract class MemberRef { @Override public void setName(String name) { - boolean itf = this.handle.getTag() == Opcodes.H_INVOKEINTERFACE; - this.handle = new org.objectweb.asm.Handle(this.handle.getTag(), this.handle.getOwner(), name, this.handle.getDesc(), itf); + this.setHandle(this.handle.getTag(), this.handle.getOwner(), name, this.handle.getDesc(), this.handle.isInterface()); } @Override @@ -274,23 +258,14 @@ public abstract class MemberRef { @Override public void setDesc(String desc) { - boolean itf = this.handle.getTag() == Opcodes.H_INVOKEINTERFACE; - this.handle = new org.objectweb.asm.Handle(this.handle.getTag(), this.handle.getOwner(), this.handle.getName(), desc, itf); + this.setHandle(this.handle.getTag(), this.handle.getOwner(), this.handle.getName(), desc, this.handle.isInterface()); } + + public void setHandle(int tag, String owner, String name, String desc, boolean isInterface) { + this.handle = new org.objectweb.asm.Handle(tag, owner, name, desc, isInterface); + } + } - - private static final int[] H_OPCODES = { - 0, // invalid - Opcodes.GETFIELD, // H_GETFIELD - Opcodes.GETSTATIC, // H_GETSTATIC - Opcodes.PUTFIELD, // H_PUTFIELD - Opcodes.PUTSTATIC, // H_PUTSTATIC - Opcodes.INVOKEVIRTUAL, // H_INVOKEVIRTUAL - Opcodes.INVOKESTATIC, // H_INVOKESTATIC - Opcodes.INVOKESPECIAL, // H_INVOKESPECIAL - Opcodes.INVOKESPECIAL, // H_NEWINVOKESPECIAL - Opcodes.INVOKEINTERFACE // H_INVOKEINTERFACE - }; /** * Whether this member is a field. @@ -357,8 +332,8 @@ public abstract class MemberRef { @Override public String toString() { - String name = Bytecode.getOpcodeName(this.getOpcode()); - return String.format("%s for %s.%s%s%s", name, this.getOwner(), this.getName(), this.isField() ? ":" : "", this.getDesc()); + return String.format("%s for %s.%s%s%s", Bytecode.getOpcodeName(this.getOpcode()), this.getOwner(), this.getName(), this.isField() ? ":" : "", + this.getDesc()); } @Override @@ -379,17 +354,4 @@ public abstract class MemberRef { return this.toString().hashCode(); } - static int opcodeFromTag(int tag) { - return (tag >= 0 && tag < MemberRef.H_OPCODES.length) ? MemberRef.H_OPCODES[tag] : 0; - } - - static int tagFromOpcode(int opcode) { - for (int tag = 1; tag < MemberRef.H_OPCODES.length; tag++) { - if (MemberRef.H_OPCODES[tag] == opcode) { - return tag; - } - } - return 0; - } - } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index 6180247b..6ed2e166 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -251,7 +251,7 @@ public final class ClassInfo { return String.format("FrameData[index=%d, type=%s, locals=%d]", this.index, FrameData.FRAMETYPES[this.type + 1], this.locals); } } - + /** * Information about a member in this class */ diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java index 0ab8774d..c94ca2ce 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -1140,7 +1140,7 @@ class MixinApplicatorStandard { */ protected final FieldNode findTargetField(FieldNode searchFor) { for (FieldNode target : this.targetClass.fields) { - if (target.name.equals(searchFor.name)) { + if (target.name.equals(searchFor.name) && target.desc.equals(searchFor.desc)) { return target; } } diff --git a/src/main/java/org/spongepowered/asm/util/Handles.java b/src/main/java/org/spongepowered/asm/util/Handles.java new file mode 100644 index 00000000..55709f2b --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/Handles.java @@ -0,0 +1,82 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.util; + +import org.objectweb.asm.Handle; +import org.objectweb.asm.Opcodes; + +/** + * Utility class for working with method and field handles + */ +public final class Handles { + + private static final int[] H_OPCODES = { + 0, // invalid + Opcodes.GETFIELD, // H_GETFIELD + Opcodes.GETSTATIC, // H_GETSTATIC + Opcodes.PUTFIELD, // H_PUTFIELD + Opcodes.PUTSTATIC, // H_PUTSTATIC + Opcodes.INVOKEVIRTUAL, // H_INVOKEVIRTUAL + Opcodes.INVOKESTATIC, // H_INVOKESTATIC + Opcodes.INVOKESPECIAL, // H_INVOKESPECIAL + Opcodes.INVOKESPECIAL, // H_NEWINVOKESPECIAL + Opcodes.INVOKEINTERFACE // H_INVOKEINTERFACE + }; + + private Handles() { + } + + public static boolean isField(Handle handle) { + switch (handle.getTag()) { + case Opcodes.H_INVOKEVIRTUAL: + case Opcodes.H_INVOKESTATIC: + case Opcodes.H_INVOKEINTERFACE: + case Opcodes.H_INVOKESPECIAL: + case Opcodes.H_NEWINVOKESPECIAL: + return false; + case Opcodes.H_GETFIELD: + case Opcodes.H_GETSTATIC: + case Opcodes.H_PUTFIELD: + case Opcodes.H_PUTSTATIC: + return true; + default: + throw new IllegalArgumentException("Invalid tag " + handle.getTag() + " for method handle " + handle + "."); + } + } + + public static int opcodeFromTag(int tag) { + return (tag >= 0 && tag < Handles.H_OPCODES.length) ? Handles.H_OPCODES[tag] : 0; + } + + public static int tagFromOpcode(int opcode) { + for (int tag = 1; tag < Handles.H_OPCODES.length; tag++) { + if (Handles.H_OPCODES[tag] == opcode) { + return tag; + } + } + return 0; + } + +} diff --git a/src/main/java/org/spongepowered/asm/util/asm/ElementNode.java b/src/main/java/org/spongepowered/asm/util/asm/ElementNode.java deleted file mode 100644 index aa213c3e..00000000 --- a/src/main/java/org/spongepowered/asm/util/asm/ElementNode.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * This file is part of Mixin, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.util.asm; - -import java.util.ArrayList; -import java.util.List; - -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.FieldNode; -import org.objectweb.asm.tree.MethodNode; - -/** - * Holder for {@link FieldNode} and {@link MethodNode} for consumers which can - * handle both types (since they have a lot in common), with common accessors - * for overlapping properties. - * - * @param node type - */ -public abstract class ElementNode { - - /** - * ElementNode for MethodNode - */ - static class ElementNodeMethod extends ElementNode { - - private MethodNode method; - - ElementNodeMethod(ClassNode owner, MethodNode method) { - super(owner); - this.method = method; - } - - @Override - public boolean isMethod() { - return true; - } - - @Override - public MethodNode getMethod() { - return this.method; - } - - @Override - public String getName() { - return this.method.name; - } - - @Override - public String getDesc() { - return this.method.desc; - } - - @Override - public String getSignature() { - return this.method.signature; - } - - @Override - public MethodNode get() { - return this.method; - } - - @Override - public String toString() { - return String.format("MethodElement[%s%s]", this.method.name, this.method.desc); - } - - } - - /** - * ElementNode for FieldNode - */ - static class ElementNodeField extends ElementNode { - - private FieldNode field; - - ElementNodeField(ClassNode owner, FieldNode field) { - super(owner); - this.field = field; - } - - @Override - public boolean isField() { - return true; - } - - @Override - public FieldNode getField() { - return this.field; - } - - @Override - public String getName() { - return this.field.name; - } - - @Override - public String getDesc() { - return this.field.desc; - } - - @Override - public String getSignature() { - return this.field.signature; - } - - @Override - public FieldNode get() { - return this.field; - } - - @Override - public String toString() { - return String.format("FieldElement[%s:%s]", this.field.name, this.field.desc); - } - - } - - /** - * The class which owns this element, can be null - */ - private final ClassNode owner; - - protected ElementNode(ClassNode owner) { - this.owner = owner; - } - - /** - * Get whether this element is a method - */ - public boolean isMethod() { - return false; - } - - /** - * get whether this element is a field - */ - public boolean isField() { - return false; - } - - /** - * Get the {@link MethodNode} if this member is a method, otherwise returns - * null - */ - public MethodNode getMethod() { - return null; - } - - /** - * Get the {@link FieldNode} if this member is a field, otherwise returns - * null - */ - public FieldNode getField() { - return null; - } - - /** - * Get the element's owner, can be null - */ - public ClassNode getOwner() { - return this.owner; - } - - /** - * Get the element owner's name, if this element has an owner, otherwise - * returns null - */ - public String getOwnerName() { - return this.owner != null ? this.owner.name : null; - } - - /** - * Get the element name - */ - public abstract String getName(); - - /** - * Get the element descriptor - */ - public abstract String getDesc(); - - /** - * Get the element signature, can be null - */ - public abstract String getSignature(); - - /** - * Returns the node with horrible duck typing - */ - public abstract TNode get(); - - /** - * Create an ElementNode wrapper for the supplied method node - * - * @param owner class which owns the method or null - * @param method Method node to wrap - * @return ElementNode - */ - public static ElementNode of(ClassNode owner, MethodNode method) { - return new ElementNodeMethod(owner, method); - } - - /** - * Create an ElementNode wrapper for the supplied field node - * - * @param owner class which owns the field or null - * @param field Field node to wrap - * @return ElementNode - */ - public static ElementNode of(ClassNode owner, FieldNode field) { - return new ElementNodeField(owner, field); - } - - /** - * Create an ElementNode wrapper for the supplied node object - * - * @param owner class which owns the node or null - * @param node Node to wrap - * @param Node type - * @return ElementNode - * @throws IllegalArgumentException if the supplied argument is not a - * {@link MethodNode} or {@link FieldNode} - */ - @SuppressWarnings("unchecked") - public static ElementNode of(ClassNode owner, TNode node) { - if (node instanceof MethodNode) { - return (ElementNode)new ElementNodeMethod(owner, (MethodNode)node); - } else if (node instanceof FieldNode) { - return (ElementNode)new ElementNodeField(owner, (FieldNode)node); - } - throw new IllegalArgumentException("Could not create ElementNode for unknown node type: " + node.getClass().getName()); - } - - /** - * Convert the supplied list of nodes to a list of wrapped ElementNodes - * - * @param owner Owner of the supplied nodes, can be null - * @param list List of nodes - * @param Node type - * @return List of wrapped nodes - */ - public static List> listOf(ClassNode owner, List list) { - List> nodes = new ArrayList>(); - for (TNode node : list) { - nodes.add(ElementNode.of(owner, node)); - } - return nodes; - } - - /** - * Get a list of wrapped ElementNodes for the fields of the supplied owner - * class - * - * @param owner Class to get fields, must not be null - * @return List of wrapped nodes - */ - public static List> fieldList(ClassNode owner) { - List> fields = new ArrayList>(); - for (FieldNode field : owner.fields) { - fields.add(new ElementNodeField(owner, field)); - } - return fields; - } - - /** - * Get a list of wrapped ElementNodes for the methods of the supplied owner - * class - * - * @param owner Class to get methods, must not be null - * @return List of wrapped nodes - */ - public static List> methodList(ClassNode owner) { - List> methods = new ArrayList>(); - for (MethodNode method : owner.methods) { - methods.add(new ElementNodeMethod(owner, method)); - } - return methods; - } - -}