From 55653f325da7bc9c7106009f2716d038758d7dfa Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 20 Feb 2015 17:26:53 +0000 Subject: [PATCH] Handle remapping of field/method/cast/whatever insns in inherited mixins --- .../asm/mixin/MixinEnvironment.java | 2 +- .../struct/CallbackInjectionInfo.java | 4 +- .../mixin/injection/struct/InjectionInfo.java | 8 +- .../struct/ModifyArgInjectionInfo.java | 4 +- .../struct/RedirectInjectionInfo.java | 4 +- .../asm/mixin/transformer/ClassInfo.java | 73 +++ .../asm/mixin/transformer/MixinConfig.java | 2 - .../asm/mixin/transformer/MixinData.java | 223 --------- .../asm/mixin/transformer/MixinInfo.java | 31 +- .../mixin/transformer/MixinTargetContext.java | 445 ++++++++++++++++++ .../mixin/transformer/MixinTransformer.java | 116 ++--- 11 files changed, 589 insertions(+), 323 deletions(-) delete mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 74dd6671..43d42a61 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -158,7 +158,7 @@ public class MixinEnvironment { } public void setActiveTransformer(IClassTransformer transformer) { - Launch.blackboard.put(MixinEnvironment.TRANSFORMER_KEY, this); + Launch.blackboard.put(MixinEnvironment.TRANSFORMER_KEY, transformer); } public MixinEnvironment setSide(Side side) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java index 48850e51..7b111a51 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java @@ -28,12 +28,12 @@ import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.injection.callback.CallbackInjector; import org.spongepowered.asm.mixin.injection.code.Injector; -import org.spongepowered.asm.mixin.transformer.MixinData; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; public class CallbackInjectionInfo extends InjectionInfo { - protected CallbackInjectionInfo(MixinData mixin, MethodNode method, AnnotationNode annotation) { + protected CallbackInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { super(mixin, method, annotation); } 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 2290e7dd..0161905e 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 @@ -40,7 +40,7 @@ import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.code.Injector; -import org.spongepowered.asm.mixin.transformer.MixinData; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; @@ -65,7 +65,7 @@ public abstract class InjectionInfo { /** * Mixin data */ - protected final MixinData mixin; + protected final MixinTargetContext mixin; /** @@ -96,7 +96,7 @@ public abstract class InjectionInfo { * @param method Injector method * @param annotation Annotation to parse */ - protected InjectionInfo(MixinData mixin, MethodNode method, AnnotationNode annotation) { + protected InjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { this.annotation = annotation; this.method = method; this.mixin = mixin; @@ -236,7 +236,7 @@ public abstract class InjectionInfo { } @SuppressWarnings("unchecked") - public static InjectionInfo parse(MixinData mixin, MethodNode method) { + public static InjectionInfo parse(MixinTargetContext mixin, MethodNode method) { AnnotationNode annotation = null; try { annotation = ASMHelper.getSingleVisibleAnnotation(method, Inject.class, ModifyArg.class, Redirect.class); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java index 85378c98..c6b7f25e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java @@ -28,12 +28,12 @@ import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.invoke.ModifyArgInjector; -import org.spongepowered.asm.mixin.transformer.MixinData; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; public class ModifyArgInjectionInfo extends InjectionInfo { - public ModifyArgInjectionInfo(MixinData mixin, MethodNode method, AnnotationNode annotation) { + public ModifyArgInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { super(mixin, method, annotation); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/RedirectInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/RedirectInjectionInfo.java index b39948ed..28f5375c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/RedirectInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/RedirectInjectionInfo.java @@ -28,11 +28,11 @@ import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.invoke.RedirectInjector; -import org.spongepowered.asm.mixin.transformer.MixinData; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; public class RedirectInjectionInfo extends InjectionInfo { - public RedirectInjectionInfo(MixinData mixin, MethodNode method, AnnotationNode annotation) { + public RedirectInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { super(mixin, method, annotation); } 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 beb9b74f..9979c5c9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -262,6 +262,12 @@ class ClassInfo extends TreeInfo { */ private final Set mixins = new HashSet(); + /** + * Map of mixin types to corresponding supertypes, to avoid repeated + * lookups + */ + private final Map correspondingTypes = new HashMap(); + /** * Mixin info if this class is a mixin itself */ @@ -592,6 +598,73 @@ class ClassInfo extends TreeInfo { return null; } + /** + * Walks up this class's hierarchy to find the first class targetted by the + * specified mixin. This is used during mixin application to translate a + * mixin reference to a "real class" reference in the context of this + * class. + * + * @param mixin Mixin class to search for + * @return corresponding (target) class for the specified mixin or null if + * no corresponding mixin was found + */ + ClassInfo findCorrespondingType(ClassInfo mixin) { + if (mixin == null || !mixin.isMixin || this.isMixin) { + return null; + } + + ClassInfo correspondingType = this.correspondingTypes.get(mixin); + if (correspondingType == null) { + correspondingType = this.findSuperTypeForMixin(mixin); + this.correspondingTypes.put(mixin, correspondingType); + } + return correspondingType; + } + + /* (non-Javadoc) + * Only used by findCorrespondingType(), used as a convenience so that + * sanity checks and caching can be handled more elegantly + */ + private ClassInfo findSuperTypeForMixin(ClassInfo mixin) { + ClassInfo superClass = this; + + while (superClass != null && superClass != ClassInfo.OBJECT) { + for (MixinInfo minion : superClass.mixins) { + if (minion.getClassInfo().equals(mixin)) { + return superClass; + } + } + + superClass = superClass.getSuperClass(); + } + + return null; + } + + /** + * Find out whether this (mixin) class has another mixin in its superclass + * hierarchy. This method always returns false for non-mixin classes. + * + * @return true if and only if one or more mixins are found in the hierarchy + * of this mixin + */ + public boolean hasMixinInHierarchy() { + if (!this.isMixin) { + return false; + } + + ClassInfo superClass = this.getSuperClass(); + + while (superClass != null && superClass != ClassInfo.OBJECT) { + if (superClass.isMixin) { + return true; + } + superClass = superClass.getSuperClass(); + } + + return false; + } + /** * Finds the specified private or protected method in this class's hierarchy * diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index 2e2803bf..12da31ee 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -174,8 +174,6 @@ class MixinConfig implements Comparable { this.mixinPackage += "."; } - Launch.classLoader.addClassLoaderExclusion(this.mixinPackage); - if (this.refMapperConfig == null) { if (this.plugin != null) { this.refMapperConfig = this.plugin.getRefMapperConfig(); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java deleted file mode 100644 index b8ac830a..00000000 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * 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.HashMap; -import java.util.Map; -import java.util.Set; - -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodNode; -import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; -import org.spongepowered.asm.mixin.injection.struct.Target; - - -/** - * Data for applying a mixin. Keeps a copy of the mixin tree and also handles - * any pre-transformations required by the mixin class itself such as method - * renaming and any other pre-processing of the mixin bytecode. - */ -public class MixinData implements IReferenceMapperContext { - - /** - * Mixin info - */ - private final MixinInfo mixin; - - /** - * Tree - */ - private final ClassNode classNode; - - /** - * - */ - private final ClassNode targetClass; - - /** - * Target ClassInfo - */ - private final ClassInfo targetClassInfo; - - /** - * Information about methods in the target class, used to keep track of - * transformations we apply - */ - private final Map targetMethods = new HashMap(); - - /** - * ctor - * - * @param info Mixin information - * @param mixin Mixin classnode - * @param target target class - */ - MixinData(MixinInfo info, ClassNode mixin, ClassNode target) { - this.mixin = info; - this.classNode = mixin; - this.targetClass = target; - this.targetClassInfo = ClassInfo.forName(target.name); - } - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return this.mixin.toString(); - } - - /** - * Get the mixin tree - * - * @return mixin tree - */ - public ClassNode getClassNode() { - return this.classNode; - } - - /** - * Get the mixin class name - * - * @return the mixin class name - */ - public String getClassName() { - return this.mixin.getClassName(); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.mixin.transformer.IReferenceMapperContext - * #getClassRef() - */ - @Override - public String getClassRef() { - return this.mixin.getClassRef(); - } - - /** - * Get the target class reference - * - * @return the reference of the target class (only valid on single-target - * mixins) - */ - public String getTargetClassRef() { - return this.targetClass.name; - } - - /** - * Get the target class - * - * @return the target class - */ - public ClassNode getTargetClass() { - return this.targetClass; - } - - /** - * Get the target classinfo - * - * @return the target class info - */ - public ClassInfo getTargetClassInfo() { - return this.targetClassInfo; - } - - /** - * Get a target method handle from the target class - * - * @param method method to get a target handle for - * @return new or existing target handle for the supplied method - */ - public Target getTargetMethod(MethodNode method) { - if (!this.targetClass.methods.contains(method)) { - throw new IllegalArgumentException("Invalid target method supplied to getTargetMethod()"); - } - - String targetName = method.name + method.desc; - Target target = this.targetMethods.get(targetName); - if (target == null) { - target = new Target(method); - this.targetMethods.put(targetName, target); - } - return target; - } - - /** - * Get the mixin priority - * - * @return the priority (only meaningful in relation to other mixins) - */ - public int getPriority() { - return this.mixin.getPriority(); - } - - /** - * Get all interfaces for this mixin - * - * @return mixin interfaces - */ - public Set getInterfaces() { - return this.mixin.getInterfaces(); - } - - /** - * Get whether to propogate the source file attribute from a mixin onto the - * target class - * - * @return true if the sourcefile property should be set on the target class - */ - public boolean shouldSetSourceFile() { - return this.mixin.getParent().shouldSetSourceFile(); - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.mixin.transformer.IReferenceMapperContext - * #getReferenceMapper() - */ - @Override - public ReferenceMapper getReferenceMapper() { - return this.mixin.getParent().getReferenceMapper(); - } - - /** - * Called immediately before the mixin is applied to targetClass - * - * @param transformedName Target class's transformed name - * @param targetClass Target class - */ - public void preApply(String transformedName, ClassNode targetClass) { - this.mixin.preApply(transformedName, targetClass); - } - - /** - * Called immediately after the mixin is applied to targetClass - * - * @param transformedName Target class's transformed name - * @param targetClass Target class - */ - public void postApply(String transformedName, ClassNode targetClass) { - this.mixin.postApply(transformedName, targetClass); - } -} 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 3ee5037c..196c9810 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -25,6 +25,7 @@ package org.spongepowered.asm.mixin.transformer; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -32,6 +33,9 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.objectweb.asm.ClassReader; @@ -68,6 +72,8 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { */ static int mixinOrder = 0; + static final Set invalidClasses = MixinInfo.$getInvalidClassesSet(); + /** * Logger */ @@ -465,14 +471,14 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } /** - * Get a new mixin data container for this info + * Get a new mixin target context object for the specified target * * @param target * @return */ - public MixinData createData(ClassNode target) { + public MixinTargetContext createContextForTarget(ClassNode target) { ClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES); - return new MixinData(this, this.prepare(classNode), target); + return new MixinTargetContext(this, this.prepare(classNode), target); } /** @@ -492,6 +498,13 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { this.logger.warn("Failed to load mixin %s, the specified mixin will not be applied", mixinClassName); throw new InvalidMixinException("An error was encountered whilst loading the mixin class", ex); } + + // Inject the mixin class name into the LaunchClassLoader's invalid + // classes set so that any classes referencing the mixin directly will + // cause the game to crash + if (MixinInfo.invalidClasses != null) { + MixinInfo.invalidClasses.add(mixinClassName); + } return mixinBytes; } @@ -535,4 +548,16 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { public String toString() { return String.format("%s:%s", this.parent.getName(), this.name); } + + @SuppressWarnings("unchecked") + private static Set $getInvalidClassesSet() { + try { + Field invalidClasses = LaunchClassLoader.class.getDeclaredField("invalidClasses"); + invalidClasses.setAccessible(true); + return (Set)invalidClasses.get(Launch.classLoader); + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java new file mode 100644 index 00000000..bf0bb30d --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -0,0 +1,445 @@ +/* + * 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.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +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.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.TypeInsnNode; +import org.spongepowered.asm.mixin.InvalidMixinException; +import org.spongepowered.asm.mixin.MixinTransformerError; +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; + + +/** + * This object keeps track of data for applying a mixin to a specific target + * class during a mixin application. This is a single-use object which + * acts as both a handle information we need when applying the mixin (such as + * the actual mixin ClassNode and the target ClassNode) and a gateway to + * context-sensitive operations such as re-targetting method and field accesses + * in the mixin to the appropriate members in the target class hierarchy. + */ +public class MixinTargetContext implements IReferenceMapperContext { + + private static final String INIT = ""; + + /** + * Mixin info + */ + private final MixinInfo mixin; + + /** + * Tree + */ + private final ClassNode classNode; + + /** + * + */ + private final ClassNode targetClass; + + /** + * Target ClassInfo + */ + private final ClassInfo targetClassInfo; + + /** + * Information about methods in the target class, used to keep track of + * transformations we apply + */ + private final Map targetMethods = new HashMap(); + + /** + * True if this mixin inherits from a mixin at any point in its hierarchy + */ + private final boolean inheritsFromMixin; + + /** + * ctor + * + * @param info Mixin information + * @param mixin Mixin classnode + * @param target target class + */ + MixinTargetContext(MixinInfo info, ClassNode mixin, ClassNode target) { + this.mixin = info; + this.classNode = mixin; + this.targetClass = target; + this.targetClassInfo = ClassInfo.forName(target.name); + this.inheritsFromMixin = info.getClassInfo().hasMixinInHierarchy(); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.mixin.toString(); + } + + /** + * Get the mixin tree + * + * @return mixin tree + */ + public ClassNode getClassNode() { + return this.classNode; + } + + /** + * Get the mixin class name + * + * @return the mixin class name + */ + public String getClassName() { + return this.mixin.getClassName(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.IReferenceMapperContext + * #getClassRef() + */ + @Override + public String getClassRef() { + return this.mixin.getClassRef(); + } + + /** + * Get the target class reference + * + * @return the reference of the target class (only valid on single-target + * mixins) + */ + public String getTargetClassRef() { + return this.targetClass.name; + } + + /** + * Get the target class + * + * @return the target class + */ + public ClassNode getTargetClass() { + return this.targetClass; + } + + /** + * Get the target classinfo + * + * @return the target class info + */ + public ClassInfo getTargetClassInfo() { + return this.targetClassInfo; + } + + /** + * Find the corresponding class type for the supplied mixin class in this + * mixin target's hierarchy + * + * @param mixin Mixin class to discover + * @return Transformed + */ + public ClassInfo findRealType(ClassInfo mixin) { + if (mixin == this.mixin.getClassInfo()) { + return this.targetClassInfo; + } + + ClassInfo type = this.targetClassInfo.findCorrespondingType(mixin); + if (type == null) { + throw new InvalidMixinException("Resolution error: unable to find corresponding type for " + + mixin + " in hierarchy of " + this.targetClassInfo); + } + + return type; + } + + /** + * Transforms field descriptors which contain mixin types to their + * appropriate target type + * + * @param field + */ + public void transformField(FieldNode field) { + this.transformDescriptor(field); + } + + /** + * Handles "re-parenting" the method supplied, changes all references to the + * mixin class to refer to the target class (for field accesses and method + * invokations) and also handles fixing up the targets of INVOKESPECIAL + * opcodes for mixins with detached targets. + * + * @param targetClass + * @param mixin + * @param method + */ + public void transformMethod(MethodNode method) { + this.transformDescriptor(method); + + String fromClass = this.getClassRef(); + boolean detached = !this.getClassNode().superName.equals(this.targetClass.superName); + + Iterator iter = method.instructions.iterator(); + while (iter.hasNext()) { + AbstractInsnNode insn = iter.next(); + + if (insn instanceof MethodInsnNode) { + MethodInsnNode methodInsn = (MethodInsnNode)insn; + this.transformDescriptor(methodInsn); + if (methodInsn.owner.equals(fromClass)) { + methodInsn.owner = this.targetClass.name; + } else if (detached && methodInsn.getOpcode() == Opcodes.INVOKESPECIAL) { + this.updateStaticBindings(this.targetClass, this, method, methodInsn); + } + } else if (insn instanceof FieldInsnNode) { + FieldInsnNode fieldInsn = (FieldInsnNode)insn; + this.transformDescriptor(fieldInsn); + if (fieldInsn.owner.equals(fromClass)) { + fieldInsn.owner = this.targetClass.name; + } + } else if (insn instanceof TypeInsnNode) { + this.transformDescriptor((TypeInsnNode)insn); + } + } + } + + /** + * Update INVOKESPECIAL opcodes to target the topmost class in the hierarchy + * which contains the specified method. + * + * @param targetClass + * @param mixin + * @param method + * @param insn + */ + private void updateStaticBindings(ClassNode targetClass, MixinTargetContext mixin, MethodNode method, MethodInsnNode insn) { + if (INIT.equals(method.name) || insn.owner.equals(targetClass.name) || targetClass.name.startsWith("<")) { + return; + } + + ClassInfo targetClassInfo = ClassInfo.forName(targetClass.name); + Method superMethod = targetClassInfo.findMethodInHierarchy(insn.name, insn.desc, false, Traversal.SUPER); + if (superMethod != null) { + if (superMethod.getOwner().isMixin()) { + throw new InvalidMixinException("Invalid INVOKESPECIAL in " + mixin + " resolved " + insn.owner + " -> " + superMethod.getOwner() + + " for " + insn.name + insn.desc); + } + insn.owner = superMethod.getOwner().getName(); + } else if (ClassInfo.forName(insn.owner).isMixin()) { + throw new MixinTransformerError("Error resolving INVOKESPECIAL target for " + insn.owner + "." + insn.name + " in " + mixin); + } + } + + /** + * Transforms a field descriptor in the context of this mixin target + * + * @param field + */ + public void transformDescriptor(FieldNode field) { + if (!this.inheritsFromMixin) { + return; + } + field.desc = this.transformSingleDescriptor(field.desc, false); + } + + /** + * Transforms a field insn descriptor in the context of this mixin target + * + * @param field + */ + 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 + * + * @param method + */ + public void transformDescriptor(MethodNode method) { + if (!this.inheritsFromMixin) { + return; + } + method.desc = this.transformMethodDescriptor(method.desc); + } + + /** + * Transforms a method insn descriptor in the context of this mixin target + * + * @param method + */ + public void transformDescriptor(MethodInsnNode method) { + if (!this.inheritsFromMixin) { + return; + } + method.desc = this.transformMethodDescriptor(method.desc); + } + + /** + * Transforms a type insn descriptor in the context of this mixin target + * + * @param typeInsn + */ + public void transformDescriptor(TypeInsnNode typeInsn) { + if (!this.inheritsFromMixin) { + return; + } + typeInsn.desc = this.transformSingleDescriptor(typeInsn.desc, true); + } + + private String transformSingleDescriptor(Type type) { + if (type.getSort() < Type.ARRAY) { + return type.toString(); + } + + return this.transformSingleDescriptor(type.toString(), false); + } + + private String transformSingleDescriptor(String desc, boolean isObject) { + String type = desc; + while (type.startsWith("[") || type.startsWith("L")) { + if (type.startsWith("[")) { + type = type.substring(1); + continue; + } + type = type.substring(1, type.indexOf(";")); + isObject = true; + } + + if (!isObject) { + return desc; + } + + ClassInfo typeInfo = ClassInfo.forName(type); + + if (!typeInfo.isMixin()) { + return desc; + } + + return desc.replace(type, this.findRealType(typeInfo).toString()); + } + + private String transformMethodDescriptor(String desc) { + StringBuilder newDesc = new StringBuilder(); + newDesc.append('('); + for (Type arg : Type.getArgumentTypes(desc)) { + newDesc.append(this.transformSingleDescriptor(arg)); + } + return newDesc.append(')').append(this.transformSingleDescriptor(Type.getReturnType(desc))).toString(); + } + + /** + * Get a target method handle from the target class + * + * @param method method to get a target handle for + * @return new or existing target handle for the supplied method + */ + public Target getTargetMethod(MethodNode method) { + if (!this.targetClass.methods.contains(method)) { + throw new IllegalArgumentException("Invalid target method supplied to getTargetMethod()"); + } + + String targetName = method.name + method.desc; + Target target = this.targetMethods.get(targetName); + if (target == null) { + target = new Target(method); + this.targetMethods.put(targetName, target); + } + return target; + } + + /** + * Get the mixin priority + * + * @return the priority (only meaningful in relation to other mixins) + */ + public int getPriority() { + return this.mixin.getPriority(); + } + + /** + * Get all interfaces for this mixin + * + * @return mixin interfaces + */ + public Set getInterfaces() { + return this.mixin.getInterfaces(); + } + + /** + * Get whether to propogate the source file attribute from a mixin onto the + * target class + * + * @return true if the sourcefile property should be set on the target class + */ + public boolean shouldSetSourceFile() { + return this.mixin.getParent().shouldSetSourceFile(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.IReferenceMapperContext + * #getReferenceMapper() + */ + @Override + public ReferenceMapper getReferenceMapper() { + return this.mixin.getParent().getReferenceMapper(); + } + + /** + * Called immediately before the mixin is applied to targetClass + * + * @param transformedName Target class's transformed name + * @param targetClass Target class + */ + public void preApply(String transformedName, ClassNode targetClass) { + this.mixin.preApply(transformedName, targetClass); + } + + /** + * Called immediately after the mixin is applied to targetClass + * + * @param transformedName Target class's transformed name + * @param targetClass Target class + */ + public void postApply(String transformedName, ClassNode targetClass) { + this.mixin.postApply(transformedName, targetClass); + } +} 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 58a1eafb..decde152 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -48,7 +48,6 @@ 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.FieldInsnNode; import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.LabelNode; @@ -62,8 +61,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.injection.struct.InjectionInfo; -import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; -import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal; import org.spongepowered.asm.transformers.TreeTransformer; import org.spongepowered.asm.util.ASMHelper; @@ -352,7 +349,7 @@ public class MixinTransformer extends TreeTransformer { for (MixinInfo mixin : mixins) { this.logger.info("Mixing {} into {}", mixin.getName(), transformedName); - this.applyMixin(transformedName, targetClass, mixin.createData(targetClass)); + this.applyMixin(transformedName, targetClass, mixin.createContextForTarget(targetClass)); } // Extension point @@ -389,7 +386,7 @@ public class MixinTransformer extends TreeTransformer { * @param targetClass Target class * @param mixin Mixin to apply */ - protected void applyMixin(String transformedName, ClassNode targetClass, MixinData mixin) { + protected void applyMixin(String transformedName, ClassNode targetClass, MixinTargetContext mixin) { try { mixin.preApply(transformedName, targetClass); this.applyMixinInterfaces(targetClass, mixin); @@ -410,7 +407,7 @@ public class MixinTransformer extends TreeTransformer { * @param targetClass * @param mixin */ - private void applyMixinInterfaces(ClassNode targetClass, MixinData mixin) { + private void applyMixinInterfaces(ClassNode targetClass, MixinTargetContext mixin) { for (String interfaceName : mixin.getInterfaces()) { if (!targetClass.interfaces.contains(interfaceName)) { targetClass.interfaces.add(interfaceName); @@ -424,7 +421,7 @@ public class MixinTransformer extends TreeTransformer { * @param targetClass * @param mixin */ - private void applyMixinAttributes(ClassNode targetClass, MixinData mixin) { + private void applyMixinAttributes(ClassNode targetClass, MixinTargetContext mixin) { if (mixin.shouldSetSourceFile()) { targetClass.sourceFile = mixin.getClassNode().sourceFile; } @@ -439,18 +436,11 @@ public class MixinTransformer extends TreeTransformer { * @param targetClass * @param mixin */ - private void applyMixinFields(ClassNode targetClass, MixinData mixin) { + private void applyMixinFields(ClassNode targetClass, MixinTargetContext mixin) { for (FieldNode field : mixin.getClassNode().fields) { - // 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(String.format("Mixin classes cannot contain visible static methods or fields, found %s", field.name)); - } - AnnotationNode shadow = ASMHelper.getVisibleAnnotation(field, Shadow.class); - String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); - if (field.name.startsWith(prefix)) { - throw new InvalidMixinException(String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", field.name, mixin)); - } + this.validateField(mixin, field, shadow); + mixin.transformField(field); FieldNode target = this.findTargetField(targetClass, field); if (target == null) { @@ -470,16 +460,35 @@ public class MixinTransformer extends TreeTransformer { } } + /** + * 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(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(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 * * @param targetClass * @param mixin */ - private void applyMixinMethods(ClassNode targetClass, MixinData mixin) { + private void applyMixinMethods(ClassNode targetClass, MixinTargetContext mixin) { for (MethodNode mixinMethod : mixin.getClassNode().methods) { // Reparent all mixin methods into the target class - this.transformMethod(mixin, targetClass, mixinMethod); + mixin.transformMethod(mixinMethod); boolean isShadow = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class) != null; boolean isOverwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null; @@ -508,67 +517,6 @@ public class MixinTransformer extends TreeTransformer { } } - /** - * Handles "re-parenting" the method supplied, changes all references to the - * mixin class to refer to the target class (for field accesses and method - * invokations) and also handles fixing up the targets of INVOKESPECIAL - * opcodes for mixins with detached targets. - * - * @param mixin - * @param target - * @param method - */ - private void transformMethod(MixinData mixin, ClassNode target, MethodNode method) { - String fromClass = mixin.getClassRef(); - boolean detached = !mixin.getClassNode().superName.equals(target.superName); - - Iterator iter = method.instructions.iterator(); - while (iter.hasNext()) { - AbstractInsnNode insn = iter.next(); - - if (insn instanceof MethodInsnNode) { - MethodInsnNode methodInsn = (MethodInsnNode) insn; - if (methodInsn.owner.equals(fromClass)) { - methodInsn.owner = target.name; - } else if (detached && methodInsn.getOpcode() == Opcodes.INVOKESPECIAL) { - this.updateStaticBindings(mixin, target, method, methodInsn); - } - } else if (insn instanceof FieldInsnNode) { - FieldInsnNode fieldInsn = (FieldInsnNode) insn; - if (fieldInsn.owner.equals(fromClass)) { - fieldInsn.owner = target.name; - } - } - } - } - - /** - * Update INVOKESPECIAL opcodes to target the topmost class in the hierarchy - * which contains the specified method. - * - * @param mixin - * @param target - * @param method - * @param insn - */ - private void updateStaticBindings(MixinData mixin, ClassNode target, MethodNode method, MethodInsnNode insn) { - if (INIT.equals(method.name) || insn.owner.equals(target.name) || target.name.startsWith("<")) { - return; - } - - ClassInfo targetClass = ClassInfo.forName(target.name); - Method superMethod = targetClass.findMethodInHierarchy(insn.name, insn.desc, false, Traversal.SUPER); - if (superMethod != null) { - if (superMethod.getOwner().isMixin()) { - throw new InvalidMixinException("Invalid INVOKESPECIAL in " + mixin + " resolved " + insn.owner + " -> " + superMethod.getOwner() - + " for " + insn.name + insn.desc); - } - insn.owner = superMethod.getOwner().getName(); - } else if (ClassInfo.forName(insn.owner).isMixin()) { - throw new MixinTransformerError("Error resolving INVOKESPECIAL target for " + insn.owner + "." + insn.name + " in " + mixin); - } - } - /** * Attempts to merge the supplied method into the target class * @@ -578,7 +526,7 @@ public class MixinTransformer extends TreeTransformer { * @param isOverwrite true if the method is annotated with an * {@link Overwrite} annotation */ - private void mergeMethod(ClassNode targetClass, MixinData mixin, MethodNode method, boolean isOverwrite) { + private void mergeMethod(ClassNode targetClass, MixinTargetContext mixin, MethodNode method, boolean isOverwrite) { MethodNode target = this.findTargetMethod(targetClass, method); if (target != null) { @@ -667,7 +615,7 @@ public class MixinTransformer extends TreeTransformer { * @param targetClass * @param mixin */ - private void applyInitialisers(ClassNode targetClass, MixinData mixin) { + private void applyInitialisers(ClassNode targetClass, MixinTargetContext mixin) { // Try to find a suitable constructor, we need a constructor with line numbers in order to extract the initialiser MethodNode ctor = this.getConstructor(mixin); if (ctor == null) { @@ -691,7 +639,7 @@ public class MixinTransformer extends TreeTransformer { /** * Finds a suitable ctor for reading the instance initialiser bytecode */ - private MethodNode getConstructor(MixinData mixin) { + private MethodNode getConstructor(MixinTargetContext mixin) { MethodNode ctor = null; for (MethodNode mixinMethod : mixin.getClassNode().methods) { @@ -828,7 +776,7 @@ public class MixinTransformer extends TreeTransformer { * @param targetClass * @param mixin */ - private void applyInjections(ClassNode targetClass, MixinData mixin) { + private void applyInjections(ClassNode targetClass, MixinTargetContext mixin) { for (MethodNode method : targetClass.methods) { InjectionInfo injectInfo = InjectionInfo.parse(mixin, method); if (injectInfo == null) {