Using StringUtils.isEmpty without losing compiler warnings about null pointers

Instead of the usual if (myString == null || myString.equals("")) I prefer to use the org.apache.commons.lang.StringUtils class and do if (StringUtils.isEmpty(myString)) .

However, this, at least the way I do it, has a gigantic flaw: because FindBugs is either a compiler warning mechanism, f. ex. from Eclipse - will no longer see an explicit check for a null value, it will no longer consider myString be potentially null, so it will no longer trigger warnings about potential (or true) null pointers on it, and these warnings are extremely useful in my opinion.

Example (ADDED):

 import org.apache.commons.lang.StringUtils; public class TestWarning { void testWarning(String myString) { //if (myString == null || myString.equals("")) // With this, the last line shows the warning. if (StringUtils.isEmpty(myString)) // With this, no warning. { // Anything. } int x = myString.length(); // Warning is here: "Potential null pointer access: The variable myString may be null at this location" } } 

So I'm just trying to make sure that there is no way to eliminate or minimize this drawback so that you can use StringUtils.isEmpty and still get null pointer warnings. Maybe some kind of annotation, for example @Nullcheck , should be added to the isEmpty method or something else?

ADDED: for example, it would be possible to create a custom annotation, for example @Nullcheck , add it to arg isEmpty as public static boolean isEmpty(@Nullcheck String str) to indicate that the method performs a null check of this argument, and make the warning mechanism the compiler or FindBugs treated if (StringUtils.isEmpty(myString)) in the same way as explicit null checking?

+6
source share
3 answers

If you annotate myString using @Nullable, i.e.

 void testWarning(@Nullable String myString) 

then at least the eclipse will give a warning when you spot it.

I agree that FindBugs should also be warned about this, but I cannot get him to do this.

+1
source

Whether you get a potential null pointer warning really depends on how good a static code analyzer is. Logically, if you use

 if (myString == null || myString.equals("")) 

your code expects myString to be null. On the next line, however, you are looking for myString. The static code analyzer sees these two facts and generates a warning with a null pointer.

If you use

 if (StringUtils.isEmpty(myString)) 

your code does not say if myString expects null. Thus, the static code analyzer does not generate a null pointer warning. A static code analyzer can certainly choose to create a warning with a null pointer, but this will lead to a lot of false positives, since it needs to assume that the input data for any method can be null.

+1
source

Eclipse code analysis can also evaluate external null annotations (starting from 4.5.0). More information can be found at https://wiki.eclipse.org/JDT_Core/Null_Analysis/External_Annotations .

+1
source

Source: https://habr.com/ru/post/970380/


All Articles