From 107b58d418be26063504183411764def76fdc57f Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 25 Apr 2016 13:59:05 +0100 Subject: [PATCH] Pass mixin prepare errors to error handlers, version bump to 0.5.4 --- gradle.properties | 2 +- .../asm/launch/MixinBootstrap.java | 2 +- .../extensibility/IMixinErrorHandler.java | 29 +++++- .../asm/mixin/transformer/MixinConfig.java | 12 ++- .../asm/mixin/transformer/MixinInfo.java | 27 ++++-- .../mixin/transformer/MixinTransformer.java | 96 +++++++++++++++---- 6 files changed, 140 insertions(+), 28 deletions(-) diff --git a/gradle.properties b/gradle.properties index 89716d50..475a7c84 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.5.3 +buildVersion=0.5.4 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 4f26b1a8..7f49b9ca 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.5.3"; + public static final String VERSION = "0.5.4"; /** * Blackboard key where the subsystem version will be stored to indicate diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinErrorHandler.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinErrorHandler.java index 2492102e..fff9bc6c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinErrorHandler.java +++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinErrorHandler.java @@ -63,6 +63,33 @@ public interface IMixinErrorHandler { } } + /** + * Called when an error occurs whilst initialising a mixin config. This + * allows the plugin to display more user-friendly error messages if + * required. + * + *

By default, when a critical error occurs the mixin processor will + * raise a warning if the config is not marked as "required" and will throw + * an {@link Error} if it is. This behaviour can be altered by returning + * different values from this method.

+ * + *

The original throwable which was caught is passed in via the + * th parameter and the default action is passed in to the + * action parameter. A plugin can choose to output a friendly message + * but leave the original behaviour intact (by returning null + * or returning action directly. Alternatively it may throw a + * different exception or error, or can reduce the severity of the error by + * returning a different {@link ErrorAction}.

+ * + * @param config Config being prepared when the error occurred + * @param th Throwable which was caught + * @param mixin Mixin which was being applied at the time of the error + * @param action Default action + * @return null to perform the default action (or return action) or new + * action to take + */ + public abstract ErrorAction onPrepareError(IMixinConfig config, Throwable th, IMixinInfo mixin, ErrorAction action); + /** * Called when an error occurs applying a mixin. This allows * the plugin to display more user-friendly error messages if required. @@ -87,6 +114,6 @@ public interface IMixinErrorHandler { * @return null to perform the default action (or return action) or new * action to take */ - public abstract ErrorAction onError(String targetClassName, Throwable th, IMixinInfo mixin, ErrorAction action); + public abstract ErrorAction onApplyError(String targetClassName, Throwable th, IMixinInfo mixin, ErrorAction action); } 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 cc1b22c9..2f1e53db 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -373,8 +373,10 @@ class MixinConfig implements Comparable, IMixinConfig { continue; } + MixinInfo mixin = null; + try { - MixinInfo mixin = new MixinInfo(this, mixinClass, true, this.plugin, suppressPlugin); + mixin = new MixinInfo(this, mixinClass, true, this.plugin, suppressPlugin); if (mixin.getTargetClasses().size() > 0) { MixinConfig.globalMixinList.add(fqMixinClass); for (String targetClass : mixin.getTargetClasses()) { @@ -387,7 +389,15 @@ class MixinConfig implements Comparable, IMixinConfig { } this.mixins.add(mixin); } + } catch (InvalidMixinException ex) { + if (this.required) { + throw ex; + } + this.logger.error(ex.getMessage(), ex); } catch (Exception ex) { + if (this.required) { + throw new InvalidMixinException(mixin, "Error initialising mixin " + mixin + " - " + ex.getClass() + ": " + ex.getMessage(), ex); + } this.logger.error(ex.getMessage(), ex); } } 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 d3285e14..f052e937 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -262,21 +262,26 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @param suppressPlugin * @throws ClassNotFoundException */ - MixinInfo(MixinConfig parent, String mixinName, boolean runTransformers, IMixinConfigPlugin plugin, boolean suppressPlugin) - throws ClassNotFoundException { + MixinInfo(MixinConfig parent, String mixinName, boolean runTransformers, IMixinConfigPlugin plugin, boolean suppressPlugin) { this.parent = parent; this.name = mixinName; this.className = parent.getMixinPackage() + mixinName; this.plugin = plugin; this.phase = MixinEnvironment.getCurrentEnvironment().getPhase(); + ClassNode classNode = null; + // Read the class bytes and transform - byte[] mixinBytes = this.loadMixinClass(this.className, runTransformers); - this.uninitialisedState = new ValidationState(mixinBytes); - this.info = this.uninitialisedState.classInfo; - this.isInterfaceMixin = this.info.isInterface(); + try { + byte[] mixinBytes = this.loadMixinClass(this.className, runTransformers); + this.uninitialisedState = new ValidationState(mixinBytes); + this.info = this.uninitialisedState.classInfo; + classNode = this.uninitialisedState.getClassNode(0); + this.isInterfaceMixin = this.info.isInterface(); + } catch (Exception ex) { + throw new InvalidMixinException(this, ex); + } - ClassNode classNode = this.uninitialisedState.getClassNode(0); this.priority = this.readPriority(classNode); this.targetClasses = this.readTargetClasses(classNode, suppressPlugin); this.targetClassNames = Collections.unmodifiableList(Lists.transform(this.targetClasses, Functions.toStringFunction())); @@ -310,6 +315,10 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @return */ protected List readTargetClasses(ClassNode classNode, boolean suppressPlugin) { + if (classNode == null) { + return Collections.emptyList(); + } + AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); if (mixin == null) { throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); @@ -379,6 +388,10 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { * @return */ protected int readPriority(ClassNode classNode) { + if (classNode == null) { + return this.parent.getDefaultMixinPriority(); + } + AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); if (mixin == null) { throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); 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 ad7db7d6..67541c3f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -72,6 +72,73 @@ import net.minecraft.launchwrapper.Launch; */ public class MixinTransformer extends TreeTransformer { + /** + * Phase during which an error occurred, delegates to functionality in + * available handler + */ + static enum ErrorPhase { + /** + * Error during initialisation of a MixinConfig + */ + PREPARE { + @Override + ErrorAction onError(IMixinErrorHandler handler, String context, InvalidMixinException ex, MixinInfo mixin, ErrorAction action) { + try { + return handler.onPrepareError(mixin.getParent(), ex, mixin, action); + } catch (AbstractMethodError ame) { + // Catch if error handler is pre-0.5.4 + return action; + } + } + + @Override + protected String getContext(MixinInfo mixin, String context) { + return String.format("preparing %s in %s", mixin.getName(), context); + } + }, + /** + * Error during application of a mixin to a target class + */ + APPLY { + @Override + ErrorAction onError(IMixinErrorHandler handler, String context, InvalidMixinException ex, MixinInfo mixin, ErrorAction action) { + try { + return handler.onApplyError(context, ex, mixin, action); + } catch (AbstractMethodError ame) { + // Catch if error handler is pre-0.5.4 + return action; + } + } + + @Override + protected String getContext(MixinInfo mixin, String context) { + return String.format("%s -> %s", mixin, context); + } + }; + + /** + * Human-readable name + */ + private final String text; + + private ErrorPhase() { + this.text = this.name().toLowerCase(); + } + + abstract ErrorAction onError(IMixinErrorHandler handler, String context, InvalidMixinException ex, MixinInfo mixin, ErrorAction action); + + protected abstract String getContext(MixinInfo mixin, String context); + + public String getLogMessage(String context, InvalidMixinException ex, MixinInfo mixin) { + return String.format("Mixin %s failed %s: %s %s", this.text, this.getContext(mixin, context), ex.getClass().getName(), ex.getMessage()); + } + + public String getErrorMessage(MixinInfo mixin, MixinConfig config, Phase phase) { + return String.format("Mixin [%s] from phase [%s] in config [%s] FAILED during %s", mixin, phase, config, this.name()); + } + + } + /** * Proxy transformer for the mixin transformer. These transformers are used * to allow the mixin transformer to be re-registered in the transformer @@ -688,14 +755,14 @@ public class MixinTransformer extends TreeTransformer { } private void handleMixinPrepareError(MixinConfig config, InvalidMixinException ex, MixinEnvironment environment) throws MixinPrepareError { - this.handleMixinError(config.getName(), ex, environment, "prepare"); + this.handleMixinError(config.getName(), ex, environment, ErrorPhase.PREPARE); } private void handleMixinApplyError(String targetClass, InvalidMixinException ex, MixinEnvironment environment) throws MixinApplyError { - this.handleMixinError(targetClass, ex, environment, "apply"); + this.handleMixinError(targetClass, ex, environment, ErrorPhase.APPLY); } - private void handleMixinError(String context, InvalidMixinException ex, MixinEnvironment environment, String operation) throws Error { + private void handleMixinError(String context, InvalidMixinException ex, MixinEnvironment environment, ErrorPhase errorPhase) throws Error { this.errorState = true; MixinInfo mixin = ex.getMixin(); @@ -708,9 +775,9 @@ public class MixinTransformer extends TreeTransformer { .add("Invalid Mixin").centre() .hr('-') .kvWidth(10) - .kv("Action", operation.toUpperCase()) + .kv("Action", errorPhase.name()) .kv("Mixin", mixin.getClassName()) - .kv("Config", mixin.getParent().getName()) + .kv("Config", config.getName()) .kv("Phase", phase) .hr('-') .add(" %s", ex.getClass().getName()) @@ -720,25 +787,20 @@ public class MixinTransformer extends TreeTransformer { .add(ex.getStackTrace(), 8) .trace(action.logLevel); } - - if (context != null) { - for (IMixinErrorHandler handler : this.getErrorHandlers(mixin.getPhase())) { - ErrorAction newAction = handler.onError(context, ex, mixin, action); - if (newAction != null) { - action = newAction; - } + + for (IMixinErrorHandler handler : this.getErrorHandlers(mixin.getPhase())) { + ErrorAction newAction = errorPhase.onError(handler, context, ex, mixin, action); + if (newAction != null) { + action = newAction; } } - 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.logger.log(action.logLevel, errorPhase.getLogMessage(context, ex, mixin), ex); this.errorState = false; if (action == ErrorAction.ERROR) { - throw new MixinApplyError(String.format("Mixin [%s] from phase [%s] in config [%s] FAILED %s", mixin, phase, - config, operation.toUpperCase()), ex); + throw new MixinApplyError(errorPhase.getErrorMessage(mixin, config, phase), ex); } }