[英]pass generic type enum as parameter (java)
i have written the following method 我写了以下方法
@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");
}
}
}
How can i pass different enum types to the second argument? 如何将不同的枚举类型传递给第二个参数? I know i coulnd't create an instance of an enum, but initialising an enum means i will be passing a single value not the entire initialised enum as follows ... Other enums would also be passed to the same method for Combo specifics
我知道我不能创建枚举的实例,但是初始化枚举意味着我将传递单个值而不是整个初始化枚举,如下所示......其他枚举也将传递给Combo细节的相同方法
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;
}
}
You probably want to pass the class, and not an enum instance: 您可能想要传递该类,而不是枚举实例:
protected <E extends Enum<E>> void populateComboWithEnumValues(Combo combo, Class<E> enumClass, String defaultSelectionValue) {...}
Here you have an usage example: 这里有一个用法示例:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.