From e3211bbfee854847c4dc3ff077a2d63c47ff4c56 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 29 Dec 2014 19:33:27 +0000 Subject: [PATCH] Get enum annotation value without reflection --- .../asm/mixin/injection/InjectionPoint.java | 2 +- .../org/spongepowered/asm/util/ASMHelper.java | 22 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) 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 6590b6e7..63ec450c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -273,7 +273,7 @@ public abstract class InjectionPoint { String at = ASMHelper.getAnnotationValue(node, "value"); List args = ASMHelper.>getAnnotationValue(node, "args"); String target = ASMHelper.getAnnotationValue(node, "target", ""); - At.Shift shift = ASMHelper.getAnnotationEnumConstantValue(node, "shift", At.Shift.class, At.Shift.NONE); + 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)); diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index 85f7da06..b9f84a91 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -46,7 +46,6 @@ import org.objectweb.asm.util.CheckClassAdapter; import java.io.PrintWriter; import java.lang.annotation.Annotation; -import java.lang.reflect.Field; import java.util.Iterator; import java.util.List; @@ -485,7 +484,7 @@ public class ASMHelper { } /** - * Get the enum constant referenced by an annotation node + * Get the value of an annotation node as the specified enum, returns defaultValue if the annotation value is not set * * @param annotationNode Annotation node to query * @param key Key to search for @@ -494,18 +493,15 @@ public class ASMHelper { * @return duck-typed annotation value or defaultValue if missing * @throws NoSuchFieldException */ - @SuppressWarnings("unchecked") - public static T getAnnotationEnumConstantValue(AnnotationNode annotationNode, String key, Class enumClass, T defaultValue) { - String[] value = ASMHelper.getAnnotationValue(annotationNode, key); - try { - Field enumField = enumClass.getField(value[1]); - if (enumField.isEnumConstant()) { - return (T) enumField; - } - } catch (NoSuchFieldException e) { - // don't care + public static > T getAnnotationValue(AnnotationNode annotationNode, String key, Class enumClass, T defaultValue) { + String[] value = ASMHelper.getAnnotationValue(annotationNode, key); + if (value == null) { + return defaultValue; } - return defaultValue; + 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]); } public static boolean methodIsStatic(MethodNode method) {