Google-guava checkNotNull和IntelliJ IDEA的"可能产生java.lang.NullPointerException"

Seb*_*ian 35 intellij-idea nullpointerexception inspection guava

有没有办法抑制此警告:

MyClass object = null;

/*Some code that 'might' set this object but I know it will*/      


Preconditions.checkNotNull(object); 
//when "assert object != null" is used here no warning is shown

merged.setName(dRElement.getName());
//"May produce 'java.lang.NullPointerException'" warning here 
Run Code Online (Sandbox Code Playgroud)

我正在使用IntelliJ IDEA 10.5,我知道这个警告是不必要的,但是我想在这里压制它并避免切换检查.

Mar*_*ers 32

通过@Contract注释和外部注释功能的组合,您现在可以注释Preconditions方法,以便IntelliJ将正确的静态分析应用于对这些方法的调用.

假设我们有这个例子

public void doSomething(Object someArg) {
    Preconditions.checkArgument(someArg != null);
    someArg.doSomethingElse();  //currently gives NPE warning

    if (someArg != null) {
        //no warning that this is always true
    }
}
Run Code Online (Sandbox Code Playgroud)

在IntelliJ(我使用13):

  • 导航到Preconditions.checkArgument(boolean).
  • 将光标放在方法名称上,然后点击Alt- Enter显示意图弹出窗口.
  • 选择"添加方法合同".
  • 使用合同文本false -> fail.
  • 出现提示时,提供External Annotations文件的位置.

现在警告someArg.doSomethingElse()消失了,事实上,IDEA将标志着if分支一如既往!

其他合同文本:

  • Preconditions.checkArgument(boolean, String) 应该 false, _ -> fail
  • Preconditions.checkNotNull(Object, String)应该是null, _ -> fail,
  • 等等

这是我的完整annotations.xml档案Preconditions:

<root>
    <item name='com.google.common.base.Preconditions T checkNotNull(T)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;null -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions T checkNotNull(T, java.lang.Object)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;null, _ -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions T checkNotNull(T, java.lang.String, java.lang.Object...)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;null, _, _ -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions void checkArgument(boolean)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;false -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions void checkArgument(boolean, java.lang.Object)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;false, _ -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions void checkArgument(boolean, java.lang.String, java.lang.Object...)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;false, _, _ -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions void checkState(boolean)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;false -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions void checkState(boolean, java.lang.Object)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;false, _ -&gt; fail&quot;"/>
        </annotation>
    </item>
    <item name='com.google.common.base.Preconditions void checkState(boolean, java.lang.String, java.lang.Object...)'>
        <annotation name='org.jetbrains.annotations.Contract'>
            <val val="&quot;false, _, _ -&gt; fail&quot;"/>
        </annotation>
    </item>
</root>
Run Code Online (Sandbox Code Playgroud)

也可以看看


Joh*_*nny 15

JetBrains Yourtrack中存在一个旧问题,即添加此类功能.我几年前投票支持它,但我没有看到任何活动.如果每个人都投票,那么我们可能会很幸运.

澄清问题包括添加功能,以便您可以将方法标记为执行某种类型的空检查.如果发生这种情况,那么您可以为Preconditions方法编写自己的包装器并对其进行注释.

更新 我厌倦了等待功能,所以我自己提交了一个补丁.它在12.1.1 build 129.239中提供.要访问配置,请执行以下操作:设置>检查>可能的错误>常量条件和例外>配置断言/检查方法.