java:反射获得枚举 [英] java: reflection to obtain an Enum
问题描述
我有一个 Map< Enum<?>,FooHandler>
映射枚举
s(我不在乎哪个类型,即使它们是同一类型,只要它们是枚举常量)到我的 FooHandler
class。
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);
}
警告#1:取消选中cast
警告#2:枚举是一个原始的类型。
Warning #1: unchecked cast Warning #2: Enum is a raw type.
我得到#1,可以只是一个警告我想,但我似乎不能打2号警告#我试过枚举<?>
,这只是给我一个关于泛型类型捕获绑定不匹配的错误。
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.
更糟糕的替代实现:
在我的之前,E扩展枚举< E>
通用返回值我尝试返回Enum,它没有工作;我收到这些警告/错误:
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>>
而这个:
static private Enum<?> getEnum(String enumFullName) {
...
Class<Enum<?>> cl = (Class<Enum<?>>)Class.forName(enumClassName);
// 1
return Enum.valueOf(cl, enumName);
// 2
- 警告:
类型安全性:来自类< capture#3-of?>的未经检查的投射类
- 错误:
绑定不匹配:通用方法valueOf(Class< T>,String )类型为Enum< E>不适用于参数(类< Enum<?>>,String)。推断类型Enum<>不是有限参数的有效替代< T extends Enum< T>
- 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>>
推荐答案
对于#1,除 SuppressWarnings(unchecked)
之外,没有解决方案。
For #1 there's no solution except SuppressWarnings("unchecked")
.
对于#2,声明有一个问题:
For #2 there's a problem with the declaration:
static private <E extends Enum<E>> E getEnum(String enumFullName)
您可以返回 E
,但编译器无法确定 E
。没有类型 E
或 Class< E>
或任何可允许的参数。你可以写它,但是在某个地方会有一个未被检查的演员,当你调用它时,你可能会得到一个 ClassCastException
。所以不要这样做。
You can return E
, but there's no way for the compiler to determine E
. There's no argument of type E
or Class<E>
or whatever, which would allow it. You can write it, but there'll be an unchecked cast somewhere and when you call it, you may get a ClassCastException
. So don't do it.
只需将其更改为
static private Enum<?> getEnum(String enumFullName)
因为这将工作,更公平。您会在每个通话网站上收到警告,这是正确的,因为有一些警告。
as this will work and is more fair. You'll get a warning on each call site and that's correct as there is something to warn of.
这篇关于java:反射获得枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!