简体   繁体   中英

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

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 .

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 .

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM