diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinPrepareError.java b/src/main/java/org/spongepowered/asm/mixin/MixinPrepareError.java new file mode 100644 index 00000000..f03e789c --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/MixinPrepareError.java @@ -0,0 +1,50 @@ +/* + * This file is part of Sponge, 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; + +/** + * Error to throw for required configs when initialising of the config fails + */ +public class MixinPrepareError extends Error { + + private static final long serialVersionUID = 1L; + + public MixinPrepareError(String message) { + super(message); + } + + public MixinPrepareError(Throwable cause) { + super(cause); + } + + public MixinPrepareError(String message, Throwable cause) { + super(message, cause); + } + + public MixinPrepareError(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} 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 a6ea42e1..0da51a79 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java @@ -174,7 +174,7 @@ public class InterfaceInfo extends TreeInfo { Type iface = ASMHelper.getAnnotationValue(node, "iface"); if (prefix == null || iface == null) { - throw new InvalidMixinException(mixin, String.format("@Interface annotation on is missing a required parameter")); + throw new InvalidMixinException(mixin, String.format("@Interface annotation on %s is missing a required parameter", mixin)); } return new InterfaceInfo(mixin, prefix, iface); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java index 347d2a98..dc6bb192 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinApplicator.java @@ -96,11 +96,11 @@ public class InterfaceMixinApplicator extends MixinApplicator { try { InjectionInfo injectInfo = InjectionInfo.parse(mixin, method); if (injectInfo != null) { - throw new InvalidMixinException(mixin, injectInfo + " is not supported"); + throw new InvalidInterfaceMixinException(mixin, injectInfo + " is not supported on interface mixin method " + method.name); } } catch (InvalidInjectionException ex) { String description = ex.getInjectionInfo() != null ? ex.getInjectionInfo().toString() : "Injection"; - throw new InvalidMixinException(mixin, description + " is not supported"); + throw new InvalidInterfaceMixinException(mixin, description + " is not supported in interface mixin"); } } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java index da26f550..84fcca3b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceMixinPreProcessor.java @@ -56,7 +56,8 @@ class InterfaceMixinPreProcessor extends MixinPreProcessor { 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); + throw new InvalidInterfaceMixinException(this.mixin, "Interface mixin contains a non-public method! Found " + method + " in " + + this.mixin); } super.prepareMethod(mixinMethod, method); @@ -72,7 +73,7 @@ class InterfaceMixinPreProcessor extends MixinPreProcessor { @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 " + throw new InvalidInterfaceMixinException(this.mixin, "Interface mixin contains an instance field! Found " + field.name + " in " + this.mixin); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/InvalidInterfaceMixinException.java b/src/main/java/org/spongepowered/asm/mixin/transformer/InvalidInterfaceMixinException.java new file mode 100644 index 00000000..c1fdbaf7 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InvalidInterfaceMixinException.java @@ -0,0 +1,58 @@ +/* + * 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; + +/** + * Exception thrown for bad states when parsing/loading interface mixins + */ +public class InvalidInterfaceMixinException extends InvalidMixinException { + + private static final long serialVersionUID = 1L; + + public InvalidInterfaceMixinException(MixinInfo mixin, String message) { + super(mixin, message); + } + + public InvalidInterfaceMixinException(MixinTargetContext mixin, String message) { + super(mixin, message); + } + + public InvalidInterfaceMixinException(MixinInfo mixin, Throwable message) { + super(mixin, message); + } + + public InvalidInterfaceMixinException(MixinTargetContext mixin, Throwable message) { + super(mixin, message); + } + + public InvalidInterfaceMixinException(MixinInfo mixin, String message, Throwable cause) { + super(mixin, message, cause); + } + + public InvalidInterfaceMixinException(MixinTargetContext mixin, String message, Throwable cause) { + super(mixin, message, cause); + } + +} 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 1619e472..a6a03cc9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java @@ -227,7 +227,8 @@ public class MixinApplicator { } catch (InvalidMixinException ex) { throw ex; } catch (Exception ex) { - throw new InvalidMixinException(current, "Unexpecteded error whilst applying the mixin class", ex); + throw new InvalidMixinException(current, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst applying the mixin class: " + + ex.getMessage(), ex); } } @@ -358,7 +359,7 @@ public class MixinApplicator { && !MixinApplicator.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC) && !isOverwrite) { throw new InvalidMixinException(mixin, - String.format("Mixin classes cannot contain visible static methods or fields, found %s", mixinMethod.name)); + String.format("Mixin %s contains non-private static method %s%s", mixin, mixinMethod.name, mixinMethod.desc)); } this.mergeMethod(mixin, mixinMethod, isOverwrite); @@ -394,7 +395,8 @@ public class MixinApplicator { this.targetClass.methods.remove(target); } } else if (isOverwrite) { - throw new InvalidMixinException(mixin, 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 target class %s", + method.name, mixin.getTargetClassRef())); } this.targetClass.methods.add(method); @@ -470,20 +472,22 @@ public class MixinApplicator { if (isOverwrite) { throw new InvalidMixinException(mixin, "@Intrinsic is not compatible with @Overwrite, remove one of these annotations on " - + method.name); + + method.name + " in " + mixin); } + String methodName = method.name + method.desc; if (MixinApplicator.hasFlag(method, Opcodes.ACC_STATIC)) { - throw new InvalidMixinException(mixin, "@Intrinsic method cannot be static, found " + method.name); + throw new InvalidMixinException(mixin, "@Intrinsic method cannot be static, found " + methodName + " in " + mixin); } AnnotationNode renamed = ASMHelper.getVisibleAnnotation(method, MixinRenamed.class); if (renamed == null || !ASMHelper.getAnnotationValue(renamed, "isInterfaceMember", false)) { - throw new InvalidMixinException(mixin, "@Intrinsic method must be prefixed interface method, no rename encountered on " + method.name); + throw new InvalidMixinException(mixin, "@Intrinsic method must be prefixed interface method, no rename encountered on " + + methodName + " in " + mixin); } if (!ASMHelper.getAnnotationValue(intrinsic, "displace", false)) { - this.logger.log(mixin.getLoggingLevel(), "Skipping Intrinsic mixin method {}", method.name); + this.logger.log(mixin.getLoggingLevel(), "Skipping Intrinsic mixin method {} for {}", methodName, mixin.getTargetClassRef()); return true; } @@ -715,7 +719,7 @@ public class MixinApplicator { if (last != null) { if (last.getOpcode() != Opcodes.PUTFIELD) { throw new InvalidMixinException(mixin, "Could not parse initialiser, expected 0xB5, found 0x" - + Integer.toHexString(last.getOpcode())); + + Integer.toHexString(last.getOpcode()) + " in " + mixin); } } @@ -739,7 +743,7 @@ public class MixinApplicator { continue; } if (node instanceof JumpInsnNode) { - throw new InvalidMixinException(mixin, "Unsupported opcode in initialiser"); + throw new InvalidMixinException(mixin, "Unsupported JUMP opcode in initialiser in " + mixin); } AbstractInsnNode imACloneNow = node.clone(labels); ctor.instructions.insert(insn, imACloneNow); 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 0afc1b4e..65f61050 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java @@ -346,24 +346,26 @@ class MixinPreProcessor { if (MixinApplicator.hasFlag(field, Opcodes.ACC_STATIC) && !MixinApplicator.hasFlag(field, Opcodes.ACC_PRIVATE) && !MixinApplicator.hasFlag(field, Opcodes.ACC_SYNTHETIC)) { - throw new InvalidMixinException(context, String.format("Mixin classes cannot contain visible static methods or fields, found %s", - field.name)); + throw new InvalidMixinException(context, String.format("Mixin %s contains non-private static field %s:%s", + context, field.name, field.desc)); } // Shadow fields can't have prefixes, it's meaningless for them anyway String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); if (field.name.startsWith(prefix)) { - throw new InvalidMixinException(context, String.format("Shadow field %s in %s has a shadow prefix. This is not allowed.", - field.name, context)); + throw new InvalidMixinException(context, String.format("@Shadow field %s.%s has a shadow prefix. This is not allowed.", + context, field.name)); } // Imaginary super fields get stripped from the class, but first we validate them if (Constants.IMAGINARY_SUPER.equals(field.name)) { if (field.access != Opcodes.ACC_PRIVATE) { - throw new InvalidMixinException(this.mixin, "Imaginary super field " + field.name + " must be private and non-final"); + throw new InvalidMixinException(this.mixin, "Imaginary super field " + context + "." + field.name + + " must be private and non-final"); } if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { - throw new InvalidMixinException(this.mixin, "Imaginary super field " + field.name + " must have the same type as the parent mixin"); + throw new InvalidMixinException(this.mixin, "Imaginary super field " + context + "." + field.name + + " must have the same type as the parent mixin"); } return false; } 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 19d9aa00..ad7db7d6 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -51,6 +51,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.mixin.MixinException; +import org.spongepowered.asm.mixin.MixinPrepareError; import org.spongepowered.asm.mixin.audit.ClassAlreadyLoadedException; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinErrorHandler; @@ -61,6 +62,7 @@ import org.spongepowered.asm.mixin.transformer.debug.IHotSwap; import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.transformers.TreeTransformer; import org.spongepowered.asm.util.Constants; +import org.spongepowered.asm.util.PrettyPrinter; import net.minecraft.launchwrapper.IClassTransformer; import net.minecraft.launchwrapper.Launch; @@ -451,20 +453,14 @@ public class MixinTransformer extends TreeTransformer { 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); - } - - this.handleMixinErrorState(transformedName, th); + this.dumpClassOnFailure(transformedName, basicClass, environment); + this.handleMixinApplyError(transformedName, th, environment); } } return basicClass; } catch (Exception ex) { - if (environment.getOption(Option.DUMP_TARGET_ON_FAILURE)) { - this.dumpClass(transformedName.replace('.', '/') + ".target", basicClass); - } - + this.dumpClassOnFailure(transformedName, basicClass, environment); throw new MixinTransformerError("An unexpected critical error was encountered", ex); } finally { this.lock.pop(); @@ -496,7 +492,7 @@ public class MixinTransformer extends TreeTransformer { this.addConfigs(environment); this.addModules(environment); - int totalMixins = this.initConfigs(); + int totalMixins = this.initConfigs(environment); this.currentEnvironment = environment; double elapsedTime = (System.currentTimeMillis() - startTime) * 0.001D; @@ -554,9 +550,10 @@ public class MixinTransformer extends TreeTransformer { /** * Initialise mixin configs * + * @param environment Environment * @return total number of mixins initialised */ - private int initConfigs() { + private int initConfigs(MixinEnvironment environment) { int totalMixins = 0; for (MixinConfig config : this.pendingConfigs) { @@ -564,6 +561,8 @@ public class MixinTransformer extends TreeTransformer { this.logger.log(this.verboseLoggingLevel, "Preparing {} ({})", config, config.getDeclaredMixinCount()); config.initialise(this.hotSwapper); totalMixins += config.getMixinCount(); + } catch (InvalidMixinException ex) { + this.handleMixinPrepareError(config, ex, environment); } catch (Exception ex) { this.logger.error("Error encountered whilst initialising mixin config '" + config.getName() + "': " + ex.getMessage(), ex); } @@ -588,6 +587,8 @@ public class MixinTransformer extends TreeTransformer { for (MixinConfig config : this.pendingConfigs) { try { config.postInitialise(this.hotSwapper); + } catch (InvalidMixinException ex) { + this.handleMixinPrepareError(config, ex, environment); } catch (Exception ex) { this.logger.error("Error encountered during mixin config postInit step'" + config.getName() + "': " + ex.getMessage(), ex); } @@ -686,32 +687,58 @@ public class MixinTransformer extends TreeTransformer { } } - private void handleMixinErrorState(String targetClassName, InvalidMixinException th) throws MixinApplyError { + private void handleMixinPrepareError(MixinConfig config, InvalidMixinException ex, MixinEnvironment environment) throws MixinPrepareError { + this.handleMixinError(config.getName(), ex, environment, "prepare"); + } + + private void handleMixinApplyError(String targetClass, InvalidMixinException ex, MixinEnvironment environment) throws MixinApplyError { + this.handleMixinError(targetClass, ex, environment, "apply"); + } + + private void handleMixinError(String context, InvalidMixinException ex, MixinEnvironment environment, String operation) throws Error { this.errorState = true; - MixinInfo mixin = th.getMixin(); + MixinInfo mixin = ex.getMixin(); MixinConfig config = mixin.getParent(); + Phase phase = mixin.getPhase(); ErrorAction action = config.isRequired() ? ErrorAction.ERROR : ErrorAction.WARN; - if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERBOSE)) { - th.printStackTrace(); + if (environment.getOption(Option.DEBUG_VERBOSE)) { + new PrettyPrinter() + .add("Invalid Mixin").centre() + .hr('-') + .kvWidth(10) + .kv("Action", operation.toUpperCase()) + .kv("Mixin", mixin.getClassName()) + .kv("Config", mixin.getParent().getName()) + .kv("Phase", phase) + .hr('-') + .add(" %s", ex.getClass().getName()) + .hr('-') + .addWrapped(" %s", ex.getMessage()) + .hr('-') + .add(ex.getStackTrace(), 8) + .trace(action.logLevel); } - for (IMixinErrorHandler handler : this.getErrorHandlers(mixin.getPhase())) { - ErrorAction newAction = handler.onError(targetClassName, th, mixin, action); - if (newAction != null) { - action = newAction; + if (context != null) { + for (IMixinErrorHandler handler : this.getErrorHandlers(mixin.getPhase())) { + ErrorAction newAction = handler.onError(context, ex, mixin, action); + if (newAction != null) { + action = newAction; + } } } - this.logger.log(action.logLevel, String.format("Mixin failed applying %s -> %s: %s %s", mixin, targetClassName, - th.getClass().getName(), th.getMessage()), th); + String ext = context != null ? String.format(" -> %s", context) : ""; + this.logger.log(action.logLevel, String.format("Mixin %s failed %s%s: %s %s", operation, mixin, ext, + ex.getClass().getName(), ex.getMessage()), ex); this.errorState = false; if (action == ErrorAction.ERROR) { - throw new MixinApplyError(String.format("Mixin [%s] from phase [%s] in config [%s] FAILED", mixin, mixin.getPhase(), - config), th); + throw new MixinApplyError(String.format("Mixin [%s] from phase [%s] in config [%s] FAILED %s", mixin, phase, + config, operation.toUpperCase()), ex); } } @@ -766,6 +793,12 @@ public class MixinTransformer extends TreeTransformer { return bytes; } + private void dumpClassOnFailure(String className, byte[] bytes, MixinEnvironment env) { + if (env.getOption(Option.DUMP_TARGET_ON_FAILURE)) { + this.dumpClass(className.replace('.', '/') + ".target", bytes); + } + } + private File dumpClass(String fileName, byte[] bytes) { File outputFile = new File(this.classExportDir, fileName + ".class"); try { diff --git a/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java b/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java index 3b84814c..ec8cfb50 100644 --- a/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java +++ b/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java @@ -683,9 +683,21 @@ public class PrettyPrinter { * @return fluent interface */ public PrettyPrinter add(Throwable th, int indent) { - String margin = Strings.repeat(" ", indent); this.add("%s: %s", th.getClass().getName(), th.getMessage()); - for (StackTraceElement st : th.getStackTrace()) { + return this.add(th.getStackTrace(), indent); + } + + /** + * Print a formatted representation of the specified stack trace with the + * specified indent + * + * @param stackTrace stack trace to print + * @param indent Indent size for stacktrace lines + * @return fluent interface + */ + public PrettyPrinter add(StackTraceElement[] stackTrace, int indent) { + String margin = Strings.repeat(" ", indent); + for (StackTraceElement st : stackTrace) { this.add("%s%s", margin, st); } return this;