diff --git a/src/main/java/org/spongepowered/asm/mixin/InvalidMixinException.java b/src/main/java/org/spongepowered/asm/mixin/MixinApplyError.java similarity index 82% rename from src/main/java/org/spongepowered/asm/mixin/InvalidMixinException.java rename to src/main/java/org/spongepowered/asm/mixin/MixinApplyError.java index 84574a2e..8ecb80f5 100644 --- a/src/main/java/org/spongepowered/asm/mixin/InvalidMixinException.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinApplyError.java @@ -24,19 +24,24 @@ */ package org.spongepowered.asm.mixin; -public class InvalidMixinException extends RuntimeException { + +/** + * Error to throw for required mixins when mixing of the mixin fails + */ +public class MixinApplyError extends Error { private static final long serialVersionUID = 1L; - public InvalidMixinException(String message) { + public MixinApplyError(String message) { super(message); } - public InvalidMixinException(Throwable message) { - super(message); + public MixinApplyError(Throwable cause) { + super(cause); } - public InvalidMixinException(String message, Throwable cause) { + public MixinApplyError(String message, Throwable cause) { super(message, cause); } + } diff --git a/src/main/java/org/spongepowered/asm/mixin/Shadow.java b/src/main/java/org/spongepowered/asm/mixin/Shadow.java index c7805732..5002b391 100644 --- a/src/main/java/org/spongepowered/asm/mixin/Shadow.java +++ b/src/main/java/org/spongepowered/asm/mixin/Shadow.java @@ -29,6 +29,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.spongepowered.asm.mixin.transformer.InvalidMixinException; + /** * Used to indicate a Mixin class member which is acting as a placeholder for a * method or field in the target class diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 24f04f05..a9c5f1e8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -42,7 +42,7 @@ import org.spongepowered.asm.mixin.injection.points.BeforeStringInvoke; import org.spongepowered.asm.mixin.injection.points.JumpInsnPoint; import org.spongepowered.asm.mixin.injection.points.MethodHead; import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; -import org.spongepowered.asm.mixin.transformer.IReferenceMapperContext; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; import com.google.common.base.Joiner; @@ -295,7 +295,7 @@ public abstract class InjectionPoint { * @return InjectionPoint parsed from the supplied data or null if parsing * failed */ - public static InjectionPoint parse(IReferenceMapperContext mixin, At at) { + public static InjectionPoint parse(MixinTargetContext mixin, At at) { return InjectionPoint.parse(mixin, at.value(), at.shift(), at.by(), Arrays.asList(at.args()), at.target(), at.ordinal(), at.opcode()); } @@ -309,7 +309,7 @@ public abstract class InjectionPoint { * @return InjectionPoint parsed from the supplied data or null if parsing * failed */ - public static InjectionPoint parse(IReferenceMapperContext mixin, AnnotationNode node) { + public static InjectionPoint parse(MixinTargetContext mixin, AnnotationNode node) { String at = ASMHelper.getAnnotationValue(node, "value"); List args = ASMHelper.>getAnnotationValue(node, "args"); String target = ASMHelper.getAnnotationValue(node, "target", ""); @@ -341,7 +341,7 @@ public abstract class InjectionPoint { * @return InjectionPoint parsed from the supplied data or null if parsing * failed */ - public static InjectionPoint parse(IReferenceMapperContext mixin, String at, At.Shift shift, int by, + public static InjectionPoint parse(MixinTargetContext mixin, String at, At.Shift shift, int by, List args, String target, int ordinal, int opcode) { InjectionPointData data = new InjectionPointData(mixin, args, target, ordinal, opcode); InjectionPoint point = null; @@ -367,10 +367,11 @@ public abstract class InjectionPoint { ctor.setAccessible(true); point = ctor.newInstance(data); } catch (Exception ex) { - throw new InvalidInjectionException("The specified class " + at + " could not be instanced or is not a valid InjectionPoint", ex); + throw new InvalidInjectionException(mixin, "The specified class " + at + + " could not be instanced or is not a valid InjectionPoint", ex); } } else { - throw new InvalidInjectionException(at + " is not a valid injection point specifier"); + throw new InvalidInjectionException(mixin, at + " is not a valid injection point specifier"); } if (point != null) { 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 985fe21f..f1ff9e3e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionException.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InvalidInjectionException.java @@ -24,23 +24,37 @@ */ package org.spongepowered.asm.mixin.injection; -import org.spongepowered.asm.mixin.InvalidMixinException; +import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; +import org.spongepowered.asm.mixin.transformer.InvalidMixinException; +import org.spongepowered.asm.mixin.transformer.MixinTargetContext; public class InvalidInjectionException extends InvalidMixinException { private static final long serialVersionUID = 1L; - public InvalidInjectionException(String message) { - super(message); + public InvalidInjectionException(MixinTargetContext mixin, String message) { + super(mixin, message); } - public InvalidInjectionException(Throwable cause) { - super(cause); + public InvalidInjectionException(InjectionInfo info, String message) { + super(info.getContext(), message); } - public InvalidInjectionException(String message, Throwable cause) { - super(message, cause); + public InvalidInjectionException(MixinTargetContext mixin, Throwable cause) { + super(mixin, cause); } + public InvalidInjectionException(InjectionInfo info, Throwable cause) { + super(info.getContext(), cause); + } + + public InvalidInjectionException(MixinTargetContext mixin, String message, Throwable cause) { + super(mixin, message, cause); + } + + public InvalidInjectionException(InjectionInfo info, String message, Throwable cause) { + super(info.getContext(), message, cause); + } + } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 3e569171..b7383a5a 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -85,13 +85,13 @@ public class CallbackInjector extends Injector { @Override protected void sanityCheck(Target target, List injectionPoints) { if (ASMHelper.methodIsStatic(target.method) != this.isStatic) { - throw new InvalidInjectionException("'static' modifier of callback method does not match target in " + this.methodNode.name); + throw new InvalidInjectionException(this.info, "'static' modifier of callback method does not match target in " + this.methodNode.name); } if (Injector.CTOR.equals(target.method.name)) { for (InjectionPoint injectionPoint : injectionPoints) { if (!injectionPoint.getClass().equals(BeforeReturn.class)) { - throw new InvalidInjectionException("Found injection point type " + injectionPoint.getClass().getSimpleName() + throw new InvalidInjectionException(this.info, "Found injection point type " + injectionPoint.getClass().getSimpleName() + " targetting a ctor in " + this.classNode.name + ". Only RETURN allowed for a ctor target"); } } @@ -142,7 +142,8 @@ public class CallbackInjector extends Injector { // If it doesn't match, then cry if (!callbackDescriptor.equals(this.methodNode.desc)) { - throw new InvalidInjectionException("Invalid descriptor on callback: expected " + callbackDescriptor + " found " + this.methodNode.desc); + throw new InvalidInjectionException(this.info, "Invalid descriptor on callback: expected " + callbackDescriptor + " found " + + this.methodNode.desc); } // These two variables keep track of the (additional) stack size required for the two actions we're going to be injecting insns to perform, diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java index 1b033ec1..81e13d52 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java @@ -52,6 +52,11 @@ public abstract class Injector { protected static final String CTOR = ""; + /** + * Injection info + */ + protected InjectionInfo info; + /** * Class node */ @@ -74,6 +79,7 @@ public abstract class Injector { */ public Injector(InjectionInfo info) { this(info.getClassNode(), info.getMethod()); + this.info = info; } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java index ac20408e..2d605de5 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java @@ -74,7 +74,7 @@ public abstract class InvokeInjector extends Injector { @Override protected void sanityCheck(Target target, List injectionPoints) { if (target.isStatic != this.isStatic) { - throw new InvalidInjectionException("'static' modifier of callback method does not match target in " + this.methodNode.name); + throw new InvalidInjectionException(this.info, "'static' modifier of callback method does not match target in " + this.methodNode.name); } } @@ -86,7 +86,7 @@ public abstract class InvokeInjector extends Injector { @Override protected void inject(Target target, AbstractInsnNode node) { if (!(node instanceof MethodInsnNode)) { - throw new InvalidInjectionException(this.annotationType + " annotation is targetting a non-method insn in " + target + throw new InvalidInjectionException(this.info, this.annotationType + " annotation is targetting a non-method insn in " + target + " in " + this.classNode.name); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java index 5498ab2a..8710caa9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java @@ -76,7 +76,7 @@ public class ModifyArgInjector extends InvokeInjector { if (this.singleArgMode) { if (!this.methodArgs[0].equals(this.returnType)) { - throw new InvalidInjectionException("@ModifyArg return type must match the parameter type." + throw new InvalidInjectionException(this.info, "@ModifyArg return type must match the parameter type." + " ARG=" + this.methodArgs[0] + " RETURN=" + this.returnType); } } @@ -118,7 +118,7 @@ public class ModifyArgInjector extends InvokeInjector { */ private int injectMultiArgHandler(Target target, Type[] args, int argIndex, InsnList insns) { if (!Arrays.equals(args, this.methodArgs)) { - throw new InvalidInjectionException("@ModifyArg method targets a method with an invalid signature " + throw new InvalidInjectionException(this.info, "@ModifyArg method targets a method with an invalid signature " + Injector.printArgs(args) + ", expected " + Injector.printArgs(this.methodArgs)); } @@ -132,7 +132,7 @@ public class ModifyArgInjector extends InvokeInjector { protected int findArgIndex(Target target, Type[] args) { if (this.index > -1) { if (this.index >= args.length || !args[this.index].equals(this.returnType)) { - throw new InvalidInjectionException("Specified index " + this.index + " for @ModifyArg is invalid for args " + throw new InvalidInjectionException(this.info, "Specified index " + this.index + " for @ModifyArg is invalid for args " + Injector.printArgs(args) + ", expected " + this.returnType); } return this.index; @@ -146,7 +146,7 @@ public class ModifyArgInjector extends InvokeInjector { } if (argIndex != -1) { - throw new InvalidInjectionException("Found duplicate args with index [" + argIndex + ", " + arg + "] matching type " + throw new InvalidInjectionException(this.info, "Found duplicate args with index [" + argIndex + ", " + arg + "] matching type " + this.returnType + " for @ModifyArg target " + target + " in " + this.classNode.name + ". Please specify index of desired arg."); } @@ -155,7 +155,7 @@ public class ModifyArgInjector extends InvokeInjector { } if (argIndex == -1) { - throw new InvalidInjectionException("Could not find arg matching type " + this.returnType + " for @ModifyArg target " + throw new InvalidInjectionException(this.info, "Could not find arg matching type " + this.returnType + " for @ModifyArg target " + target + " in " + this.classNode.name); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java index f38ea73f..0fc4d711 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java @@ -90,7 +90,7 @@ public class RedirectInjector extends InvokeInjector { if (alternateDesc.equals(this.methodNode.desc)) { injectTargetParams = true; } else { - throw new InvalidInjectionException("@Redirect handler method has an invalid signature " + throw new InvalidInjectionException(this.info, "@Redirect handler method has an invalid signature " + ", expected " + desc + " found " + this.methodNode.desc); } } 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 0161905e..b88cb062 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 @@ -33,13 +33,13 @@ import java.util.List; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; -import org.spongepowered.asm.mixin.InvalidMixinException; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.InjectionPoint; 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.InvalidMixinException; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; @@ -118,7 +118,7 @@ public abstract class InjectionInfo { String method = ASMHelper.getAnnotationValue(this.annotation, "method"); if (method == null) { - throw new InvalidInjectionException(type + " annotation on " + this.method.name + " is missing method name"); + throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing method name"); } List ats = null; @@ -129,20 +129,20 @@ public abstract class InjectionInfo { ats = new ArrayList(); ats.add((AnnotationNode)atValue); } else { - throw new InvalidInjectionException(type + " annotation on " + this.method.name + " is missing 'at' value(s)"); + throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing 'at' value(s)"); } MemberInfo targetMember = MemberInfo.parse(method, this.mixin); if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) { - throw new InvalidInjectionException(type + " annotation on " + this.method.name + " specifies a target class '" + throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '" + targetMember.owner + "', which is not supported"); } this.findMethods(targetMember); if (this.targets.size() == 0) { - throw new InvalidInjectionException(type + " annotation on " + this.method.name + " could not find '" + targetMember.name + "'"); + throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " could not find '" + targetMember.name + "'"); } for (AnnotationNode at : ats) { @@ -178,6 +178,13 @@ public abstract class InjectionInfo { } } + /** + * Get the mixin target context for this injection + */ + public MixinTargetContext getContext() { + return this.mixin; + } + /** * Get the annotation which this InjectionInfo was created from * @@ -241,7 +248,8 @@ public abstract class InjectionInfo { try { annotation = ASMHelper.getSingleVisibleAnnotation(method, Inject.class, ModifyArg.class, Redirect.class); } catch (IllegalArgumentException ex) { - throw new InvalidMixinException("Error parsing annotations on " + method.name + " in " + mixin.getClassName() + ": " + ex.getMessage()); + throw new InvalidMixinException(mixin, "Error parsing annotations on " + method.name + " in " + mixin.getClassName() + ": " + + ex.getMessage()); } if (annotation == null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java index 340c2880..72af7c2c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java @@ -32,8 +32,6 @@ import org.objectweb.asm.Type; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; -import org.spongepowered.asm.mixin.Implements; -import org.spongepowered.asm.mixin.InvalidMixinException; import org.spongepowered.asm.util.ASMHelper; @@ -43,6 +41,11 @@ import org.spongepowered.asm.util.ASMHelper; */ public class InterfaceInfo extends TreeInfo { + /** + * Parent mixin + */ + private final MixinInfo mixin; + /** * Prefix for interface methods. Any methods using this prefix must exist in * the target interface @@ -62,14 +65,16 @@ public class InterfaceInfo extends TreeInfo { /** * Make with the new thing already * + * @param mixin Parent mixin * @param prefix Method prefix * @param iface Interface to load */ - private InterfaceInfo(String prefix, Type iface) { + private InterfaceInfo(MixinInfo mixin, String prefix, Type iface) { if (prefix == null || prefix.length() < 2 || !prefix.endsWith("$")) { - throw new InvalidMixinException(String.format("Prefix %s for iface %s is not valid", prefix, iface.toString())); + throw new InvalidMixinException(mixin, String.format("Prefix %s for iface %s is not valid", prefix, iface.toString())); } + this.mixin = mixin; this.prefix = prefix; this.iface = iface; } @@ -94,7 +99,7 @@ public class InterfaceInfo extends TreeInfo { ClassReader classReader = new ClassReader(TreeInfo.loadClass(ifaceName, true)); classReader.accept(ifaceNode, 0); } catch (Exception ex) { - throw new InvalidMixinException("An error was encountered parsing the interface " + this.iface.toString()); + throw new InvalidMixinException(this.mixin, "An error was encountered parsing the interface " + this.iface.toString()); } for (MethodNode ifaceMethod : ifaceNode.methods) { @@ -156,7 +161,7 @@ public class InterfaceInfo extends TreeInfo { String realName = method.name.substring(this.prefix.length()); String signature = realName + method.desc; if (!this.methods.contains(signature)) { - throw new InvalidMixinException(String.format("%s does not exist in target interface %s", realName, this.iface.toString())); + throw new InvalidMixinException(this.mixin, String.format("%s does not exist in target interface %s", realName, this.iface.toString())); } method.name = realName; @@ -167,17 +172,18 @@ public class InterfaceInfo extends TreeInfo { * Convert an {@link Interface} annotation node into an * {@link InterfaceInfo} * + * @param mixin Parent mixin * @param node Annotation node to process * @return parsed InterfaceInfo object */ - static InterfaceInfo fromAnnotation(AnnotationNode node) { + static InterfaceInfo fromAnnotation(MixinInfo mixin, AnnotationNode node) { String prefix = ASMHelper.getAnnotationValue(node, "prefix"); Type iface = ASMHelper.getAnnotationValue(node, "iface"); if (prefix == null || iface == null) { - throw new InvalidMixinException(String.format("@Interface annotation on is missing a required parameter")); + throw new InvalidMixinException(mixin, String.format("@Interface annotation on is missing a required parameter")); } - return new InterfaceInfo(prefix, iface); + return new InterfaceInfo(mixin, prefix, iface); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InvalidMixinException.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InvalidMixinException.java new file mode 100644 index 00000000..e7d9ee98 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InvalidMixinException.java @@ -0,0 +1,66 @@ +/* + * 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; + +public class InvalidMixinException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + private final MixinInfo mixin; + + public InvalidMixinException(MixinInfo mixin, String message) { + super(message); + this.mixin = mixin; + } + + public InvalidMixinException(MixinTargetContext mixin, String message) { + super(message); + this.mixin = mixin.getInfo(); + } + + public InvalidMixinException(MixinInfo mixin, Throwable message) { + super(message); + this.mixin = mixin; + } + + public InvalidMixinException(MixinTargetContext mixin, Throwable message) { + super(message); + this.mixin = mixin.getInfo(); + } + + public InvalidMixinException(MixinInfo mixin, String message, Throwable cause) { + super(message, cause); + this.mixin = mixin; + } + + public InvalidMixinException(MixinTargetContext mixin, String message, Throwable cause) { + super(message, cause); + this.mixin = mixin.getInfo(); + } + + public MixinInfo getMixin() { + return this.mixin; + } +} 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 218e46d7..d38e57c0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -39,6 +39,7 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.helpers.Booleans; +import org.spongepowered.asm.launch.MixinInitialisationError; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; @@ -85,6 +86,14 @@ class MixinConfig implements Comparable { @SerializedName("minVersion") private String version; + /** + * Determines whether failures in this mixin config are considered terminal + * errors. Use this setting to indicate that failing to apply a mixin in + * this config is a critical error and should cause the game to shutdown. + */ + @SerializedName("required") + private boolean required; + /** * Configuration priority */ @@ -188,6 +197,11 @@ class MixinConfig implements Comparable { if (minVersion.compareTo(curVersion) > 0) { this.logger.warn("Mixin config {} requires mixin subsystem version {} but {} was found. The mixin config will not be applied.", this.name, minVersion, curVersion); + + if (this.required) { + throw new MixinInitialisationError("Required mixin config " + this.name + " requires mixin subsystem version " + minVersion); + } + return false; } @@ -313,6 +327,14 @@ class MixinConfig implements Comparable { } } } + + /** + * True if this mixin is required (failure to apply a defined mixin + * is an error condition). + */ + public boolean isRequired() { + return this.required; + } /** * Get the name of the file from which this configuration object was @@ -401,6 +423,11 @@ class MixinConfig implements Comparable { } return mixins; } + + @Override + public String toString() { + return this.name; + } /* (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index 2778638c..507a5c9e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -49,7 +49,6 @@ import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.Implements; -import org.spongepowered.asm.mixin.InvalidMixinException; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; @@ -198,7 +197,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { private List readTargetClasses(ClassNode classNode, boolean suppressPlugin) { AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); if (mixin == null) { - throw new InvalidMixinException(String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); + throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } List targets = new ArrayList(); @@ -230,10 +229,11 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) { ClassInfo targetInfo = ClassInfo.forName(targetClassName); if (targetInfo.isInterface()) { - throw new InvalidMixinException("@Mixin target " + targetClassName + " is an interface in " + this); + throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is an interface in " + this); } if (checkPublic && targetInfo.isPublic()) { - throw new InvalidMixinException("@Mixin target " + targetClassName + " is public in " + this + " and must be specified in value"); + throw new InvalidMixinException(this, "@Mixin target " + targetClassName + " is public in " + this + + " and must be specified in value"); } if (!outTargets.contains(targetInfo)) { outTargets.add(targetInfo); @@ -252,7 +252,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { private int readPriority(ClassNode classNode) { AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); if (mixin == null) { - throw new InvalidMixinException(String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); + throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } Integer priority = ASMHelper.getAnnotationValue(mixin, "priority"); @@ -269,7 +269,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } if (!targetClass.hasSuperClass(classNode.superName, ClassInfo.Traversal.NONE)) { - throw new InvalidMixinException("Super class '" + classNode.superName.replace('/', '.') + "' of " + this.name + throw new InvalidMixinException(this, "Super class '" + classNode.superName.replace('/', '.') + "' of " + this.name + " was not found in the hierarchy of target class '" + targetClass + "'"); } @@ -287,7 +287,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { private void validateMixin(ClassNode classNode) { // isInner (shouldn't) return true for static inner classes if (this.classInfo.isInner()) { - throw new InvalidMixinException("Inner class mixin must be declared static"); + throw new InvalidMixinException(this, "Inner class mixin must be declared static"); } // Can't have remappable fields or methods on a multi-target mixin, because after obfuscation the fields will remap to conflicting names @@ -300,7 +300,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { this.checkRemappable(Shadow.class, method.name, ASMHelper.getVisibleAnnotation(method, Shadow.class)); AnnotationNode overwrite = ASMHelper.getVisibleAnnotation(method, Overwrite.class); if (overwrite != null && ((method.access & Opcodes.ACC_STATIC) == 0 || (method.access & Opcodes.ACC_PUBLIC) == 0)) { - throw new InvalidMixinException("Found @Overwrite annotation on " + method.name + " in " + this); + throw new InvalidMixinException(this, "Found @Overwrite annotation on " + method.name + " in " + this); } } } @@ -308,7 +308,8 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { private void checkRemappable(Class annotationClass, String name, AnnotationNode annotation) { if (annotation != null && ASMHelper.getAnnotationValue(annotation, "remap", Boolean.TRUE)) { - throw new InvalidMixinException("Found a remappable @" + annotationClass.getSimpleName() + " annotation on " + name + " in " + this); + throw new InvalidMixinException(this, "Found a remappable @" + annotationClass.getSimpleName() + " annotation on " + name + + " in " + this); } } @@ -338,7 +339,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } for (AnnotationNode interfaceNode : interfaces) { - InterfaceInfo interfaceInfo = InterfaceInfo.fromAnnotation(interfaceNode); + InterfaceInfo interfaceInfo = InterfaceInfo.fromAnnotation(this, interfaceNode); this.softImplements.add(interfaceInfo); this.interfaces.add(interfaceInfo.getInternalName()); } @@ -515,7 +516,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { throw new ClassNotFoundException(String.format("The specified mixin '%s' was not found", mixinClassName)); } catch (IOException ex) { 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); + throw new InvalidMixinException(this, "An error was encountered whilst loading the mixin class", ex); } // Inject the mixin class name into the LaunchClassLoader's invalid diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index f354c590..15ff52cf 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -38,8 +38,6 @@ 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.SoftOverride; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.asm.mixin.injection.struct.Target; @@ -184,7 +182,7 @@ public class MixinTargetContext implements IReferenceMapperContext { ClassInfo type = this.targetClassInfo.findCorrespondingType(mixin); if (type == null) { - throw new InvalidMixinException("Resolution error: unable to find corresponding type for " + throw new InvalidMixinException(this, "Resolution error: unable to find corresponding type for " + mixin + " in hierarchy of " + this.targetClassInfo); } @@ -201,10 +199,10 @@ public class MixinTargetContext implements IReferenceMapperContext { public boolean transformField(FieldNode field) { if (MixinTargetContext.IMAGINARY_SUPER.equals(field.name)) { if (field.access != Opcodes.ACC_PRIVATE) { - throw new InvalidMixinException("Imaginary super field " + field.name + " must be private and non-final"); + throw new InvalidMixinException(this, "Imaginary super field " + field.name + " must be private and non-final"); } if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { - throw new InvalidMixinException("Imaginary super field " + field.name + " must have the same type as the parent mixin"); + throw new InvalidMixinException(this, "Imaginary super field " + field.name + " must have the same type as the parent mixin"); } return false; } @@ -226,7 +224,7 @@ public class MixinTargetContext implements IReferenceMapperContext { if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) != null) { Method superMethod = this.targetClassInfo.findMethodInHierarchy(method.name, method.desc, false, Traversal.SUPER); if (superMethod == null || !superMethod.isInjected()) { - throw new InvalidMixinException("Mixin method " + method.name + method.desc + " is tagged with @SoftOverride but no " + throw new InvalidMixinException(this, "Mixin method " + method.name + method.desc + " is tagged with @SoftOverride but no " + "valid method was found in superclasses of " + this.targetClass.name); } } @@ -279,21 +277,21 @@ public class MixinTargetContext implements IReferenceMapperContext { private void processImaginarySuper(MethodNode method, FieldInsnNode fieldInsn) { if (fieldInsn.getOpcode() != Opcodes.GETFIELD) { if (MixinTargetContext.INIT.equals(method.name)) { - throw new InvalidMixinException("Illegal imaginary super declaration: field " + fieldInsn.name + throw new InvalidMixinException(this, "Illegal imaginary super declaration: field " + fieldInsn.name + " must not specify an initialiser"); } - throw new InvalidMixinException("Illegal imaginary super access: found " + ASMHelper.getOpcodeName(fieldInsn.getOpcode()) + throw new InvalidMixinException(this, "Illegal imaginary super access: found " + ASMHelper.getOpcodeName(fieldInsn.getOpcode()) + " opcode in " + method.name + method.desc); } if ((method.access & Opcodes.ACC_PRIVATE) != 0 || (method.access & Opcodes.ACC_STATIC) != 0) { - throw new InvalidMixinException("Illegal imaginary super access: method " + method.name + method.desc + throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name + method.desc + " is private or static"); } if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) == null) { - throw new InvalidMixinException("Illegal imaginary super access: method " + method.name + method.desc + throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name + method.desc + " is not decorated with @SoftOverride"); } @@ -309,7 +307,7 @@ public class MixinTargetContext implements IReferenceMapperContext { } } - throw new InvalidMixinException("Illegal imaginary super access: could not find INVOKE for " + method.name + method.desc); + throw new InvalidMixinException(this, "Illegal imaginary super access: could not find INVOKE for " + method.name + method.desc); } /** @@ -327,7 +325,7 @@ public class MixinTargetContext implements IReferenceMapperContext { Method superMethod = this.targetClassInfo.findMethodInHierarchy(insn.name, insn.desc, false, Traversal.SUPER); if (superMethod != null) { if (superMethod.getOwner().isMixin()) { - throw new InvalidMixinException("Invalid INVOKESPECIAL in " + this + " resolved " + insn.owner + " -> " + superMethod.getOwner() + throw new InvalidMixinException(this, "Invalid INVOKESPECIAL in " + this + " resolved " + insn.owner + " -> " + superMethod.getOwner() + " for " + insn.name + insn.desc); } insn.owner = superMethod.getOwner().getName(); @@ -456,6 +454,13 @@ public class MixinTargetContext implements IReferenceMapperContext { } return target; } + + /** + * Get the mixin info for this mixin + */ + MixinInfo getInfo() { + return this.mixin; + } /** * Get the mixin priority 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 672470ed..555b3655 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -40,6 +40,7 @@ import net.minecraft.launchwrapper.IClassTransformer; import net.minecraft.launchwrapper.Launch; import org.apache.commons.io.FileUtils; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.helpers.Booleans; @@ -56,9 +57,8 @@ import org.objectweb.asm.tree.LineNumberNode; import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import org.objectweb.asm.util.CheckClassAdapter; -import org.spongepowered.asm.mixin.InvalidMixinException; +import org.spongepowered.asm.mixin.MixinApplyError; import org.spongepowered.asm.mixin.MixinEnvironment; -import org.spongepowered.asm.mixin.MixinTransformerError; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; @@ -270,7 +270,14 @@ public class MixinTransformer extends TreeTransformer { try { basicClass = this.applyMixins(transformedName, basicClass, mixins); } catch (InvalidMixinException th) { - this.logger.warn(String.format("Class mixin failed: %s %s", th.getClass().getName(), th.getMessage()), th); + MixinConfig config = th.getMixin().getParent(); + this.logger.log(config.isRequired() ? Level.FATAL : Level.WARN, String.format("Mixin failed applying %s -> %s: %s %s", + th.getMixin(), transformedName, th.getClass().getName(), th.getMessage()), th); + + if (config.isRequired()) { + throw new MixinApplyError("Mixin [" + th.getMixin() + "] FAILED for REQUIRED config [" + config + "]", th); + } + th.printStackTrace(); } } @@ -408,8 +415,10 @@ public class MixinTransformer extends TreeTransformer { this.applyInitialisers(targetClass, mixin); this.applyInjections(targetClass, mixin); mixin.postApply(transformedName, targetClass); + } catch (InvalidMixinException ex) { + throw ex; } catch (Exception ex) { - throw new InvalidMixinException("Unexpecteded error whilst applying the mixin class", ex); + throw new InvalidMixinException(mixin, "Unexpecteded error whilst applying the mixin class", ex); } } @@ -462,7 +471,7 @@ public class MixinTransformer extends TreeTransformer { if (target == null) { // If this field is a shadow field but is NOT found in the target class, that's bad, mmkay if (shadow != null) { - throw new InvalidMixinException(String.format("Shadow field %s was not located in the target class", field.name)); + throw new InvalidMixinException(mixin, String.format("Shadow field %s was not located in the target class", field.name)); } // This is just a local field, so add it @@ -470,7 +479,7 @@ public class MixinTransformer extends TreeTransformer { } else { // Check that the shadow field has a matching descriptor if (!target.desc.equals(field.desc)) { - throw new InvalidMixinException(String.format("The field %s in the target class has a conflicting signature", field.name)); + throw new InvalidMixinException(mixin, String.format("The field %s in the target class has a conflicting signature", field.name)); } } } @@ -485,13 +494,15 @@ public class MixinTransformer extends TreeTransformer { 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)); + throw new InvalidMixinException(mixin, String.format("Mixin classes cannot contain visible static methods or fields, found %s", + field.name)); } // Shadow fields can't have prefixes, it's meaningless for them anyway String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); if (field.name.startsWith(prefix)) { - throw new InvalidMixinException(String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", field.name, mixin)); + throw new InvalidMixinException(mixin, String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", + field.name, mixin)); } } @@ -514,14 +525,14 @@ public class MixinTransformer extends TreeTransformer { // For shadow (and abstract, which can be used as a shorthand for Shadow) methods, we just check they're present MethodNode target = this.findTargetMethod(targetClass, mixinMethod); if (target == null) { - throw new InvalidMixinException(String.format("Shadow method %s was not located in the target class", mixinMethod.name)); + throw new InvalidMixinException(mixin, String.format("Shadow method %s was not located in the target class", mixinMethod.name)); } } else if (!mixinMethod.name.startsWith("<")) { if (MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC) && !isOverwrite) { - throw new InvalidMixinException( + throw new InvalidMixinException(mixin, String.format("Mixin classes cannot contain visible static methods or fields, found %s", mixinMethod.name)); } @@ -565,7 +576,7 @@ public class MixinTransformer extends TreeTransformer { targetClass.methods.remove(target); } else if (isOverwrite) { - throw new InvalidMixinException(String.format("Overwrite target %s was not located in the target class", method.name)); + throw new InvalidMixinException(mixin, String.format("Overwrite target %s was not located in the target class", method.name)); } targetClass.methods.add(method); @@ -639,7 +650,7 @@ public class MixinTransformer extends TreeTransformer { } // Find the initialiser instructions in the candidate ctor - InsnList initialiser = this.getInitialiser(ctor); + InsnList initialiser = this.getInitialiser(mixin, ctor); if (initialiser == null || initialiser.size() == 0) { return; } @@ -720,7 +731,7 @@ public class MixinTransformer extends TreeTransformer { * @return initialiser bytecode extracted from the supplied constructor, or * null if the constructor range could not be parsed */ - private InsnList getInitialiser(MethodNode ctor) { + private InsnList 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()) { @@ -763,7 +774,7 @@ public class MixinTransformer extends TreeTransformer { if (opcode == ivalidOp) { // At the moment I don't handle any transient locals because I haven't seen any in the wild, but let's avoid writing // code which will likely break things and fix it if a real test case ever appears - throw new InvalidMixinException("Cannot handle " + ASMHelper.getOpcodeName(opcode) + " opcode (0x" + throw new InvalidMixinException(mixin, "Cannot handle " + ASMHelper.getOpcodeName(opcode) + " opcode (0x" + Integer.toHexString(opcode).toUpperCase() + ") in class initialiser"); } } @@ -777,7 +788,8 @@ public class MixinTransformer extends TreeTransformer { AbstractInsnNode last = initialiser.getLast(); if (last != null) { if (last.getOpcode() != Opcodes.PUTFIELD) { - throw new InvalidMixinException("Could not parse initialiser, expected 0xB5, found 0x" + Integer.toHexString(last.getOpcode())); + throw new InvalidMixinException(mixin, "Could not parse initialiser, expected 0xB5, found 0x" + + Integer.toHexString(last.getOpcode())); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerError.java similarity index 97% rename from src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java rename to src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerError.java index 80585b28..bc3ea3d4 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinTransformerError.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformerError.java @@ -22,7 +22,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.asm.mixin; +package org.spongepowered.asm.mixin.transformer; /**