Support constraints on injector annotations, bump to 0.5.8, closes #135

This commit is contained in:
Mumfrey committed 2016-06-15 16:59:28 +01:00
1 parent 45cf3d6ced
commit 19ca12970a
12 files changed
+179 -57

No files matched your search

@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5.7";
public static final String VERSION = "0.5.8";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -34,6 +34,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
/**
* Specifies that this mixin method should inject a callback (or
@@ -174,4 +175,12 @@ public @interface Inject {
*/
public int expect() default 1;
/**
* Returns constraints which must be validated for this injector to
* succeed. See {@link Constraint} for details of constraint formats.
*
* @return Constraints for this annotation
*/
public String constraints() default "";
}
@@ -32,6 +32,7 @@ import java.lang.annotation.Target;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
/**
* Specifies that this mixin method should inject an argument modifier to itself
@@ -124,5 +125,13 @@ public @interface ModifyArg {
* @return Minimum number of <em>expected</em> callbacks, default 1
*/
public int expect() default 1;
/**
* Returns constraints which must be validated for this injector to
* succeed. See {@link Constraint} for details of constraint formats.
*
* @return Constraints for this annotation
*/
public String constraints() default "";
}
@@ -32,6 +32,7 @@ import java.lang.annotation.Target;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
/**
* Specifies that this mixin method should inject a constant modifier callback
@@ -122,5 +123,13 @@ public @interface ModifyConstant {
* @return Minimum number of <em>expected</em> callbacks, default 1
*/
public int expect() default 1;
/**
* Returns constraints which must be validated for this injector to
* succeed. See {@link Constraint} for details of constraint formats.
*
* @return Constraints for this annotation
*/
public String constraints() default "";
}
@@ -32,6 +32,7 @@ import java.lang.annotation.Target;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
/**
* Specifies that this mixin method should inject a variable modifier callback
@@ -185,5 +186,13 @@ public @interface ModifyVariable {
* @return Minimum number of <em>expected</em> callbacks, default 1
*/
public int expect() default 1;
/**
* Returns constraints which must be validated for this injector to
* succeed. See {@link Constraint} for details of constraint formats.
*
* @return Constraints for this annotation
*/
public String constraints() default "";
}
@@ -32,6 +32,7 @@ import java.lang.annotation.Target;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
/**
* <p>Specifies that this mixin method should redirect the specified
@@ -184,5 +185,13 @@ public @interface Redirect {
* @return Minimum number of <em>expected</em> callbacks, default 1
*/
public int expect() default 1;
/**
* Returns constraints which must be validated for this injector to
* succeed. See {@link Constraint} for details of constraint formats.
*
* @return Constraints for this annotation
*/
public String constraints() default "";
}
@@ -24,6 +24,7 @@
*/
package org.spongepowered.asm.mixin.transformer;
import java.lang.annotation.Annotation;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
@@ -52,6 +53,11 @@ import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
@@ -63,11 +69,25 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
import org.spongepowered.asm.util.throwables.ConstraintViolationException;
import org.spongepowered.asm.util.throwables.InvalidConstraintException;
import com.google.common.collect.ImmutableList;
/**
* Applies mixins to a target class
*/
public class MixinApplicatorStandard {
/**
* Annotations which can have constraints
*/
protected static final List<Class<? extends Annotation>> CONSTRAINED_ANNOTATIONS = ImmutableList.<Class<? extends Annotation>>of(
Overwrite.class,
Inject.class,
ModifyArg.class,
Redirect.class,
ModifyVariable.class,
ModifyConstant.class
);
/**
* Passes the mixin applicator applies to each mixin
*/
@@ -324,7 +344,7 @@ public class MixinApplicatorStandard {
this.mergeAnnotations(shadow, target);
// Strip the FINAL flag from @Mutable non-private fields
if (entry.getValue().isDecoratedMutable() && !MixinApplicatorStandard.hasFlag(target, Opcodes.ACC_PRIVATE)) {
if (entry.getValue().isDecoratedMutable() && !ASMHelper.hasFlag(target, Opcodes.ACC_PRIVATE)) {
target.access &= ~Opcodes.ACC_FINAL;
}
}
@@ -359,21 +379,8 @@ public class MixinApplicatorStandard {
mixin.transformMethod(mixinMethod);
if (!mixinMethod.name.startsWith("<")) {
AnnotationNode overwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class);
boolean isOverwrite = overwrite != null;
if (isOverwrite) {
this.checkConstraints(mixin, overwrite);
}
if (MixinApplicatorStandard.hasFlag(mixinMethod, Opcodes.ACC_STATIC)
&& !MixinApplicatorStandard.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE)
&& !MixinApplicatorStandard.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC)
&& !isOverwrite) {
throw new InvalidMixinException(mixin,
String.format("Mixin %s contains non-private static method %s%s", mixin, mixinMethod.name, mixinMethod.desc));
}
this.mergeMethod(mixin, mixinMethod, isOverwrite);
this.checkMethodConstraints(mixin, mixinMethod);
this.mergeMethod(mixin, mixinMethod);
} else if (Constants.CLINIT.equals(mixinMethod.name)) {
// Class initialiser insns get appended
this.appendInsns(mixinMethod);
@@ -389,7 +396,8 @@ public class MixinApplicatorStandard {
* @param isOverwrite true if the method is annotated with an
* {@link Overwrite} annotation
*/
protected void mergeMethod(MixinTargetContext mixin, MethodNode method, boolean isOverwrite) {
protected void mergeMethod(MixinTargetContext mixin, MethodNode method) {
boolean isOverwrite = ASMHelper.getVisibleAnnotation(method, Overwrite.class) != null;
MethodNode target = this.findTargetMethod(method);
if (target != null) {
@@ -441,8 +449,8 @@ public class MixinApplicatorStandard {
throw new ClassFormatError("Invalid @MixinMerged annotation found in" + mixin + " at " + method.name + " in " + this.targetClass.name);
}
if (MixinApplicatorStandard.hasFlag(target, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)
&& MixinApplicatorStandard.hasFlag(method, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)) {
if (ASMHelper.hasFlag(target, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)
&& ASMHelper.hasFlag(method, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE)) {
if (mixin.getEnvironment().getOption(Option.DEBUG_VERBOSE)) {
this.logger.warn("Synthetic bridge method clash for {} in {}", method.name, mixin);
}
@@ -487,7 +495,7 @@ public class MixinApplicatorStandard {
}
String methodName = method.name + method.desc;
if (MixinApplicatorStandard.hasFlag(method, Opcodes.ACC_STATIC)) {
if (ASMHelper.hasFlag(method, Opcodes.ACC_STATIC)) {
throw new InvalidMixinException(mixin, "@Intrinsic method cannot be static, found " + methodName + " in " + mixin);
}
@@ -785,24 +793,40 @@ public class MixinApplicatorStandard {
mixin.applyInjections();
}
/**
* Check constraints in annotations on the specified mixin method
*
* @param mixin Target context
* @param method Mixin method
*/
protected void checkMethodConstraints(MixinTargetContext mixin, MethodNode method) {
for (Class<? extends Annotation> annotationType : MixinApplicatorStandard.CONSTRAINED_ANNOTATIONS) {
AnnotationNode annotation = ASMHelper.getVisibleAnnotation(method, annotationType);
if (annotation != null) {
this.checkConstraints(mixin, method, annotation);
}
}
}
/**
* Check constraints for the specified annotation based on token values in
* the current environment
*
* @param mixin Mixin being applied
* @param method annotated method
* @param annotation Annotation node to check constraints
*/
protected final void checkConstraints(MixinTargetContext mixin, AnnotationNode annotation) {
protected final void checkConstraints(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
try {
Constraint constraint = ConstraintParser.parse(annotation);
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
try {
constraint.check(environment);
} catch (ConstraintViolationException ex) {
if (environment.getOption(Option.IGNORE_CONSTRAINTS)) {
this.logger.warn("Constraint violation: {}", ex.getMessage());
} else {
throw new InvalidMixinException(mixin, ex.getMessage(), ex);
String message = String.format("Constraint violation: %s on %s in %s", ex.getMessage(), method, mixin);
this.logger.warn(message);
if (!environment.getOption(Option.IGNORE_CONSTRAINTS)) {
throw new InvalidMixinException(mixin, message, ex);
}
}
} catch (InvalidConstraintException ex) {
@@ -970,25 +994,4 @@ public class MixinApplicatorStandard {
return null;
}
/**
* Check whether the specified flag is set on the specified method
*
* @param method
* @param flag
* @return True if the specified flag is set in this method's access flags
*/
protected static boolean hasFlag(MethodNode method, int flag) {
return (method.access & flag) == flag;
}
/**
* Check whether the specified flag is set on the specified field
*
* @param field
* @param flag
* @return True if the specified flag is set in this field's access flags
*/
protected static boolean hasFlag(FieldNode field, int flag) {
return (field.access & flag) == flag;
}
}
@@ -37,6 +37,7 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.lib.ClassReader;
import org.spongepowered.asm.lib.MethodVisitor;
import org.spongepowered.asm.lib.Opcodes;
import org.spongepowered.asm.lib.tree.AnnotationNode;
import org.spongepowered.asm.lib.tree.ClassNode;
@@ -71,6 +72,29 @@ import net.minecraft.launchwrapper.LaunchClassLoader;
*/
class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
/**
* A MethodNode in a mixin
*/
public class MixinMethodNode extends MethodNode {
private final String originalName;
public MixinMethodNode(int access, String name, String desc, String signature, String[] exceptions) {
super(Opcodes.ASM5, access, name, desc, signature, exceptions);
this.originalName = name;
}
@Override
public String toString() {
return String.format("%s%s", this.originalName, this.desc);
}
public String getOriginalName() {
return this.originalName;
}
}
/**
* ClassNode for a MixinInfo
*/
@@ -88,6 +112,13 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
return MixinInfo.this;
}
@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) {
MethodNode method = new MixinMethodNode(access, name, desc, signature, exceptions);
this.methods.add(method);
return method;
}
}
/**
@@ -31,6 +31,7 @@ import org.spongepowered.asm.lib.tree.FieldNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidInterfaceMixinException;
import org.spongepowered.asm.util.ASMHelper;
/**
* Bytecode preprocessor for interface mixins, simply performs some additional
@@ -56,7 +57,7 @@ class MixinPreProcessorInterface extends MixinPreProcessorStandard {
@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 (!MixinApplicatorStandard.hasFlag(mixinMethod, Opcodes.ACC_PUBLIC)) {
if (!ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_PUBLIC)) {
throw new InvalidInterfaceMixinException(this.mixin, "Interface mixin contains a non-public method! Found " + method + " in "
+ this.mixin);
}
@@ -73,7 +74,7 @@ class MixinPreProcessorInterface extends MixinPreProcessorStandard {
*/
@Override
protected boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) {
if (!MixinApplicatorStandard.hasFlag(field, Opcodes.ACC_STATIC)) {
if (!ASMHelper.hasFlag(field, Opcodes.ACC_STATIC)) {
throw new InvalidInterfaceMixinException(this.mixin, "Interface mixin contains an instance field! Found " + field.name + " in "
+ this.mixin);
}
@@ -213,6 +213,14 @@ class MixinPreProcessorStandard {
}
protected boolean validateMethod(MixinTargetContext context, MethodNode mixinMethod) {
if (ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_STATIC)
&& !ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE)
&& !ASMHelper.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC)
&& !(ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null)) {
throw new InvalidMixinException(context,
String.format("Mixin %s contains non-private static method %s", context, mixinMethod));
}
return true;
}
@@ -259,7 +267,7 @@ class MixinPreProcessorStandard {
MethodNode target = context.findMethod(mixinMethod, annotation);
if (method.isUnique()) {
throw new InvalidMixinException(this.mixin, ASMHelper.getSimpleName(type) + " method " + mixinMethod.name + " cannot be @Unique");
throw new InvalidMixinException(this.mixin, "@" + ASMHelper.getSimpleName(type) + " method " + mixinMethod.name + " cannot be @Unique");
}
if (target == null) {
@@ -268,7 +276,7 @@ class MixinPreProcessorStandard {
}
target = context.findRemappedMethod(mixinMethod);
if (target == null) {
throw new InvalidMixinException(this.mixin, ASMHelper.getSimpleName(type) + " method " + mixinMethod.name
throw new InvalidMixinException(this.mixin, "@" + ASMHelper.getSimpleName(type) + " method " + mixinMethod.name
+ " was not located in the target class");
}
mixinMethod.name = target.name;
@@ -386,7 +394,7 @@ class MixinPreProcessorStandard {
}
field.setDecoratedFinal(isFinal, isMutable);
if (this.verboseLogging && MixinApplicatorStandard.hasFlag(target, Opcodes.ACC_FINAL) != isFinal) {
if (this.verboseLogging && ASMHelper.hasFlag(target, Opcodes.ACC_FINAL) != isFinal) {
String message = isFinal
? "@Shadow field {}::{} is decorated with @Final but target is not final"
: "@Shadow target {}::{} is final but shadow is not decorated with @Final";
@@ -400,9 +408,9 @@ class MixinPreProcessorStandard {
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 (MixinApplicatorStandard.hasFlag(field, Opcodes.ACC_STATIC)
&& !MixinApplicatorStandard.hasFlag(field, Opcodes.ACC_PRIVATE)
&& !MixinApplicatorStandard.hasFlag(field, Opcodes.ACC_SYNTHETIC)
if (ASMHelper.hasFlag(field, Opcodes.ACC_STATIC)
&& !ASMHelper.hasFlag(field, Opcodes.ACC_PRIVATE)
&& !ASMHelper.hasFlag(field, Opcodes.ACC_SYNTHETIC)
&& shadow == null) {
throw new InvalidMixinException(context, String.format("Mixin %s contains non-private static field %s:%s",
context, field.name, field.desc));
@@ -1026,7 +1026,7 @@ public class ASMHelper {
}
public static String getSimpleName(Class<? extends Annotation> annotationType) {
return String.format("@%s", annotationType.getSimpleName());
return annotationType.getSimpleName();
}
public static String getSimpleName(AnnotationNode annotation) {
@@ -1085,4 +1085,38 @@ public class ASMHelper {
int index = Ints.indexOf(ASMHelper.CONSTANTS_ALL, insn.getOpcode());
return index < 0 ? null : Type.getType(ASMHelper.CONSTANTS_TYPES[index]);
}
/**
* Check whether the specified flag is set on the specified class
*
* @param classNode
* @param flag
* @return True if the specified flag is set in this method's access flags
*/
public static boolean hasFlag(ClassNode classNode, int flag) {
return (classNode.access & flag) == flag;
}
/**
* Check whether the specified flag is set on the specified method
*
* @param method
* @param flag
* @return True if the specified flag is set in this method's access flags
*/
public static boolean hasFlag(MethodNode method, int flag) {
return (method.access & flag) == flag;
}
/**
* Check whether the specified flag is set on the specified field
*
* @param field
* @param flag
* @return True if the specified flag is set in this field's access flags
*/
public static boolean hasFlag(FieldNode field, int flag) {
return (field.access & flag) == flag;
}
}