简体   繁体   English

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

[英]Google-guava checkNotNull and IntelliJ IDEA's “may produce java.lang.NullPointerException”

Is there any way to suppress this warning:有没有办法抑制这个警告:

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 

I'm using IntelliJ IDEA 10.5 and I know this warning is unnecessary however I would like to suppress it just here and avoid switching inspections off.我正在使用 IntelliJ IDEA 10.5,我知道这个警告是不必要的,但是我想在这里压制它并避免关闭检查。

Through a combination of @Contract annotations and the External Annotations feature, you can now annotate Preconditions methods such that IntelliJ applies the correct static analysis to calls to these methods.通过@Contract注释和外部注释功能的组合,您现在可以注释Preconditions方法,以便 IntelliJ 将正确的 static 分析应用于对这些方法的调用。

Let's say we have this example假设我们有这个例子

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
    }
}

In IntelliJ (I'm using 13):在 IntelliJ 中(我使用的是 13):

  • Navigate to Preconditions.checkArgument(boolean) .导航到Preconditions.checkArgument(boolean)
  • Place your cursor on the method name, and hit Alt - Enter to bring up the intentions popup.将 cursor 放在方法名称上,然后按 Alt - Enter以弹出意图弹出窗口。
  • Select "Add method contract". Select“添加方法合同”。
  • Use the contract text false -> fail .使用合同文本false -> fail
  • When prompted, provide a location for the External Annotations file.出现提示时,提供外部注释文件的位置。

Now the warning at someArg.doSomethingElse() goes away, and IDEA will, in fact, flag the if branch as always true!现在someArg.doSomethingElse()的警告消失了,IDEA 实际上会将if分支标记为始终为真!

Other contract texts:其他合同文本:

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

Here is my full annotations.xml file for Preconditions :这是我的完整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>

See Also也可以看看

An old issue exists in JetBrains Yourtrack to add this type of functionality. JetBrains Yourtrack 中存在添加此类功能的老问题 I voted for it years ago, but I don't see an activity.几年前我投票支持它,但我没有看到任何活动。 If everyone votes for it, then we might get lucky.如果每个人都投票支持它,那么我们可能会很幸运。

To clarify the issue covers adding functionality so that you could tag a method as performing some type of null check.澄清问题包括添加功能,以便您可以将方法标记为执行某种类型的 null 检查。 If that happened then you could write your own wrapper for the Preconditions method and annotate it.如果发生这种情况,那么您可以为 Preconditions 方法编写自己的包装器并对其进行注释。

UPDATE I got tired of waiting for the functionality so I submitted a patch myself.更新我厌倦了等待功能,所以我自己提交了一个补丁。 It is available in 12.1.1 build 129.239.它在 12.1.1 版本 129.239 中可用。 To access the configuration: Settings > Inspections > Probable Bugs > Constant Conditions & Exceptions > Configure Assert/Check Methods.要访问配置:设置 > 检查 > 可能的错误 > 常量条件和异常 > 配置断言/检查方法。

Extract a method?提取方法?

private MyClass getMyClass() {
    /* This always returns an instance of MyClass, never null. */      
}

...

MyClass object = getMyClass();
Preconditions.checkNotNull(object);
merged.setName(object.getName());

You can also assign returned value from checkNotNull method, which is non-null: http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html#checkNotNull(T)您还可以从 checkNotNull 方法分配返回值,该值非空: http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/Preconditions.html# checkNotNull(T)

MyClass object = null;
object = Preconditions.checkNotNull(object); 
merged.setName(dRElement.getName());

From IntelliJ IDEA 14, you don't need to worry about this.从 IntelliJ IDEA 14 开始,您无需担心这一点。

@Nullable
String extractPrefix(@Nullable String url) {
    if (StringUtils.isEmpty(url)) return null;

    if (url.startsWith("jar://")) {
...

Earlier IntelliJ IDEA had no clue that code execution wouldn't even reach the “startsWith” call if the url is null because it didn't look inside StringUtils.isEmpty.早期的 IntelliJ IDEA 不知道如果 url 是 null,代码执行甚至不会到达“startsWith”调用,因为它没有查看 StringUtils.isEmpty 内部。 You could of course remove those yellow warnings in extractPrefix by looking inside isEmpty yourself and adding a contract “null -> true”, but that's so boring, That's exactly a job for computer, not you, and now IntelliJ IDEA does it automatically.您当然可以通过自己查看 isEmpty 并添加合同“null -> true”来删除 extractPrefix 中的那些黄色警告,但这太无聊了,这正是计算机的工作,而不是你,现在 IntelliJ IDEA 会自动完成。 looking at byte and source code.查看字节和源代码。

http://blog.jetbrains.com/idea/2014/10/automatic-notnullnullablecontract-inference-in-intellij-idea-14/ http://blog.jetbrains.com/idea/2014/10/automatic-notnullablecontract-inference-in-intellij-idea-14/

Apart from other options, you can try-除了其他选择,您可以尝试-

Suppress warnings at method level by using -通过使用 - 在方法级别抑制警告 -

@SuppressWarnings({"NullableProblems"})
public void someMethod(){
   ...
   ...
}

or suppress warning at statement level by using comment -或通过使用注释来抑制语句级别的警告 -

//noinspection NullableProblems
someMethodCallProducingNullWarning(null);

But before doing this - make sure that it doesn't really produce NPE但在这样做之前 - 确保它不会真正产生 NPE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ Idea mapstruct java:映射处理器内部错误:java.lang.NullPointerException - IntelliJ Idea mapstruct java: Internal error in the mapping processor: java.lang.NullPointerException IntelliJ错误:java.lang.NullPointerException - IntelliJ Error: java.lang.NullPointerException IntelliJ错误:java.lang.NullPointerException - IntelliJ error: java.lang.NullPointerException 对jar的IntelliJ给出了“java.lang.NullPointerException:Location is required”。 - IntelliJ to jar is giving “java.lang.NullPointerException: Location is required.” Intellij不编译Play 2.4应用程序:模块&#39;root&#39;生产:java.lang.NullPointerException - Intellij doesn't compile Play 2.4 apps: Module 'root' production: java.lang.NullPointerException Java Servlet java.lang.NullPointerException 在 doGet() - Java Servlet java.lang.NullPointerException at doGet() Spring 引导运行 java: java.lang.NullPointerException 没有错误描述,intellij Mac OS Catalina - Spring Boot run java: java.lang.NullPointerException without error description, intellij Mac OS Catalina 改进 Intellij 代码检查可能会产生 NullPointerException 警告 - Improve Intellij Code Inspection for may produce NullPointerException warnings Maven Guava依赖关系在IntelliJ IDEA中“无法解析符号&#39;google&#39;” - Maven Guava Dependency “Cannot resolve symbol 'google'” in IntelliJ IDEA IntelliJ IDEA中的Google Java格式 - Google java format in IntelliJ IDEA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM