英文:
How to deal with Unsafe null type conversion
问题
我有一段代码,其中 "Currencies" 是枚举,"Currency" 是实体:
public final Map<Currencies, Currency> currencies = new EnumMap<>(Currencies.class);
但是 Eclipse 生成了一个警告:
Unsafe null type conversion (type annotations):
The value of type '@NonNull EnumMap<TestDataServiceImpl.@NonNull Currencies,Currency>'
is made accessible using the less-annotated type 'Map<TestDataServiceImpl.Currencies,Currency>',
corresponding supertype is 'Map<TestDataServiceImpl.@NonNull Currencies,Currency>'
处理它的正确方式是什么?我是否应该使用 Optional 包装我的 map?这是强制性的吗?我应该忽略这个警告吗?因为我知道不会有空的情况。
英文:
I have a part of a code where "Currencies" is Enum and "Currency" is Entity:
public final Map<Currencies, Currency> currencies = new EnumMap<>(Currencies.class);
But Eclipse generates a warning:
Unsafe null type conversion (type annotations):
The value of type '@NonNull EnumMap<TestDataServiceImpl.@NonNull Currencies,Currency>'
is made accessible using the less-annotated type 'Map<TestDataServiceImpl.Currencies,Currency>',
corresponding supertype is 'Map<TestDataServiceImpl.@NonNull Currencies,Currency>'
What is the right way to handle it? Should I wrap my map with Optional? Is it mandatory? Should I ignore the warning as I know there will be no null case?
专注分享java语言的经验与见解,让所有开发者获益!
评论