intellij-idea - 谷歌 Guava checkNotNull 和 IntelliJ IDEA 的 "may produce java.lang.NullPointerException"

标签 intellij-idea nullpointerexception guava inspection

有什么办法可以抑制这个警告:

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 

我正在使用 IntelliJ IDEA 10.5,我知道这个警告是不必要的,但是我想在这里抑制它并避免关闭检查。

最佳答案

通过@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
    }
}

在 IntelliJ 中(我使用的是 13):
  • 导航至 Preconditions.checkArgument(boolean) .
  • 将光标放在方法名称上,然后按 Alt-Enter 调出意图弹出窗口。
  • 选择“添加方法契约(Contract)”。
  • 使用契约(Contract)文本 false -> fail .
  • 出现提示时,提供外部注释文件的位置。

  • 现在警告在 someArg.doSomethingElse()消失了,实际上 IDEA 会标记 if分支一如既往的真实!

    其他契约(Contract)文本:
  • 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>
    

    也可以看看
  • IDEA-113391: Edit Method Contract intention for library methods
  • IDEA-93372: Implement something similar to ReSharper contract annotations for Java
  • IDEA-60343: False positive NPE warning when using guava Preconditions - Constant conditions & Expectations
  • 关于intellij-idea - 谷歌 Guava checkNotNull 和 IntelliJ IDEA 的 "may produce java.lang.NullPointerException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6598228/

    相关文章:

    Instrumentation.execStartActivity 中的 Android NullPointerException

    java - Java 中的空指针异常,不知道为什么

    java - Guava 库 : is Iterators. cycle() 线程安全?

    android - Junit 使用 ActivityInstrumentationTestCase2 - 构造函数 : Stub 中的异常

    android-studio - 在 Android Studio 中对书签进行分组

    intellij-idea - 为什么 IntelliJ 中的 SBT 可以通过 Internet 访问 Maven,尽管我为它配置了本地 Nexus?

    java - 为什么空指针异常不提供为空的表达式?

    java - 如何将 SimpleTimeLimiter (Guava) 与 Vaadin 一起使用

    scala - 构建自动化 - sbt : Compile/Test against multiple dependencies

    intellij-idea - 如何在 Gradle/IntelliJ 中添加测试资源根