Java:使用反射实例化枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3735927/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:06:29  来源:igfitidea点击:

Java: instantiating an enum using reflection

javareflectionenums

提问by dagnelies

Suppose you have a text file like:

假设您有一个文本文件,例如:

my_setting = ON
some_method = METHOD_A
verbosity = DEBUG
...

That you wish to to update a corresponding object accordingly:

您希望相应地更新相应的对象:

Setting my_setting = ON;
Method some_method = METHOD_A;
Verbosity verbosity = DEBUG;
...

Where all are different kind of enums.

所有都是不同类型的枚举。

I would like to have a generic way to instantiate the enum values. That is, at runtime using reflection, and without knowing the enum types of the object in advance.

我想要一种通用的方法来实例化枚举值。也就是说,在运行时使用反射,并且事先不知道对象的枚举类型。

I would have imagined something like this:

我会想象这样的事情:

for (ConfigLine line : lines)
{
   String[] tokens = line.string.split("=", 2);
   String name = tokens[0].trim();
   String value = tokens[1].trim();

   try
   {
      Field field = this.getClass().getDeclaredField(name);   
      if(field.getType().isEnum())
      {
         // doesn't work (cannot convert String to enum)
         field.set(this, value);
         // invalid code (some strange generics issue)
         field.set(this, Enum.valueOf(field.getType().getClass(), value));
      }
      else
      { /*...*/ }
   }
   catch //...
}

The question is: what should there be instead? Is it even possible to instantiate an unknown enum given its String representation?

问题是:取而代之的是什么?鉴于其字符串表示形式,甚至可以实例化未知枚举吗?

采纳答案by Bozho

field.set(this, Enum.valueOf((Class<Enum>) field.getType(), value));
  • getClass()after getType()should not be called - it returns the class of a Classinstance
  • You can cast Class<Enum>, to avoid generic problems, because you already know that the Classis an enum
  • getClass()aftergetType()不应该被调用 - 它返回一个Class实例的类
  • 您可以强制转换Class<Enum>, 以避免一般性问题,因为您已经知道Classenum

回答by Matthew Flaschen

You have an extra getClasscall, and you have to cast (more specific cast per Bozho):

你有一个额外的getClass电话,你必须施法(每个 Bozho 更具体的施法):

field.set(test, Enum.valueOf((Class<Enum>) field.getType(), value));

回答by ThiamTeck

You may code your Enum similar tho this:

您可以对您的 Enum 进行类似的编码:

public enum Setting {

    ON("ON"),OFF("OFF");

    private final String setting;

    private static final Map<String, Setting> stringToEnum = new ConcurrentHashMap<String, Setting>();
    static {
        for (Setting set: values()){
            stringToEnum.put(set.setting, set);
        }
    }

    private Setting(String setting) {
        this.setting = setting;
    }

    public String toString(){
        return this.setting;
    }

    public static RequestStatus fromString(String setting){
        return stringToEnum.get(setting);
    }   
}

Then you may easily create Enum from String without reflection:

然后您可以轻松地从 String 创建 Enum 而无需反射:

Setting my_settings = Setting.fromString("ON");

This solution is not originated from myself. I read it from somewhere else, but I can't recall the source.

这个解决方案不是来自我自己。我是从别处读到的,但我想不起来出处了。

回答by Rebzie

Alternative solution with no casting

没有铸造的替代解决方案

try {
    Method valueOf = field.getType().getMethod("valueOf", String.class);
    Object value = valueOf.invoke(null, param);
    field.set(test, value);
} catch ( ReflectiveOperationException e) {
    // handle error here
}

回答by Dev Vercer

The accepted answer results in warnings because it uses the raw type Enum instead of Enum<T extends Enum<T>>.

接受的答案会导致警告,因为它使用原始类型 Enum 而不是 Enum<T extends Enum<T>>。

To get around this you need to use a generic method like this:

要解决这个问题,您需要使用这样的通用方法:

@SuppressWarnings("unchecked")
private <T extends Enum<T>> T createEnumInstance(String name, Type type) {
  return Enum.valueOf((Class<T>) type, name);
}

Call it like this:

像这样调用它:

Enum<?> enum = createEnumInstance(name, field.getType());
field.set(this, enum);