简体   繁体   中英

Dynamically Loading Both a Java Enum Class and an Enum Constant

I've seen many StackOverflow posts on how to dynamically get an enum's value given that you know the Enum class at compile time, but I'm trying to dynamically load the enum class and an enum value, and I'm not seeing how to do this.

I'm trying to do something like this:

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

If you know (or can figure out) the fully-qualified class name of the enumeration you want to load a value from, you can do this:

String enumClassName = "com.mycompany.MyEnum";
String enumConstant = "MyValue";

Class enumClass = Class.forName(enumClassName);
Object enumValue = Enum.valueOf(enumClass, enumConstant);

assert enumValue == MyEnum.MyValue; // passes

You can use reflection. Here is a short example:

public class Test {

    public enum TestEnum {

        MyEnumValue1,

        MyEnumValue2;
    }

    public static void main(String[] args) {
        try {
            Object enumValue = getEnumValue("test.Test$TestEnum", "MyEnumValue2");
            System.out.println(enumValue.getClass().getName() + " = "
                    + enumValue);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static Object getEnumValue(String enumClassName, String enumValueName)
            throws ClassNotFoundException, NoSuchMethodException,
            SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        Class<?> enumClass = Class.forName(enumClassName);
        Method valueOfMethod = enumClass.getDeclaredMethod("valueOf",
            String.class);
        return valueOfMethod.invoke(null, enumValueName);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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