简体   繁体   English

Java:使用反射实例化枚举

[英]Java: instantiating an enum using reflection

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? 是否有可能在给定String表示的情况下实例化一个未知的枚举?

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

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
}

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

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

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. 我是从其他地方读到的,但我记不起来了。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM