How to tell IDEA/Studio that the null check has been done?

后端 未结 6 1919
我在风中等你
我在风中等你 2020-12-06 06:14

I\'m developing with Android Studio/IntelliJ IDEA.

I have enabled the inspection check called "Constant conditions & exceptions" that shows a warning if

相关标签:
6条回答
  • 2020-12-06 06:52

    Unfortunately marked as "right answer" solution is of date. But I found equivalent for me solution.

    The new versions of IDE work correctly with static methods. So the example from the question won't throw warning anymore.

    TextUtils#isEmpty(String);
    
    public static boolean isEmpty(CharSequence str) {
        // your checks
    }
    
    0 讨论(0)
  • 2020-12-06 06:54

    You could use //noinspection ConstantConditions that will remove the NPE warning for the following line, like this:

    String encoding = contentEncoding == null ? null : contentEncoding.getValue();
    
    //noinspection ConstantConditions
    if (!TextUtils.isEmpty(encoding) && encoding.equalsIgnoreCase("gzip")) {
        inputStream = new GZIPInputStream(entity.getContent());
    } else {
        inputStream = entity.getContent();
    }
    
    0 讨论(0)
  • 2020-12-06 06:56

    You can use @SuppressWarnings("ConstantConditions") annotation.

    @SuppressWarnings("ConstantConditions")
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int indexViewType) {
        if (inflater == null) {
            inflater = LayoutInflater.from(parent.getContext());
        }
        ItemViewProvider provider = getProviderByIndex(indexViewType);
        provider.adapter = MultiTypeAdapter.this;
        return provider.onCreateViewHolder(inflater, parent);
    }
    
    0 讨论(0)
  • 2020-12-06 07:08
    1. Select "TextUtils.isEmpty".
    2. Right Click -> Show Context Actions -> Add Method Contract.
    3. Enter "null -> true".
    4. Save the configuration xml.

    Please check the details here

    0 讨论(0)
  • 2020-12-06 07:10

    You can look into the link that Peter Gromov mention in his answer.

    Created some simple classes that resemble your setup:

    A class with a method annotated with @Nullable:

    enter image description here

    The TextUtil class with it's isEmpty method:

    enter image description here

    And finally the main class calling the TextUtil#isEmpty:

    enter image description here

    Now if you enter the File -> Settings... and go to Inspections ->Constant conditions & exceptions part you can change the Configure Assert/Check Methods to cater for your isEmpty method:

    enter image description here

    Add a new IsNull check method:

    enter image description here

    Enter the TextUtil class, isEmpty method and CharSequence parameter:

    enter image description here

    This gives this Assert/Check Method Configuration window:

    enter image description here

    Press Ok and then Ok again to go back to the editor view and you'll see that the inspection disappeared:

    enter image description here

    You are actually telling IntelliJ that the isEmpty method is doing a null check on the str parameter.

    0 讨论(0)
  • 2020-12-06 07:15

    See http://www.jetbrains.com/idea/webhelp/configuring-check-assert-methods.html for IDEA 12. In IDEA 13 EAP, you can add method contract: http://youtrack.jetbrains.com/issue/IDEA-93372

    0 讨论(0)
提交回复
热议问题