Many of the features we’re going to talk about in this post have been mentioned elsewhere, but here we’re going to bring them all together and show how to use them.
You’ll have seen before that IntelliJ IDEA can analyze your code and tell you if something isn’t needed, or perhaps doesn’t make sense. IntelliJ IDEA 2017.2 has even better analysis in several areas. The first we’re going to look at is Control Flow Analysis – IntelliJ IDEA can look at the logic in your code and tell you if certain cases just won’t occur.
For example, if a Collection or String is known to be empty, IntelliJ IDEA will tell you that certain types of operations will error or simply aren’t required, like iterating over an empty collection.
This applies to a number of methods on List, Map and Set, as well as String.
There’s also improved analysis around nulls, particularly when passing Collections as parameters. If, for example, you have a Collection that can contain nulls and you use it when calling a method that expects a Collection that does not contain nulls, you’ll get a warning.
See the blog post on Smarter Flow Control Analysis for more details on the analysis of empty Collections/Strings and potential null problems.
Similarly, IntelliJ IDEA 2017.2 can also detect if a number is going to be out of range and warn you about this before your program errors at run time.
See the section on Smarter control flow analysis: number ranges in this blog post for more details.
The Reflection API has always had its risks, which is one of the reasons Java 9 modularity (JPMS) aims to provide better encapsulation. IntelliJ IDEA now has more help for working with reflection if you do need to use it, with coding assistance and added warnings.
For more information see the Reflection API section in this blog post.
More Advanced Refactoring
As well as improved warnings that can spot bugs before running the code, IntelliJ IDEA 2017.2 also helps you to refactor existing code to simplify it or to make it more readable. For example, if you have a statement that contains a number of
or clauses, this can be converted into a
Set.contains statement – new values can then easily be added to the Set, rather than added to a long and difficult to read condition.
IntelliJ IDEA continues to add refactoring and suggestions to help you use the features in Java 8. Now, if you extract a variable in a Stream operation, the IDE can suggest this is extracted as an additional map operation rather than adding an extra variable to the lambda expression.
Previously, you could extract functional parameters, letting you change a method to accept a lambda expression as a parameter. Now you can also extract functional variables, so you reuse can some of these lambda expressions, or as a stepping stone to a larger refactoring, or maybe simply to create more readable code.
The last Java 8 refactoring to mention is the ability to reshape your code to use method references immediately. You may previously have used something like a combination of Extract Method and the “Lambda can be replaced with method reference” inspection in order to achieve this, now you can cut out a number of steps and extract to a method reference directly.
Better Code Completion
Code completion has also been improved in IntelliJ IDEA 2017.2, smart completion can suggest much more than before. For example, if your code uses the classic builder pattern to create an instance (rather than calling
new directly), smart completion will suggest using the builder when values of that type are required.
When you select this option, the caret will automatically be placed into the correct position to set more values via the builder.
For more information see the Smart completion: builder classes section of this blog post.
Smart completion is also better at suggesting appropriate method chains in certain situations – if, for example, you frequently call a chain of methods this is now suggested on the first call to smart completion, whereas it was previously only suggested on the second call.
This feature relies on the project being indexed, and having been built by IntelliJ IDEA (not the Gradle compiler).
See the Smart Completion blog post for more information on this feature for chained method calls.
Notable Bug Fixes
Of course, releases aren’t just about new features, they’re also about improving the quality of the tool. Here’s a summary of the most notable bug fixes that went into 2017.2.
- The project window sometimes didn’t close on Windows IDEA-171010
- The editor tabs limit didn’t work properly when set to 1 (2017.2.1) IDEA-173369
- The project window didn’t properly restored in a maximised state on Windows IDEA-96168
- When re-importing a Maven project, the IDE didn’t respect the language level IDEA-115977
- The active editor/tool window tab was difficult to distinguish from a non-active IDEA-131683 and IDEA-103206
- Per-monitor HiDPI scale-factor on Windows IDEA-164566
- Automatically adjusted font size based on the HiDPI scale factor IDEA-151754