Refactoring - extract annotation utilities from ASM helper

This commit is contained in:
Mumfrey committed 2017-02-01 17:51:51 +00:00
1 parent 20d0468b44
commit c30a17e872
23 files changed
+600 -544

No files matched your search

@@ -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<? extends Annotation> 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.<String>getAnnotationValue(this.annotation);
String name = Annotations.<String>getValue(this.annotation);
if (Strings.isNullOrEmpty(name)) {
String inflectedTarget = this.inflectTarget();
if (inflectedTarget == null) {
@@ -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.<String>getAnnotationValue(node, "value");
List<String> args = ASMHelper.<List<String>>getAnnotationValue(node, "args");
String target = ASMHelper.<String>getAnnotationValue(node, "target", "");
String slice = ASMHelper.<String>getAnnotationValue(node, "slice", "");
At.Shift shift = ASMHelper.<At.Shift>getAnnotationValue(node, "shift", At.Shift.class, At.Shift.NONE);
int by = ASMHelper.<Integer>getAnnotationValue(node, "by", Integer.valueOf(0));
int ordinal = ASMHelper.<Integer>getAnnotationValue(node, "ordinal", Integer.valueOf(-1));
int opcode = ASMHelper.<Integer>getAnnotationValue(node, "opcode", Integer.valueOf(0));
String at = Annotations.<String>getValue(node, "value");
List<String> args = Annotations.<List<String>>getValue(node, "args");
String target = Annotations.<String>getValue(node, "target", "");
String slice = Annotations.<String>getValue(node, "slice", "");
At.Shift shift = Annotations.<At.Shift>getValue(node, "shift", At.Shift.class, At.Shift.NONE);
int by = Annotations.<Integer>getValue(node, "by", Integer.valueOf(0));
int ordinal = Annotations.<Integer>getValue(node, "ordinal", Integer.valueOf(-1));
int opcode = Annotations.<Integer>getValue(node, "opcode", Integer.valueOf(0));
if (args == null) {
args = ImmutableList.<String>of();
@@ -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.<String>getAnnotationValue(annotation, "name");
String name = Annotations.<String>getValue(annotation, "name");
if (name == null || name.isEmpty()) {
name = defaultGroup;
}
InjectorGroupInfo groupInfo = this.forName(name);
Integer min = ASMHelper.<Integer>getAnnotationValue(annotation, "min");
Integer min = Annotations.<Integer>getValue(annotation, "min");
if (min != null && min.intValue() != -1) {
groupInfo.setMinRequired(min.intValue());
}
Integer max = ASMHelper.<Integer>getAnnotationValue(annotation, "max");
Integer max = Annotations.<Integer>getValue(annotation, "max");
if (max != null && max.intValue() != -1) {
groupInfo.setMaxAllowed(max.intValue());
}
@@ -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 {
@@ -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.<String>getAnnotationValue(node, "id");
String id = Annotations.<String>getValue(node, "id");
AnnotationNode from = ASMHelper.<AnnotationNode>getAnnotationValue(node, "from");
AnnotationNode to = ASMHelper.<AnnotationNode>getAnnotationValue(node, "to");
AnnotationNode from = Annotations.<AnnotationNode>getValue(node, "from");
AnnotationNode to = Annotations.<AnnotationNode>getValue(node, "to");
InjectionPoint fromPoint = from != null ? InjectionPoint.parse(info, from) : null;
InjectionPoint toPoint = to != null ? InjectionPoint.parse(info, to) : null;
@@ -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.<AnnotationNode>getAnnotationValue(annotation, "slice", true)) {
for (AnnotationNode node : Annotations.<AnnotationNode>getValue(annotation, "slice", true)) {
MethodSlice slice = MethodSlice.parse(info, node);
slices.add(slice);
}
@@ -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);
}
@@ -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.<Boolean>getAnnotationValue(annotation, "argsOnly", Boolean.FALSE).booleanValue();
int ordinal = ASMHelper.<Integer>getAnnotationValue(annotation, "ordinal", -1);
int index = ASMHelper.<Integer>getAnnotationValue(annotation, "index", -1);
boolean argsOnly = Annotations.<Boolean>getValue(annotation, "argsOnly", Boolean.FALSE).booleanValue();
int ordinal = Annotations.<Integer>getValue(annotation, "ordinal", -1);
int index = Annotations.<Integer>getValue(annotation, "index", -1);
Set<String> names = new HashSet<String>();
List<String> namesList = ASMHelper.<List<String>>getAnnotationValue(annotation, "name", (List<String>)null);
List<String> namesList = Annotations.<List<String>>getValue(annotation, "name", (List<String>)null);
if (namesList != null) {
names.addAll(namesList);
}
@@ -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.<Boolean>getAnnotationValue(injectAnnotation, "cancellable", Boolean.FALSE);
LocalCapture locals = ASMHelper.<LocalCapture>getAnnotationValue(injectAnnotation, "locals", LocalCapture.class, LocalCapture.NO_CAPTURE);
String identifier = ASMHelper.<String>getAnnotationValue(injectAnnotation, "id", "");
boolean cancellable = Annotations.<Boolean>getValue(injectAnnotation, "cancellable", Boolean.FALSE);
LocalCapture locals = Annotations.<LocalCapture>getValue(injectAnnotation, "locals", LocalCapture.class, LocalCapture.NO_CAPTURE);
String identifier = Annotations.<String>getValue(injectAnnotation, "id", "");
return new CallbackInjector(this, cancellable, locals, identifier);
}
@@ -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.<String>getAnnotationValue(this.annotation, "method");
String method = Annotations.<String>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<AnnotationNode> readInjectionPoints(String type) {
List<AnnotationNode> ats = ASMHelper.<AnnotationNode>getAnnotationValue(this.annotation, "at", false);
List<AnnotationNode> ats = Annotations.<AnnotationNode>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.<Integer>getAnnotationValue(this.annotation, "expect");
Integer expect = Annotations.<Integer>getValue(this.annotation, "expect");
if (expect != null) {
this.expectedCallbackCount = expect.intValue();
}
Integer require = ASMHelper.<Integer>getAnnotationValue(this.annotation, "require");
Integer require = Annotations.<Integer>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.<String>getAnnotationValue(merged, "mixin");
int priority = ASMHelper.<Integer>getAnnotationValue(merged, "priority");
String owner = Annotations.<String>getValue(merged, "mixin");
int priority = Annotations.<Integer>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,
@@ -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.<Integer>getAnnotationValue(injectAnnotation, "index", -1);
int index = Annotations.<Integer>getValue(injectAnnotation, "index", -1);
return new ModifyArgInjector(this, index);
}
@@ -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.<Boolean>getAnnotationValue(node, "nullValue", (Boolean)null);
this.ordinal = ASMHelper.<Integer>getAnnotationValue(node, "ordinal", Integer.valueOf(-1));
Boolean empty = Annotations.<Boolean>getValue(node, "nullValue", (Boolean)null);
this.ordinal = Annotations.<Integer>getValue(node, "ordinal", Integer.valueOf(-1));
this.nullValue = empty != null ? empty.booleanValue() : false;
this.intValue = ASMHelper.<Integer>getAnnotationValue(node, "intValue", (Integer)null);
this.floatValue = ASMHelper.<Float>getAnnotationValue(node, "floatValue", (Float)null);
this.longValue = ASMHelper.<Long>getAnnotationValue(node, "longValue", (Long)null);
this.doubleValue = ASMHelper.<Double>getAnnotationValue(node, "doubleValue", (Double)null);
this.stringValue = ASMHelper.<String>getAnnotationValue(node, "stringValue", (String)null);
this.typeValue = ASMHelper.<Type>getAnnotationValue(node, "classValue", (Type)null);
this.intValue = Annotations.<Integer>getValue(node, "intValue", (Integer)null);
this.floatValue = Annotations.<Float>getValue(node, "floatValue", (Float)null);
this.longValue = Annotations.<Long>getValue(node, "longValue", (Long)null);
this.doubleValue = Annotations.<Double>getValue(node, "doubleValue", (Double)null);
this.stringValue = Annotations.<String>getValue(node, "stringValue", (String)null);
this.typeValue = Annotations.<Type>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.<Boolean>getAnnotationValue(node, "log", Boolean.FALSE).booleanValue();
this.log = Annotations.<Boolean>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<Integer> opcodes = new HashSet<Integer>();
for (Condition condition : ASMHelper.<Condition>getAnnotationValue(node, "expandZeroConditions", true, Condition.class)) {
for (Condition condition : Annotations.<Condition>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<AnnotationNode> readInjectionPoints(String type) {
AnnotationNode constantAnnotation = ASMHelper.<AnnotationNode>getAnnotationValue(this.annotation, "constant");
AnnotationNode constantAnnotation = Annotations.<AnnotationNode>getValue(this.annotation, "constant");
List<AnnotationNode> ats = new ArrayList<AnnotationNode>();
ats.add(constantAnnotation);
return ats;
@@ -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.<Boolean>getAnnotationValue(injectAnnotation, "print", Boolean.FALSE).booleanValue();
boolean print = Annotations.<Boolean>getValue(injectAnnotation, "print", Boolean.FALSE).booleanValue();
return new ModifyVariableInjector(this, print, LocalVariableDiscriminator.parse(injectAnnotation));
}
@@ -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);
}
}
@@ -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.<String>getAnnotationValue(node, "prefix");
Type iface = ASMHelper.<Type>getAnnotationValue(node, "iface");
Boolean unique = ASMHelper.<Boolean>getAnnotationValue(node, "unique");
String prefix = Annotations.<String>getValue(node, "prefix");
Type iface = Annotations.<Type>getValue(node, "iface");
Boolean unique = Annotations.<Boolean>getValue(node, "unique");
if (prefix == null || iface == null) {
throw new InvalidMixinException(mixin, String.format("@Interface annotation on %s is missing a required parameter", mixin));
@@ -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.<String>getAnnotationValue(merged, "sessionId");
String sessionId = Annotations.<String>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.<String>getAnnotationValue(merged, "mixin");
int priority = ASMHelper.<Integer>getAnnotationValue(merged, "priority");
String owner = Annotations.<String>getValue(merged, "mixin");
int priority = Annotations.<Integer>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<? extends Annotation> annotationType : MixinApplicatorStandard.CONSTRAINED_ANNOTATIONS) {
AnnotationNode annotation = ASMHelper.getVisibleAnnotation(method, annotationType);
AnnotationNode annotation = Annotations.getVisible(method, annotationType);
if (annotation != null) {
this.checkConstraints(mixin, method, annotation);
}
@@ -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<MixinInfo>, IMixinInfo {
}
public AnnotationNode getVisibleAnnotation(Class<? extends Annotation> annotationClass) {
return ASMHelper.getVisibleAnnotation(this, annotationClass);
return Annotations.getVisible(this, annotationClass);
}
public AnnotationNode getInjectorAnnotation() {
@@ -276,7 +276,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, 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<MixinInfo>, 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<MixinInfo>, IMixinInfo {
}
private void validateRemappable(Class<Shadow> 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<MixinInfo>, 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<AnnotationNode> interfaces = ASMHelper.getAnnotationValue(implementsAnnotation);
List<AnnotationNode> interfaces = Annotations.getValue(implementsAnnotation);
if (interfaces == null) {
return;
}
@@ -817,14 +817,14 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
return Collections.<ClassInfo>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<ClassInfo> targets = new ArrayList<ClassInfo>();
List<org.spongepowered.asm.lib.Type> publicTargets = ASMHelper.getAnnotationValue(mixin, "value");
List<String> privateTargets = ASMHelper.getAnnotationValue(mixin, "targets");
List<org.spongepowered.asm.lib.Type> publicTargets = Annotations.getValue(mixin, "value");
List<String> privateTargets = Annotations.getValue(mixin, "targets");
if (publicTargets != null) {
this.readTargets(targets, Lists.transform(publicTargets, new Function<org.spongepowered.asm.lib.Type, String>() {
@@ -907,17 +907,17 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, 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() {
@@ -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.<String>getAnnotationValue(shadowAnnotation, "prefix", Shadow.class);
String prefix = Annotations.<String>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<FieldNode> 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.<String>getAnnotationValue(shadow, "prefix", Shadow.class);
String prefix = Annotations.<String>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));
@@ -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<String> aliases = new LinkedList<String>();
aliases.add(method.name);
if (annotation != null) {
List<String> aka = ASMHelper.<List<String>>getAnnotationValue(annotation, "aliases");
List<String> aka = Annotations.<List<String>>getValue(annotation, "aliases");
if (aka != null) {
aliases.addAll(aka);
}
@@ -865,7 +866,7 @@ public class MixinTargetContext implements IMixinContext {
Deque<String> aliases = new LinkedList<String>();
aliases.add(field.name);
if (shadow != null) {
List<String> aka = ASMHelper.<List<String>>getAnnotationValue(shadow, "aliases");
List<String> aka = Annotations.<List<String>>getValue(shadow, "aliases");
if (aka != null) {
aliases.addAll(aka);
}
@@ -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.<String>getValue(classDebugAnnotation, "export"));
if (Boolean.TRUE.equals(Annotations.<String>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.<String>getValue(methodDebugAnnotation, "print"))) {
ASMHelper.textify(method, System.err);
}
}
@@ -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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<AnnotationNode> addAnnotation(List<AnnotationNode> annotations, AnnotationNode node) {
if (annotations == null) {
annotations = new ArrayList<AnnotationNode>(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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation>... 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<? extends Annotation>... 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<AnnotationNode>[] 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<AnnotationNode> 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<AnnotationNode> annotations, Class<? extends Annotation>[] annotationClasses) {
List<AnnotationNode> nodes = new ArrayList<AnnotationNode>();
for (Class<? extends Annotation> 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 <T> 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> 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 <T> 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> 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 <T> 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> 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 <T> 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> 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 <T> 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 extends Enum<T>> T getAnnotationValue(AnnotationNode annotation, String key, Class<T> enumClass, T defaultValue) {
String[] value = ASMHelper.<String[]>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 <T> 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 <T> List<T> getAnnotationValue(AnnotationNode annotation, String key, boolean notNull) {
Object value = ASMHelper.<Object>getAnnotationValue(annotation, key);
if (value instanceof List) {
return (List<T>)value;
} else if (value != null) {
List<T> list = new ArrayList<T>();
list.add((T)value);
return list;
}
return Collections.<T>emptyList();
}
/**
* Return the specified annotation node value as a list of enums
*
* @param <T> 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 <T extends Enum<T>> List<T> getAnnotationValue(AnnotationNode annotation, String key, boolean notNull, Class<T> enumClass) {
Object value = ASMHelper.<Object>getAnnotationValue(annotation, key);
if (value instanceof List) {
for (ListIterator<Object> iter = ((List<Object>)value).listIterator(); iter.hasNext();) {
iter.set(ASMHelper.toEnumValue(enumClass, (String[])iter.next()));
}
return (List<T>)value;
} else if (value instanceof String[]) {
List<T> list = new ArrayList<T>();
list.add(ASMHelper.toEnumValue(enumClass, (String[])value));
return list;
}
return Collections.<T>emptyList();
}
private static <T extends Enum<T>> T toEnumValue(Class<T> 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
@@ -0,0 +1,471 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<AnnotationNode> add(List<AnnotationNode> annotations, AnnotationNode node) {
if (annotations == null) {
annotations = new ArrayList<AnnotationNode>(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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation>... 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<? extends Annotation>... 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> 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<AnnotationNode>[] 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<AnnotationNode> 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<AnnotationNode> annotations, Class<? extends Annotation>[] annotationClasses) {
List<AnnotationNode> nodes = new ArrayList<AnnotationNode>();
for (Class<? extends Annotation> 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 <T> 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> 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 <T> 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> 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 <T> 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> 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 <T> 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> 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 <T> 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 extends Enum<T>> T getValue(AnnotationNode annotation, String key, Class<T> enumClass, T defaultValue) {
String[] value = Annotations.<String[]>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 <T> 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 <T> List<T> getValue(AnnotationNode annotation, String key, boolean notNull) {
Object value = Annotations.<Object>getValue(annotation, key);
if (value instanceof List) {
return (List<T>)value;
} else if (value != null) {
List<T> list = new ArrayList<T>();
list.add((T)value);
return list;
}
return Collections.<T>emptyList();
}
/**
* Return the specified annotation node value as a list of enums
*
* @param <T> 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 <T extends Enum<T>> List<T> getValue(AnnotationNode annotation, String key, boolean notNull, Class<T> enumClass) {
Object value = Annotations.<Object>getValue(annotation, key);
if (value instanceof List) {
for (ListIterator<Object> iter = ((List<Object>)value).listIterator(); iter.hasNext();) {
iter.set(Annotations.toEnumValue(enumClass, (String[])iter.next()));
}
return (List<T>)value;
} else if (value instanceof String[]) {
List<T> list = new ArrayList<T>();
list.add(Annotations.toEnumValue(enumClass, (String[])value));
return list;
}
return Collections.<T>emptyList();
}
private static <T extends Enum<T>> T toEnumValue(Class<T> 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]);
}
}
@@ -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);
}