java 当枚举类型引用是 Class<?> 时,如何将 String 转换为枚举值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17051495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert String to enum value when enum type reference is a Class<?>?
提问by Sam Goldberg
I have code which is setting values on an object using its setter methods. One of the setters takes an Enum type as the method parameter. The code looks something like this:
我有使用 setter 方法在对象上设置值的代码。其中一个 setter 将 Enum 类型作为方法参数。代码如下所示:
String value = "EnumValue1";
Method setter = getBeanWriteMethod("setMyEnumValue");
Class<?> type = setter.getParameterTypes()[0];
Object convertedValue = null;
if (type.isEnum()) {
convertedValue = convertToEnum(value, type);
} else {
convertedValue = ClassUtils.convertType(value, type);
}
return convertedValue;
The question is what to put in the convertToEnum
method. I know I could "brute force it" by iterating the enum constants (or the fields) of the type
object, matching the value. Am I overlooking a simpler way to do it using Reflection? (I looked at several examples, but didn't find any where the enum was only know via Class).
问题是在convertToEnum
方法中放什么。我知道我可以通过迭代对象的枚举常量(或字段)type
,匹配值来“暴力破解” 。我是否忽略了使用反射的更简单方法?(我查看了几个示例,但没有找到任何只能通过 Class 知道枚举的地方)。
回答by Adrian Pronk
Off the top of my head:
在我的头顶:
Enum<?> convertedValue = Enum.valueOf((Class<Enum>)type, value);
This will convert a string to an enum constant of the Enum class of type
这会将字符串转换为 Enum 类型的枚举常量
Edit: Now that I have a computer handy, I can see what actually works. Either of the below cases ran correctly without compiler warnings:
编辑:现在我手边有一台电脑,我可以看到实际工作的内容。以下任何一种情况都可以正确运行而没有编译器警告:
Enum<?> convertedValueA = Enum.valueOf(type, value);
Enum<?> convertedValueB = Enum.valueOf(type.asSubclass(Enum.class), value);
The second one calls asSubClass() which would do a runtime check to ensure that type
is some enum class, but the valueOf()
method has to make that check anyway in order to work correctly.
第二个调用 asSubClass() ,它会进行运行时检查以确保它type
是某个枚举类,但该valueOf()
方法无论如何都必须进行该检查才能正常工作。
The following gave me a compile error:
以下给了我一个编译错误:
Enum<?> convertedValueC = Enum.valueOf((Class<? extends Enum <?>>)type, value);
java: Foo.java:22: <T>valueOf(java.lang.Class<T>,java.lang.String) in java.lang.Enum cannot be applied to (java.lang.Class<capture#134 of ? extends java.lang.Enum<?>>,java.lang.String)
The intricacies of casting to wildcard types confuses me so I've probably tried the wrong cast. Plus the fact that it has no runtime effect means that it's easy to get it wrong and never find out.
转换为通配符类型的复杂性让我感到困惑,所以我可能尝试了错误的转换。加上它没有运行时效应的事实意味着它很容易出错并且永远不会被发现。
回答by Peter Lawrey
You can use
您可以使用
Class enumType = ....
String name = ....
Enum e = Enum.valueOf(enumType, name);
e.g.
例如
import java.lang.annotation.*;
public class Main {
public static void main(String[] args) {
Class enumType = RetentionPolicy.class;
String name = "SOURCE";
Enum e = Enum.valueOf(enumType, name);
System.out.println(e);
}
}
prints
印刷
SOURCE
回答by NimChimpsky
Enum.valueOf(yrEnum, myString)
Returns the enum where myString is the name of the enum instance you want.
And yrEnum.values()
returns an array of all the different possible Enums instances.
返回枚举,其中 myString 是您想要的枚举实例的名称。并yrEnum.values()
返回所有不同的可能 Enums 实例的数组。