diff --git a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java index 4af81e80..a405d5cc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/gen/AccessorInfo.java @@ -42,6 +42,7 @@ import org.spongepowered.asm.mixin.refmap.IMixinContext; import org.spongepowered.asm.mixin.struct.SpecialMethodInfo; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; @@ -165,7 +166,7 @@ public class AccessorInfo extends SpecialMethodInfo { } protected AccessorInfo(MixinTargetContext mixin, MethodNode method, Class annotationClass) { - super(mixin, method, ASMHelper.getVisibleAnnotation(method, annotationClass)); + super(mixin, method, Annotations.getVisible(method, annotationClass)); this.argTypes = Type.getArgumentTypes(method.desc); this.returnType = Type.getReturnType(method.desc); this.type = this.initType(); @@ -206,7 +207,7 @@ public class AccessorInfo extends SpecialMethodInfo { } protected String getTargetName() { - String name = ASMHelper.getAnnotationValue(this.annotation); + String name = Annotations.getValue(this.annotation); if (Strings.isNullOrEmpty(name)) { String inflectedTarget = this.inflectTarget(); if (inflectedTarget == null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index af6de2e8..4d29fae8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -57,7 +57,7 @@ import org.spongepowered.asm.mixin.injection.points.MethodHead; import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.mixin.refmap.IMixinContext; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -505,14 +505,14 @@ public abstract class InjectionPoint { * failed */ public static InjectionPoint parse(IMixinContext context, MethodNode method, AnnotationNode parent, AnnotationNode node) { - String at = ASMHelper.getAnnotationValue(node, "value"); - List args = ASMHelper.>getAnnotationValue(node, "args"); - String target = ASMHelper.getAnnotationValue(node, "target", ""); - String slice = ASMHelper.getAnnotationValue(node, "slice", ""); - At.Shift shift = ASMHelper.getAnnotationValue(node, "shift", At.Shift.class, At.Shift.NONE); - int by = ASMHelper.getAnnotationValue(node, "by", Integer.valueOf(0)); - int ordinal = ASMHelper.getAnnotationValue(node, "ordinal", Integer.valueOf(-1)); - int opcode = ASMHelper.getAnnotationValue(node, "opcode", Integer.valueOf(0)); + String at = Annotations.getValue(node, "value"); + List args = Annotations.>getValue(node, "args"); + String target = Annotations.getValue(node, "target", ""); + String slice = Annotations.getValue(node, "slice", ""); + At.Shift shift = Annotations.getValue(node, "shift", At.Shift.class, At.Shift.NONE); + int by = Annotations.getValue(node, "by", Integer.valueOf(0)); + int ordinal = Annotations.getValue(node, "ordinal", Integer.valueOf(-1)); + int opcode = Annotations.getValue(node, "opcode", Integer.valueOf(0)); if (args == null) { args = ImmutableList.of(); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java index e0442e2c..d6b695ba 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java @@ -35,7 +35,7 @@ import org.spongepowered.asm.lib.tree.AnnotationNode; import org.spongepowered.asm.lib.tree.MethodNode; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.throwables.InjectionValidationException; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; /** * Information store for injector groups @@ -81,7 +81,7 @@ public class InjectorGroupInfo { * @return Group or NO_GROUP if no group */ public InjectorGroupInfo parseGroup(MethodNode method, String defaultGroup) { - return this.parseGroup(ASMHelper.getInvisibleAnnotation(method, Group.class), defaultGroup); + return this.parseGroup(Annotations.getInvisible(method, Group.class), defaultGroup); } /** @@ -97,18 +97,18 @@ public class InjectorGroupInfo { return InjectorGroupInfo.Map.NO_GROUP; } - String name = ASMHelper.getAnnotationValue(annotation, "name"); + String name = Annotations.getValue(annotation, "name"); if (name == null || name.isEmpty()) { name = defaultGroup; } InjectorGroupInfo groupInfo = this.forName(name); - Integer min = ASMHelper.getAnnotationValue(annotation, "min"); + Integer min = Annotations.getValue(annotation, "min"); if (min != null && min.intValue() != -1) { groupInfo.setMinRequired(min.intValue()); } - Integer max = ASMHelper.getAnnotationValue(annotation, "max"); + Integer max = Annotations.getValue(annotation, "max"); if (max != null && max.intValue() != -1) { groupInfo.setMaxAllowed(max.intValue()); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index fdd36271..dbf8d628 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -41,6 +41,7 @@ import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.throwables.InjectionError; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.Locals; import org.spongepowered.asm.util.PrettyPrinter; @@ -256,7 +257,7 @@ public class CallbackInjector extends Injector { return false; // Reference types must match exactly } - AnnotationNode coerce = ASMHelper.getInvisibleParameterAnnotation(this.handler, Coerce.class, arg); + AnnotationNode coerce = Annotations.getInvisibleParameter(this.handler, Coerce.class, arg); if (coerce == null) { return false; // No @Coerce specified, types must match } @@ -376,7 +377,7 @@ public class CallbackInjector extends Injector { // if changes to the local variable table are EXPECTED for some // reason. MethodNode surrogateHandler = ASMHelper.findMethod(this.classNode, this.methodNode.name, callback.getDescriptor()); - if (surrogateHandler != null && ASMHelper.getVisibleAnnotation(surrogateHandler, Surrogate.class) != null) { + if (surrogateHandler != null && Annotations.getVisible(surrogateHandler, Surrogate.class) != null) { // Found a matching method, use it callbackMethod = surrogateHandler; } else { @@ -409,7 +410,7 @@ public class CallbackInjector extends Injector { } MethodNode surrogateHandler = ASMHelper.findMethod(this.classNode, this.methodNode.name, callback.getDescriptor()); - if (surrogateHandler != null && ASMHelper.getVisibleAnnotation(surrogateHandler, Surrogate.class) != null) { + if (surrogateHandler != null && Annotations.getVisible(surrogateHandler, Surrogate.class) != null) { // Found a matching surrogate method, use it callbackMethod = surrogateHandler; } else { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java index 369a0e0d..d0edcefc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/MethodSlice.java @@ -40,6 +40,7 @@ import org.spongepowered.asm.mixin.injection.Slice; import org.spongepowered.asm.mixin.injection.throwables.InjectionError; import org.spongepowered.asm.mixin.injection.throwables.InvalidSliceException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import com.google.common.base.Strings; @@ -454,10 +455,10 @@ public final class MethodSlice { * @return parsed MethodSlice */ public static MethodSlice parse(ISliceContext info, AnnotationNode node) { - String id = ASMHelper.getAnnotationValue(node, "id"); + String id = Annotations.getValue(node, "id"); - AnnotationNode from = ASMHelper.getAnnotationValue(node, "from"); - AnnotationNode to = ASMHelper.getAnnotationValue(node, "to"); + AnnotationNode from = Annotations.getValue(node, "from"); + AnnotationNode to = Annotations.getValue(node, "to"); InjectionPoint fromPoint = from != null ? InjectionPoint.parse(info, from) : null; InjectionPoint toPoint = to != null ? InjectionPoint.parse(info, to) : null; 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 17a0107d..a8f3792a 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 @@ -30,7 +30,7 @@ import java.util.Map; import org.spongepowered.asm.lib.tree.AnnotationNode; import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.throwables.InvalidSliceException; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; /** * Represents a collection of {@link MethodSlice}s, mapped by ID. Stored ids may @@ -101,7 +101,7 @@ public final class MethodSlices { AnnotationNode annotation = info.getAnnotation(); if (annotation != null) { - for (AnnotationNode node : ASMHelper.getAnnotationValue(annotation, "slice", true)) { + for (AnnotationNode node : Annotations.getValue(annotation, "slice", true)) { MethodSlice slice = MethodSlice.parse(info, node); slices.add(slice); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java index a06316da..ca586a7f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java @@ -52,6 +52,7 @@ import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Constants; import com.google.common.base.Joiner; @@ -152,7 +153,7 @@ public class RedirectInjector extends InvokeInjector { super(info, annotationType); int priority = info.getContext().getPriority(); - boolean isFinal = ASMHelper.getVisibleAnnotation(this.methodNode, Final.class) != null; + boolean isFinal = Annotations.getVisible(this.methodNode, Final.class) != null; this.meta = new Meta(priority, isFinal, this.info.toString(), this.methodNode.desc); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java index 5445a9f2..3a11669c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java @@ -38,6 +38,7 @@ import org.spongepowered.asm.lib.tree.LocalVariableNode; import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator.Context.Local; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Locals; import org.spongepowered.asm.util.PrettyPrinter; import org.spongepowered.asm.util.SignaturePrinter; @@ -368,12 +369,12 @@ public class LocalVariableDiscriminator { * @return discriminator configured using values from the annoation */ public static LocalVariableDiscriminator parse(AnnotationNode annotation) { - boolean argsOnly = ASMHelper.getAnnotationValue(annotation, "argsOnly", Boolean.FALSE).booleanValue(); - int ordinal = ASMHelper.getAnnotationValue(annotation, "ordinal", -1); - int index = ASMHelper.getAnnotationValue(annotation, "index", -1); + boolean argsOnly = Annotations.getValue(annotation, "argsOnly", Boolean.FALSE).booleanValue(); + int ordinal = Annotations.getValue(annotation, "ordinal", -1); + int index = Annotations.getValue(annotation, "index", -1); Set names = new HashSet(); - List namesList = ASMHelper.>getAnnotationValue(annotation, "name", (List)null); + List namesList = Annotations.>getValue(annotation, "name", (List)null); if (namesList != null) { names.addAll(namesList); } 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 eb624220..85168466 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 @@ -31,7 +31,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInjector; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import com.google.common.base.Strings; @@ -46,9 +46,9 @@ public class CallbackInjectionInfo extends InjectionInfo { @Override protected Injector parseInjector(AnnotationNode injectAnnotation) { - 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", ""); + boolean cancellable = Annotations.getValue(injectAnnotation, "cancellable", Boolean.FALSE); + LocalCapture locals = Annotations.getValue(injectAnnotation, "locals", LocalCapture.class, LocalCapture.NO_CAPTURE); + String identifier = Annotations.getValue(injectAnnotation, "id", ""); return new CallbackInjector(this, cancellable, locals, identifier); } 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 6b299272..8b755a15 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 @@ -61,6 +61,7 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; /** * Contructs information about an injection from an {@link Inject} annotation @@ -155,7 +156,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } protected MemberInfo parseTarget(String type) { - String method = ASMHelper.getAnnotationValue(this.annotation, "method"); + String method = Annotations.getValue(this.annotation, "method"); if (method == null) { throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing method name"); } @@ -174,7 +175,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } protected List readInjectionPoints(String type) { - List ats = ASMHelper.getAnnotationValue(this.annotation, "at", false); + List ats = Annotations.getValue(this.annotation, "at", false); if (ats == null) { throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing 'at' value(s)"); } @@ -188,12 +189,12 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC protected void parseRequirements() { this.group = this.mixin.getInjectorGroups().parseGroup(this.method, this.mixin.getDefaultInjectorGroup()).add(this); - Integer expect = ASMHelper.getAnnotationValue(this.annotation, "expect"); + Integer expect = Annotations.getValue(this.annotation, "expect"); if (expect != null) { this.expectedCallbackCount = expect.intValue(); } - Integer require = ASMHelper.getAnnotationValue(this.annotation, "require"); + Integer require = Annotations.getValue(this.annotation, "require"); if (require != null && require.intValue() > -1) { this.requiredCallbackCount = require.intValue(); } else if (this.group.isDefault()) { @@ -350,7 +351,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC for (MethodNode target : this.classNode.methods) { if (searchFor.matches(target.name, target.desc, ordinal)) { - boolean isMixinMethod = ASMHelper.getVisibleAnnotation(target, MixinMerged.class) != null; + boolean isMixinMethod = Annotations.getVisible(target, MixinMerged.class) != null; if (searchFor.matchAll && (ASMHelper.methodIsStatic(target) != this.isStatic || target == this.method || isMixinMethod)) { continue; } @@ -368,20 +369,20 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } private void checkTarget(MethodNode target) { - AnnotationNode merged = ASMHelper.getVisibleAnnotation(target, MixinMerged.class); + AnnotationNode merged = Annotations.getVisible(target, MixinMerged.class); if (merged == null) { return; } - String owner = ASMHelper.getAnnotationValue(merged, "mixin"); - int priority = ASMHelper.getAnnotationValue(merged, "priority"); + String owner = Annotations.getValue(merged, "mixin"); + int priority = Annotations.getValue(merged, "priority"); if (priority >= this.mixin.getPriority() && !owner.equals(this.mixin.getClassName())) { throw new InvalidInjectionException(this, this + " cannot inject into " + this.classNode.name + "::" + target.name + target.desc + " merged by " + owner + " with priority " + priority); } - if (ASMHelper.getVisibleAnnotation(target, Final.class) != null) { + if (Annotations.getVisible(target, Final.class) != null) { throw new InvalidInjectionException(this, this + " cannot inject into @Final method " + this.classNode.name + "::" + target.name + target.desc + " merged by " + owner); } @@ -431,7 +432,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC public static AnnotationNode getInjectorAnnotation(IMixinInfo mixin, MethodNode method) { AnnotationNode annotation = null; try { - annotation = ASMHelper.getSingleVisibleAnnotation(method, + annotation = Annotations.getSingleVisible(method, Inject.class, ModifyArg.class, Redirect.class, diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java index 681f5466..a7fc753e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyArgInjectionInfo.java @@ -30,7 +30,7 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.invoke.ModifyArgInjector; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; /** * Information about a {@link ModifyArg} injector @@ -43,7 +43,7 @@ public class ModifyArgInjectionInfo extends InjectionInfo { @Override protected Injector parseInjector(AnnotationNode injectAnnotation) { - int index = ASMHelper.getAnnotationValue(injectAnnotation, "index", -1); + int index = Annotations.getValue(injectAnnotation, "index", -1); return new ModifyArgInjector(this, index); } 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 11eeccc5..7b5ab095 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 @@ -46,6 +46,7 @@ import org.spongepowered.asm.mixin.injection.invoke.ModifyConstantInjector; import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Constants; import com.google.common.primitives.Ints; @@ -84,21 +85,21 @@ public class ModifyConstantInjectionInfo extends InjectionInfo { 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)); + Boolean empty = Annotations.getValue(node, "nullValue", (Boolean)null); + this.ordinal = Annotations.getValue(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.intValue = Annotations.getValue(node, "intValue", (Integer)null); + this.floatValue = Annotations.getValue(node, "floatValue", (Float)null); + this.longValue = Annotations.getValue(node, "longValue", (Long)null); + this.doubleValue = Annotations.getValue(node, "doubleValue", (Double)null); + this.stringValue = Annotations.getValue(node, "stringValue", (String)null); + this.typeValue = Annotations.getValue(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(); + this.log = Annotations.getValue(node, "log", Boolean.FALSE).booleanValue(); } private String validateDiscriminator(InjectionInfo info, String returnType, Boolean empty) { @@ -113,7 +114,7 @@ public class ModifyConstantInjectionInfo extends InjectionInfo { private int[] parseExpandOpcodes(AnnotationNode node) { Set opcodes = new HashSet(); - for (Condition condition : ASMHelper.getAnnotationValue(node, "expandZeroConditions", true, Condition.class)) { + for (Condition condition : Annotations.getValue(node, "expandZeroConditions", true, Condition.class)) { Condition actual = condition.getEquivalentCondition(); for (int opcode : actual.getOpcodes()) { opcodes.add(Integer.valueOf(opcode)); @@ -220,7 +221,7 @@ public class ModifyConstantInjectionInfo extends InjectionInfo { @Override protected List readInjectionPoints(String type) { - AnnotationNode constantAnnotation = ASMHelper.getAnnotationValue(this.annotation, "constant"); + AnnotationNode constantAnnotation = Annotations.getValue(this.annotation, "constant"); List ats = new ArrayList(); ats.add(constantAnnotation); return ats; diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyVariableInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyVariableInjectionInfo.java index 0c254f82..7699c288 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyVariableInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ModifyVariableInjectionInfo.java @@ -31,7 +31,7 @@ import org.spongepowered.asm.mixin.injection.code.Injector; import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator; import org.spongepowered.asm.mixin.injection.modify.ModifyVariableInjector; import org.spongepowered.asm.mixin.transformer.MixinTargetContext; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; /** * Information about a {@link ModifyVariable} injector @@ -44,7 +44,7 @@ public class ModifyVariableInjectionInfo extends InjectionInfo { @Override protected Injector parseInjector(AnnotationNode injectAnnotation) { - boolean print = ASMHelper.getAnnotationValue(injectAnnotation, "print", Boolean.FALSE).booleanValue(); + boolean print = Annotations.getValue(injectAnnotation, "print", Boolean.FALSE).booleanValue(); return new ModifyVariableInjector(this, print, LocalVariableDiscriminator.parse(injectAnnotation)); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index 22e48188..29617fd6 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -52,7 +52,7 @@ import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.mixin.transformer.ClassInfo.Member.Type; import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.ClassSignature; import com.google.common.collect.ImmutableList; @@ -411,16 +411,16 @@ public final class ClassInfo extends TreeInfo { @SuppressWarnings("unchecked") public Method(MethodNode method) { this(method, false); - this.setUnique(ASMHelper.getVisibleAnnotation(method, Unique.class) != null); - this.isAccessor = ASMHelper.getSingleVisibleAnnotation(method, Accessor.class, Invoker.class) != null; + this.setUnique(Annotations.getVisible(method, Unique.class) != null); + this.isAccessor = Annotations.getSingleVisible(method, Accessor.class, Invoker.class) != null; } @SuppressWarnings("unchecked") public Method(MethodNode method, boolean injected) { super(Type.METHOD, method.name, method.desc, method.access, injected); this.frames = this.gatherFrames(method); - this.setUnique(ASMHelper.getVisibleAnnotation(method, Unique.class) != null); - this.isAccessor = ASMHelper.getSingleVisibleAnnotation(method, Accessor.class, Invoker.class) != null; + this.setUnique(Annotations.getVisible(method, Unique.class) != null); + this.isAccessor = Annotations.getSingleVisible(method, Accessor.class, Invoker.class) != null; } public Method(String name, String desc) { @@ -514,11 +514,11 @@ public final class ClassInfo extends TreeInfo { public Field(FieldNode field, boolean injected) { super(Type.FIELD, field.name, field.desc, field.access, injected); - this.setUnique(ASMHelper.getVisibleAnnotation(field, Unique.class) != null); + this.setUnique(Annotations.getVisible(field, Unique.class) != null); - if (ASMHelper.getVisibleAnnotation(field, Shadow.class) != null) { - boolean decoratedFinal = ASMHelper.getVisibleAnnotation(field, Final.class) != null; - boolean decoratedMutable = ASMHelper.getVisibleAnnotation(field, Mutable.class) != null; + if (Annotations.getVisible(field, Shadow.class) != null) { + boolean decoratedFinal = Annotations.getVisible(field, Final.class) != null; + boolean decoratedMutable = Annotations.getVisible(field, Mutable.class) != null; this.setDecoratedFinal(decoratedFinal, decoratedMutable); } } 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 b53a8e81..8bcd7c7f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/InterfaceInfo.java @@ -36,7 +36,7 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; /** * Information about an interface being runtime-patched onto a mixin target @@ -192,7 +192,7 @@ public final class InterfaceInfo extends TreeInfo { throw new InvalidMixinException(this.mixin, String.format("%s cannot implement %s because it is not visible", realName, this.getName())); } - ASMHelper.setVisibleAnnotation(method, MixinRenamed.class, "originalName", method.name, "isInterfaceMember", true); + Annotations.setVisible(method, MixinRenamed.class, "originalName", method.name, "isInterfaceMember", true); this.decorateUniqueMethod(method); method.name = realName; return true; @@ -209,8 +209,8 @@ public final class InterfaceInfo extends TreeInfo { return; } - if (ASMHelper.getVisibleAnnotation(method, Unique.class) == null) { - ASMHelper.setVisibleAnnotation(method, Unique.class); + if (Annotations.getVisible(method, Unique.class) == null) { + Annotations.setVisible(method, Unique.class); this.mixin.getClassInfo().findMethod(method).setUnique(true); } } @@ -224,9 +224,9 @@ public final class InterfaceInfo extends TreeInfo { * @return parsed InterfaceInfo object */ static InterfaceInfo fromAnnotation(MixinInfo mixin, AnnotationNode node) { - String prefix = ASMHelper.getAnnotationValue(node, "prefix"); - Type iface = ASMHelper.getAnnotationValue(node, "iface"); - Boolean unique = ASMHelper.getAnnotationValue(node, "unique"); + String prefix = Annotations.getValue(node, "prefix"); + Type iface = Annotations.getValue(node, "iface"); + Boolean unique = Annotations.getValue(node, "unique"); if (prefix == null || iface == null) { throw new InvalidMixinException(mixin, String.format("@Interface annotation on %s is missing a required parameter", mixin)); 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 2f5e6471..ac45077f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -59,6 +59,7 @@ import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.ConstraintParser; import org.spongepowered.asm.util.ConstraintParser.Constraint; @@ -424,7 +425,7 @@ class MixinApplicatorStandard { * @param method Method to merge */ protected void mergeMethod(MixinTargetContext mixin, MethodNode method) { - boolean isOverwrite = ASMHelper.getVisibleAnnotation(method, Overwrite.class) != null; + boolean isOverwrite = Annotations.getVisible(method, Overwrite.class) != null; MethodNode target = this.findTargetMethod(method); if (target != null) { @@ -432,7 +433,7 @@ class MixinApplicatorStandard { return; } - AnnotationNode intrinsic = ASMHelper.getInvisibleAnnotation(method, Intrinsic.class); + AnnotationNode intrinsic = Annotations.getInvisible(method, Intrinsic.class); if (intrinsic != null) { if (this.mergeIntrinsic(mixin, method, isOverwrite, target, intrinsic)) { return; @@ -467,16 +468,16 @@ class MixinApplicatorStandard { * @return true if the target was already merged and should be skipped */ protected boolean isAlreadyMerged(MixinTargetContext mixin, MethodNode method, boolean isOverwrite, MethodNode target) { - AnnotationNode merged = ASMHelper.getVisibleAnnotation(target, MixinMerged.class); + AnnotationNode merged = Annotations.getVisible(target, MixinMerged.class); if (merged == null) { - if (ASMHelper.getVisibleAnnotation(target, Final.class) != null) { + if (Annotations.getVisible(target, Final.class) != null) { this.logger.warn("Overwrite prohibited for @Final method {} in {}. Skipping method.", method.name, mixin); return true; } return false; } - String sessionId = ASMHelper.getAnnotationValue(merged, "sessionId"); + String sessionId = Annotations.getValue(merged, "sessionId"); if (!this.context.getSessionId().equals(sessionId)) { throw new ClassFormatError("Invalid @MixinMerged annotation found in" + mixin + " at " + method.name + " in " + this.targetClass.name); @@ -490,15 +491,15 @@ class MixinApplicatorStandard { return true; } - String owner = ASMHelper.getAnnotationValue(merged, "mixin"); - int priority = ASMHelper.getAnnotationValue(merged, "priority"); + String owner = Annotations.getValue(merged, "mixin"); + int priority = Annotations.getValue(merged, "priority"); if (priority >= mixin.getPriority() && !owner.equals(mixin.getClassName())) { this.logger.warn("Method overwrite conflict for {} in {}, previously written by {}. Skipping method.", method.name, mixin, owner); return true; } - if (ASMHelper.getVisibleAnnotation(target, Final.class) != null) { + if (Annotations.getVisible(target, Final.class) != null) { this.logger.warn("Method overwrite conflict for @Final method {} in {} declared by {}. Skipping method.", method.name, mixin, owner); return true; } @@ -532,13 +533,13 @@ class MixinApplicatorStandard { 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", Boolean.FALSE)) { + AnnotationNode renamed = Annotations.getVisible(method, MixinRenamed.class); + if (renamed == null || !Annotations.getValue(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", Boolean.FALSE)) { + if (!Annotations.getValue(intrinsic, "displace", Boolean.FALSE)) { this.logger.log(mixin.getLoggingLevel(), "Skipping Intrinsic mixin method {} for {}", methodName, mixin.getTargetClassRef()); return true; } @@ -912,7 +913,7 @@ class MixinApplicatorStandard { 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)) { + && !(Annotations.getVisible(mixinMethod, Overwrite.class) != null)) { throw new InvalidMixinException(mixin, String.format("Mixin %s contains non-private static method %s", mixin, mixinMethod)); } @@ -930,7 +931,7 @@ class MixinApplicatorStandard { */ protected void checkMethodConstraints(MixinTargetContext mixin, MethodNode method) { for (Class annotationType : MixinApplicatorStandard.CONSTRAINED_ANNOTATIONS) { - AnnotationNode annotation = ASMHelper.getVisibleAnnotation(method, annotationType); + AnnotationNode annotation = Annotations.getVisible(method, annotationType); if (annotation != null) { this.checkConstraints(mixin, method, annotation); } 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 87d687fc..88b333ca 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -63,7 +63,7 @@ import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.mixin.transformer.throwables.MixinReloadException; import org.spongepowered.asm.mixin.transformer.throwables.MixinTargetAlreadyLoadedException; -import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.launchwrapper.LaunchClassLoaderUtil; import com.google.common.base.Function; @@ -107,7 +107,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } public AnnotationNode getVisibleAnnotation(Class annotationClass) { - return ASMHelper.getVisibleAnnotation(this, annotationClass); + return Annotations.getVisible(this, annotationClass); } public AnnotationNode getInjectorAnnotation() { @@ -276,7 +276,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { type.validate(this, targetClasses); this.detachedSuper = type.isDetachedSuper(); - this.unique = ASMHelper.getVisibleAnnotation(this.getClassNode(), Unique.class) != null; + this.unique = Annotations.getVisible(this.getClassNode(), Unique.class) != null; // Pre-flight checks this.validateInner(); @@ -318,12 +318,12 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { // Can't have remappable fields or methods on a multi-target mixin, because after obfuscation the fields will remap to conflicting names if (targetClasses.size() > 1) { for (FieldNode field : this.classNode.fields) { - this.validateRemappable(Shadow.class, field.name, ASMHelper.getVisibleAnnotation(field, Shadow.class)); + this.validateRemappable(Shadow.class, field.name, Annotations.getVisible(field, Shadow.class)); } for (MethodNode method : this.classNode.methods) { - this.validateRemappable(Shadow.class, method.name, ASMHelper.getVisibleAnnotation(method, Shadow.class)); - AnnotationNode overwrite = ASMHelper.getVisibleAnnotation(method, Overwrite.class); + this.validateRemappable(Shadow.class, method.name, Annotations.getVisible(method, Shadow.class)); + AnnotationNode overwrite = Annotations.getVisible(method, Overwrite.class); if (overwrite != null && ((method.access & Opcodes.ACC_STATIC) == 0 || (method.access & Opcodes.ACC_PUBLIC) == 0)) { throw new InvalidMixinException(MixinInfo.this, "Found @Overwrite annotation on " + method.name + " in " + this); } @@ -332,7 +332,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { } private void validateRemappable(Class annotationClass, String name, AnnotationNode annotation) { - if (annotation != null && ASMHelper.getAnnotationValue(annotation, "remap", Boolean.TRUE)) { + if (annotation != null && Annotations.getValue(annotation, "remap", Boolean.TRUE)) { throw new InvalidMixinException(MixinInfo.this, "Found a remappable @" + annotationClass.getSimpleName() + " annotation on " + name + " in " + this); } @@ -345,12 +345,12 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { this.interfaces.addAll(this.classNode.interfaces); this.interfaces.addAll(type.getInterfaces()); - AnnotationNode implementsAnnotation = ASMHelper.getInvisibleAnnotation(this.classNode, Implements.class); + AnnotationNode implementsAnnotation = Annotations.getInvisible(this.classNode, Implements.class); if (implementsAnnotation == null) { return; } - List interfaces = ASMHelper.getAnnotationValue(implementsAnnotation); + List interfaces = Annotations.getValue(implementsAnnotation); if (interfaces == null) { return; } @@ -817,14 +817,14 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { return Collections.emptyList(); } - AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); + AnnotationNode mixin = Annotations.getInvisible(classNode, Mixin.class); if (mixin == null) { throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } List targets = new ArrayList(); - List publicTargets = ASMHelper.getAnnotationValue(mixin, "value"); - List privateTargets = ASMHelper.getAnnotationValue(mixin, "targets"); + List publicTargets = Annotations.getValue(mixin, "value"); + List privateTargets = Annotations.getValue(mixin, "targets"); if (publicTargets != null) { this.readTargets(targets, Lists.transform(publicTargets, new Function() { @@ -907,17 +907,17 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { return this.parent.getDefaultMixinPriority(); } - AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); + AnnotationNode mixin = Annotations.getInvisible(classNode, Mixin.class); if (mixin == null) { throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } - Integer priority = ASMHelper.getAnnotationValue(mixin, "priority"); + Integer priority = Annotations.getValue(mixin, "priority"); return priority == null ? this.parent.getDefaultMixinPriority() : priority.intValue(); } protected boolean readPseudo(ClassNode classNode) { - return ASMHelper.getInvisibleAnnotation(classNode, Pseudo.class) != null; + return Annotations.getInvisible(classNode, Pseudo.class) != null; } private boolean isReloading() { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java index d0aa05a5..64d07a92 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorStandard.java @@ -52,6 +52,7 @@ import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinMethodNode; import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.Constants; /** @@ -134,14 +135,14 @@ class MixinPreProcessorStandard { } protected void prepareShadow(MixinMethodNode mixinMethod, Method method) { - AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); + AnnotationNode shadowAnnotation = Annotations.getVisible(mixinMethod, Shadow.class); if (shadowAnnotation == null) { return; } - String prefix = ASMHelper.getAnnotationValue(shadowAnnotation, "prefix", Shadow.class); + String prefix = Annotations.getValue(shadowAnnotation, "prefix", Shadow.class); if (mixinMethod.name.startsWith(prefix)) { - ASMHelper.setVisibleAnnotation(mixinMethod, MixinRenamed.class, "originalName", mixinMethod.name); + Annotations.setVisible(mixinMethod, MixinRenamed.class, "originalName", mixinMethod.name); String newName = mixinMethod.name.substring(prefix.length()); mixinMethod.name = method.renameTo(newName); } @@ -405,7 +406,7 @@ class MixinPreProcessorStandard { protected void attachFields(MixinTargetContext context) { for (Iterator iter = this.classNode.fields.iterator(); iter.hasNext();) { FieldNode mixinField = iter.next(); - AnnotationNode shadow = ASMHelper.getVisibleAnnotation(mixinField, Shadow.class); + AnnotationNode shadow = Annotations.getVisible(mixinField, Shadow.class); boolean isShadow = shadow != null; if (!this.validateField(context, mixinField, shadow)) { @@ -495,7 +496,7 @@ class MixinPreProcessorStandard { } // Shadow fields can't have prefixes, it's meaningless for them anyway - String prefix = ASMHelper.getAnnotationValue(shadow, "prefix", Shadow.class); + String prefix = Annotations.getValue(shadow, "prefix", Shadow.class); if (field.name.startsWith(prefix)) { throw new InvalidMixinException(context, String.format("@Shadow field %s.%s has a shadow prefix. This is not allowed.", context, field.name)); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index b7e514fd..6a0e9812 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -66,6 +66,7 @@ import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError; import org.spongepowered.asm.obfuscation.RemapperChain; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.ClassSignature; import org.spongepowered.asm.util.Constants; @@ -217,7 +218,7 @@ public class MixinTargetContext implements IMixinContext { this.mergedMethods.add(method); this.targetClassInfo.addMethod(method); - ASMHelper.setVisibleAnnotation(method, MixinMerged.class, + Annotations.setVisible(method, MixinMerged.class, "mixin", this.getClassName(), "priority", this.getPriority(), "sessionId", this.sessionId); @@ -431,7 +432,7 @@ public class MixinTargetContext implements IMixinContext { */ private void validateMethod(MethodNode method) { // Any method tagged with @SoftOverride must have an implementation visible from - if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) != null) { + if (Annotations.getInvisible(method, SoftOverride.class) != null) { ClassInfo.Method superMethod = this.targetClassInfo.findMethodInHierarchy(method.name, method.desc, SearchType.SUPER_CLASSES_ONLY, Traversal.SUPER); if (superMethod == null || !superMethod.isInjected()) { @@ -661,7 +662,7 @@ public class MixinTargetContext implements IMixinContext { + " is private or static"); } - if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) == null) { + if (Annotations.getInvisible(method, SoftOverride.class) == null) { throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name + method.desc + " is not decorated with @SoftOverride"); } @@ -839,7 +840,7 @@ public class MixinTargetContext implements IMixinContext { Deque aliases = new LinkedList(); aliases.add(method.name); if (annotation != null) { - List aka = ASMHelper.>getAnnotationValue(annotation, "aliases"); + List aka = Annotations.>getValue(annotation, "aliases"); if (aka != null) { aliases.addAll(aka); } @@ -865,7 +866,7 @@ public class MixinTargetContext implements IMixinContext { Deque aliases = new LinkedList(); aliases.add(field.name); if (shadow != null) { - List aka = ASMHelper.>getAnnotationValue(shadow, "aliases"); + List aka = Annotations.>getValue(shadow, "aliases"); if (aka != null) { aliases.addAll(aka); } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java index 294aa732..27da4f2f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java @@ -40,6 +40,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.transformer.meta.SourceMap; import org.spongepowered.asm.util.ASMHelper; +import org.spongepowered.asm.util.Annotations; import org.spongepowered.asm.util.ClassSignature; /** @@ -297,17 +298,17 @@ class TargetClassContext { return; } - AnnotationNode classDebugAnnotation = ASMHelper.getVisibleAnnotation(this.classNode, Debug.class); + AnnotationNode classDebugAnnotation = Annotations.getVisible(this.classNode, Debug.class); if (classDebugAnnotation != null) { - this.forceExport = Boolean.TRUE.equals(ASMHelper.getAnnotationValue(classDebugAnnotation, "export")); - if (Boolean.TRUE.equals(ASMHelper.getAnnotationValue(classDebugAnnotation, "print"))) { + this.forceExport = Boolean.TRUE.equals(Annotations.getValue(classDebugAnnotation, "export")); + if (Boolean.TRUE.equals(Annotations.getValue(classDebugAnnotation, "print"))) { ASMHelper.textify(this.classNode, System.err); } } for (MethodNode method : this.classNode.methods) { - AnnotationNode methodDebugAnnotation = ASMHelper.getVisibleAnnotation(method, Debug.class); - if (methodDebugAnnotation != null && Boolean.TRUE.equals(ASMHelper.getAnnotationValue(methodDebugAnnotation, "print"))) { + AnnotationNode methodDebugAnnotation = Annotations.getVisible(method, Debug.class); + if (methodDebugAnnotation != null && Boolean.TRUE.equals(Annotations.getValue(methodDebugAnnotation, "print"))) { ASMHelper.textify(method, System.err); } } diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index f5b57091..8984f504 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -24,18 +24,13 @@ */ package org.spongepowered.asm.util; -import static com.google.common.base.Preconditions.*; import static org.spongepowered.asm.lib.ClassWriter.*; 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; @@ -306,427 +301,6 @@ public final class ASMHelper { return opcode >= 0 ? String.valueOf(opcode) : "UNKNOWN"; } - - /** - * Set a runtime-visible annotation of the specified class on the supplied - * field node - * - * @param field Target field - * @param annotationClass Type of annotation to search for - * @param value Values (interleaved key/value pairs) to set - */ - public static void setVisibleAnnotation(FieldNode field, Class annotationClass, Object... value) { - AnnotationNode node = ASMHelper.makeAnnotationNode(Type.getDescriptor(annotationClass), value); - field.visibleAnnotations = ASMHelper.addAnnotation(field.visibleAnnotations, node); - } - - /** - * Set an invisible annotation of the specified class on the supplied field - * node - * - * @param field Target field - * @param annotationClass Type of annotation to search for - * @param value Values (interleaved key/value pairs) to set - */ - public static void setInvisibleAnnotation(FieldNode field, Class annotationClass, Object... value) { - AnnotationNode node = ASMHelper.makeAnnotationNode(Type.getDescriptor(annotationClass), value); - field.invisibleAnnotations = ASMHelper.addAnnotation(field.invisibleAnnotations, node); - } - - /** - * Set a runtime-visible annotation of the specified class on the supplied - * method node - * - * @param method Target method - * @param annotationClass Type of annotation to search for - * @param value Values (interleaved key/value pairs) to set - */ - public static void setVisibleAnnotation(MethodNode method, Class annotationClass, Object... value) { - AnnotationNode node = ASMHelper.makeAnnotationNode(Type.getDescriptor(annotationClass), value); - method.visibleAnnotations = ASMHelper.addAnnotation(method.visibleAnnotations, node); - } - - /** - * Set a invisible annotation of the specified class on the supplied method - * node - * - * @param method Target method - * @param annotationClass Type of annotation to search for - * @param value Values (interleaved key/value pairs) to set - */ - public static void setInvisibleAnnotation(MethodNode method, Class annotationClass, Object... value) { - AnnotationNode node = ASMHelper.makeAnnotationNode(Type.getDescriptor(annotationClass), value); - method.invisibleAnnotations = ASMHelper.addAnnotation(method.invisibleAnnotations, node); - } - - /** - * Create a new annotation node with the supplied values - * - * @param annotationType Name (internal name) of the annotation interface to - * create - * @param value Interleaved key/value pairs. Keys must be strings - * @return new annotation node - */ - private static AnnotationNode makeAnnotationNode(String annotationType, Object... value) { - AnnotationNode node = new AnnotationNode(annotationType); - for (int pos = 0; pos < value.length - 1; pos += 2) { - if (!(value[pos] instanceof String)) { - throw new IllegalArgumentException("Annotation keys must be strings, found " + value[pos].getClass().getSimpleName() - + " with " + value[pos].toString() + " at index " + pos + " creating " + annotationType); - } - node.visit((String)value[pos], value[pos + 1]); - } - return node; - } - - private static List addAnnotation(List annotations, AnnotationNode node) { - if (annotations == null) { - annotations = new ArrayList(1); - } else { - annotations.remove(ASMHelper.getAnnotation(annotations, node.desc)); - } - annotations.add(node); - return annotations; - } - - /** - * Get a runtime-visible annotation of the specified class from the supplied - * field node - * - * @param field Source field - * @param annotationClass Type of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getVisibleAnnotation(FieldNode field, Class annotationClass) { - return ASMHelper.getAnnotation(field.visibleAnnotations, Type.getDescriptor(annotationClass)); - } - - /** - * Get an invisible annotation of the specified class from the supplied - * field node - * - * @param field Source field - * @param annotationClass Type of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getInvisibleAnnotation(FieldNode field, Class annotationClass) { - return ASMHelper.getAnnotation(field.invisibleAnnotations, Type.getDescriptor(annotationClass)); - } - - /** - * Get a runtime-visible annotation of the specified class from the supplied - * method node - * - * @param method Source method - * @param annotationClass Type of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getVisibleAnnotation(MethodNode method, Class annotationClass) { - return ASMHelper.getAnnotation(method.visibleAnnotations, Type.getDescriptor(annotationClass)); - } - - /** - * Get an invisible annotation of the specified class from the supplied - * method node - * - * @param method Source method - * @param annotationClass Type of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getInvisibleAnnotation(MethodNode method, Class annotationClass) { - return ASMHelper.getAnnotation(method.invisibleAnnotations, Type.getDescriptor(annotationClass)); - } - - /** - * Get a runtime-visible annotation of the specified class from the supplied - * method node - * - * @param method Source method - * @param annotationClasses Types of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getSingleVisibleAnnotation(MethodNode method, Class... annotationClasses) { - return ASMHelper.getSingleAnnotation(method.visibleAnnotations, annotationClasses); - } - - /** - * Get an invisible annotation of the specified class from the supplied - * method node - * - * @param method Source method - * @param annotationClasses Types of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getSingleInvisibleAnnotation(MethodNode method, Class... annotationClasses) { - return ASMHelper.getSingleAnnotation(method.invisibleAnnotations, annotationClasses); - } - - /** - * Get a runtime-visible annotation of the specified class from the supplied - * class node - * - * @param classNode Source classNode - * @param annotationClass Type of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getVisibleAnnotation(ClassNode classNode, Class annotationClass) { - return ASMHelper.getAnnotation(classNode.visibleAnnotations, Type.getDescriptor(annotationClass)); - } - - /** - * Get an invisible annotation of the specified class from the supplied - * class node - * - * @param classNode Source classNode - * @param annotationClass Type of annotation to search for - * @return the annotation, or null if not present - */ - public static AnnotationNode getInvisibleAnnotation(ClassNode classNode, Class annotationClass) { - return ASMHelper.getAnnotation(classNode.invisibleAnnotations, Type.getDescriptor(annotationClass)); - } - - /** - * Get a runtime-visible parameter annotation of the specified class from - * the supplied method node - * - * @param method Source method - * @param annotationClass Type of annotation to search for - * @param paramIndex Index of the parameter to fetch annotation for - * @return the annotation, or null if not present - */ - public static AnnotationNode getVisibleParameterAnnotation(MethodNode method, Class annotationClass, int paramIndex) { - return ASMHelper.getParameterAnnotation(method.visibleParameterAnnotations, Type.getDescriptor(annotationClass), paramIndex); - } - - /** - * Get an invisible parameter annotation of the specified class from the - * supplied method node - * - * @param method Source method - * @param annotationClass Type of annotation to search for - * @param paramIndex Index of the parameter to fetch annotation for - * @return the annotation, or null if not present - */ - public static AnnotationNode getInvisibleParameterAnnotation(MethodNode method, Class annotationClass, int paramIndex) { - return ASMHelper.getParameterAnnotation(method.invisibleParameterAnnotations, Type.getDescriptor(annotationClass), paramIndex); - } - - /** - * Get a parameter annotation of the specified class from the supplied - * method node - * - * @param parameterAnnotations Annotations for the parameter - * @param annotationType Type of annotation to search for - * @param paramIndex Index of the parameter to fetch annotation for - * @return the annotation, or null if not present - */ - public static AnnotationNode getParameterAnnotation(List[] parameterAnnotations, String annotationType, int paramIndex) { - if (parameterAnnotations == null || paramIndex < 0 || paramIndex >= parameterAnnotations.length) { - return null; - } - - return ASMHelper.getAnnotation(parameterAnnotations[paramIndex], annotationType); - } - - /** - * Search for and return an annotation node matching the specified type - * within the supplied - * collection of annotation nodes - * - * @param annotations Haystack - * @param annotationType Needle - * @return matching annotation node or null if the annotation doesn't exist - */ - public static AnnotationNode getAnnotation(List annotations, String annotationType) { - if (annotations == null) { - return null; - } - - for (AnnotationNode annotation : annotations) { - if (annotationType.equals(annotation.desc)) { - return annotation; - } - } - - return null; - } - - private static AnnotationNode getSingleAnnotation(List annotations, Class[] annotationClasses) { - List nodes = new ArrayList(); - for (Class annotationClass : annotationClasses) { - AnnotationNode annotation = ASMHelper.getAnnotation(annotations, Type.getDescriptor(annotationClass)); - if (annotation != null) { - nodes.add(annotation); - } - } - - int foundNodes = nodes.size(); - if (foundNodes > 1) { - throw new IllegalArgumentException("Conflicting annotations found: " + annotationClasses); - } - - return foundNodes == 0 ? null : nodes.get(0); - } - - /** - * Duck type the "value" entry (if any) of the specified annotation node - * - * @param duck type - * @param annotation Annotation node to query - * @return duck-typed annotation value, null if missing, or inevitable - * {@link ClassCastException} if your duck is actually a rooster - */ - public static T getAnnotationValue(AnnotationNode annotation) { - return ASMHelper.getAnnotationValue(annotation, "value"); - } - - /** - * Get the value of an annotation node and do pseudo-duck-typing via Java's - * crappy generics - * - * @param duck type - * @param annotation Annotation node to query - * @param key Key to search for - * @param defaultValue Value to return if the specified key is not found or - * is null - * @return duck-typed annotation value, null if missing, or inevitable - * {@link ClassCastException} if your duck is actually a rooster - */ - public static T getAnnotationValue(AnnotationNode annotation, String key, T defaultValue) { - T returnValue = ASMHelper.getAnnotationValue(annotation, key); - return returnValue != null ? returnValue : defaultValue; - } - - /** - * Gets an annotation value or returns the default value of the annotation - * if the annotation value is not present - * - * @param duck type - * @param annotation Annotation node to query - * @param key Key to search for - * @param annotationClass Annotation class to query reflectively for the - * default value - * @return Value of the specified annotation node, default value if not - * specified, or null if no value or default - */ - @SuppressWarnings("unchecked") - public static T getAnnotationValue(AnnotationNode annotation, String key, Class annotationClass) { - checkNotNull(annotationClass, "annotationClass cannot be null"); - T value = ASMHelper.getAnnotationValue(annotation, key); - if (value == null) { - try { - value = (T)annotationClass.getDeclaredMethod(key).getDefaultValue(); - } catch (NoSuchMethodException ex) { - // Don't care - } - } - return value; - } - - /** - * Get the value of an annotation node and do pseudo-duck-typing via Java's - * crappy generics - * - * @param duck type - * @param annotation Annotation node to query - * @param key Key to search for - * @return duck-typed annotation value, null if missing, or inevitable - * {@link ClassCastException} if your duck is actually a rooster - */ - @SuppressWarnings("unchecked") - public static T getAnnotationValue(AnnotationNode annotation, String key) { - boolean getNextValue = false; - - if (annotation == null || annotation.values == null) { - return null; - } - - // Keys and value are stored in successive pairs, search for the key and if found return the following entry - for (Object value : annotation.values) { - if (getNextValue) { - return (T) value; - } - if (value.equals(key)) { - getNextValue = true; - } - } - - return null; - } - - /** - * Get the value of an annotation node as the specified enum, returns - * defaultValue if the annotation value is not set - * - * @param duck type - * @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 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 diff --git a/src/main/java/org/spongepowered/asm/util/Annotations.java b/src/main/java/org/spongepowered/asm/util/Annotations.java new file mode 100644 index 00000000..5dce79da --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/Annotations.java @@ -0,0 +1,471 @@ +/* + * 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.util; + +import static com.google.common.base.Preconditions.*; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; + +import org.spongepowered.asm.lib.Type; +import org.spongepowered.asm.lib.tree.AnnotationNode; +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.lib.tree.FieldNode; +import org.spongepowered.asm.lib.tree.MethodNode; + +/** + * Utility class for working with ASM annotations + */ +public final class Annotations { + + private Annotations() { + // Utility class + } + + /** + * Set a runtime-visible annotation of the specified class on the supplied + * field node + * + * @param field Target field + * @param annotationClass Type of annotation to search for + * @param value Values (interleaved key/value pairs) to set + */ + public static void setVisible(FieldNode field, Class annotationClass, Object... value) { + AnnotationNode node = Annotations.createNode(Type.getDescriptor(annotationClass), value); + field.visibleAnnotations = Annotations.add(field.visibleAnnotations, node); + } + + /** + * Set an invisible annotation of the specified class on the supplied field + * node + * + * @param field Target field + * @param annotationClass Type of annotation to search for + * @param value Values (interleaved key/value pairs) to set + */ + public static void setInvisible(FieldNode field, Class annotationClass, Object... value) { + AnnotationNode node = Annotations.createNode(Type.getDescriptor(annotationClass), value); + field.invisibleAnnotations = Annotations.add(field.invisibleAnnotations, node); + } + + /** + * Set a runtime-visible annotation of the specified class on the supplied + * method node + * + * @param method Target method + * @param annotationClass Type of annotation to search for + * @param value Values (interleaved key/value pairs) to set + */ + public static void setVisible(MethodNode method, Class annotationClass, Object... value) { + AnnotationNode node = Annotations.createNode(Type.getDescriptor(annotationClass), value); + method.visibleAnnotations = Annotations.add(method.visibleAnnotations, node); + } + + /** + * Set a invisible annotation of the specified class on the supplied method + * node + * + * @param method Target method + * @param annotationClass Type of annotation to search for + * @param value Values (interleaved key/value pairs) to set + */ + public static void setInvisible(MethodNode method, Class annotationClass, Object... value) { + AnnotationNode node = Annotations.createNode(Type.getDescriptor(annotationClass), value); + method.invisibleAnnotations = Annotations.add(method.invisibleAnnotations, node); + } + + /** + * Create a new annotation node with the supplied values + * + * @param annotationType Name (internal name) of the annotation interface to + * create + * @param value Interleaved key/value pairs. Keys must be strings + * @return new annotation node + */ + private static AnnotationNode createNode(String annotationType, Object... value) { + AnnotationNode node = new AnnotationNode(annotationType); + for (int pos = 0; pos < value.length - 1; pos += 2) { + if (!(value[pos] instanceof String)) { + throw new IllegalArgumentException("Annotation keys must be strings, found " + value[pos].getClass().getSimpleName() + + " with " + value[pos].toString() + " at index " + pos + " creating " + annotationType); + } + node.visit((String)value[pos], value[pos + 1]); + } + return node; + } + + private static List add(List annotations, AnnotationNode node) { + if (annotations == null) { + annotations = new ArrayList(1); + } else { + annotations.remove(Annotations.get(annotations, node.desc)); + } + annotations.add(node); + return annotations; + } + + /** + * Get a runtime-visible annotation of the specified class from the supplied + * field node + * + * @param field Source field + * @param annotationClass Type of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getVisible(FieldNode field, Class annotationClass) { + return Annotations.get(field.visibleAnnotations, Type.getDescriptor(annotationClass)); + } + + /** + * Get an invisible annotation of the specified class from the supplied + * field node + * + * @param field Source field + * @param annotationClass Type of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getInvisible(FieldNode field, Class annotationClass) { + return Annotations.get(field.invisibleAnnotations, Type.getDescriptor(annotationClass)); + } + + /** + * Get a runtime-visible annotation of the specified class from the supplied + * method node + * + * @param method Source method + * @param annotationClass Type of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getVisible(MethodNode method, Class annotationClass) { + return Annotations.get(method.visibleAnnotations, Type.getDescriptor(annotationClass)); + } + + /** + * Get an invisible annotation of the specified class from the supplied + * method node + * + * @param method Source method + * @param annotationClass Type of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getInvisible(MethodNode method, Class annotationClass) { + return Annotations.get(method.invisibleAnnotations, Type.getDescriptor(annotationClass)); + } + + /** + * Get a runtime-visible annotation of the specified class from the supplied + * method node + * + * @param method Source method + * @param annotationClasses Types of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getSingleVisible(MethodNode method, Class... annotationClasses) { + return Annotations.getSingle(method.visibleAnnotations, annotationClasses); + } + + /** + * Get an invisible annotation of the specified class from the supplied + * method node + * + * @param method Source method + * @param annotationClasses Types of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getSingleInvisible(MethodNode method, Class... annotationClasses) { + return Annotations.getSingle(method.invisibleAnnotations, annotationClasses); + } + + /** + * Get a runtime-visible annotation of the specified class from the supplied + * class node + * + * @param classNode Source classNode + * @param annotationClass Type of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getVisible(ClassNode classNode, Class annotationClass) { + return Annotations.get(classNode.visibleAnnotations, Type.getDescriptor(annotationClass)); + } + + /** + * Get an invisible annotation of the specified class from the supplied + * class node + * + * @param classNode Source classNode + * @param annotationClass Type of annotation to search for + * @return the annotation, or null if not present + */ + public static AnnotationNode getInvisible(ClassNode classNode, Class annotationClass) { + return Annotations.get(classNode.invisibleAnnotations, Type.getDescriptor(annotationClass)); + } + + /** + * Get a runtime-visible parameter annotation of the specified class from + * the supplied method node + * + * @param method Source method + * @param annotationClass Type of annotation to search for + * @param paramIndex Index of the parameter to fetch annotation for + * @return the annotation, or null if not present + */ + public static AnnotationNode getVisibleParameter(MethodNode method, Class annotationClass, int paramIndex) { + return Annotations.getParameter(method.visibleParameterAnnotations, Type.getDescriptor(annotationClass), paramIndex); + } + + /** + * Get an invisible parameter annotation of the specified class from the + * supplied method node + * + * @param method Source method + * @param annotationClass Type of annotation to search for + * @param paramIndex Index of the parameter to fetch annotation for + * @return the annotation, or null if not present + */ + public static AnnotationNode getInvisibleParameter(MethodNode method, Class annotationClass, int paramIndex) { + return Annotations.getParameter(method.invisibleParameterAnnotations, Type.getDescriptor(annotationClass), paramIndex); + } + + /** + * Get a parameter annotation of the specified class from the supplied + * method node + * + * @param parameterAnnotations Annotations for the parameter + * @param annotationType Type of annotation to search for + * @param paramIndex Index of the parameter to fetch annotation for + * @return the annotation, or null if not present + */ + public static AnnotationNode getParameter(List[] parameterAnnotations, String annotationType, int paramIndex) { + if (parameterAnnotations == null || paramIndex < 0 || paramIndex >= parameterAnnotations.length) { + return null; + } + + return Annotations.get(parameterAnnotations[paramIndex], annotationType); + } + + /** + * Search for and return an annotation node matching the specified type + * within the supplied + * collection of annotation nodes + * + * @param annotations Haystack + * @param annotationType Needle + * @return matching annotation node or null if the annotation doesn't exist + */ + public static AnnotationNode get(List annotations, String annotationType) { + if (annotations == null) { + return null; + } + + for (AnnotationNode annotation : annotations) { + if (annotationType.equals(annotation.desc)) { + return annotation; + } + } + + return null; + } + + private static AnnotationNode getSingle(List annotations, Class[] annotationClasses) { + List nodes = new ArrayList(); + for (Class annotationClass : annotationClasses) { + AnnotationNode annotation = Annotations.get(annotations, Type.getDescriptor(annotationClass)); + if (annotation != null) { + nodes.add(annotation); + } + } + + int foundNodes = nodes.size(); + if (foundNodes > 1) { + throw new IllegalArgumentException("Conflicting annotations found: " + annotationClasses); + } + + return foundNodes == 0 ? null : nodes.get(0); + } + + /** + * Duck type the "value" entry (if any) of the specified annotation node + * + * @param duck type + * @param annotation Annotation node to query + * @return duck-typed annotation value, null if missing, or inevitable + * {@link ClassCastException} if your duck is actually a rooster + */ + public static T getValue(AnnotationNode annotation) { + return Annotations.getValue(annotation, "value"); + } + + /** + * Get the value of an annotation node and do pseudo-duck-typing via Java's + * crappy generics + * + * @param duck type + * @param annotation Annotation node to query + * @param key Key to search for + * @param defaultValue Value to return if the specified key is not found or + * is null + * @return duck-typed annotation value, null if missing, or inevitable + * {@link ClassCastException} if your duck is actually a rooster + */ + public static T getValue(AnnotationNode annotation, String key, T defaultValue) { + T returnValue = Annotations.getValue(annotation, key); + return returnValue != null ? returnValue : defaultValue; + } + + /** + * Gets an annotation value or returns the default value of the annotation + * if the annotation value is not present + * + * @param duck type + * @param annotation Annotation node to query + * @param key Key to search for + * @param annotationClass Annotation class to query reflectively for the + * default value + * @return Value of the specified annotation node, default value if not + * specified, or null if no value or default + */ + @SuppressWarnings("unchecked") + public static T getValue(AnnotationNode annotation, String key, Class annotationClass) { + checkNotNull(annotationClass, "annotationClass cannot be null"); + T value = Annotations.getValue(annotation, key); + if (value == null) { + try { + value = (T)annotationClass.getDeclaredMethod(key).getDefaultValue(); + } catch (NoSuchMethodException ex) { + // Don't care + } + } + return value; + } + + /** + * Get the value of an annotation node and do pseudo-duck-typing via Java's + * crappy generics + * + * @param duck type + * @param annotation Annotation node to query + * @param key Key to search for + * @return duck-typed annotation value, null if missing, or inevitable + * {@link ClassCastException} if your duck is actually a rooster + */ + @SuppressWarnings("unchecked") + public static T getValue(AnnotationNode annotation, String key) { + boolean getNextValue = false; + + if (annotation == null || annotation.values == null) { + return null; + } + + // Keys and value are stored in successive pairs, search for the key and if found return the following entry + for (Object value : annotation.values) { + if (getNextValue) { + return (T) value; + } + if (value.equals(key)) { + getNextValue = true; + } + } + + return null; + } + + /** + * Get the value of an annotation node as the specified enum, returns + * defaultValue if the annotation value is not set + * + * @param duck type + * @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 getValue(AnnotationNode annotation, String key, Class enumClass, T defaultValue) { + String[] value = Annotations.getValue(annotation, key); + if (value == null) { + return defaultValue; + } + return Annotations.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 getValue(AnnotationNode annotation, String key, boolean notNull) { + Object value = Annotations.getValue(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 getValue(AnnotationNode annotation, String key, boolean notNull, Class enumClass) { + Object value = Annotations.getValue(annotation, key); + if (value instanceof List) { + for (ListIterator iter = ((List)value).listIterator(); iter.hasNext();) { + iter.set(Annotations.toEnumValue(enumClass, (String[])iter.next())); + } + return (List)value; + } else if (value instanceof String[]) { + List list = new ArrayList(); + list.add(Annotations.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]); + } + +} diff --git a/src/main/java/org/spongepowered/asm/util/ConstraintParser.java b/src/main/java/org/spongepowered/asm/util/ConstraintParser.java index 92363fcc..794ba973 100644 --- a/src/main/java/org/spongepowered/asm/util/ConstraintParser.java +++ b/src/main/java/org/spongepowered/asm/util/ConstraintParser.java @@ -310,7 +310,7 @@ public final class ConstraintParser { * @return parsed constraint */ public static Constraint parse(AnnotationNode annotation) { - String constraints = ASMHelper.getAnnotationValue(annotation, "constraints", ""); + String constraints = Annotations.getValue(annotation, "constraints", ""); return ConstraintParser.parse(constraints); }