How to tell IDEA/Studio that the null check has been done?
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
:
The TextUtil
class with it's isEmpty
method:
And finally the main class calling the TextUtil#isEmpty
:
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:
Add a new IsNull
check method:
Enter the TextUtil
class, isEmpty
method and CharSequence
parameter:
This gives this Assert/Check Method Configuration
window:
Press Ok
and then Ok
again to go back to the editor view and you'll see that the inspection disappeared:
You are actually telling IntelliJ that the isEmpty
method is doing a null check on the str
parameter.
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();
}