This is similar but not quite the same as Java: instantiating an enum using reflection
I have a Map<Enum<?>, FooHandler>
that I want to use to map Enum
s (I don't care which type or even if they are the same type, just as long as they are enum constants) to my FooHandler
class.
I would like to populate this map using a text file that I read. I can get it to work, but I have two warnings I would like to get around:
static private <E extends Enum<E>> E getEnum(String enumFullName) {
// see https://stackoverflow.com/questions/4545937/
String[] x = enumFullName.split("\.(?=[^\.]+$)");
if (x.length == 2)
{
String enumClassName = x[0];
String enumName = x[1];
try {
Class<E> cl = (Class<E>)Class.forName(enumClassName);
// #1
return Enum.valueOf(cl, enumName);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
public void someMethod(String enumName, String fooHandlerName)
{
FooHandler fooHandler = getFooHandler(fooHandlerName);
Enum e = getEnum(enumName);
// #2
map.put(e, fooHandler);
}
Warning #1: unchecked cast
Warning #2: Enum is a raw type.
I get #1 and could just put a warning I suppose, but I can't seem to beat warning #2; I've tried Enum<?>
and that just gives me an error about generic type capture bound mismatch.
Alternative implementations that are worse:
Before my <E extends Enum<E>>
generic return value, I tried returning Enum and it didn't work; I got these warnings/errors:
static private Enum<?> getEnum(String enumFullName) {
...
Class<?> cl = (Class<?>)Class.forName(enumClassName);
// 1
return Enum.valueOf(cl, enumName);
// 2
}
warnings:
- Type safety: Unchecked cast from Class<capture#3-of ?> to Class<Enum>
- Enum is a raw type. References to generic type Enum<E> should be parameterized
- Enum is a raw type. References to generic type Enum<E> should be parameterized
- Unnecessary cast from Class<capture#3-of ?> to Class<?>
errors:
- Type mismatch: cannot convert from capture#5-of ? to Enum<?>
- Type safety: Unchecked invocation valueOf(Class<Enum>, String) of the generic method
valueOf(Class<T>, String) of type Enum
- Bound mismatch: The generic method valueOf(Class<T>, String) of type Enum<E> is not
applicable for the arguments (Class<capture#5-of ?>, String). The inferred type capture#5-of ? is not
a valid substitute for the bounded parameter <T extends Enum<T>>
and this:
static private Enum<?> getEnum(String enumFullName) {
...
Class<Enum<?>> cl = (Class<Enum<?>>)Class.forName(enumClassName);
// 1
return Enum.valueOf(cl, enumName);
// 2
- warning:
Type safety: Unchecked cast from Class<capture#3-of ?> to Class<Enum<?>>
- error:
Bound mismatch: The generic method valueOf(Class<T>, String) of type Enum<E> is not applicable for the arguments (Class<Enum<?>>, String). The inferred type Enum<?> is not a valid substitute for the bounded parameter <T extends Enum<T>>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…