How can I make IntelliJ IDEA understand my null-checking method?

跟風遠走 提交于 2019-12-10 13:15:56

问题


I have a method where a parameter is marked with the @Nonnull annotation. The code which calls the method has to check whether the value is null. Rather than just a straight x != null check, it is calling a utility method on another class. (In the real code, the utility method also checks whether it is a blank String).

My problem is that Intellij Idea is showing an inspection warning on the Nonnull method call, saying that my variable "might be null". I know it cannot be null because of the utility method check - how can I tell the inspector that?

Since that is a bit abstract, here's a minimal example of what I mean:

package org.ethelred.ideatest;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * tests annotations
 */
public class AnnotationChecker
{
    public static void main(String[] args)
    {
        String x = null;
        if(args.length > 0)
        {
            x = args[0];
        }

        if(!isNull(x))
        {
            useObject(x);
        }

        if(x != null)
        {
            useObject(x);
        }
    }

    public static boolean isNull(@CheckForNull Object o)
    {
        return o == null;
    }


    public static void useObject(@Nonnull Object o)
    {
        System.out.println(o);
    }
}

This uses the JSR 305 annotations.

In this example, in the first call to useObject Intellij puts a warning on the x parameter saying "Argument 'x' might be null". In the second call, there is no warning.


回答1:


I don't believe there's any way to resolve the warning with the code written as it is. I was hoping to find that IntelliJ supports a value for @SuppressWarnings that you could use on the useObject(x) statement, but according to this source it does not. You may just have to bite the bullet and change your code to something like the following:

if (x != null && !isBlank(x)) {
    useObject(x);
}

Notice that I renamed the method isNull to isBlank since it is my understanding that the actual method you're calling that does the check for null checks other conditions as well.




回答2:


In IDEA 13 very fancy feature was added, called Method Contracts. For example, you could have a method, that throws validation exception if it encounters null:

@Contract("null -> fail")
public static void validateNull(@Nullable final Object object) {
    if (object == null) {
        throw new ValidationException();
    }
}

IDEA will analyze the annotation and won't show up warnings, if you call it before possible NPE:

validateNull(user);
user.setSomething("something"); // no warning

You have full documentation in IDEA's settings (just search for Method Contract). For this to work you need additional dependency on jetbrain's annotations jar:

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>13.0</version>
</dependency>



回答3:


With IDEA 12 you can configure the NotNull-check methods: http://youtrack.jetbrains.com/issue/IDEA-35808#tab=Comments




回答4:


I've dealt with this issue by using a static method to whitelist object calls:

/** Wrapper to mask NullPointerException warnings. */
private static <T> T _(T obj) {

    return obj;
}

The method name is intended to not interfere with readability. Thus,

mObject.method();

becomes

_(mObject).method();

Furthermore, if a NullPointerException does occur it will still refer to the same line number.



来源:https://stackoverflow.com/questions/10636483/how-can-i-make-intellij-idea-understand-my-null-checking-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!