Pass mixin prepare errors to error handlers, version bump to 0.5.4

This commit is contained in:
Mumfrey committed 2016-04-25 13:59:05 +01:00
1 parent 0a7067a6e0
commit 107b58d418
6 files changed
+140 -28

No files matched your search

@@ -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
@@ -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.
*
* <p>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.</p>
*
* <p>The original throwable which was caught is passed in via the <code>
* th</code> parameter and the default action is passed in to the <code>
* action</code> parameter. A plugin can choose to output a friendly message
* but leave the original behaviour intact (by returning <code>null</code>
* or returning <code>action</code> directly. Alternatively it may throw a
* different exception or error, or can reduce the severity of the error by
* returning a different {@link ErrorAction}.</p>
*
* @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);
}
@@ -373,8 +373,10 @@ class MixinConfig implements Comparable<MixinConfig>, 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<MixinConfig>, 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);
}
}
@@ -262,21 +262,26 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, 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<MixinInfo>, IMixinInfo {
* @return
*/
protected List<ClassInfo> readTargetClasses(ClassNode classNode, boolean suppressPlugin) {
if (classNode == null) {
return Collections.<ClassInfo>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<MixinInfo>, 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));
@@ -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);
}
}