Eclipse null analysis: The expression of type int needs unchecked conversion to conform to '@Nonnull Integer'

删除回忆录丶 提交于 2019-12-22 05:38:42

问题


When configuring Eclipse 4.2.0 to perform a null analysis (configured to use @javax.annotation.Nonnull etc.), the following code will generate the warning

Null type safety: The expression of type int needs unchecked conversion to conform to '@Nonnull Integer'

class C
{
  static void foo(int i)
  {
    bar(i); // Warning
  }

  static void bar(@javax.annotation.Nonnull Integer i)
  {
  }
}

How am I supposed to fix this (without using @SuppressWarnings("null"))? It seems that the analyzer does not know that boxed primitives cannot be null.


回答1:


I think it's a bug in Eclipse. I tried the same with IntelliJ, and it works as expected there.

When I pass a Null, IntelliJ correctly tells me that nulls are not allowed.

However, when I use the normal i variable, no error pops ups up, which is quite correct.

I even tried this, just for the experiment, so I'm not sure if you're stuck to Eclipse, else I'd recommend using either the free Community version of IntelliJ, or the Ultimate edition.



来源:https://stackoverflow.com/questions/16241983/eclipse-null-analysis-the-expression-of-type-int-needs-unchecked-conversion-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!