Java: instantiating an enum using reflection

前端 未结 5 1486
甜味超标
甜味超标 2020-12-08 09:07

Suppose you have a text file like:

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

That you wish to to update a corresponding

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 10:09

    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
    }
    

提交回复
热议问题