diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index a72f67f7..894b94dc 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -49,6 +49,7 @@ import net.minecraftforge.srg2source.rangeapplier.MethodData; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.util.Constants; import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass; @@ -116,8 +117,6 @@ class AnnotatedMixin { } } - private static final String CTOR = ""; - /** * Mixin annotation */ @@ -408,7 +407,7 @@ class AnnotatedMixin { if (this.targetType.isImaginary()) { this.mixins.printMessage(Kind.WARNING, "@Inject target requires method signature because enclosing type information is unavailable", method, inject); - } else if (!AnnotatedMixin.CTOR.equals(targetMember.name)) { + } else if (!Constants.INIT.equals(targetMember.name)) { this.mixins.printMessage(Kind.WARNING, "Unable to determine signature for @Inject target method", method, inject); } return; @@ -416,7 +415,7 @@ class AnnotatedMixin { MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + targetMember.name, desc)); if (obfMethod == null) { - Kind error = AnnotatedMixin.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; + Kind error = Constants.INIT.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; this.mixins.printMessage(error, "No obfuscation mapping for @Inject target " + targetMember.name, method, inject); return; } diff --git a/src/main/java/org/spongepowered/asm/mixin/Shadow.java b/src/main/java/org/spongepowered/asm/mixin/Shadow.java index 5002b391..4bd1a556 100644 --- a/src/main/java/org/spongepowered/asm/mixin/Shadow.java +++ b/src/main/java/org/spongepowered/asm/mixin/Shadow.java @@ -104,4 +104,25 @@ public @interface Shadow { * obfuscation mappings for this annotation */ public boolean remap() default true; + + /** + * Supplies possible aliases for this shadow member. This should only + * be used in the following scenarios: + * + * + * + *

Only private members may be given aliases. This is because + * aliases can only be calculated when the mixin is applied and thus would + * otherwise invalidate the calculated class metadata if another mixin had + * already been applied in the hierarchy.

+ * + * @return Aliases for this member + */ + public String[] aliases() default { }; } 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 51ce2454..6d662d43 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 @@ -51,6 +51,7 @@ import org.spongepowered.asm.mixin.injection.points.BeforeReturn; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.Locals; import org.spongepowered.asm.util.PrettyPrinter; import org.spongepowered.asm.util.SignaturePrinter; @@ -365,7 +366,7 @@ public class CallbackInjector extends Injector { insns.add(new TypeInsnNode(Opcodes.NEW, errorClass)); insns.add(new InsnNode(Opcodes.DUP)); insns.add(new LdcInsnNode(message)); - insns.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, errorClass, "", "(Ljava/lang/String;)V", false)); + insns.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, errorClass, Constants.INIT, "(Ljava/lang/String;)V", false)); insns.add(new InsnNode(Opcodes.ATHROW)); return method; } 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 4707f1cd..e480a7e0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -34,9 +34,11 @@ import java.util.Set; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -106,58 +108,67 @@ class ClassInfo extends TreeInfo { } /** - * Information about a method in this class + * Information about a member in this class */ - class Method { + abstract static class Member { + + static enum Type { + METHOD, + FIELD + } /** - * The original name of the method + * Member type */ - private final String methodName; + private final Type type; /** - * The method signature + * The original name of the member */ - private final String methodDesc; + private final String memberName; /** - * True if this method was injected by a mixin, false if it was + * The member's signature + */ + private final String memberDesc; + + /** + * True if this member was injected by a mixin, false if it was * originally part of the class */ private final boolean isInjected; /** - * Current name of the method, may be different from {@link methodName} - * if the method has been renamed + * Access modifiers + */ + private final int modifiers; + + /** + * Current name of the member, may be different from {@link #memberName} + * if the member has been renamed */ private String currentName; - public Method(MethodNode method) { - this(method, false); + protected Member(Member member) { + this(member.type, member.memberName, member.memberDesc, member.modifiers, member.isInjected); + this.currentName = member.currentName; } - public Method(Method method) { - this(method.methodName, method.methodDesc, method.isInjected); - this.currentName = method.currentName; - } - - public Method(MethodNode method, boolean injected) { - this(method.name, method.desc, injected); + protected Member(Type type, String name, String desc, int access) { + this(type, name, desc, access, false); } - public Method(String name, String desc) { - this(name, desc, false); - } - - public Method(String name, String desc, boolean injected) { - this.methodName = name; - this.methodDesc = desc; + protected Member(Type type, String name, String desc, int access, boolean injected) { + this.type = type; + this.memberName = name; + this.memberDesc = desc; this.isInjected = injected; this.currentName = name; + this.modifiers = access; } public String getOriginalName() { - return this.methodName; + return this.memberName; } public String getName() { @@ -165,7 +176,7 @@ class ClassInfo extends TreeInfo { } public String getDesc() { - return this.methodDesc; + return this.memberDesc; } public boolean isInjected() { @@ -173,33 +184,40 @@ class ClassInfo extends TreeInfo { } public boolean isRenamed() { - return this.currentName != this.methodName; + return this.currentName != this.memberName; } - - public ClassInfo getOwner() { - return ClassInfo.this; + + public boolean isPrivate() { + return (this.modifiers & Opcodes.ACC_PRIVATE) != 0; } + + // Abstract because this has to be static in order to contain the enum + public abstract ClassInfo getOwner(); + public int getAccess() { + return this.modifiers; + } + public void renameTo(String name) { this.currentName = name; } public boolean equals(String name, String desc) { - return (this.methodName.equals(name) + return (this.memberName.equals(name) || this.currentName.equals(name)) - && this.methodDesc.equals(desc); + && this.memberDesc.equals(desc); } @Override public boolean equals(Object obj) { - if (!(obj instanceof Method)) { + if (!(obj instanceof Member)) { return false; } - Method other = (Method)obj; - return (other.methodName.equals(this.methodName) + Member other = (Member)obj; + return (other.memberName.equals(this.memberName) || other.currentName.equals(this.currentName)) - && other.methodDesc.equals(this.methodDesc); + && other.memberDesc.equals(this.memberDesc); } @Override @@ -209,7 +227,91 @@ class ClassInfo extends TreeInfo { @Override public String toString() { - return this.methodName + this.methodDesc; + return this.memberName + this.memberDesc; + } + } + + /** + * A method + */ + class Method extends Member { + + public Method(Member member) { + super(member); + } + + public Method(MethodNode method) { + this(method, false); + } + + public Method(MethodNode method, boolean injected) { + super(Type.METHOD, method.name, method.desc, method.access, injected); + } + + public Method(String name, String desc) { + super(Type.METHOD, name, desc, Opcodes.ACC_PUBLIC, false); + } + + public Method(String name, String desc, int access) { + super(Type.METHOD, name, desc, access, false); + } + + public Method(String name, String desc, int access, boolean injected) { + super(Type.METHOD, name, desc, access, injected); + } + + @Override + public ClassInfo getOwner() { + return ClassInfo.this; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Method)) { + return false; + } + + return super.equals(obj); + } + } + + /** + * A field + */ + class Field extends Member { + + public Field(Member member) { + super(member); + } + + public Field(FieldNode field) { + this(field, false); + } + + public Field(FieldNode field, boolean injected) { + super(Type.FIELD, field.name, field.desc, field.access, injected); + } + + public Field(String name, String desc, int access) { + super(Type.FIELD, name, desc, access, false); + } + + public Field(String name, String desc, int access, boolean injected) { + super(Type.FIELD, name, desc, access, injected); + } + + @Override + public ClassInfo getOwner() { + return ClassInfo.this; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Field)) { + return false; + } + + return super.equals(obj); } } @@ -261,7 +363,7 @@ class ClassInfo extends TreeInfo { /** * Public and protected fields in this class */ - private final List fields; + private final Set fields; /** * Mixins which target this class @@ -325,7 +427,7 @@ class ClassInfo extends TreeInfo { new Method("wait", "()V"), new Method("finalize", "()V") ); - this.fields = Collections.emptyList(); + this.fields = Collections.emptySet(); this.isInterface = false; this.interfaces = Collections.emptyList(); this.access = Opcodes.ACC_PUBLIC; @@ -342,7 +444,7 @@ class ClassInfo extends TreeInfo { this.name = classNode.name; this.superName = classNode.superName != null ? classNode.superName : ClassInfo.JAVA_LANG_OBJECT; this.methods = new HashSet(); - this.fields = new ArrayList(); + this.fields = new HashSet(); this.isInterface = ((classNode.access & Opcodes.ACC_INTERFACE) != 0); this.interfaces = Collections.unmodifiableList(classNode.interfaces); this.access = classNode.access; @@ -365,8 +467,10 @@ class ClassInfo extends TreeInfo { outerName = outerName.substring(1, outerName.length() - 1); } } - } else if ((field.access & Opcodes.ACC_PRIVATE) == 0) { - this.fields.add(field.name + "()" + field.desc); + } + + if ((field.access & Opcodes.ACC_STATIC) == 0) { + this.fields.add(new Field(field, this.isMixin)); } } } @@ -380,9 +484,7 @@ class ClassInfo extends TreeInfo { } private void addMethod(MethodNode method, boolean injected) { - if (!method.name.startsWith("<") - && (method.access & Opcodes.ACC_PRIVATE) == 0 - && (method.access & Opcodes.ACC_STATIC) == 0) { + if (!method.name.startsWith("<") && (method.access & Opcodes.ACC_STATIC) == 0) { this.methods.add(new Method(method, injected)); } } @@ -732,7 +834,20 @@ class ClassInfo extends TreeInfo { * @return the method object or null if the method could not be resolved */ public Method findMethodInHierarchy(MethodNode method, boolean includeThisClass) { - return this.findMethodInHierarchy(method.name, method.desc, includeThisClass); + return this.findMethodInHierarchy(method.name, method.desc, includeThisClass, Traversal.NONE); + } + + /** + * Finds the specified private or protected method in this class's hierarchy + * + * @param method Method to search for + * @param includeThisClass True to return this class if the method exists + * here, or false to search only superclasses + * @param includePrivate Include private members in the search + * @return the method object or null if the method could not be resolved + */ + public Method findMethodInHierarchy(MethodNode method, boolean includeThisClass, boolean includePrivate) { + return this.findMethodInHierarchy(method.name, method.desc, includeThisClass, Traversal.NONE, includePrivate); } /** @@ -744,7 +859,20 @@ class ClassInfo extends TreeInfo { * @return the method object or null if the method could not be resolved */ public Method findMethodInHierarchy(MethodInsnNode method, boolean includeThisClass) { - return this.findMethodInHierarchy(method.name, method.desc, includeThisClass); + return this.findMethodInHierarchy(method.name, method.desc, includeThisClass, Traversal.NONE); + } + + /** + * Finds the specified public or protected method in this class's hierarchy + * + * @param method Method to search for + * @param includeThisClass True to return this class if the method exists + * here, or false to search only superclasses + * @param includePrivate Include private members in the search + * @return the method object or null if the method could not be resolved + */ + public Method findMethodInHierarchy(MethodInsnNode method, boolean includeThisClass, boolean includePrivate) { + return this.findMethodInHierarchy(method.name, method.desc, includeThisClass, Traversal.NONE, includePrivate); } /** @@ -771,17 +899,141 @@ class ClassInfo extends TreeInfo { * @return the method object or null if the method could not be resolved */ public Method findMethodInHierarchy(String name, String desc, boolean includeThisClass, Traversal traversal) { + return this.findMethodInHierarchy(name, desc, includeThisClass, traversal, false); + } + + /** + * Finds the specified public or protected method in this class's hierarchy + * + * @param name Method name to search for + * @param desc Method descriptor + * @param includeThisClass True to return this class if the method exists + * here, or false to search only superclasses + * @param traversal Traversal type to allow during this lookup + * @param includePrivate Include private members in the search + * @return the method object or null if the method could not be resolved + */ + public Method findMethodInHierarchy(String name, String desc, boolean includeThisClass, Traversal traversal, boolean includePrivate) { + return this.findInHierarchy(name, desc, includeThisClass, traversal, includePrivate, Type.METHOD); + } + + /** + * Finds the specified private or protected field in this class's hierarchy + * + * @param field Field to search for + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(FieldNode field, boolean includeThisClass) { + return this.findFieldInHierarchy(field.name, field.desc, includeThisClass, Traversal.NONE); + } + + /** + * Finds the specified private or protected field in this class's hierarchy + * + * @param field Field to search for + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @param includePrivate Include private members in the search + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(FieldNode field, boolean includeThisClass, boolean includePrivate) { + return this.findFieldInHierarchy(field.name, field.desc, includeThisClass, Traversal.NONE, includePrivate); + } + + /** + * Finds the specified public or protected field in this class's hierarchy + * + * @param field Field to search for + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(FieldInsnNode field, boolean includeThisClass) { + return this.findFieldInHierarchy(field.name, field.desc, includeThisClass, Traversal.NONE); + } + + /** + * Finds the specified public or protected field in this class's hierarchy + * + * @param field Field to search for + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @param includePrivate Include private members in the search + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(FieldInsnNode field, boolean includeThisClass, boolean includePrivate) { + return this.findFieldInHierarchy(field.name, field.desc, includeThisClass, Traversal.NONE, includePrivate); + } + + /** + * Finds the specified public or protected field in this class's hierarchy + * + * @param name Field name to search for + * @param desc Field descriptor + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(String name, String desc, boolean includeThisClass) { + return this.findFieldInHierarchy(name, desc, includeThisClass, Traversal.NONE); + } + + /** + * Finds the specified public or protected field in this class's hierarchy + * + * @param name Field name to search for + * @param desc Field descriptor + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @param traversal Traversal type to allow during this lookup + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(String name, String desc, boolean includeThisClass, Traversal traversal) { + return this.findFieldInHierarchy(name, desc, includeThisClass, traversal, false); + } + + /** + * Finds the specified public or protected field in this class's hierarchy + * + * @param name Field name to search for + * @param desc Field descriptor + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @param traversal Traversal type to allow during this lookup + * @param includePrivate Include private members in the search + * @return the field object or null if the field could not be resolved + */ + public Field findFieldInHierarchy(String name, String desc, boolean includeThisClass, Traversal traversal, boolean includePrivate) { + return this.findInHierarchy(name, desc, includeThisClass, traversal, includePrivate, Type.FIELD); + } + + /** + * Finds a public or protected member in the hierarchy of this class which + * matches the supplied details + * + * @param name Member name to search + * @param desc Member descriptor + * @param includeThisClass True to return this class if the field exists + * here, or false to search only superclasses + * @param traversal Traversal type to allow during this lookup + * @param priv Include private members in the search + * @param type Type of member to search for (field or method) + * @return the discovered member or null if the member could not be resolved + */ + private M findInHierarchy(String name, String desc, boolean includeThisClass, Traversal traversal, boolean priv, Type type) { if (includeThisClass) { - Method method = this.findMethod(name, desc); - if (method != null) { - return method; + M member = this.findMember(name, desc, priv, type); + if (member != null) { + return member; } if (traversal.canTraverse()) { for (MixinInfo mixin : this.mixins) { - Method mixinMethod = mixin.getClassInfo().findMethod(name, desc); - if (mixinMethod != null) { - return new Method(mixinMethod); + M mixinMember = mixin.getClassInfo().findMember(name, desc, priv, type); + if (mixinMember != null) { + return this.cloneMember(mixinMember); } } } @@ -790,9 +1042,10 @@ class ClassInfo extends TreeInfo { ClassInfo superClassInfo = this.getSuperClass(); if (superClassInfo != null) { for (ClassInfo superTarget : superClassInfo.getTargets()) { - Method method = superTarget.findMethodInHierarchy(name, desc, true, traversal.next()); - if (method != null) { - return method; + // Do not search for private members in superclasses, pass priv as false + M member = superTarget.findInHierarchy(name, desc, true, traversal.next(), false, type); + if (member != null) { + return member; } } } @@ -800,6 +1053,24 @@ class ClassInfo extends TreeInfo { return null; } + /** + * Effectively a clone method for member, placed here so that the enclosing + * instance for the inner class is this class and not the enclosing instance + * of the existing class. Basically creates a cloned member with this + * ClassInfo as its parent. + * + * @param member + * @return + */ + @SuppressWarnings("unchecked") + private M cloneMember(M member) { + if (member instanceof Method) { + return (M)new Method(member); + } + + return (M)new Field(member); + } + /** * Finds the specified public or protected method in this class * @@ -807,7 +1078,18 @@ class ClassInfo extends TreeInfo { * @return the method object or null if the method could not be resolved */ public Method findMethod(MethodNode method) { - return this.findMethod(method.name, method.desc); + return this.findMethod(method.name, method.desc, false); + } + + /** + * Finds the specified public or protected method in this class + * + * @param method Method to search for + * @param includePrivate also search private fields + * @return the method object or null if the method could not be resolved + */ + public Method findMethod(MethodNode method, boolean includePrivate) { + return this.findMethod(method.name, method.desc, includePrivate); } /** @@ -817,7 +1099,18 @@ class ClassInfo extends TreeInfo { * @return the method object or null if the method could not be resolved */ public Method findMethod(MethodInsnNode method) { - return this.findMethod(method.name, method.desc); + return this.findMethod(method.name, method.desc, false); + } + + /** + * Finds the specified public or protected method in this class + * + * @param method Method to search for + * @param includePrivate also search private fields + * @return the method object or null if the method could not be resolved + */ + public Method findMethod(MethodInsnNode method, boolean includePrivate) { + return this.findMethod(method.name, method.desc, includePrivate); } /** @@ -825,18 +1118,68 @@ class ClassInfo extends TreeInfo { * * @param method Method name to search for * @param desc Method signature to search for + * @param includePrivate also search private fields * @return the method object or null if the method could not be resolved */ - public Method findMethod(String name, String desc) { - for (Method method : this.methods) { - if (method.equals(name, desc)) { - return method; + public Method findMethod(String name, String desc, boolean includePrivate) { + return this.findMember(name, desc, includePrivate, Type.METHOD); + } + + /** + * Finds the specified field in this class + * + * @param field Field to search for + * @return the field object or null if the field could not be resolved + */ + public Field findField(FieldNode field) { + return this.findField(field.name, field.desc, (field.access & Opcodes.ACC_PRIVATE) != 0); + } + + /** + * Finds the specified public or protected method in this class + * + * @param field Field to search for + * @param includePrivate also search private fields + * @return the field object or null if the field could not be resolved + */ + public Field findField(FieldInsnNode field, boolean includePrivate) { + return this.findField(field.name, field.desc, includePrivate); + } + + /** + * Finds the specified field in this class + * + * @param name Field name to search for + * @param desc Field signature to search for + * @param includePrivate also search private fields + * @return the field object or null if the field could not be resolved + */ + public Field findField(String name, String desc, boolean includePrivate) { + return this.findMember(name, desc, includePrivate, Type.FIELD); + } + + /** + * Finds the specified member in this class + * + * @param name Field name to search for + * @param desc Field signature to search for + * @param includePrivate also search private fields + * @param memberType Type of member list to search + * @return the field object or null if the field could not be resolved + */ + private M findMember(String name, String desc, boolean includePrivate, Type memberType) { + @SuppressWarnings("unchecked") + Set members = (Set)(memberType == Type.METHOD ? this.methods : this.fields); + + for (M member : members) { + if (member.equals(name, desc) && (includePrivate || !member.isPrivate())) { + return member; } } return null; } - + /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index eeb6f34f..5ff115df 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -29,7 +29,6 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Set; @@ -42,12 +41,10 @@ import org.apache.logging.log4j.Logger; import org.objectweb.asm.ClassReader; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; -import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.InnerClassNode; -import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.Implements; import org.spongepowered.asm.mixin.Mixin; @@ -55,7 +52,6 @@ import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; -import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.util.ASMHelper; import com.google.common.base.Function; @@ -190,7 +186,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { this.validateMixin(this.validationClassNode); this.readImplementations(this.validationClassNode); this.readInnerClasses(this.validationClassNode); - this.prepare(this.validationClassNode); + new MixinPreProcessor(this, this.validationClassNode).prepare(); this.validationClassNode = null; } @@ -320,15 +316,6 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } } - /** - * Prepare the mixin, applies any pre-processing transformations - */ - private ClassNode prepare(ClassNode classNode) { - this.findRenamedMethods(classNode); - this.transformMethods(classNode); - return classNode; - } - /** * Read and process any {@link Implements} annotations on the mixin */ @@ -370,50 +357,6 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } } - /** - * Let's do this - */ - private void findRenamedMethods(ClassNode classNode) { - for (MethodNode mixinMethod : classNode.methods) { - Method method = this.classInfo.findMethod(mixinMethod); - - AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); - if (shadowAnnotation != null) { - String prefix = ASMHelper.getAnnotationValue(shadowAnnotation, "prefix", Shadow.class); - if (mixinMethod.name.startsWith(prefix)) { - ASMHelper.setVisibleAnnotation(mixinMethod, MixinRenamed.class, "originalName", mixinMethod.name); - String newName = mixinMethod.name.substring(prefix.length()); - method.renameTo(newName); - mixinMethod.name = newName; - } - } - - for (InterfaceInfo iface : this.softImplements) { - if (iface.renameMethod(mixinMethod)) { - method.renameTo(mixinMethod.name); - } - } - } - } - - /** - * Apply discovered method renames to method invokations in the mixin - */ - private void transformMethods(ClassNode classNode) { - for (MethodNode mixinMethod : classNode.methods) { - for (Iterator iter = mixinMethod.instructions.iterator(); iter.hasNext();) { - AbstractInsnNode insn = iter.next(); - if (insn instanceof MethodInsnNode) { - MethodInsnNode methodNode = (MethodInsnNode)insn; - Method method = this.classInfo.findMethodInHierarchy(methodNode, true); - if (method != null && method.isRenamed()) { - methodNode.name = method.getName(); - } - } - } - } - } - ClassInfo getClassInfo() { return this.classInfo; } @@ -492,6 +435,13 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { return this.targetClassNames; } + /** + * Get the soft implementations for this mixin + */ + List getSoftImplements() { + return Collections.unmodifiableList(this.softImplements); + } + /** * Get the synthetic inner classes for this mixin */ @@ -529,9 +479,8 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @param target * @return */ - public MixinTargetContext createContextForTarget(ClassNode target) { - ClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES); - return new MixinTargetContext(this, this.prepare(classNode), target); + public MixinTargetContext createContextFor(ClassNode target) { + return new MixinPreProcessor(this, this.getClassNode(ClassReader.EXPAND_FRAMES)).createContextFor(target); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java new file mode 100644 index 00000000..34accce2 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java @@ -0,0 +1,337 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * 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 java.util.Deque; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.AnnotationNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Field; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; +import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Constants; + + +/** + *

Mixin bytecode pre-processor. This class is responsible for bytecode pre- + * processing tasks required to be performed on mixin bytecode before the mixin + * can be applied. In previous versions the duties performed by this class were + * performed by {@link MixinInfo}.

+ * + *

Before a mixin can be applied to the target class, it is necessary to + * convert certain aspects of the mixin bytecode into the intended final form of + * the mixin, this involves for example stripping the prefix from shadow and + * soft-implemented methods. This preparation is done in two stages: first the + * target-context-insensitive transformations are applied (this also acts as a + * validation pass when the mixin is first loaded) and then transformations + * which depend on the target class are applied in a second stage.

+ * + *

The validation pass propagates method renames into the metadata tree and + * thus changes made during this phase are visible to all other mixins. The + * target-context-sensitive pass on the other hand can only operate on private + * class members for obvious reasons.

+ */ +class MixinPreProcessor { + + /** + * The mixin + */ + private final MixinInfo mixin; + + /** + * Mixin class node + */ + private final ClassNode classNode; + + private boolean prepared, attached; + + MixinPreProcessor(MixinInfo mixin, ClassNode classNode) { + this.mixin = mixin; + this.classNode = classNode; + } + + /** + * Run the first pass. Propagates changes into the metadata tree. + * + * @return Prepared classnode + */ + ClassNode prepare() { + if (!this.prepared) { + this.prepared = true; + + for (MethodNode mixinMethod : this.classNode.methods) { + Method method = this.mixin.getClassInfo().findMethod(mixinMethod); + this.prepareShadow(mixinMethod, method); + this.prepareSoftImplements(mixinMethod, method); + } + } + + return this.classNode; + } + + private void prepareShadow(MethodNode mixinMethod, Method method) { + AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); + if (shadowAnnotation == null) { + return; + } + + String prefix = ASMHelper.getAnnotationValue(shadowAnnotation, "prefix", Shadow.class); + if (mixinMethod.name.startsWith(prefix)) { + ASMHelper.setVisibleAnnotation(mixinMethod, MixinRenamed.class, "originalName", mixinMethod.name); + String newName = mixinMethod.name.substring(prefix.length()); + method.renameTo(newName); + mixinMethod.name = newName; + } + } + + private void prepareSoftImplements(MethodNode mixinMethod, Method method) { + for (InterfaceInfo iface : this.mixin.getSoftImplements()) { + if (iface.renameMethod(mixinMethod)) { + method.renameTo(mixinMethod.name); + } + } + } + + MixinTargetContext createContextFor(ClassNode target) { + this.prepare(); + MixinTargetContext context = new MixinTargetContext(this.mixin, this.classNode, target); + this.attach(context); + return context; + } + + /** + * Run the second pass, attach to the specified context + * + * @param context + */ + void attach(MixinTargetContext context) { + if (this.attached) { + throw new IllegalStateException("Preprocessor was already attached"); + } + + this.attached = true; + + // Perform context-sensitive attachment phase + this.attachMethods(context); + this.attachFields(context); + + // Apply transformations to the mixin bytecode + this.transform(context); + } + + private void attachMethods(MixinTargetContext context) { + for (MethodNode mixinMethod : this.classNode.methods) { + AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); + if (shadowAnnotation == null) { + continue; + } + + Method method = this.mixin.getClassInfo().findMethod(mixinMethod, true); + MethodNode target = MixinPreProcessor.findMethod(context.getTargetClass(), mixinMethod, shadowAnnotation); + + if (target == null) { + throw new InvalidMixinException(this.mixin, "Shadow method " + mixinMethod.name + " was not located in the target class"); + } + + if (Constants.INIT.equals(target.name)) { + throw new InvalidMixinException(this.mixin, "Nice try! Cannot alias a constructor!"); + } + + if (!target.name.equals(mixinMethod.name)) { + if ((target.access & Opcodes.ACC_PRIVATE) == 0) { + throw new InvalidMixinException(this.mixin, "Non-private method cannot be aliased. Found " + target.name); + } + + mixinMethod.name = target.name; + method.renameTo(target.name); + } + } + } + + private void attachFields(MixinTargetContext context) { + for (Iterator iter = this.classNode.fields.iterator(); iter.hasNext();) { + FieldNode mixinField = iter.next(); + AnnotationNode shadow = ASMHelper.getVisibleAnnotation(mixinField, Shadow.class); + if (!this.validateField(context, mixinField, shadow)) { + iter.remove(); + continue; + } + + context.transformDescriptor(mixinField); + + Field field = this.mixin.getClassInfo().findField(mixinField); + FieldNode target = this.findField(context.getTargetClass(), mixinField, shadow); + if (target == null) { + // If this field is a shadow field but is NOT found in the target class, that's bad, mmkay + if (shadow != null) { + throw new InvalidMixinException(this.mixin, "Shadow field " + mixinField.name + " was not located in the target class"); + } + } else { + // Check that the shadow field has a matching descriptor + if (!target.desc.equals(mixinField.desc)) { + throw new InvalidMixinException(this.mixin, "The field " + mixinField.name + " in the target class has a conflicting signature"); + } + + if (!target.name.equals(mixinField.name)) { + if ((target.access & Opcodes.ACC_PRIVATE) == 0) { + throw new InvalidMixinException(this.mixin, "Non-private field cannot be aliased. Found " + target.name); + } + + mixinField.name = target.name; + field.renameTo(target.name); + } + + // Shadow fields get stripped from the mixin class + iter.remove(); + } + } + } + + private boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) { + // Public static fields will fall foul of early static binding in java, including them in a mixin is an error condition + if (MixinTransformer.hasFlag(field, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(field, Opcodes.ACC_PRIVATE)) { + throw new InvalidMixinException(context, String.format("Mixin classes cannot contain visible static methods or fields, found %s", + field.name)); + } + + // Shadow fields can't have prefixes, it's meaningless for them anyway + String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); + if (field.name.startsWith(prefix)) { + throw new InvalidMixinException(context, String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", + field.name, context)); + } + + // Imaginary super fields get stripped from the class, but first we validate them + if (Constants.IMAGINARY_SUPER.equals(field.name)) { + if (field.access != Opcodes.ACC_PRIVATE) { + throw new InvalidMixinException(this.mixin, "Imaginary super field " + field.name + " must be private and non-final"); + } + if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { + throw new InvalidMixinException(this.mixin, "Imaginary super field " + field.name + " must have the same type as the parent mixin"); + } + return false; + } + + return true; + } + + /** + * Apply discovered method and field renames to method invocations and field + * accesses in the mixin + */ + private void transform(MixinTargetContext context) { + for (MethodNode mixinMethod : this.classNode.methods) { + for (Iterator iter = mixinMethod.instructions.iterator(); iter.hasNext();) { + AbstractInsnNode insn = iter.next(); + if (insn instanceof MethodInsnNode) { + MethodInsnNode methodNode = (MethodInsnNode)insn; + Method method = this.mixin.getClassInfo().findMethodInHierarchy(methodNode, true, true); + if (method != null && method.isRenamed()) { + methodNode.name = method.getName(); + } + } else if (insn instanceof FieldInsnNode) { + FieldInsnNode fieldNode = (FieldInsnNode)insn; + Field field = this.mixin.getClassInfo().findField(fieldNode, true); + if (field != null && field.isRenamed()) { + fieldNode.name = field.getName(); + } + } + } + } + } + + private static MethodNode findMethod(ClassNode classNode, MethodNode method, AnnotationNode shadow) { + Deque aliases = new LinkedList(); + aliases.add(method.name); + if (shadow != null) { + List aka = ASMHelper.>getAnnotationValue(shadow, "aliases"); + if (aka != null) { + aliases.addAll(aka); + } + } + + return MixinPreProcessor.findMethod(classNode, aliases, method.desc); + } + + private static MethodNode findMethod(ClassNode classNode, Deque aliases, String desc) { + String alias = aliases.poll(); + if (alias == null) { + return null; + } + + for (MethodNode target : classNode.methods) { + if (target.name.equals(alias) && target.desc.equals(desc)) { + return target; + } + } + + return MixinPreProcessor.findMethod(classNode, aliases, desc); + } + + private FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) { + Deque aliases = new LinkedList(); + aliases.add(field.name); + if (shadow != null) { + List aka = ASMHelper.>getAnnotationValue(shadow, "aliases"); + if (aka != null) { + aliases.addAll(aka); + } + } + + return this.findField(classNode, aliases, field.desc); + } + + /** + * Finds a field in the target class + * + * @param aliases + * @param desc + * @return Target field or null if not found + */ + private FieldNode findField(ClassNode classNode, Deque aliases, String desc) { + String alias = aliases.poll(); + if (alias == null) { + return null; + } + + for (FieldNode target : classNode.fields) { + if (target.name.equals(alias) && target.desc.equals(desc)) { + return target; + } + } + + return this.findField(classNode, aliases, desc); + } +} 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 bf6a313d..fcb11d01 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -45,6 +45,7 @@ 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; /** @@ -56,9 +57,6 @@ import org.spongepowered.asm.util.ASMHelper; * in the mixin to the appropriate members in the target class hierarchy. */ public class MixinTargetContext implements IReferenceMapperContext { - - private static final String INIT = ""; - private static final String IMAGINARY_SUPER = "super$"; /** * Mixin info @@ -99,16 +97,16 @@ public class MixinTargetContext implements IReferenceMapperContext { /** * ctor * - * @param info Mixin information - * @param mixin Mixin classnode + * @param mixin Mixin information + * @param classNode Mixin classnode * @param target target class */ - MixinTargetContext(MixinInfo info, ClassNode mixin, ClassNode target) { - this.mixin = info; - this.classNode = mixin; + MixinTargetContext(MixinInfo mixin, ClassNode classNode, ClassNode target) { + this.mixin = mixin; + this.classNode = classNode; this.targetClass = target; this.targetClassInfo = ClassInfo.forName(target.name); - this.inheritsFromMixin = info.getClassInfo().hasMixinInHierarchy() || this.targetClassInfo.hasMixinTargetInHierarchy(); + this.inheritsFromMixin = mixin.getClassInfo().hasMixinInHierarchy() || this.targetClassInfo.hasMixinTargetInHierarchy(); this.detachedSuper = !this.getClassNode().superName.equals(this.targetClass.superName); } @@ -196,28 +194,6 @@ public class MixinTargetContext implements IReferenceMapperContext { return type; } - /** - * Transforms field descriptors which contain mixin types to their - * appropriate target type - * - * @param field Field to transform - * @return true if the field should be processed further, false to remove it - */ - public boolean transformField(FieldNode field) { - if (MixinTargetContext.IMAGINARY_SUPER.equals(field.name)) { - if (field.access != Opcodes.ACC_PRIVATE) { - throw new InvalidMixinException(this, "Imaginary super field " + field.name + " must be private and non-final"); - } - if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { - throw new InvalidMixinException(this, "Imaginary super field " + field.name + " must have the same type as the parent mixin"); - } - return false; - } - - this.transformDescriptor(field); - return true; - } - /** * Handles "re-parenting" the method supplied, changes all references to the * mixin class to refer to the target class (for field accesses and method @@ -256,7 +232,7 @@ public class MixinTargetContext implements IReferenceMapperContext { } } else if (insn instanceof FieldInsnNode) { FieldInsnNode fieldInsn = (FieldInsnNode)insn; - if (MixinTargetContext.IMAGINARY_SUPER.equals(fieldInsn.name)) { + if (Constants.IMAGINARY_SUPER.equals(fieldInsn.name)) { this.processImaginarySuper(method, fieldInsn); iter.remove(); } @@ -288,7 +264,7 @@ public class MixinTargetContext implements IReferenceMapperContext { */ private void processImaginarySuper(MethodNode method, FieldInsnNode fieldInsn) { if (fieldInsn.getOpcode() != Opcodes.GETFIELD) { - if (MixinTargetContext.INIT.equals(method.name)) { + if (Constants.INIT.equals(method.name)) { throw new InvalidMixinException(this, "Illegal imaginary super declaration: field " + fieldInsn.name + " must not specify an initialiser"); } @@ -345,7 +321,7 @@ public class MixinTargetContext implements IReferenceMapperContext { } private void updateBinding(MethodNode method, MethodInsnNode insn, Traversal traversal) { - if (INIT.equals(method.name) || insn.owner.equals(this.targetClass.name) || this.targetClass.name.startsWith("<")) { + if (Constants.INIT.equals(method.name) || insn.owner.equals(this.targetClass.name) || this.targetClass.name.startsWith("<")) { return; } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index c642b973..d5294f26 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -66,6 +66,7 @@ import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.transformers.TreeTransformer; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Constants; /** * Transformer which applies Mixin classes to their declared target classes @@ -142,9 +143,6 @@ public class MixinTransformer extends TreeTransformer { } } - private static final String INIT = ""; - private static final String CLINIT = ""; - /** * List of opcodes which must not appear in a class initialiser, mainly a * sanity check so that if any of the specified opcodes are found, we can @@ -401,7 +399,7 @@ public class MixinTransformer extends TreeTransformer { for (MixinInfo mixin : mixins) { this.logger.log(mixin.getLoggingLevel(), "Mixing {} into {}", mixin.getName(), transformedName); - this.applyMixin(transformedName, targetClass, mixin.createContextForTarget(targetClass)); + this.applyMixin(transformedName, targetClass, mixin.createContextFor(targetClass)); } // Extension point @@ -500,54 +498,15 @@ public class MixinTransformer extends TreeTransformer { * @param mixin */ private void applyMixinFields(ClassNode targetClass, MixinTargetContext mixin) { - for (Iterator iter = mixin.getClassNode().fields.iterator(); iter.hasNext();) { - FieldNode field = iter.next(); - AnnotationNode shadow = ASMHelper.getVisibleAnnotation(field, Shadow.class); - this.validateField(mixin, field, shadow); - if (!mixin.transformField(field)) { - iter.remove(); - continue; - } - - FieldNode target = this.findTargetField(targetClass, field); + for (FieldNode field : mixin.getClassNode().fields) { + FieldNode target = MixinTransformer.findTargetField(targetClass, field); if (target == null) { - // If this field is a shadow field but is NOT found in the target class, that's bad, mmkay - if (shadow != null) { - throw new InvalidMixinException(mixin, String.format("Shadow field %s was not located in the target class", field.name)); - } - // This is just a local field, so add it targetClass.fields.add(field); - } else { - // Check that the shadow field has a matching descriptor - if (!target.desc.equals(field.desc)) { - throw new InvalidMixinException(mixin, String.format("The field %s in the target class has a conflicting signature", field.name)); - } } } } - /** - * Field sanity checks - * @param mixin - * @param field - * @param shadow - */ - private void validateField(MixinTargetContext mixin, FieldNode field, AnnotationNode shadow) { - // Public static fields will fall foul of early static binding in java, including them in a mixin is an error condition - if (MixinTransformer.hasFlag(field, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(field, Opcodes.ACC_PRIVATE)) { - throw new InvalidMixinException(mixin, String.format("Mixin classes cannot contain visible static methods or fields, found %s", - field.name)); - } - - // Shadow fields can't have prefixes, it's meaningless for them anyway - String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); - if (field.name.startsWith(prefix)) { - throw new InvalidMixinException(mixin, String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", - field.name, mixin)); - } - } - /** * Mixin methods from the mixin class into the target class * @@ -560,16 +519,12 @@ public class MixinTransformer extends TreeTransformer { mixin.transformMethod(mixinMethod); boolean isShadow = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class) != null; - boolean isOverwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null; - boolean isAbstract = MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_ABSTRACT); - if (isShadow || isAbstract) { - // For shadow (and abstract, which can be used as a shorthand for Shadow) methods, we just check they're present - MethodNode target = this.findTargetMethod(targetClass, mixinMethod); - if (target == null) { - throw new InvalidMixinException(mixin, String.format("Shadow method %s was not located in the target class", mixinMethod.name)); - } + if (isShadow) { + continue; } else if (!mixinMethod.name.startsWith("<")) { + boolean isOverwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null; + if (MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC) @@ -579,7 +534,7 @@ public class MixinTransformer extends TreeTransformer { } this.mergeMethod(targetClass, mixin, mixinMethod, isOverwrite); - } else if (MixinTransformer.CLINIT.equals(mixinMethod.name)) { + } else if (Constants.CLINIT.equals(mixinMethod.name)) { // Class initialiser insns get appended this.appendInsns(targetClass, mixinMethod.name, mixinMethod); } @@ -596,7 +551,7 @@ public class MixinTransformer extends TreeTransformer { * {@link Overwrite} annotation */ private void mergeMethod(ClassNode targetClass, MixinTargetContext mixin, MethodNode method, boolean isOverwrite) { - MethodNode target = this.findTargetMethod(targetClass, method); + MethodNode target = MixinTransformer.findTargetMethod(targetClass, method); if (target != null) { if (this.alreadyMerged(targetClass, mixin, method, isOverwrite, target)) { @@ -795,7 +750,7 @@ public class MixinTransformer extends TreeTransformer { // Patch the initialiser into the target class ctors for (MethodNode method : targetClass.methods) { - if (MixinTransformer.INIT.equals(method.name)) { + if (Constants.INIT.equals(method.name)) { method.maxStack = Math.max(method.maxStack, ctor.maxStack); this.injectInitialiser(method, initialiser); } @@ -809,7 +764,7 @@ public class MixinTransformer extends TreeTransformer { MethodNode ctor = null; for (MethodNode mixinMethod : mixin.getClassNode().methods) { - if (MixinTransformer.INIT.equals(mixinMethod.name)) { + if (Constants.INIT.equals(mixinMethod.name)) { boolean hasLineNumbers = false; for (Iterator iter = mixinMethod.instructions.iterator(); iter.hasNext();) { if (iter.next() instanceof LineNumberNode) { @@ -846,7 +801,7 @@ public class MixinTransformer extends TreeTransformer { if (insn instanceof LineNumberNode) { line = ((LineNumberNode)insn).line; } else if (insn instanceof MethodInsnNode) { - if (insn.getOpcode() == Opcodes.INVOKESPECIAL && MixinTransformer.INIT.equals(((MethodInsnNode)insn).name) && superIndex == -1) { + if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.INIT.equals(((MethodInsnNode)insn).name) && superIndex == -1) { superIndex = ctor.instructions.indexOf(insn); start = line; } @@ -943,7 +898,7 @@ public class MixinTransformer extends TreeTransformer { private void injectInitialiser(MethodNode ctor, InsnList initialiser) { for (Iterator iter = ctor.instructions.iterator(0); iter.hasNext();) { AbstractInsnNode insn = iter.next(); - if (insn.getOpcode() == Opcodes.INVOKESPECIAL && MixinTransformer.INIT.equals(((MethodInsnNode)insn).name)) { + if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.INIT.equals(((MethodInsnNode)insn).name)) { ctor.instructions.insert(insn, initialiser); } } @@ -985,7 +940,7 @@ public class MixinTransformer extends TreeTransformer { * @param searchFor * @return Target method matching searchFor, or null if not found */ - private MethodNode findTargetMethod(ClassNode targetClass, MethodNode searchFor) { + private static MethodNode findTargetMethod(ClassNode targetClass, MethodNode searchFor) { for (MethodNode target : targetClass.methods) { if (target.name.equals(searchFor.name) && target.desc.equals(searchFor.desc)) { return target; @@ -1002,7 +957,7 @@ public class MixinTransformer extends TreeTransformer { * @param searchFor * @return Target field matching searchFor, or null if not found */ - private FieldNode findTargetField(ClassNode targetClass, FieldNode searchFor) { + private static FieldNode findTargetField(ClassNode targetClass, FieldNode searchFor) { for (FieldNode target : targetClass.fields) { if (target.name.equals(searchFor.name)) { return target; @@ -1019,7 +974,7 @@ public class MixinTransformer extends TreeTransformer { * @param flag * @return True if the specified flag is set in this method's access flags */ - private static boolean hasFlag(MethodNode method, int flag) { + static boolean hasFlag(MethodNode method, int flag) { return (method.access & flag) == flag; } @@ -1030,7 +985,7 @@ public class MixinTransformer extends TreeTransformer { * @param flag * @return True if the specified flag is set in this field's access flags */ - private static boolean hasFlag(FieldNode field, int flag) { + static boolean hasFlag(FieldNode field, int flag) { return (field.access & flag) == flag; } } diff --git a/src/main/java/org/spongepowered/asm/util/Constants.java b/src/main/java/org/spongepowered/asm/util/Constants.java new file mode 100644 index 00000000..06bd1fc6 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/Constants.java @@ -0,0 +1,38 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * 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; + + +/** + * Miscellaneous shared constants + */ +public abstract class Constants { + + public static final String INIT = ""; + public static final String CLINIT = ""; + public static final String IMAGINARY_SUPER = "super$"; + + private Constants() {} +}