From 46e20b1f4e864a7d7b27bb61e45cb8b59c4fe26f Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 15 Jan 2016 15:35:09 +0000 Subject: [PATCH] Interface mixins dev --- gradle.properties | 2 +- .../tools/obfuscation/AnnotatedMixin.java | 23 +++- .../tools/obfuscation/MixinProcessor.java | 4 +- .../validation/TargetValidator.java | 22 ++- .../asm/launch/MixinBootstrap.java | 2 +- .../asm/mixin/MixinEnvironment.java | 8 +- .../asm/mixin/extensibility/IMixinInfo.java | 8 ++ .../injection/InvalidInjectionException.java | 12 ++ .../asm/mixin/transformer/ClassInfo.java | 2 +- .../transformer/IMixinTransformerModule.java | 16 +-- .../transformer/InterfaceMixinApplicator.java | 108 +++++++++++++++ .../InterfaceMixinPreProcessor.java | 82 +++++++++++ .../mixin/transformer/MixinApplicator.java | 79 ++++++----- .../asm/mixin/transformer/MixinInfo.java | 78 +++++++++-- .../mixin/transformer/MixinPreProcessor.java | 72 ++++++---- .../mixin/transformer/MixinTransformer.java | 54 ++++---- .../MixinTransformerModuleCheckClass.java | 17 +-- ...ixinTransformerModuleInterfaceChecker.java | 16 +-- .../mixin/transformer/TargetClassContext.java | 130 ++++++++++++++++++ 19 files changed, 585 insertions(+), 150 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java diff --git a/gradle.properties b/gradle.properties index 2069d9c3..0fc458be 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.4.13 +buildVersion=0.4.14 buildType=SNAPSHOT diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 467767b1..d2f4d560 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -37,6 +37,7 @@ import java.util.Set; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; @@ -230,6 +231,11 @@ class AnnotatedMixin { */ private final TypeHandle handle; + /** + * True if this is an interface mixin + */ + private final boolean isInterface; + /** * Specified targets */ @@ -266,11 +272,11 @@ class AnnotatedMixin { private final Map> methodMappings = new HashMap>(); public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type) { - this.annotation = MirrorUtils.getAnnotation(type, Mixin.class); this.mixins = mixins; this.mixin = type; this.handle = new TypeHandle(type); + this.isInterface = type.getKind() == ElementKind.INTERFACE; this.classRef = type.getQualifiedName().toString().replace('.', '/'); TypeHandle primaryTarget = this.initTargets(); @@ -376,6 +382,13 @@ class AnnotatedMixin { return this.handle; } + /** + * Get whether this is an interface mixin + */ + public boolean isInterface() { + return this.isInterface; + } + /** * Get the mixin's targets */ @@ -544,6 +557,10 @@ class AnnotatedMixin { * @return pending error message or null if no error condition */ public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) { + if (this.isInterface) { + this.mixins.printMessage(Kind.ERROR, "Injector in interface is unsupported", method); + } + String originalReference = MirrorUtils.getAnnotationValue(inject, "method"); MemberInfo targetMember = MemberInfo.parse(originalReference); if (targetMember.name == null) { @@ -591,6 +608,10 @@ class AnnotatedMixin { * and process the references */ public int registerInjectionPoint(Element element, AnnotationMirror inject, AnnotationMirror at) { + if (this.isInterface) { + this.mixins.printMessage(Kind.ERROR, "Injector in interface is unsupported", element); + } + if (!AnnotatedMixins.getRemapValue(at)) { return 0; } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java index 77bb75d0..6c585c48 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java @@ -61,10 +61,10 @@ public abstract class MixinProcessor extends AbstractProcessor { this.mixins.onPassStarted(); for (Element elem : roundEnv.getElementsAnnotatedWith(Mixin.class)) { - if (elem.getKind() == ElementKind.CLASS) { + if (elem.getKind() == ElementKind.CLASS || elem.getKind() == ElementKind.INTERFACE) { this.mixins.registerMixin((TypeElement)elem); } else { - this.mixins.printMessage(Kind.ERROR, "Found an @Mixin annotation on an element which is not a class", elem); + this.mixins.printMessage(Kind.ERROR, "Found an @Mixin annotation on an element which is not a class or interface", elem); } } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java index 22bf1883..13bd46b4 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -29,6 +29,7 @@ import java.util.Collection; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; @@ -67,6 +68,25 @@ public class TargetValidator extends MixinValidator { return true; } + if (mixin.getKind() == ElementKind.INTERFACE) { + this.validateInterfaceMixin(mixin, targets); + } else { + this.validateClassMixin(mixin, targets); + } + + return true; + } + + private void validateInterfaceMixin(TypeElement mixin, Collection targets) { + for (TypeHandle target : targets) { + TypeElement targetType = target.getElement(); + if (targetType != null && !(targetType.getKind() == ElementKind.INTERFACE)) { + this.error("Targetted type '" + target + " of " + mixin + " is not an interface", mixin); + } + } + } + + private void validateClassMixin(TypeElement mixin, Collection targets) { TypeMirror superClass = mixin.getSuperclass(); for (TypeHandle target : targets) { @@ -75,8 +95,6 @@ public class TargetValidator extends MixinValidator { this.error("Superclass " + superClass + " of " + mixin + " was not found in the hierarchy of target class " + targetType, mixin); } } - - return true; } private boolean validateSuperClass(TypeMirror targetType, TypeMirror superClass) { diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index aeaad19a..036209b9 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,7 +60,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.4.13"; + public static final String VERSION = "0.4.14"; /** * Blackboard key where the subsystem version will be stored to indicate diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index b6a3e0b1..61686407 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -437,11 +437,11 @@ public class MixinEnvironment implements ITokenProvider { private final int classVersion; - private final boolean resolveMethodsInInterfaces; + private final boolean supportsMethodsInInterfaces; private CompatibilityLevel(int classVersion, boolean resolveMethodsInInterfaces) { this.classVersion = classVersion; - this.resolveMethodsInInterfaces = resolveMethodsInInterfaces; + this.supportsMethodsInInterfaces = resolveMethodsInInterfaces; } boolean isSupported() { @@ -452,8 +452,8 @@ public class MixinEnvironment implements ITokenProvider { return this.classVersion; } - public boolean resolveMethodsInInterfaces() { - return this.resolveMethodsInInterfaces; + public boolean supportsMethodsInInterfaces() { + return this.supportsMethodsInInterfaces; } public boolean isAtLeast(CompatibilityLevel level) { diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java index e5237400..696417c2 100644 --- a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java @@ -102,4 +102,12 @@ public interface IMixinInfo { * @return the phase */ public abstract Phase getPhase(); + + /** + * Get whether this is an interface mixin or not + * + * @return true if this is an interface mixin + */ + public abstract boolean isInterfaceMixin(); + } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionException.java b/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionException.java index a698cd90..03d551e3 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionException.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionException.java @@ -32,29 +32,41 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext; public class InvalidInjectionException extends InvalidMixinException { private static final long serialVersionUID = 1L; + + private final InjectionInfo info; public InvalidInjectionException(MixinTargetContext mixin, String message) { super(mixin, message); + this.info = null; } public InvalidInjectionException(InjectionInfo info, String message) { super(info.getContext(), message); + this.info = info; } public InvalidInjectionException(MixinTargetContext mixin, Throwable cause) { super(mixin, cause); + this.info = null; } public InvalidInjectionException(InjectionInfo info, Throwable cause) { super(info.getContext(), cause); + this.info = info; } public InvalidInjectionException(MixinTargetContext mixin, String message, Throwable cause) { super(mixin, message, cause); + this.info = null; } public InvalidInjectionException(InjectionInfo info, String message, Throwable cause) { super(info.getContext(), message, cause); + this.info = info; + } + + public InjectionInfo getInjectionInfo() { + return this.info; } } 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 8fdc1a3d..05b1f95b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -1282,7 +1282,7 @@ public class ClassInfo extends TreeInfo { } } - if (type == Type.METHOD && (this.isInterface || MixinEnvironment.getCompatibilityLevel().resolveMethodsInInterfaces())) { + if (type == Type.METHOD && (this.isInterface || MixinEnvironment.getCompatibilityLevel().supportsMethodsInInterfaces())) { for (String implemented : this.interfaces) { ClassInfo iface = ClassInfo.forName(implemented); if (iface == null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/IMixinTransformerModule.java b/src/main/java/org/spongepowered/asm/mixin/transformer/IMixinTransformerModule.java index dc82fd25..75919aaa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/IMixinTransformerModule.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/IMixinTransformerModule.java @@ -24,10 +24,6 @@ */ package org.spongepowered.asm.mixin.transformer; -import java.util.SortedSet; - -import org.spongepowered.asm.lib.tree.ClassNode; - /** * Mixin Transformer Module interface for pre- and post-processors */ @@ -36,19 +32,15 @@ public interface IMixinTransformerModule { /** * Called before the mixins are applied * - * @param transformedName Target class name - * @param targetClass Target class tree - * @param mixins Mixins to be applied + * @param context Target class context */ - public abstract void preApply(String transformedName, ClassNode targetClass, SortedSet mixins); + public abstract void preApply(TargetClassContext context); /** * Called after the mixins are applied * - * @param transformedName Target class name - * @param targetClass Target class tree - * @param mixins Mixins which were applied + * @param context Target class context */ - public abstract void postApply(String transformedName, ClassNode targetClass, SortedSet mixins); + public abstract void postApply(TargetClassContext context); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java new file mode 100644 index 00000000..5a309940 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java @@ -0,0 +1,108 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.transformer; + +import org.spongepowered.asm.lib.tree.FieldNode; +import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.mixin.injection.InvalidInjectionException; +import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; + +/** + * Applicator for interface mixins, mainly just disables things which aren't + * supported for interface mixins + */ +public class InterfaceMixinApplicator extends MixinApplicator { + + /** + * ctor + * + * @param context applier context + */ + InterfaceMixinApplicator(TargetClassContext context) { + super(context); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.MixinApplicator + * #applyInterfaces( + * org.spongepowered.asm.mixin.transformer.MixinTargetContext) + */ + @Override + protected void applyInterfaces(MixinTargetContext mixin) { + for (String interfaceName : mixin.getInterfaces()) { + if (!this.targetClass.name.equals(interfaceName) && !this.targetClass.interfaces.contains(interfaceName)) { + this.targetClass.interfaces.add(interfaceName); + mixin.getTargetClassInfo().addInterface(interfaceName); + } + } + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.MixinApplicator + * #applyFields( + * org.spongepowered.asm.mixin.transformer.MixinTargetContext) + */ + @Override + protected void applyFields(MixinTargetContext mixin) { + // shadow fields have no meaning for interfaces, just spam the dev with messages + for (FieldNode shadow : mixin.getShadowFields()) { + this.logger.error("Ignoring redundant @Shadow field {}:{} in {}", shadow.name, shadow.desc, mixin); + } + + this.mergeNewFields(mixin); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.MixinApplicator + * #applyInitialisers( + * org.spongepowered.asm.mixin.transformer.MixinTargetContext) + */ + @Override + protected void applyInitialisers(MixinTargetContext mixin) { + // disabled for interface mixins + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.MixinApplicator + * #applyInjections( + * org.spongepowered.asm.mixin.transformer.MixinTargetContext) + */ + @Override + protected void applyInjections(MixinTargetContext mixin) { + // disabled for interface mixins + for (MethodNode method : this.targetClass.methods) { + try { + InjectionInfo injectInfo = InjectionInfo.parse(mixin, method); + if (injectInfo != null) { + throw new InvalidMixinException(mixin, injectInfo + " is not supported"); + } + } catch (InvalidInjectionException ex) { + String description = ex.getInjectionInfo() != null ? ex.getInjectionInfo().toString() : "Injection"; + throw new InvalidMixinException(mixin, description + " is not supported"); + } + } + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java new file mode 100644 index 00000000..da26f550 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java @@ -0,0 +1,82 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.transformer; + +import org.spongepowered.asm.lib.Opcodes; +import org.spongepowered.asm.lib.tree.AnnotationNode; +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.lib.tree.FieldNode; +import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; + +/** + * Bytecode preprocessor for interface mixins, simply performs some additional + * verification for things which are unsupported in interfaces + */ +class InterfaceMixinPreProcessor extends MixinPreProcessor { + + /** + * Ctor + * + * @param mixin Mixin info + * @param classNode Mixin classnode + */ + InterfaceMixinPreProcessor(MixinInfo mixin, ClassNode classNode) { + super(mixin, classNode); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.MixinPreProcessor + * #prepareMethod(org.spongepowered.asm.lib.tree.MethodNode, + * org.spongepowered.asm.mixin.transformer.ClassInfo.Method) + */ + @Override + protected void prepareMethod(MethodNode mixinMethod, Method method) { + // I have no idea how the hell you'd compile this, but we make sure anyway! + if (!MixinApplicator.hasFlag(mixinMethod, Opcodes.ACC_PUBLIC)) { + throw new InvalidMixinException(this.mixin, "Interface mixin contains a non-public method! Found " + method + " in " + this.mixin); + } + + super.prepareMethod(mixinMethod, method); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.transformer.MixinPreProcessor + * #validateField( + * org.spongepowered.asm.mixin.transformer.MixinTargetContext, + * org.spongepowered.asm.lib.tree.FieldNode, + * org.spongepowered.asm.lib.tree.AnnotationNode) + */ + @Override + protected boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) { + if (!MixinApplicator.hasFlag(field, Opcodes.ACC_STATIC)) { + throw new InvalidMixinException(this.mixin, "Interface mixin contains an instance field! Found " + field.name + " in " + + this.mixin); + } + + return super.validateField(context, field, shadow); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java index 142ada0b..6a49c70b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java @@ -146,7 +146,7 @@ public class MixinApplicator { * locals are shifted so that they don't interfere with locals in the * receiving constructor. */ - private static final int[] INITIALISER_OPCODE_BLACKLIST = { + protected static final int[] INITIALISER_OPCODE_BLACKLIST = { Opcodes.RETURN, Opcodes.ILOAD, Opcodes.LLOAD, Opcodes.FLOAD, Opcodes.DLOAD, Opcodes.IALOAD, Opcodes.LALOAD, Opcodes.FALOAD, Opcodes.DALOAD, Opcodes.AALOAD, Opcodes.BALOAD, Opcodes.CALOAD, Opcodes.SALOAD, Opcodes.ISTORE, Opcodes.LSTORE, Opcodes.FSTORE, Opcodes.DSTORE, Opcodes.ASTORE, Opcodes.IASTORE, Opcodes.LASTORE, Opcodes.FASTORE, Opcodes.DASTORE, Opcodes.AASTORE, Opcodes.BASTORE, Opcodes.CASTORE, @@ -156,29 +156,29 @@ public class MixinApplicator { /** * Log more things */ - private final Logger logger = LogManager.getLogger("mixin"); + protected final Logger logger = LogManager.getLogger("mixin"); /** * Session ID, used as a check when parsing {@link MixinMerged} annotations * to prevent them being applied at compile time by people trying to * circumvent mixin application */ - private final String sessionId; + protected final String sessionId; /** * Target class name */ - private final String targetName; + protected final String targetName; /** * Target class tree */ - private final ClassNode targetClass; + protected final ClassNode targetClass; - MixinApplicator(String sessionId, String transformedName, ClassNode targetClass) { - this.sessionId = sessionId; - this.targetName = transformedName; - this.targetClass = targetClass; + MixinApplicator(TargetClassContext context) { + this.sessionId = context.getSessionId(); + this.targetName = context.getName(); + this.targetClass = context.getClassNode(); } /** @@ -196,7 +196,7 @@ public class MixinApplicator { * * @param mixin Mixin to apply */ - private void applyMixin(MixinTargetContext mixin) { + protected final void applyMixin(MixinTargetContext mixin) { try { mixin.preApply(this.targetName, this.targetClass); @@ -221,7 +221,7 @@ public class MixinApplicator { * * @param mixin */ - private void applyInterfaces(MixinTargetContext mixin) { + protected void applyInterfaces(MixinTargetContext mixin) { for (String interfaceName : mixin.getInterfaces()) { if (!this.targetClass.interfaces.contains(interfaceName)) { this.targetClass.interfaces.add(interfaceName); @@ -235,7 +235,7 @@ public class MixinApplicator { * * @param mixin */ - private void applyAttributes(MixinTargetContext mixin) { + protected void applyAttributes(MixinTargetContext mixin) { if (mixin.shouldSetSourceFile()) { this.targetClass.sourceFile = mixin.getSourceFile(); } @@ -247,7 +247,7 @@ public class MixinApplicator { * * @param mixin */ - private void applyAnnotations(MixinTargetContext mixin) { + protected void applyAnnotations(MixinTargetContext mixin) { ClassNode sourceClass = mixin.getClassNode(); this.mergeAnnotations(sourceClass, this.targetClass); } @@ -260,14 +260,21 @@ public class MixinApplicator { * * @param mixin */ - private void applyFields(MixinTargetContext mixin) { + protected void applyFields(MixinTargetContext mixin) { + this.mergeShadowFieldAnnotations(mixin); + this.mergeNewFields(mixin); + } + + protected void mergeShadowFieldAnnotations(MixinTargetContext mixin) { for (FieldNode shadow : mixin.getShadowFields()) { FieldNode target = this.findTargetField(shadow); if (target != null) { this.mergeAnnotations(shadow, target); } } - + } + + protected void mergeNewFields(MixinTargetContext mixin) { for (FieldNode field : mixin.getFields()) { FieldNode target = this.findTargetField(field); if (target == null) { @@ -282,7 +289,7 @@ public class MixinApplicator { * * @param mixin */ - private void applyMethods(MixinTargetContext mixin) { + protected void applyMethods(MixinTargetContext mixin) { for (MethodNode shadow : mixin.getShadowMethods()) { MethodNode target = this.findTargetMethod(shadow); if (target != null) { @@ -325,11 +332,11 @@ public class MixinApplicator { * @param isOverwrite true if the method is annotated with an * {@link Overwrite} annotation */ - private void mergeMethod(MixinTargetContext mixin, MethodNode method, boolean isOverwrite) { + protected void mergeMethod(MixinTargetContext mixin, MethodNode method, boolean isOverwrite) { MethodNode target = this.findTargetMethod(method); if (target != null) { - if (this.alreadyMerged(mixin, method, isOverwrite, target)) { + if (this.isAlreadyMerged(mixin, method, isOverwrite, target)) { return; } @@ -365,7 +372,7 @@ public class MixinApplicator { * @param target target method being checked * @return true if the target was already merged and should be skipped */ - private boolean alreadyMerged(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, MethodNode target) { + protected boolean isAlreadyMerged(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, MethodNode target) { AnnotationNode merged = ASMHelper.getVisibleAnnotation(target, MixinMerged.class); if (merged == null) { return false; @@ -406,7 +413,7 @@ public class MixinApplicator { * @return true if the intrinsic method was skipped (short-circuit further * merge operations) */ - private boolean mergeIntrinsic(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, + protected boolean mergeIntrinsic(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, MethodNode target, AnnotationNode intrinsic) { if (isOverwrite) { @@ -439,7 +446,7 @@ public class MixinApplicator { * @param method Method being merged * @param target target method being checked */ - private void displaceIntrinsic(MixinTargetContext mixin, MethodNode method, MethodNode target) { + protected void displaceIntrinsic(MixinTargetContext mixin, MethodNode method, MethodNode target) { // Deliberately include invalid character in the method name so that // we guarantee no hackiness String proxyName = "proxy+" + target.name; @@ -463,7 +470,7 @@ public class MixinApplicator { * * @param method */ - private void appendInsns(MethodNode method) { + protected final void appendInsns(MethodNode method) { if (Type.getReturnType(method.desc) != Type.VOID_TYPE) { throw new IllegalArgumentException("Attempted to merge insns from a method which does not return void"); } @@ -498,7 +505,7 @@ public class MixinApplicator { * * @param mixin */ - private void applyInitialisers(MixinTargetContext mixin) { + protected void applyInitialisers(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) { @@ -526,7 +533,7 @@ public class MixinApplicator { * @param mixin mixin to search * @return appropriate ctor or null if none found */ - private MethodNode getConstructor(MixinTargetContext mixin) { + protected MethodNode getConstructor(MixinTargetContext mixin) { MethodNode ctor = null; for (MethodNode mixinMethod : mixin.getMethods()) { @@ -598,7 +605,7 @@ public class MixinApplicator { * @return initialiser bytecode extracted from the supplied constructor, or * null if the constructor range could not be parsed */ - private Deque getInitialiser(MixinTargetContext mixin, MethodNode ctor) { + protected final Deque getInitialiser(MixinTargetContext mixin, MethodNode ctor) { // Find the range of line numbers which corresponds to the constructor body Range init = this.getConstructorRange(ctor); if (!init.isValid()) { @@ -669,7 +676,7 @@ public class MixinApplicator { * @param ctor * @param initialiser */ - private void injectInitialiser(MixinTargetContext mixin, MethodNode ctor, Deque initialiser) { + protected final void injectInitialiser(MixinTargetContext mixin, MethodNode ctor, Deque initialiser) { Map labels = ASMHelper.cloneLabels(ctor.instructions); for (Iterator iter = ctor.instructions.iterator(0); iter.hasNext();) { @@ -698,7 +705,7 @@ public class MixinApplicator { * methods * @param mixin */ - private void applyInjections(MixinTargetContext mixin) { + protected void applyInjections(MixinTargetContext mixin) { List injected = new ArrayList(); for (MethodNode method : this.targetClass.methods) { @@ -727,7 +734,7 @@ public class MixinApplicator { * @param mixin Mixin being applied * @param annotation Annotation node to check constraints */ - private void checkConstraints(MixinTargetContext mixin, AnnotationNode annotation) { + protected final void checkConstraints(MixinTargetContext mixin, AnnotationNode annotation) { try { Constraint constraint = ConstraintParser.parse(annotation); MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment(); @@ -756,7 +763,7 @@ public class MixinApplicator { * @param from ClassNode to merge annotations from * @param to ClassNode to merge annotations to */ - private void mergeAnnotations(ClassNode from, ClassNode to) { + protected final void mergeAnnotations(ClassNode from, ClassNode to) { to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name); to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name); } @@ -772,7 +779,7 @@ public class MixinApplicator { * @param from MethodNode to merge annotations from * @param to MethodNode to merge annotations to */ - private void mergeAnnotations(MethodNode from, MethodNode to) { + protected final void mergeAnnotations(MethodNode from, MethodNode to) { to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name); to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name); } @@ -788,7 +795,7 @@ public class MixinApplicator { * @param from FieldNode to merge annotations from * @param to FieldNode to merge annotations to */ - private void mergeAnnotations(FieldNode from, FieldNode to) { + protected final void mergeAnnotations(FieldNode from, FieldNode to) { to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name); to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name); } @@ -840,7 +847,7 @@ public class MixinApplicator { * @param opcode opcode to search for * @return found node or null if not found */ - private static AbstractInsnNode findInsn(MethodNode method, int opcode) { + protected static AbstractInsnNode findInsn(MethodNode method, int opcode) { Iterator findReturnIter = method.instructions.iterator(); while (findReturnIter.hasNext()) { AbstractInsnNode insn = findReturnIter.next(); @@ -872,7 +879,7 @@ public class MixinApplicator { * * @return Target method matching searchFor, or null if not found */ - private MethodNode findTargetMethod(MethodNode searchFor) { + protected final MethodNode findTargetMethod(MethodNode searchFor) { for (MethodNode target : this.targetClass.methods) { if (target.name.equals(searchFor.name) && target.desc.equals(searchFor.desc)) { return target; @@ -888,7 +895,7 @@ public class MixinApplicator { * * @return Target field matching searchFor, or null if not found */ - private FieldNode findTargetField(FieldNode searchFor) { + protected final FieldNode findTargetField(FieldNode searchFor) { for (FieldNode target : this.targetClass.fields) { if (target.name.equals(searchFor.name)) { return target; @@ -905,7 +912,7 @@ public class MixinApplicator { * @param flag * @return True if the specified flag is set in this method's access flags */ - static boolean hasFlag(MethodNode method, int flag) { + protected static boolean hasFlag(MethodNode method, int flag) { return (method.access & flag) == flag; } @@ -916,7 +923,7 @@ public class MixinApplicator { * @param flag * @return True if the specified flag is set in this field's access flags */ - static boolean hasFlag(FieldNode field, int flag) { + protected static boolean hasFlag(FieldNode field, int flag) { return (field.access & flag) == flag; } } 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 c2d94a33..dc5a90c8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -234,6 +234,11 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * Cached class info */ private final transient ClassInfo info; + + /** + * True if this is an interface mixin rather than a regular mixin + */ + private final transient boolean isInterfaceMixin; /** * Holds state that currently is not fully initialised or validated @@ -267,6 +272,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { byte[] mixinBytes = this.loadMixinClass(this.className, runTransformers); this.uninitialisedState = new ValidationState(mixinBytes); this.info = this.uninitialisedState.classInfo; + this.isInterfaceMixin = this.info.isInterface(); ClassNode classNode = this.uninitialisedState.getClassNode(0); this.priority = this.readPriority(classNode); @@ -284,7 +290,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { if (this.uninitialisedState instanceof ReloadedState) { ((ReloadedState) this.uninitialisedState).validateChanges(); } else { - new MixinPreProcessor(this, classNode).prepare(); + this.createPreProcessor(classNode).prepare(); } this.uninitialisedState.classNode = null; // the state is now fully initialised and validated @@ -316,7 +322,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { @Override public String apply(Type input) { return input.getClassName(); - }; + } }), suppressPlugin, false); } @@ -335,16 +341,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { targetClassName = targetClassName.replace('/', '.'); if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) { ClassInfo targetInfo = ClassInfo.forName(targetClassName); - if (targetInfo == null) { - throw new RuntimeException("@Mixin target " + targetClassName + " was not found " + this); - } - if (targetInfo.isInterface()) { - throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is an interface in " + this); - } - if (checkPublic && targetInfo.isPublic()) { - throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is public in " + this - + " and must be specified in value"); - } + this.checkTarget(targetClassName, targetInfo, checkPublic); if (!outTargets.contains(targetInfo)) { outTargets.add(targetInfo); targetInfo.addMixin(this); @@ -353,6 +350,21 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } } + private void checkTarget(String targetClassName, ClassInfo targetInfo, boolean checkPublic) throws InvalidMixinException { + if (targetInfo == null) { + throw new RuntimeException("@Mixin target " + targetClassName + " was not found " + this); + } + boolean targetIsInterface = targetInfo.isInterface(); + if (targetIsInterface != this.isInterfaceMixin) { + String not = targetIsInterface ? "" : "not "; + throw new InvalidMixinException(this, "@Mixin target type mismatch: " + targetClassName + " is " + not + "an interface in " + this); + } + if (checkPublic && targetInfo.isPublic()) { + throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is public in " + this + + " and must be specified in value"); + } + } + /** * Read the priority from the {@link Mixin} annotation * @@ -370,6 +382,21 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } private boolean validateTargetClasses(ClassNode classNode) { + if (this.isInterfaceMixin) { + return this.validateInterfaceMixinTargets(classNode); + } + return this.validateClassMixinTargets(classNode); + } + + private boolean validateInterfaceMixinTargets(ClassNode classNode) { + if (!"java/lang/Object".equals(classNode.superName)) { + throw new InvalidMixinException(this, "Super class of " + this + " is invalid, found " + classNode.superName.replace('/', '.')); + } + + return false; + } + + private boolean validateClassMixinTargets(ClassNode classNode) { boolean detached = false; for (ClassInfo targetClass : this.targetClasses) { @@ -405,6 +432,10 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @param state State to validate */ private void validateMixin(ValidationState state) { + if (this.isInterfaceMixin && !MixinEnvironment.getCompatibilityLevel().supportsMethodsInInterfaces()) { + throw new InvalidMixinException(this, "Interface mixin not supported in current enviromnment"); + } + // isInner (shouldn't) return true for static inner classes if (!state.classInfo.isProbablyStatic()) { throw new InvalidMixinException(this, "Inner class mixin must be declared static"); @@ -568,6 +599,14 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { public Phase getPhase() { return this.phase; } + + /** + * Get whether this is an interface mixin or not + */ + @Override + public boolean isInterfaceMixin() { + return this.isInterfaceMixin; + } /** * Get a new tree for the class bytecode @@ -637,7 +676,20 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @return new context */ public MixinTargetContext createContextFor(ClassNode target) { - return new MixinPreProcessor(this, this.getClassNode(ClassReader.EXPAND_FRAMES)).createContextFor(target); + ClassNode classNode = this.getClassNode(ClassReader.EXPAND_FRAMES); + return this.createPreProcessor(classNode).prepare().createContextFor(target); + } + + /** + * Create a preprocessor based on this mixin's type + * + * @return new preprocessor + */ + private MixinPreProcessor createPreProcessor(ClassNode classNode) { + if (this.isInterfaceMixin) { + return new InterfaceMixinPreProcessor(this, classNode); + } + return new MixinPreProcessor(this, classNode); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java index dfa23fbe..ae0dc6ea 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java @@ -81,12 +81,12 @@ class MixinPreProcessor { /** * The mixin */ - private final MixinInfo mixin; + protected final MixinInfo mixin; /** * Mixin class node */ - private final ClassNode classNode; + protected final ClassNode classNode; private final boolean verboseLogging; @@ -103,21 +103,29 @@ class MixinPreProcessor { * * @return Prepared classnode */ - ClassNode prepare() { + MixinPreProcessor 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); + this.prepareMethod(mixinMethod, method); + } + + for (FieldNode mixinField : this.classNode.fields) { + this.prepareField(mixinField); } } - return this.classNode; + return this; } - private void prepareShadow(MethodNode mixinMethod, Method method) { + protected void prepareMethod(MethodNode mixinMethod, Method method) { + this.prepareShadow(mixinMethod, method); + this.prepareSoftImplements(mixinMethod, method); + } + + protected void prepareShadow(MethodNode mixinMethod, Method method) { AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); if (shadowAnnotation == null) { return; @@ -132,7 +140,7 @@ class MixinPreProcessor { } } - private void prepareSoftImplements(MethodNode mixinMethod, Method method) { + protected void prepareSoftImplements(MethodNode mixinMethod, Method method) { for (InterfaceInfo iface : this.mixin.getSoftImplements()) { if (iface.renameMethod(mixinMethod)) { method.renameTo(mixinMethod.name); @@ -140,8 +148,11 @@ class MixinPreProcessor { } } + protected void prepareField(FieldNode mixinField) { + // stub + } + MixinTargetContext createContextFor(ClassNode target) { - this.prepare(); MixinTargetContext context = new MixinTargetContext(this.mixin, this.classNode, target); this.attach(context); return context; @@ -167,10 +178,15 @@ class MixinPreProcessor { this.transform(context); } - private void attachMethods(MixinTargetContext context) { + protected void attachMethods(MixinTargetContext context) { for (Iterator iter = this.classNode.methods.iterator(); iter.hasNext();) { MethodNode mixinMethod = iter.next(); + if (!this.validateMethod(context, mixinMethod)) { + iter.remove(); + continue; + } + if (this.processMethod(context, mixinMethod, Shadow.class, true, true)) { iter.remove(); context.addShadowMethod(mixinMethod); @@ -181,7 +197,11 @@ class MixinPreProcessor { } } - private boolean processMethod(MixinTargetContext context, MethodNode mixinMethod, Class annotationType, + protected boolean validateMethod(MixinTargetContext context, MethodNode mixinMethod) { + return true; + } + + protected boolean processMethod(MixinTargetContext context, MethodNode mixinMethod, Class annotationType, boolean mustExist, boolean mustBePrivate) { AnnotationNode annotation = ASMHelper.getVisibleAnnotation(mixinMethod, annotationType); if (annotation == null) { @@ -220,7 +240,7 @@ class MixinPreProcessor { return true; } - private void attachFields(MixinTargetContext context) { + protected void attachFields(MixinTargetContext context) { for (Iterator iter = this.classNode.fields.iterator(); iter.hasNext();) { FieldNode mixinField = iter.next(); AnnotationNode shadow = ASMHelper.getVisibleAnnotation(mixinField, Shadow.class); @@ -282,7 +302,7 @@ class MixinPreProcessor { } } - private boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) { + protected 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 (MixinApplicator.hasFlag(field, Opcodes.ACC_STATIC) && !MixinApplicator.hasFlag(field, Opcodes.ACC_PRIVATE) @@ -316,7 +336,7 @@ class MixinPreProcessor { * Apply discovered method and field renames to method invocations and field * accesses in the mixin */ - private void transform(MixinTargetContext context) { + protected void transform(MixinTargetContext context) { for (MethodNode mixinMethod : this.classNode.methods) { for (Iterator iter = mixinMethod.instructions.iterator(); iter.hasNext();) { AbstractInsnNode insn = iter.next(); @@ -337,7 +357,7 @@ class MixinPreProcessor { } } - private static MethodNode findMethod(ClassNode classNode, MethodNode method, AnnotationNode annotation) { + protected static MethodNode findMethod(ClassNode classNode, MethodNode method, AnnotationNode annotation) { Deque aliases = new LinkedList(); aliases.add(method.name); if (annotation != null) { @@ -347,10 +367,10 @@ class MixinPreProcessor { } } - return MixinPreProcessor.findMethod(classNode, aliases, method.desc); + return MixinPreProcessor.findMethodRecursive(classNode, aliases, method.desc); } - private static MethodNode findRemappedMethod(ClassNode classNode, MethodNode method) { + protected static MethodNode findRemappedMethod(ClassNode classNode, MethodNode method) { RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers(); String remappedName = remapperChain.mapMethodName(classNode.name, method.name, method.desc); if (remappedName.equals(method.name)) { @@ -360,10 +380,10 @@ class MixinPreProcessor { Deque aliases = new LinkedList(); aliases.add(remappedName); - return MixinPreProcessor.findMethod(classNode, aliases, method.desc); + return MixinPreProcessor.findMethodRecursive(classNode, aliases, method.desc); } - private static MethodNode findMethod(ClassNode classNode, Deque aliases, String desc) { + private static MethodNode findMethodRecursive(ClassNode classNode, Deque aliases, String desc) { String alias = aliases.poll(); if (alias == null) { return null; @@ -375,10 +395,10 @@ class MixinPreProcessor { } } - return MixinPreProcessor.findMethod(classNode, aliases, desc); + return MixinPreProcessor.findMethodRecursive(classNode, aliases, desc); } - private static FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) { + protected static FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) { Deque aliases = new LinkedList(); aliases.add(field.name); if (shadow != null) { @@ -388,10 +408,10 @@ class MixinPreProcessor { } } - return MixinPreProcessor.findField(classNode, aliases, field.desc); + return MixinPreProcessor.findFieldRecursive(classNode, aliases, field.desc); } - private static FieldNode findRemappedField(ClassNode classNode, FieldNode field) { + protected static FieldNode findRemappedField(ClassNode classNode, FieldNode field) { RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers(); String remappedName = remapperChain.mapFieldName(classNode.name, field.name, field.desc); if (remappedName.equals(field.name)) { @@ -400,7 +420,7 @@ class MixinPreProcessor { Deque aliases = new LinkedList(); aliases.add(remappedName); - return MixinPreProcessor.findField(classNode, aliases, field.desc); + return MixinPreProcessor.findFieldRecursive(classNode, aliases, field.desc); } /** @@ -410,7 +430,7 @@ class MixinPreProcessor { * @param desc * @return Target field or null if not found */ - private static FieldNode findField(ClassNode classNode, Deque aliases, String desc) { + private static FieldNode findFieldRecursive(ClassNode classNode, Deque aliases, String desc) { String alias = aliases.poll(); if (alias == null) { return null; @@ -422,6 +442,6 @@ class MixinPreProcessor { } } - return MixinPreProcessor.findField(classNode, aliases, desc); + return MixinPreProcessor.findFieldRecursive(classNode, aliases, desc); } } 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 c1d9a6c7..ca19c0f9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -434,7 +434,10 @@ public class MixinTransformer extends TreeTransformer { } try { - basicClass = this.applyMixins(transformedName, basicClass, mixins); + // Tree for target class + ClassNode targetClassNode = this.readClass(basicClass, true); + TargetClassContext context = new TargetClassContext(this.sessionId, transformedName, targetClassNode, mixins); + basicClass = this.applyMixins(context); } catch (InvalidMixinException th) { if (environment.getOption(Option.DUMP_TARGET_ON_FAILURE)) { this.dumpClass(transformedName.replace('.', '/') + ".target", basicClass); @@ -606,65 +609,52 @@ public class MixinTransformer extends TreeTransformer { * Apply mixins for specified target class to the class described by the * supplied byte array. * - * @param transformedName - * @param basicClass - * @param mixins + * @param context * @return class bytecode after application of mixins */ - private byte[] applyMixins(String transformedName, byte[] basicClass, SortedSet mixins) { - // Tree for target class - ClassNode targetClass = this.readClass(basicClass, true); - - this.preApply(transformedName, targetClass, mixins); - this.apply(transformedName, targetClass, mixins); + private byte[] applyMixins(TargetClassContext context) { + this.preApply(context); + this.apply(context); try { - this.postApply(transformedName, targetClass, mixins); + this.postApply(context); } catch (ValidationFailedException ex) { this.logger.info(ex.getMessage()); // If verify is enabled and failed, write out the bytecode to allow us to inspect it if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) { - this.writeClass(transformedName, targetClass); + this.writeClass(context); } } - - return this.writeClass(transformedName, targetClass); + return this.writeClass(context); } /** * Process tasks before mixin application * - * @param transformedName Target class transformed name - * @param targetClass Target class - * @param mixins Mixin which were just applied + * @param context Target class context */ - private void preApply(String transformedName, ClassNode targetClass, SortedSet mixins) { + private void preApply(TargetClassContext context) { for (IMixinTransformerModule module : this.modules) { - module.preApply(transformedName, targetClass, mixins); + module.preApply(context); } } /** * Apply the mixins to the target class * - * @param transformedName Target class transformed name - * @param targetClass Target class - * @param mixins Mixin which were just applied + * @param context Target class context */ - private void apply(String transformedName, ClassNode targetClass, SortedSet mixins) { - MixinApplicator applicator = new MixinApplicator(this.sessionId, transformedName, targetClass); - applicator.apply(mixins); + private void apply(TargetClassContext context) { + context.applyMixins(); } /** * Process tasks after mixin application * - * @param transformedName Target class transformed name - * @param targetClass Target class - * @param mixins Mixin which were just applied + * @param context Target class context */ - private void postApply(String transformedName, ClassNode targetClass, SortedSet mixins) { + private void postApply(TargetClassContext context) { for (IMixinTransformerModule module : this.modules) { - module.postApply(transformedName, targetClass, mixins); + module.postApply(context); } } @@ -725,6 +715,10 @@ public class MixinTransformer extends TreeTransformer { return Pattern.compile(this.prepareFilter(filter), Pattern.CASE_INSENSITIVE).matcher(subject).matches(); } + private byte[] writeClass(TargetClassContext context) { + return this.writeClass(context.getName(), context.getClassNode()); + } + private byte[] writeClass(String transformedName, ClassNode targetClass) { // Collapse tree to bytes byte[] bytes = this.writeClass(targetClass); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleCheckClass.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleCheckClass.java index ab6b07db..ac9029fb 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleCheckClass.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleCheckClass.java @@ -24,10 +24,7 @@ */ package org.spongepowered.asm.mixin.transformer; -import java.util.SortedSet; - import org.spongepowered.asm.lib.ClassWriter; -import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.lib.util.CheckClassAdapter; /** @@ -56,22 +53,20 @@ public class MixinTransformerModuleCheckClass implements IMixinTransformerModule /* (non-Javadoc) * @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule - * #preTransform(java.lang.String, org.objectweb.asm.tree.ClassNode, - * java.util.SortedSet) + * #preApply(org.spongepowered.asm.mixin.transformer.TargetClassContext) */ @Override - public void preApply(String transformedName, ClassNode targetClass, SortedSet mixins) { + public void preApply(TargetClassContext context) { } - + /* (non-Javadoc) * @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule - * #postTransform(java.lang.String, org.objectweb.asm.tree.ClassNode, - * java.util.SortedSet) + * #postApply(org.spongepowered.asm.mixin.transformer.TargetClassContext) */ @Override - public void postApply(String transformedName, ClassNode targetClass, SortedSet mixins) { + public void postApply(TargetClassContext context) { try { - targetClass.accept(new CheckClassAdapter(new MixinClassWriter(ClassWriter.COMPUTE_FRAMES))); + context.getClassNode().accept(new CheckClassAdapter(new MixinClassWriter(ClassWriter.COMPUTE_FRAMES))); } catch (RuntimeException ex) { throw new ValidationFailedException(ex.getMessage(), ex); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleInterfaceChecker.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleInterfaceChecker.java index a9d733bc..89c88f0b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleInterfaceChecker.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerModuleInterfaceChecker.java @@ -32,10 +32,8 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import java.util.Set; -import java.util.SortedSet; import org.apache.commons.io.IOUtils; -import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal; import org.spongepowered.asm.util.PrettyPrinter; @@ -90,12 +88,11 @@ public class MixinTransformerModuleInterfaceChecker implements IMixinTransformer /* (non-Javadoc) * @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule - * #preTransform(java.lang.String, org.objectweb.asm.tree.ClassNode, - * java.util.SortedSet) + * #preApply(org.spongepowered.asm.mixin.transformer.TargetClassContext) */ @Override - public void preApply(String transformedName, ClassNode targetClass, SortedSet mixins) { - ClassInfo targetClassInfo = ClassInfo.forName(targetClass.name); + public void preApply(TargetClassContext context) { + ClassInfo targetClassInfo = context.getClassInfo(); for (Method m : targetClassInfo.getInterfaceMethods(false)) { this.interfaceMethods.put(targetClassInfo, m); } @@ -103,12 +100,11 @@ public class MixinTransformerModuleInterfaceChecker implements IMixinTransformer /* (non-Javadoc) * @see org.spongepowered.asm.mixin.transformer.IMixinTransformerModule - * #postTransform(java.lang.String, org.objectweb.asm.tree.ClassNode, - * java.util.SortedSet) + * #postApply(org.spongepowered.asm.mixin.transformer.TargetClassContext) */ @Override - public void postApply(String transformedName, ClassNode targetClass, SortedSet mixins) { - ClassInfo targetClassInfo = ClassInfo.forName(targetClass.name); + public void postApply(TargetClassContext context) { + ClassInfo targetClassInfo = context.getClassInfo(); String className = targetClassInfo.getName().replace('/', '.'); int missingMethodCount = 0; diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java new file mode 100644 index 00000000..58585486 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java @@ -0,0 +1,130 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.transformer; + +import java.util.SortedSet; + +import org.spongepowered.asm.lib.tree.ClassNode; + +/** + * Struct for containing target class information during mixin application + */ +class TargetClassContext { + + /** + * Transformer session ID + */ + private final String sessionId; + + /** + * Target class name + */ + private final String name; + + /** + * Target class as tree + */ + private final ClassNode classNode; + + /** + * Target class metadata + */ + private final ClassInfo classInfo; + + /** + * Mixins to apply + */ + private final SortedSet mixins; + + /** + * True once mixins have been applied to this class + */ + private boolean applied; + + TargetClassContext(String sessionId, String name, ClassNode classNode, SortedSet mixins) { + this.sessionId = sessionId; + this.name = name; + this.classNode = classNode; + this.classInfo = ClassInfo.fromClassNode(classNode); + this.mixins = mixins; + } + + /** + * Get the transformer session ID + */ + public String getSessionId() { + return this.sessionId; + } + + /** + * Get the class name + */ + public String getName() { + return this.name; + } + + /** + * Get the class tree + */ + public ClassNode getClassNode() { + return this.classNode; + } + + /** + * Get the target class metadata + */ + public ClassInfo getClassInfo() { + return this.classInfo; + } + + /** + * Get the mixins for this target class + */ + public SortedSet getMixins() { + return this.mixins; + } + + /** + * Apply mixins to this class + * + * @param sessionId + */ + public void applyMixins() { + if (this.applied) { + throw new IllegalStateException("Mixins already applied to target class " + this.name); + } + this.applied = true; + MixinApplicator applicator = this.createApplicator(); + applicator.apply(this.mixins); + } + + private MixinApplicator createApplicator() { + if (this.classInfo.isInterface()) { + return new InterfaceMixinApplicator(this); + } + return new MixinApplicator(this); + } + +}