diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MemberRef.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MemberRef.java new file mode 100644 index 00000000..fc0bf2d5 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MemberRef.java @@ -0,0 +1,320 @@ +/* + * 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.transformer; + +import org.spongepowered.asm.lib.Opcodes; +import org.spongepowered.asm.lib.tree.FieldInsnNode; +import org.spongepowered.asm.lib.tree.MethodInsnNode; +import org.spongepowered.asm.util.ASMHelper; + +/** + * 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. + */ +public abstract class MemberRef { + + /** + * A static reference to a method backed by an invoke instruction + */ + public static final class Method extends MemberRef { + + /** + * Method invocation instruction + */ + public final MethodInsnNode insn; + + /** + * ctor + * + * @param insn Method instruction of this member reference + */ + public Method(MethodInsnNode insn) { + this.insn = insn; + } + + @Override + public boolean isField() { + return false; + } + + @Override + public int getOpcode() { + return this.insn.getOpcode(); + } + + @Override + public String getOwner() { + return this.insn.owner; + } + + @Override + public void setOwner(String owner) { + this.insn.owner = owner; + } + + @Override + public String getName() { + return this.insn.name; + } + + @Override + public String getDesc() { + return this.insn.desc; + } + + @Override + public void setDesc(String desc) { + this.insn.desc = desc; + } + } + + /** + * A static reference to a field backed by field get/put instruction + */ + public static final class Field extends MemberRef { + + /** + * Field accessor instruction + */ + public final FieldInsnNode insn; + + /** + * ctor + * + * @param insn Field instruction this member reference + */ + public Field(FieldInsnNode insn) { + this.insn = insn; + } + + @Override + public boolean isField() { + return true; + } + + @Override + public int getOpcode() { + return this.insn.getOpcode(); + } + + @Override + public String getOwner() { + return this.insn.owner; + } + + @Override + public void setOwner(String owner) { + this.insn.owner = owner; + } + + @Override + public String getName() { + return this.insn.name; + } + + @Override + public String getDesc() { + return this.insn.desc; + } + + @Override + public void setDesc(String desc) { + this.insn.desc = desc; + } + } + + /** + * A reference to a field or method backed by a method handle + */ + public static final class Handle extends MemberRef { + + private org.spongepowered.asm.lib.Handle handle; + + /** + * Creates a member reference initially referring to the member referred + * to by the method handle and the invocation instruction of the method + * handle. + * + * @param handle Initial method handle. + */ + public Handle(org.spongepowered.asm.lib.Handle handle) { + this.handle = handle; + } + + /** + * Gets a method handle for the member this is object is referring to. + * + * @return Method handle representing this object + */ + public org.spongepowered.asm.lib.Handle getMethodHandle() { + return this.handle; + } + + @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 + "."); + } + } + + @Override + public int getOpcode() { + switch (this.handle.getTag()) { + case Opcodes.H_INVOKEVIRTUAL: + return Opcodes.INVOKEVIRTUAL; + case Opcodes.H_INVOKESTATIC: + return Opcodes.INVOKESTATIC; + case Opcodes.H_INVOKEINTERFACE: + return Opcodes.INVOKEINTERFACE; + case Opcodes.H_INVOKESPECIAL: + case Opcodes.H_NEWINVOKESPECIAL: + return Opcodes.INVOKESPECIAL; + case Opcodes.H_GETFIELD: + return Opcodes.GETFIELD; + case Opcodes.H_GETSTATIC: + return Opcodes.GETSTATIC; + case Opcodes.H_PUTFIELD: + return Opcodes.PUTFIELD; + case Opcodes.H_PUTSTATIC: + return Opcodes.PUTSTATIC; + default: + throw new MixinTransformerError("Invalid tag " + this.handle.getTag() + " for method handle " + this.handle + "."); + } + } + + @Override + public String getOwner() { + return this.handle.getOwner(); + } + + @Override + public void setOwner(String owner) { + this.handle = new org.spongepowered.asm.lib.Handle(this.handle.getTag(), owner, this.handle.getName(), this.handle.getDesc()); + } + + @Override + public String getName() { + return this.handle.getName(); + } + + @Override + public String getDesc() { + return this.handle.getDesc(); + } + + @Override + public void setDesc(String desc) { + this.handle = new org.spongepowered.asm.lib.Handle(this.handle.getTag(), this.handle.getOwner(), this.handle.getName(), desc); + } + } + + /** + * Whether this member is a field. + * + * @return If this member is a field, else it is a method + */ + public abstract boolean isField(); + + /** + * The opcode of the invocation. + * + * @return The opcode of the invocation + */ + public abstract int getOpcode(); + + /** + * The internal name for the owner of this member. + * + * @return The owners name + */ + public abstract String getOwner(); + + /** + * Changes the owner of this + * + * @param owner New owner + */ + public abstract void setOwner(String owner); + + /** + * Name of this member. + * + * @return Name of this member. + */ + public abstract String getName(); + + /** + * Descriptor of this member. + * + * @return Descriptor of this member + */ + public abstract String getDesc(); + + /** + * Changes the descriptor of this member + * + * @param desc New descriptor of this member + */ + public abstract void setDesc(String desc); + + @Override + public String toString() { + return ASMHelper.getOpcodeName(this.getOpcode()) + " for " + + this.getOwner() + "." + this.getName() + (this.isField() ? ":" : "") + this.getDesc(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MemberRef)) { + return false; + } + + MemberRef other = (MemberRef)obj; + return this.getOpcode() == other.getOpcode() + && this.getOwner().equals(other.getOwner()) + && this.getName().equals(other.getName()) + && this.getDesc().equals(other.getDesc()); + } + + @Override + public int hashCode() { + return this.toString().hashCode(); + } +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassWriter.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassWriter.java index b61c0235..7f521ec1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassWriter.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinClassWriter.java @@ -53,10 +53,10 @@ public class MixinClassWriter extends ClassWriter { ClassInfo d = ClassInfo.forName(type2); if (c.hasSuperClass(d)) { - return type1; + return type2; } if (d.hasSuperClass(c)) { - return type2; + return type1; } if (c.isInterface() || d.isInterface()) { return MixinClassWriter.JAVA_LANG_OBJECT; @@ -68,7 +68,7 @@ public class MixinClassWriter extends ClassWriter { return MixinClassWriter.JAVA_LANG_OBJECT; } - } while (!c.hasSuperClass(d)); + } while (!d.hasSuperClass(c)); return c.getName(); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java index 769da82d..53a4c4aa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java @@ -130,6 +130,7 @@ class MixinPreProcessor { MixinTargetContext createContextFor(ClassNode target) { this.prepare(); MixinTargetContext context = new MixinTargetContext(this.mixin, this.classNode, target); + target.version = Math.max(this.classNode.version, target.version); this.attach(context); return context; } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index a5316fa5..c18799c0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -32,20 +32,13 @@ import java.util.Map; import java.util.Set; import org.apache.logging.log4j.Level; +import org.spongepowered.asm.lib.Handle; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; -import org.spongepowered.asm.lib.tree.AbstractInsnNode; -import org.spongepowered.asm.lib.tree.ClassNode; -import org.spongepowered.asm.lib.tree.FieldInsnNode; -import org.spongepowered.asm.lib.tree.FieldNode; -import org.spongepowered.asm.lib.tree.LdcInsnNode; -import org.spongepowered.asm.lib.tree.MethodInsnNode; -import org.spongepowered.asm.lib.tree.MethodNode; -import org.spongepowered.asm.lib.tree.TypeInsnNode; +import org.spongepowered.asm.lib.tree.*; import org.spongepowered.asm.mixin.SoftOverride; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.asm.mixin.injection.struct.Target; -import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal; import org.spongepowered.asm.util.ASMHelper; import org.spongepowered.asm.util.Constants; @@ -237,13 +230,15 @@ public class MixinTargetContext implements IReferenceMapperContext { AbstractInsnNode insn = iter.next(); if (insn instanceof MethodInsnNode) { - this.transformMethodNode(method, iter, (MethodInsnNode)insn); + this.transformMethodRef(method, iter, new MemberRef.Method((MethodInsnNode)insn)); } else if (insn instanceof FieldInsnNode) { - this.transformFieldNode(method, iter, (FieldInsnNode)insn); + this.transformFieldRef(method, iter, new MemberRef.Field((FieldInsnNode)insn)); } else if (insn instanceof TypeInsnNode) { this.transformTypeNode(method, iter, (TypeInsnNode)insn); } else if (insn instanceof LdcInsnNode) { this.transformConstantNode(method, iter, (LdcInsnNode)insn); + } else if (insn instanceof InvokeDynamicInsnNode) { + this.transformInvokeDynamicNode(method, iter, (InvokeDynamicInsnNode)insn); } } } @@ -257,7 +252,7 @@ public class MixinTargetContext implements IReferenceMapperContext { private void validateMethod(MethodNode method) { // Any method tagged with @SoftOverride must have an implementation visible from if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) != null) { - Method superMethod = this.targetClassInfo.findMethodInHierarchy(method.name, method.desc, false, Traversal.SUPER); + ClassInfo.Method superMethod = this.targetClassInfo.findMethodInHierarchy(method.name, method.desc, false, Traversal.SUPER); if (superMethod == null || !superMethod.isInjected()) { throw new InvalidMixinException(this, "Mixin method " + method.name + method.desc + " is tagged with @SoftOverride but no " + "valid method was found in superclasses of " + this.targetClass.name); @@ -266,51 +261,56 @@ public class MixinTargetContext implements IReferenceMapperContext { } /** - * Transforms method invocations in the method. Updates static and dynamic - * bindings. + * Transforms a method invocation/reference in the method. Updates static + * and dynamic bindings. * * @param method Method being processed * @param iter Insn interator - * @param methodInsn Insn to transform + * @param methodRef Method reference to transform */ - private void transformMethodNode(MethodNode method, Iterator iter, MethodInsnNode methodInsn) { - this.transformDescriptor(methodInsn); + private void transformMethodRef(MethodNode method, Iterator iter, MemberRef methodRef) { + this.transformDescriptor(methodRef); - if (methodInsn.owner.equals(this.getClassRef())) { - methodInsn.owner = this.targetClass.name; + if (methodRef.getOwner().equals(this.getClassRef())) { + methodRef.setOwner(this.targetClass.name); } else if ((this.detachedSuper || this.inheritsFromMixin)) { - if (methodInsn.getOpcode() == Opcodes.INVOKESPECIAL) { - this.updateStaticBinding(method, methodInsn); - } else if (methodInsn.getOpcode() == Opcodes.INVOKEVIRTUAL && ClassInfo.forName(methodInsn.owner).isMixin()) { - this.updateDynamicBinding(method, methodInsn); + if (methodRef.getOpcode() == Opcodes.INVOKESPECIAL) { + this.updateStaticBinding(method, methodRef); + } else if (methodRef.getOpcode() == Opcodes.INVOKEVIRTUAL && ClassInfo.forName(methodRef.getOwner()).isMixin()) { + this.updateDynamicBinding(method, methodRef); } } } /** - * Transforms field accesses in the method. Handles imaginary super accesses - * and converts them to real super-invocations and rewrites field accesses - * which refer to mixin or supermixin classes to their relevant targets. + * Transforms field access/reference in the method. Handles imaginary super + * accesses and converts them to real super-invocations and rewrites field + * accesses which refer to mixin or supermixin classes to their relevant + * targets. * * @param method Method being processed * @param iter Insn interator - * @param fieldInsn Insn to transform + * @param fieldRef Field Reference to transform */ - private void transformFieldNode(MethodNode method, Iterator iter, FieldInsnNode fieldInsn) { - if (Constants.IMAGINARY_SUPER.equals(fieldInsn.name)) { - this.processImaginarySuper(method, fieldInsn); - iter.remove(); + private void transformFieldRef(MethodNode method, Iterator iter, MemberRef fieldRef) { + if (Constants.IMAGINARY_SUPER.equals(fieldRef.getName())) { + if (fieldRef instanceof MemberRef.Field) { + this.processImaginarySuper(method, ((MemberRef.Field) fieldRef).insn); + iter.remove(); + } else { + throw new InvalidMixinException(this.mixin, "Cannot call imaginary super from method handle."); + } } - this.transformDescriptor(fieldInsn); + this.transformDescriptor(fieldRef); - if (fieldInsn.owner.equals(this.getClassRef())) { - fieldInsn.owner = this.targetClass.name; + if (fieldRef.getOwner().equals(this.getClassRef())) { + fieldRef.setOwner(this.targetClass.name); } else { - ClassInfo fieldOwner = ClassInfo.forName(fieldInsn.owner); + ClassInfo fieldOwner = ClassInfo.forName(fieldRef.getOwner()); if (fieldOwner.isMixin()) { ClassInfo actualOwner = this.targetClassInfo.findCorrespondingType(fieldOwner); - fieldInsn.owner = actualOwner != null ? actualOwner.getName() : this.targetClass.name; + fieldRef.setOwner(actualOwner != null ? actualOwner.getName() : this.targetClass.name); } } } @@ -333,22 +333,73 @@ public class MixinTargetContext implements IReferenceMapperContext { } /** - * Transforms class literals in the method being processed. + * Transforms class literals and method handle loads in the method being + * processed. * * @param method Method being processed * @param iter Insn interator * @param ldcInsn Insn to transform */ private void transformConstantNode(MethodNode method, Iterator iter, LdcInsnNode ldcInsn) { - if (ldcInsn.cst instanceof Type) { - Type type = (Type)ldcInsn.cst; - String desc = this.transformSingleDescriptor(type); - if (!type.toString().equals(desc)) { - ldcInsn.cst = Type.getType(desc); - } + ldcInsn.cst = this.transformConstant(method, iter, ldcInsn.cst); + } + + /** + * Transforms a invoke dynamic instruction in the method being processed. + * + * @param method Method being processed + * @param iter Insn interator + * @param dynInsn Insn to transform + */ + private void transformInvokeDynamicNode(MethodNode method, Iterator iter, InvokeDynamicInsnNode dynInsn) { + dynInsn.desc = this.transformMethodDescriptor(dynInsn.desc); + dynInsn.bsm = this.transformHandle(method, iter, dynInsn.bsm); + for (int i = 0; i < dynInsn.bsmArgs.length; i++) { + dynInsn.bsmArgs[i] = this.transformConstant(method, iter, dynInsn.bsmArgs[i]); } } + /** + * Transforms a constant in the constant pool. + * + * @param method Method being processed + * @param iter Insn interator + * @param constant Consatnt pool entry + * @return Transformed constant + */ + private Object transformConstant(MethodNode method, Iterator iter, Object constant) { + if (constant instanceof Type) { + Type type = (Type)constant; + String desc = this.transformDescriptor(type); + if (!type.toString().equals(desc)) { + return Type.getType(desc); + } else { + return constant; + } + } else if (constant instanceof Handle) { + return this.transformHandle(method, iter, (Handle)constant); + } + return constant; + } + + /** + * Transforms a method handle that is referenced from the method being + * processed. + * + * @param method Method being processed + * @param iter Insn interator + * @param handle Handle to transform + */ + private Handle transformHandle(MethodNode method, Iterator iter, Handle handle) { + MemberRef.Handle memberRef = new MemberRef.Handle(handle); + if (memberRef.isField()) { + this.transformFieldRef(method, iter, memberRef); + } else { + this.transformMethodRef(method, iter, memberRef); + } + return memberRef.getMethodHandle(); + } + /** * Handle "imaginary super" invokations, these are invokations in * non-derived mixins for accessing methods known to exist in a supermixin @@ -388,7 +439,7 @@ public class MixinTargetContext implements IReferenceMapperContext { MethodInsnNode methodNode = (MethodInsnNode)insn; if (methodNode.owner.equals(this.getClassRef()) && methodNode.name.equals(method.name) && methodNode.desc.equals(method.desc)) { methodNode.setOpcode(Opcodes.INVOKESPECIAL); - this.updateStaticBinding(method, methodNode); + this.updateStaticBinding(method, new MemberRef.Method(methodNode)); return; } } @@ -402,10 +453,10 @@ public class MixinTargetContext implements IReferenceMapperContext { * which contains the specified method. * * @param method Method containing the instruction - * @param insn INVOKE instruction node + * @param methodRef Unbound reference to the method */ - private void updateStaticBinding(MethodNode method, MethodInsnNode insn) { - this.updateBinding(method, insn, Traversal.SUPER); + private void updateStaticBinding(MethodNode method, MemberRef methodRef) { + this.updateBinding(method, methodRef, Traversal.SUPER); } /** @@ -413,27 +464,27 @@ public class MixinTargetContext implements IReferenceMapperContext { * which contains the specified method. * * @param method Method containing the instruction - * @param insn INVOKE instruction node + * @param methodRef Unbound reference to the method */ - private void updateDynamicBinding(MethodNode method, MethodInsnNode insn) { - this.updateBinding(method, insn, Traversal.ALL); + private void updateDynamicBinding(MethodNode method, MemberRef methodRef) { + this.updateBinding(method, methodRef, Traversal.ALL); } - private void updateBinding(MethodNode method, MethodInsnNode insn, Traversal traversal) { - if (Constants.INIT.equals(method.name) || insn.owner.equals(this.targetClass.name) || this.targetClass.name.startsWith("<")) { + private void updateBinding(MethodNode method, MemberRef methodRef, Traversal traversal) { + if (Constants.INIT.equals(method.name) || methodRef.getOwner().equals(this.targetClass.name) || this.targetClass.name.startsWith("<")) { return; } - Method superMethod = this.targetClassInfo.findMethodInHierarchy(insn.name, insn.desc, traversal == Traversal.ALL, traversal); + ClassInfo.Method superMethod = this.targetClassInfo.findMethodInHierarchy(methodRef.getName(), methodRef.getDesc(), + traversal == Traversal.ALL, traversal); if (superMethod != null) { if (superMethod.getOwner().isMixin()) { - throw new InvalidMixinException(this, "Invalid " + ASMHelper.getOpcodeName(insn) + " in " + this + " resolved " + insn.owner - + " -> " + superMethod.getOwner() + " for " + insn.name + insn.desc); + throw new InvalidMixinException(this, "Invalid " + methodRef + " in " + this + " resolved " + superMethod.getOwner() + + " but is mixin."); } - insn.owner = superMethod.getOwner().getName(); - } else if (ClassInfo.forName(insn.owner).isMixin()) { - throw new MixinTransformerError("Error resolving " + ASMHelper.getOpcodeName(insn) + " target for " + insn.owner + "." + insn.name - + " in " + this); + methodRef.setOwner(superMethod.getOwner().getName()); + } else if (ClassInfo.forName(methodRef.getOwner()).isMixin()) { + throw new MixinTransformerError("Error resolving " + methodRef + " in " + this); } } @@ -449,18 +500,6 @@ public class MixinTargetContext implements IReferenceMapperContext { field.desc = this.transformSingleDescriptor(field.desc, false); } - /** - * Transforms a field insn descriptor in the context of this mixin target - * - * @param field Field instruction node to transform - */ - public void transformDescriptor(FieldInsnNode field) { - if (!this.inheritsFromMixin) { - return; - } - field.desc = this.transformSingleDescriptor(field.desc, false); - } - /** * Transforms a method descriptor in the context of this mixin target * @@ -474,15 +513,20 @@ public class MixinTargetContext implements IReferenceMapperContext { } /** - * Transforms a method insn descriptor in the context of this mixin target + * Transforms a method or field reference descriptor in the context of this + * mixin target * - * @param method Method instruction node to transform + * @param member Reference to the method or field */ - public void transformDescriptor(MethodInsnNode method) { + public void transformDescriptor(MemberRef member) { if (!this.inheritsFromMixin) { return; } - method.desc = this.transformMethodDescriptor(method.desc); + if (member.isField()) { + member.setDesc(this.transformSingleDescriptor(member.getDesc(), false)); + } else { + member.setDesc(this.transformMethodDescriptor(member.getDesc())); + } } /** @@ -496,6 +540,13 @@ public class MixinTargetContext implements IReferenceMapperContext { } typeInsn.desc = this.transformSingleDescriptor(typeInsn.desc, true); } + + private String transformDescriptor(Type type) { + if (type.getSort() == Type.METHOD) { + return this.transformMethodDescriptor(type.getDescriptor()); + } + return this.transformSingleDescriptor(type); + } private String transformSingleDescriptor(Type type) { if (type.getSort() < Type.ARRAY) {