如何处理不安全的空类型转换

huangapple 未分类评论56阅读模式
英文:

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&lt;Currencies, Currency&gt; currencies = new EnumMap&lt;&gt;(Currencies.class);

But Eclipse generates a warning:

Unsafe null type conversion (type annotations):
The value of type &#39;@NonNull EnumMap&lt;TestDataServiceImpl.@NonNull Currencies,Currency&gt;&#39;
is made accessible using the less-annotated type &#39;Map&lt;TestDataServiceImpl.Currencies,Currency&gt;&#39;,
corresponding supertype is &#39;Map&lt;TestDataServiceImpl.@NonNull Currencies,Currency&gt;&#39;

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?

huangapple
  • 本文由 发表于 2020年5月30日 09:21:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/62096730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定