我已经看到许多StackOverflow帖子关于如何动态获取枚举的值,因为你知道编译时Enum类,但我试图动态加载枚举类和枚举值,我没有看到如何做到这一点。动态加载Java枚举类和枚举常量
我试图做这样的事情:
public interface Config {
public ConfigValue getConfigValue();
}
public enum AwsConfig implements Config {
ACCESS_KEY_ID(new ConfigValue(...)),
API_URL(new ConfigValue(...))
...
public static String getName() { return "AwsConfig"; }
...
}
public enum PaginatorConfig implements Config {
DEFAULT_PAGE_SIZE(new ConfigValue(...)),
...
public static String getName() { return "PaginatorConfig"; }
...
}
public void myMethod(ConfigValue configValue) {
String enumClassName = configValue.getName();
String enumConstantKeyName = configValue.getKey();
// I now have the enum's class name as enumClassName
// I now have the enum constant's name as enumConstantKeyName
// How can I dynamically get the enum constant's configValue based on the enumClassName and enumConstantKeyName?
// I know I can get the value dynamically if I know the Enum constant...
ConfigValue configValue = AwsConfig.valueOf("API_URL")
// But I wish I could do something like this...
ConfigValue configValue = Enum.valueOf("AwsConfig", "API_URL");
}
所以你想使用反射? – msrd0 2014-09-19 16:48:34