Eclipse null分析:int类型的表达式需要未经检查的转换才能符合“@Nonnull Integer” [英] Eclipse null analysis: The expression of type int needs unchecked conversion to conform to '@Nonnull Integer'

查看:299
本文介绍了Eclipse null分析:int类型的表达式需要未经检查的转换才能符合“@Nonnull Integer”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当配置Eclipse 4.2.0执行空分析(配置为使用 @ javax.annotation.Nonnull 等)时,以下代码将生成警告


空类型安全性:int类型的表达式需要取消选中
转换才能符合@Nonnull Integer




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

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

我应该如何解决这个问题(不使用 @SuppressWarnings(null))?看来分析器不知道盒装的原语不能是 null

解决方案

我认为这是Eclipse中的一个bug。我尝试了与IntelliJ相同的功能,它的工作原理如预期那样。



当我传递一个Null时,IntelliJ正确地告诉我null是不允许的。 b
$ b



但是,当我使用普通的i变量时,没有弹出错误,这是非常正确的。



I甚至尝试这个,只是为了实验,所以我不知道你是否坚持使用Eclipse,否则我建议使用免费的社区版本,或终极版版本




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.

这篇关于Eclipse null分析:int类型的表达式需要未经检查的转换才能符合“@Nonnull Integer”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆