java - 将通用类型枚举作为参数传递 (java)

标签 java enums

我写了下面的方法

@SuppressWarnings("unchecked")
protected <E extends Enum<E>> void populateComboWithEnumValues(Combo combo, E enumData, String defaultSelectionValue) {

    // populate commbo
    for (Enum<E> enumVal: enumData.getClass().getEnumConstants()) {  
        combo.add(enumVal.toString());
    }  

    // select default selection
    for (Enum<E> enumVal: enumData.getClass().getEnumConstants()) {  
        if(enumVal.toString().equals(defaultSelectionValue)) {
            try {
                combo.select((Integer) enumVal.getClass().getMethod("getSelectionIndex").invoke(enumVal));
            } catch (IllegalArgumentException e) {
                LOGGER.debug("an IllegalArgumentException exception occured");
            } catch (SecurityException e) {
                LOGGER.debug("an SecurityException exception occured");
            } catch (IllegalAccessException e) {
                LOGGER.debug("an IllegalAccessException exception occured");
            } catch (InvocationTargetException e) {
                LOGGER.debug("an InvocationTargetException exception occured");
            } catch (NoSuchMethodException e) {
                LOGGER.debug("an NoSuchMethodException exception occured");
            }
        }
    } 

如何将不同的枚举类型传递给第二个参数?我知道我无法创建枚举的实例,但初始化枚举意味着我将传递单个值而不是整个初始化的枚举,如下所示......其他枚举也将传递给相同的方法用于组合细节

public enum ServerEnvironmentName {

    /** 
     * The CFD environment name. 
     * Selection Index
     */
    CFD("CFD", 0),

    /** 
     * The PIT environment name. 
     * Selection Index
     */
    PIT("PIT", 1),

    /** 
     * The SIT environment name. 
     * Selection Index
     */
    SIT("SIT", 2),

    /** 
     * The DEV environment name. 
     * Selection Index
     */
    DEV("DEV", 3);

    /** The input string to identify the environment. */
    private String envURL;

    /** The index value for view selection.*/
    private int selectionIndex;

    /**
     * Enum constructor to initialise default values. 
     * 
     * @param selectionIndex index value for view selection
     * @param envURL input parameter for environment
     */
    ServerEnvironmentName(String envURL, int selectionIndex) {
        this.envURL = envURL;
        this.selectionIndex = selectionIndex;
    }

    /**
     * Getter for the envURL.
     * 
     * @return the environment string 
     */
    public String getEnvironmentUrl() {
        return envURL;
    }

    /**
     * This method returns the index of the enum value.
     * 
     * @return the selection index
     */
    public int getSelectionIndex() {
        return selectionIndex;
    }
}

最佳答案

您可能想要传递类,而不是枚举实例:

protected <E extends Enum<E>> void populateComboWithEnumValues(Combo combo, Class<E> enumClass, String defaultSelectionValue) {...}

这里有一个用法示例:

public class EnumTest {

    protected static <E extends Enum<E>> void enumValues(Class<E> enumData) {
        for (Enum<E> enumVal: enumData.getEnumConstants()) {  
            System.out.println(enumVal.toString());
        }  
    }

    public static enum TestEnum {
        ONE, TWO, THREE;
    }

    public static void main(String param [] ) {
        EnumTest.enumValues(EnumTest.TestEnum.class);
    }
}

关于java - 将通用类型枚举作为参数传递 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11724442/

相关文章:

java - 逆向递归java方法

java - net.ucanaccess.jdbc.UcanaccessSQLException 游标状态无效

java - 使用 Dropbox Java API 将文件上传到 Dropbox

java - 当消息发送到 Azure 事件中心主题时,垃圾值添加到 header

java - 如何从枚举中提取元素而不重复

java - 获取异常 : IOException input buffer is closed exception while extracting a tar file

ios - Swift 枚举作为函数中的参数

c# - NHibernate:具有通用枚举属性的映射类

swift - 是否有可能在 Swift 的枚举中将多个案例分组并表示为另一个案例?

c - C语言中如何打印enum中的所有内容?