IntelliJ IDEA 2017.2 EAP, with Smarter Control Flow Analysis
Earlier we mentioned that IntelliJ IDEA 2017.2 EAP features improved control flow analysis that infers the contracts of certain methods of String, Array, Collection and Map classes.
With today’s EAP build, this analysis becomes even smarter. If that sounds like something you might be interested in, read on and see how the new checks are going to work.
In our first example, Collection#isEmpty() returns true, which means that there are no characters in that string to iterate over, and IntelliJ IDEA is here to tell us that.
There’s more to this case, though. String#charAt(int index) will throw an exception when called on an empty string, or when index exceeds the value returned by String#length() method, so you can see that IntelliJ IDEA insight into that method comes handy in many cases.
The same intelligence applies to: String#isEmpty(), String#chatAt(int index), List$#get(int index), Set#contains(Object item), Map#containsKey(Object key), Map#containsValue(Object value), and many other methods.
Code analysis that reports nullability issues has also been improved, in particular when it comes to working with Collection and Map type parameters.
When you are trying to pass a collection that may contain null values where a collection with non-nullable items is expected, you get a warning that you probably shouldn’t:
Just the same way IntelliJ IDEA will make sure you’re safe when iterating over a collection that may contain null values:
When you are trying to check whether an expression is equal to one of several string literals, IntelliJ IDEA can help you to replace these checks with a Set#contains(Object key) call to make code more concise:
This new build has two improvements specifically aimed to help with migration to Java 9.
There is already an inspection that reports when you spin in a while loop on a non-volatile field, but now, if you’re using Java 9, IntelliJ IDEA suggests that you called Thread#onSpinWait() inside such a loop:
Doing so may significantly improve performance of your code.
Another Java 9 inspection suggests to use Set#of(String…items) where possible:
Last, but not least, you can pause file indexing if you need to free CPU to do other tasks, and then resume it again when it’s appropriate:
That’s it for now. We hope you’ll enjoy these improvements, and give the new build a try. You can download it right from the website, or update via Toolbox App. Patches for the in-product update should also be available if you’re using previous EAP build.
Develop with Pleasure!
Subscribe to Blog updates
Thanks, we've got you!