diff --git a/README.MD b/README.MD index ed39aaa7..4266a915 100644 --- a/README.MD +++ b/README.MD @@ -453,5 +453,14 @@ plugin developed by [DemonWav](https://github.com/demonwav). January 2017 + + 0.6.8 + + + + February 2017 + diff --git a/gradle.properties b/gradle.properties index 86f56e55..045814e2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.6.7 +buildVersion=0.6.8 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 9b75463f..39c1f11c 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -62,7 +62,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.6.7"; + public static final String VERSION = "0.6.8"; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Constant.java b/src/main/java/org/spongepowered/asm/mixin/injection/Constant.java index 8600d46e..0cf8669b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Constant.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Constant.java @@ -27,6 +27,8 @@ package org.spongepowered.asm.mixin.injection; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import org.spongepowered.asm.lib.Opcodes; + /** * Annotation for specifying the injection point for an {@link ModifyConstant} * injector. Leaving all values unset causes the injection point to match all @@ -41,14 +43,94 @@ import java.lang.annotation.RetentionPolicy; public @interface Constant { /** - * Causes this injector to match ACONST_NULL (null object) literals + * Available options for the {@link Constant#expandZeroConditions} setting. + * Each option matches the inverse instructions as well because in the + * compiled code it is not unusual for if (x > 0) to be compiled + * as if (!(x <= 0)) + * + *

Note that all of these options assume that x is on the + * left-hand side of the expression in question. For expressions where + * zero is on the right-hand side you should choose the inverse.

+ */ + public enum Condition { + + /** + * Match < operators and >= instructions: + * + * x < 0 + */ + LESS_THAN_ZERO(Opcodes.IFLT, Opcodes.IFGE), + + /** + * Match <= operators and > instructions + * + * x <= 0 + */ + LESS_THAN_OR_EQUAL_TO_ZERO(Opcodes.IFLE, Opcodes.IFGT), + + /** + * Match >= operators and < instructions, equivalent to + * {@link #LESS_THAN_ZERO} + * + * x >= 0 + */ + GREATER_THAN_OR_EQUAL_TO_ZERO(Condition.LESS_THAN_ZERO), + + /** + * Match > operators and <= instructions, equivalent to + * {@link #LESS_THAN_OR_EQUAL_TO_ZERO} + * + * x > 0 + */ + GREATER_THAN_ZERO(Condition.LESS_THAN_OR_EQUAL_TO_ZERO); + + private final int[] opcodes; + + private final Condition equivalence; + + private Condition(int... opcodes) { + this(null, opcodes); + } + + private Condition(Condition equivalence) { + this(equivalence, equivalence.opcodes); + } + + private Condition(Condition equivalence, int... opcodes) { + this.equivalence = equivalence != null ? equivalence : this; + this.opcodes = opcodes; + } + + /** + * Get the condition which is equivalent to this condition + */ + public Condition getEquivalentCondition() { + return this.equivalence; + } + + /** + * Get the opcodes for this condition + */ + public int[] getOpcodes() { + return this.opcodes; + } + + } + + + /** + * Causes this injector to match ACONST_NULL (null object) literals * * @return true to match null */ public boolean nullValue() default false; /** - * Specify an integer constant to match, includes byte and short values + * Specify an integer constant to match, includes byte and short values. + * + *

Special note for referencing 0 (zero) which forms part of + * a comparison expression: See the {@link #expandZeroConditions} option + * below.

* * @return integer value to match */ @@ -102,10 +184,53 @@ public @interface Constant { */ public int ordinal() default -1; + /** + * Whilst most constants can be located in the compiled method with relative + * ease, there exists a special case when a zero is used in a + * conditional expression. For example: + * + *
if (x >= 0)
+ * + *

This special case occurs because java includes explicit instructions + * for this type of comparison, and thus the compiled code might look more + * like this:

+ * + *
if (x.isGreaterThanOrEqualToZero())
+ * + *

Of course if we know that the constant we are searching for is part of + * a comparison, then we can explicitly search for the + * isGreaterThanOrEqualToZero and convert it back to the original + * form in order to redirect it just like any other constant access.

+ * + *

To enable this behaviour, you may specify one or more values for this + * argument based on the type of expression you wish to expand. Since the + * Java compiler is wont to compile certain expressions as the inverse + * of their source-level counterparts (eg. compiling a do this if + * greater than structure to a ignore this if less than or equal + * structure); specifying a particular expression type implicitly + * includes the inverse expression as well.

+ * + *

It is worth noting that the effect on ordinals may be hard to predict, + * and thus care should be taken to ensure that the selected injection + * points match the expected locations.

+ * + *

Specifying this option has the following effects:

+ * + * + */ + public Condition[] expandZeroConditions() default {}; + /** * @return true to enable verbose debug logging for this injection point */ public boolean log() default false; + } - - diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java index a5146bad..3aad3656 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java @@ -49,6 +49,10 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint; * *

The callback receives the original value of the constant, and should * return the new value.

+ * + *

Like other Redirect injectors, the callback signature can + * optionally include the target method arguments by simply appending them to + * the handler method signature.

*/ @Target({ ElementType.METHOD }) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlices.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlices.java index d0178f84..17a0107d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlices.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlices.java @@ -24,9 +24,7 @@ */ package org.spongepowered.asm.mixin.injection.code; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.spongepowered.asm.lib.tree.AnnotationNode; @@ -98,21 +96,12 @@ public final class MethodSlices { * @param info owning injector * @return parsed slice collection */ - @SuppressWarnings("unchecked") public static MethodSlices parse(InjectionInfo info) { MethodSlices slices = new MethodSlices(info); AnnotationNode annotation = info.getAnnotation(); if (annotation != null) { - List sliceNodes = new ArrayList(); - Object sliceValue = ASMHelper.getAnnotationValue(annotation, "slice"); - if (sliceValue instanceof List) { - sliceNodes.addAll((List)sliceValue); - } else if (sliceValue instanceof AnnotationNode) { - sliceNodes.add((AnnotationNode)sliceValue); - } - - for (AnnotationNode node : sliceNodes) { + for (AnnotationNode node : ASMHelper.getAnnotationValue(annotation, "slice", true)) { MethodSlice slice = MethodSlice.parse(info, node); slices.add(slice); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java index dca2f087..f81276a8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyConstantInjector.java @@ -28,6 +28,8 @@ import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.InsnList; +import org.spongepowered.asm.lib.tree.InsnNode; +import org.spongepowered.asm.lib.tree.JumpInsnNode; import org.spongepowered.asm.lib.tree.VarInsnNode; import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; @@ -39,6 +41,12 @@ import org.spongepowered.asm.util.ASMHelper; * A bytecode injector which allows a specific */ public class ModifyConstantInjector extends RedirectInjector { + + /** + * Offset between "implicit zero" opcodes and "explicit int comparison" + * opcodes + */ + private static final int OPCODE_OFFSET = Opcodes.IF_ICMPLT - Opcodes.IFLT; /** * @param info Injection info @@ -57,30 +65,56 @@ public class ModifyConstantInjector extends RedirectInjector { throw new UnsupportedOperationException("Target failure for " + this.info); } - if (ASMHelper.isConstant(node.getCurrentTarget())) { - this.injectConstantModifier(target, node); + AbstractInsnNode targetNode = node.getCurrentTarget(); + if (targetNode instanceof JumpInsnNode) { + this.injectExpandedConstantModifier(target, (JumpInsnNode)targetNode); return; } - throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting an invalid insn in " + if (ASMHelper.isConstant(targetNode)) { + this.injectConstantModifier(target, targetNode); + return; + } + + throw new InvalidInjectionException(this.info, this.annotationType + " annotation is targetting an invalid insn in " + target + " in " + this); } - private void injectConstantModifier(Target target, InjectionNode node) { - final AbstractInsnNode constNode = node.getCurrentTarget(); + /** + * Injects a constant modifier at an implied-zero + * + * @param target target method + * @param jumpNode jump instruction (must be IFLT, IFGE, IFGT or IFLE) + */ + private void injectExpandedConstantModifier(Target target, JumpInsnNode jumpNode) { + int opcode = jumpNode.getOpcode(); + if (opcode < Opcodes.IFLT || opcode > Opcodes.IFLE) { + throw new InvalidInjectionException(this.info, this.annotationType + " annotation selected an invalid opcode " + + ASMHelper.getOpcodeName(opcode) + " in " + target + " in " + this); + } + + final InsnList insns = new InsnList(); + insns.add(new InsnNode(Opcodes.ICONST_0)); + AbstractInsnNode invoke = this.invokeConstantHandler(Type.getType("I"), target, insns, insns); + insns.add(new JumpInsnNode(opcode + ModifyConstantInjector.OPCODE_OFFSET, jumpNode.label)); + target.replaceNode(jumpNode, invoke, insns); + target.addToStack(1); + } + + private void injectConstantModifier(Target target, AbstractInsnNode constNode) { final Type constantType = ASMHelper.getConstantType(constNode); final InsnList before = new InsnList(); final InsnList after = new InsnList(); - AbstractInsnNode invoke = this.invokeConstantHandler(constNode, constantType, target, before, after); + AbstractInsnNode invoke = this.invokeConstantHandler(constantType, target, before, after); target.wrapNode(constNode, invoke, before, after); } - private AbstractInsnNode invokeConstantHandler(AbstractInsnNode targetNode, Type constantType, Target target, InsnList before, InsnList after) { + private AbstractInsnNode invokeConstantHandler(Type constantType, Target target, InsnList before, InsnList after) { final String handlerDesc = ASMHelper.generateDescriptor(constantType, constantType); final boolean withArgs = this.checkDescriptor(handlerDesc, target, "getter"); if (!this.isStatic) { - before.add(new VarInsnNode(Opcodes.ALOAD, 0)); + before.insert(new VarInsnNode(Opcodes.ALOAD, 0)); target.addToStack(1); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java index 62d8188c..eb624220 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java @@ -46,7 +46,7 @@ public class CallbackInjectionInfo extends InjectionInfo { @Override protected Injector parseInjector(AnnotationNode injectAnnotation) { - boolean cancellable = ASMHelper.getAnnotationValue(injectAnnotation, "cancellable", false); + boolean cancellable = ASMHelper.getAnnotationValue(injectAnnotation, "cancellable", Boolean.FALSE); LocalCapture locals = ASMHelper.getAnnotationValue(injectAnnotation, "locals", LocalCapture.class, LocalCapture.NO_CAPTURE); String identifier = ASMHelper.getAnnotationValue(injectAnnotation, "id", ""); 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 22d677fe..6b299272 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 @@ -173,17 +173,12 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } } - @SuppressWarnings("unchecked") protected List readInjectionPoints(String type) { - Object atValue = ASMHelper.getAnnotationValue(this.annotation, "at"); - if (atValue instanceof List) { - return (List)atValue; - } else if (atValue instanceof AnnotationNode) { - List ats = new ArrayList(); - ats.add((AnnotationNode)atValue); - return ats; + List ats = ASMHelper.getAnnotationValue(this.annotation, "at", false); + if (ats == null) { + throw new InvalidInjectionException(this, 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)"); + return ats; } protected void parseInjectionPoints(List ats) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyConstantInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyConstantInjectionInfo.java index aa9b42ef..11eeccc5 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyConstantInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyConstantInjectionInfo.java @@ -26,8 +26,10 @@ package org.spongepowered.asm.mixin.injection.struct; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.ListIterator; +import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -37,6 +39,7 @@ import org.spongepowered.asm.lib.tree.AnnotationNode; import org.spongepowered.asm.lib.tree.InsnList; import org.spongepowered.asm.lib.tree.MethodNode; import org.spongepowered.asm.mixin.injection.Constant; +import org.spongepowered.asm.mixin.injection.Constant.Condition; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.invoke.ModifyConstantInjector; @@ -45,6 +48,8 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; import org.spongepowered.asm.util.Constants; +import com.google.common.primitives.Ints; + /** * Information about a constant modifier injector */ @@ -71,55 +76,67 @@ public class ModifyConstantInjectionInfo extends InjectionInfo { private final String stringValue; private final Type typeValue; + private final int[] expandOpcodes; + private final boolean expand; + private final String matchByType; private final boolean log; public BeforeConstant(InjectionInfo info, AnnotationNode node, String returnType) { Boolean empty = ASMHelper.getAnnotationValue(node, "nullValue", (Boolean)null); - this.ordinal = ASMHelper.getAnnotationValue(node, "ordinal", Integer.valueOf(-1)); - this.nullValue = empty != null ? empty.booleanValue() : false;; - this.intValue = ASMHelper.getAnnotationValue(node, "intValue", (Integer)null); - this.floatValue = ASMHelper.getAnnotationValue(node, "floatValue", (Float)null); - this.longValue = ASMHelper.getAnnotationValue(node, "longValue", (Long)null); + this.ordinal = ASMHelper.getAnnotationValue(node, "ordinal", Integer.valueOf(-1)); + this.nullValue = empty != null ? empty.booleanValue() : false; + this.intValue = ASMHelper.getAnnotationValue(node, "intValue", (Integer)null); + this.floatValue = ASMHelper.getAnnotationValue(node, "floatValue", (Float)null); + this.longValue = ASMHelper.getAnnotationValue(node, "longValue", (Long)null); this.doubleValue = ASMHelper.getAnnotationValue(node, "doubleValue", (Double)null); this.stringValue = ASMHelper.getAnnotationValue(node, "stringValue", (String)null); - this.typeValue = ASMHelper.getAnnotationValue(node, "classValue", (Type)null); + this.typeValue = ASMHelper.getAnnotationValue(node, "classValue", (Type)null); + this.matchByType = this.validateDiscriminator(info, returnType, empty); + this.expandOpcodes = this.parseExpandOpcodes(node); + this.expand = this.expandOpcodes.length > 0; + + this.log = ASMHelper.getAnnotationValue(node, "log", Boolean.FALSE).booleanValue(); + } + + private String validateDiscriminator(InjectionInfo info, String returnType, Boolean empty) { int c = BeforeConstant.count(empty, this.intValue, this.floatValue, this.longValue, this.doubleValue, this.stringValue, this.typeValue); if (c == 1) { returnType = null; } else if (c > 1) { throw new InvalidInjectionException(info, "Conflicting constant discriminators specified on @Constant annotation for " + info); } - - this.matchByType = returnType; - - this.log = ASMHelper.getAnnotationValue(node, "log", Boolean.FALSE).booleanValue(); + return returnType; + } + + private int[] parseExpandOpcodes(AnnotationNode node) { + Set opcodes = new HashSet(); + for (Condition condition : ASMHelper.getAnnotationValue(node, "expandZeroConditions", true, Condition.class)) { + Condition actual = condition.getEquivalentCondition(); + for (int opcode : actual.getOpcodes()) { + opcodes.add(Integer.valueOf(opcode)); + } + } + return Ints.toArray(opcodes); } @Override public boolean find(String desc, InsnList insns, Collection nodes) { boolean found = false; - if (this.log) { - BeforeConstant.logger.info("BeforeConstant is searching for an constants in method with descriptor {}", desc); - } + this.log("BeforeConstant is searching for constants in method with descriptor {}", desc); ListIterator iter = insns.iterator(); for (int ordinal = 0; iter.hasNext();) { AbstractInsnNode insn = iter.next(); - boolean matchesInsn = this.matchesInsn(insn); + boolean matchesInsn = this.expand ? this.matchesConditionalInsn(insn) : this.matchesConstantInsn(insn); if (matchesInsn) { - if (this.log) { - String byType = this.matchByType != null ? " TYPE" : " value"; - BeforeConstant.logger.info(" BeforeConstant found a matching constant{} at ordinal {}", byType, ordinal); - } + this.log(" BeforeConstant found a matching constant{} at ordinal {}", this.matchByType != null ? " TYPE" : " value", ordinal); if (this.ordinal == -1 || this.ordinal == ordinal) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found {}", ASMHelper.describeNode(insn).trim()); - } + this.log(" BeforeConstant found {}", ASMHelper.describeNode(insn).trim()); nodes.add(insn); found = true; } @@ -130,52 +147,61 @@ public class ModifyConstantInjectionInfo extends InjectionInfo { return found; } - private boolean matchesInsn(AbstractInsnNode insn) { + private boolean matchesConditionalInsn(AbstractInsnNode insn) { + for (int conditionalOpcode : this.expandOpcodes) { + if (insn.getOpcode() == conditionalOpcode) { + this.log(" BeforeConstant found %s instruction", ASMHelper.getOpcodeName(conditionalOpcode)); + return true; + } + } + + if (this.intValue != null && this.intValue.intValue() == 0 && ASMHelper.isConstant(insn)) { + Object value = ASMHelper.getConstant(insn); + this.log(" BeforeConstant found INTEGER constant: value = {}", value); + return value instanceof Integer && ((Integer)value).intValue() == 0; + } + + return false; + } + + private boolean matchesConstantInsn(AbstractInsnNode insn) { if (!ASMHelper.isConstant(insn)) { return false; } Object value = ASMHelper.getConstant(insn); if (value == null) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found NULL constant: nullValue = {}", this.nullValue); - } + this.log(" BeforeConstant found NULL constant: nullValue = {}", this.nullValue); return this.nullValue || Constants.OBJECT.equals(this.matchByType); } else if (value instanceof Integer) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found INTEGER constant: value = {}, intValue = {}", value, this.intValue); - } + this.log(" BeforeConstant found INTEGER constant: value = {}, intValue = {}", value, this.intValue); return value.equals(this.intValue) || "I".equals(this.matchByType); } else if (value instanceof Float) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found FLOAT constant: value = {}, floatValue = {}", value, this.floatValue); - } + this.log(" BeforeConstant found FLOAT constant: value = {}, floatValue = {}", value, this.floatValue); return value.equals(this.floatValue) || "F".equals(this.matchByType); } else if (value instanceof Long) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found LONG constant: value = {}, longValue = {}", value, this.longValue); - } + this.log(" BeforeConstant found LONG constant: value = {}, longValue = {}", value, this.longValue); return value.equals(this.longValue) || "J".equals(this.matchByType); } else if (value instanceof Double) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found DOUBLE constant: value = {}, doubleValue = {}", value, this.doubleValue); - } + this.log(" BeforeConstant found DOUBLE constant: value = {}, doubleValue = {}", value, this.doubleValue); return value.equals(this.doubleValue) || "D".equals(this.matchByType); } else if (value instanceof String) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found STRING constant: value = {}, stringValue = {}", value, this.stringValue); - } + this.log(" BeforeConstant found STRING constant: value = {}, stringValue = {}", value, this.stringValue); return value.equals(this.stringValue) || Constants.STRING.equals(this.matchByType); } else if (value instanceof Type) { - if (this.log) { - BeforeConstant.logger.info(" BeforeConstant found CLASS constant: value = {}, typeValue = {}", value, this.typeValue); - } + this.log(" BeforeConstant found CLASS constant: value = {}, typeValue = {}", value, this.typeValue); return value.equals(this.typeValue) || Constants.CLASS.equals(this.matchByType); } return false; } + protected void log(String message, Object... params) { + if (this.log) { + BeforeConstant.logger.info(message, params); + } + } + private static int count(Object... values) { int counter = 0; for (Object value : values) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java index 84a7b588..2f5e6471 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -533,12 +533,12 @@ class MixinApplicatorStandard { } AnnotationNode renamed = ASMHelper.getVisibleAnnotation(method, MixinRenamed.class); - if (renamed == null || !ASMHelper.getAnnotationValue(renamed, "isInterfaceMember", false)) { + if (renamed == null || !ASMHelper.getAnnotationValue(renamed, "isInterfaceMember", Boolean.FALSE)) { throw new InvalidMixinException(mixin, "@Intrinsic method must be prefixed interface method, no rename encountered on " + methodName + " in " + mixin); } - if (!ASMHelper.getAnnotationValue(intrinsic, "displace", false)) { + if (!ASMHelper.getAnnotationValue(intrinsic, "displace", Boolean.FALSE)) { this.logger.log(mixin.getLoggingLevel(), "Skipping Intrinsic mixin method {} for {}", methodName, mixin.getTargetClassRef()); return true; } diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index 6aa3643f..f5b57091 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -31,9 +31,11 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Map; import org.spongepowered.asm.lib.ClassReader; @@ -657,23 +659,75 @@ public final class ASMHelper { * defaultValue if the annotation value is not set * * @param duck type - * @param annotationNode Annotation node to query + * @param annotation Annotation node to query * @param key Key to search for * @param enumClass Class of enum containing the enum constant to search for * @param defaultValue Value to return if the specified key isn't found * @return duck-typed annotation value or defaultValue if missing */ - public static > T getAnnotationValue(AnnotationNode annotationNode, String key, Class enumClass, T defaultValue) { - String[] value = ASMHelper.getAnnotationValue(annotationNode, key); + public static > T getAnnotationValue(AnnotationNode annotation, String key, Class enumClass, T defaultValue) { + String[] value = ASMHelper.getAnnotationValue(annotation, key); if (value == null) { return defaultValue; } + return ASMHelper.toEnumValue(enumClass, value); + } + + /** + * Return the specified annotation node value as a list of nodes + * + * @param list element type + * @param annotation Annotation node to query + * @param key Key to search for + * @param notNull if true, return an empty list instead of null if the + * annotation value is absent + */ + @SuppressWarnings("unchecked") + public static List getAnnotationValue(AnnotationNode annotation, String key, boolean notNull) { + Object value = ASMHelper.getAnnotationValue(annotation, key); + if (value instanceof List) { + return (List)value; + } else if (value != null) { + List list = new ArrayList(); + list.add((T)value); + return list; + } + return Collections.emptyList(); + } + + /** + * Return the specified annotation node value as a list of enums + * + * @param list element type + * @param annotation Annotation node to query + * @param key Key to search for + * @param notNull if true, return an empty list instead of null if the + * annotation value is absent + * @param enumClass Class of enum containing the enum constant to use + */ + @SuppressWarnings("unchecked") + public static > List getAnnotationValue(AnnotationNode annotation, String key, boolean notNull, Class enumClass) { + Object value = ASMHelper.getAnnotationValue(annotation, key); + if (value instanceof List) { + for (ListIterator iter = ((List)value).listIterator(); iter.hasNext();) { + iter.set(ASMHelper.toEnumValue(enumClass, (String[])iter.next())); + } + return (List)value; + } else if (value instanceof String[]) { + List list = new ArrayList(); + list.add(ASMHelper.toEnumValue(enumClass, (String[])value)); + return list; + } + return Collections.emptyList(); + } + + private static > T toEnumValue(Class enumClass, String[] value) { if (!enumClass.getName().equals(Type.getType(value[0]).getClassName())) { throw new IllegalArgumentException("The supplied enum class does not match the stored enum value"); } return Enum.valueOf(enumClass, value[1]); } - + /** * Returns true if the supplied method node is static *