java 反射获取枚举类,java:反射获得枚举

I have a Map, FooHandler> that I want to use to map Enums (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 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 cl = (Class)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 > 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 to Class

- Enum is a raw type. References to generic type Enum should be parameterized

- Enum is a raw type. References to generic type Enum should be parameterized

- Unnecessary cast from Class to Class>

errors:

- Type mismatch: cannot convert from capture#5-of ? to Enum>

- Type safety: Unchecked invocation valueOf(Class, String) of the generic method

valueOf(Class, String) of type Enum

- Bound mismatch: The generic method valueOf(Class, String) of type Enum is not

applicable for the arguments (Class, String). The inferred type capture#5-of ? is not

a valid substitute for the bounded parameter >

and this:

static private Enum> getEnum(String enumFullName) {

...

Class> cl = (Class>)Class.forName(enumClassName);

// 1

return Enum.valueOf(cl, enumName);

// 2

warning: Type safety: Unchecked cast from Class to Class>

error: Bound mismatch: The generic method valueOf(Class, String) of type Enum is not applicable for the arguments (Class>, String). The inferred type Enum> is not a valid substitute for the bounded parameter >

解决方案

For #1 there's no solution except SuppressWarnings("unchecked").

For #2 there's a problem with the declaration:

static private > E getEnum(String enumFullName)

You can return E, but there's no way for the compiler to determine E. There's no argument of type E or Class 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.

Just change it into

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值