IntelliJ IDEA 2021.1
Tips to get you productive with the new or improved features in IntelliJ IDEA 2021.1
There's a new release of IntelliJ IDEA three times a year. See the highlights of the current release with this list of tips, or see What's New for more detailed information.
Use a lightweight inspection profile on commit
Fully customise the checks you must perform before you commit your changes to version control
You can select an inspection profile for running analysis before commit. Using an inspection profile with a much smaller number of checks, for example only Java errors and warnings, can run much faster than our full inspection profile and prevent us from committing breaking changes.
Data flow analysis
Data flow analysis can show us potential errors in our logic
Data Flow Analysis is a powerful tool that shows you how data moves through your code. It can locate places where we could have errors in our code. For example, if we use a negative number for initialising an array, or checking for negative values where the value couldn't be negative. Use Code | Analyze Code | Data Flow to Here or Code | Analyze Code | Data Flow from Here to analyze upstream or downstream data flow**.
Preview HTML updates in real time
See what your changes look like as you make them
IntelliJ IDEA comes with a built-in preview for HTML, so we can get a good idea of what our changes will look like as we're making them.
Evaluate JSON Path expressions
Use IntelliJ IDEA to check if your JSON Path expression are correct.
IntelliJ IDEA comes with support for JSONPath expressions. You can open up a window to evaluate JSONPath expressions by going to Edit -> Find -> "Evaluate JSONPath Expression...". If a JSON file is open, it will use this file to evaluate the expression.
If you have JSONPath expressions as Strings in code, use "inject language" and say this is a JSONPath expression. You'll get code formatting and completion inside this string.
You can evaluate a JSONPath expression to check it. This will open up a tool window: enter a snippet of JSON into the input and try out if an expression finds the expected path.
Prevent a package depending upon another
Enforce architecture rules, such as data access layers not depending upon UI layers.
Kotlin developers can configure an inspection to define illegal package dependencies, which can prevent code from accidentally depending upon the wrong packages, for example the UI layer talking directly to the data access layer.
Generate UML diagrams for your Kotlin code
JVM developers working with Kotlin code can create UML diagrams for their Kotlin classes, just like they could with Java classes
IntelliJ IDEA Ultimate supports creating and maintaining UML diagrams for Java code. This is now also supported for Kotlin classes, so if you're working in a polyglot environment you can comfortably use diagrams whether you're writing Java or Kotlin code.
Run your code with Docker, SSH or WSL
Set a Run Target to change where your code runs
You can configure a Run Target of Docker, SSH or WSL for Java unit tests, Maven and Gradle projects, and for Spring Boot, Micronaut and Quarkus applications.
This means you can, for example, run Spring Boot applications with a Docker target.
Profile your application
See how your application is performing with the built-in profiler
Access the profiler window using Find Action, ⌘⇧A (macOS) / Ctrl+Shift+A, or via the quick access buttons, and it shows a list of running processes
Click on a process to attach a profiler to it, for example a CPU profiler. The IDE will show the results, for example as a Flame Graph.
Click on a process in the profiler window to edit the configurations of the profilers.
Right-click on a process in the profiler window to capture a memory snapshot at this moment in time. Open this to see which objects are taking up the most space on the heap.
Right-click on a process in the profiler window to see the CPU and Memory use in real time. This is really helpful for getting a current view on what's happening in the application right now.
Find out more about the profiler in IntelliJ IDEA's help documentation.
Change your code font weight
We spend hours looking at our code. We can make this as pleasant as possible.
You can change the font weights for your code. In settings or preferences, ⌘, (macOS) / Ctrl+Alt+S (Windows/Linux), go to Editor -> Font, choose a different weight for the main code, and for the bolder sections. There's a preview area on the right so you can see the effect of your changes.
Tips for working with JetBrains Space
Space is an all-in-one collaboration tool, including chat, code repository, code review, issue tracking, and much more.
The plugin for JetBrains Space, a new team collaboration tool, is bundled in IntelliJ IDEA. Log in to the team instance to access your projects.
If you're logged in to Space, you can clone a repo via "Get from VCS", and you can configure where the code is cloned to.
If you're working with a Space project, you can right click on a commit in the Git log to open it in Space.
See more tips and tricks for working with Space in our Space Guide.
Code Reviews with Space
Perform code reviews without leaving the IDE.
The bundled plugin for JetBrains Space is really helpful for code reviews. You can perform the whole review, as an author or reviewer, inside the IDE.
Open the Space Code Reviews tool window to see a list of reviews that need your attention. There are a number of pre-set searches here, for example you can look at all the closed reviews.
You can see the review title, the authors and reviewers. You can see the files that were changed. The editor shows the history of the review, with the commits that were added, the changes to the review, any commit-level comments, any line-level comments, and the status of the discussions.
You can make changes to the review in the editor, and reply to anything that needs your attention. Space code reviews are shaped like a series of conversations, so you can start threads on anything in the history, or "chat" any of your thoughts.
There's a more detailed walk through of code reviews on the blog, and you can see more tips and tricks for working with Space in the Space Guide.
Create Record
Create a new Java record class
Create a new record by pressing ⌘N (macOS) / Alt+Insert (Windows/Linux) on the Project Window. From the New Java Class dialog, type the name of the record and select the Record type.
Convert class to record
Use inspections to convert classes to Java 16 records
IntelliJ IDEA can identify classes that are data holders and can be converted to records. The class will be highlighted with a warning (yellow underline) - press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and IntelliJ IDEA will suggest converting the class to a record.
The fields will be become record components, the constructor and accessors will be removed. If we had equals, hashCode and toString methods on our class, we can optionally remove these and rely on the default record implementation.
Convert from Record
Use quick intentions to convert a record to a class
If we decide that maybe a record should be a full Java class, we can get IntelliJ IDEA to automatically convert a record into a Java class.
Press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) on the record name, and IntelliJ IDEA offers a suggestion to convert the record to a class.
Use Pattern Matching for instanceof
Inspections can guide us to use new Java features
If our code uses an instanceof followed by a cast, IntelliJ IDEA highlights these with a warning and suggests replacing this with pattern matching for instanceof. Note that this is only available if you're using a language level of Java 16 or above.
Press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and choose Replace with pattern variable.
public class PatternMatchingSwitch {
void outputValueInUpperCase(Object obj) {
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s);
}
}
}
You can press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and choose Replace 's' with pattern variable.
public class PatternMatchingSwitch {
void outputValueInUpperCase(Object obj) {
if (obj instanceof String s) {
System.out.println(s);
}
}
}
Identify and fix deprecated calls
IntelliJ IDEA will warn you if you're using deprecated methods, and they will be shown with a strikethrough if they're marked for removal
IntelliJ IDEA shows deprecated methods that are marked for removal with a red strikethrough. JDK 16 has marked the wrapper class constructors for removal, so you'll see these with a red strikethrough in IntelliJ IDEA.
Use new methods on the Streams API
Inspections can show us new features from Java 16
Java 16 introduced some new methods on the Streams API. IntelliJ IDEA shows us places that can use the new methods and can automatically convert the code to use these new methods.
The code will be highlighted in yellow as a warning. Press ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) and choose "Replace collect(toUnmodifiableList()) with toList()".
Turn on Preview Features to try new Java features
The latest versions of Java contain "Preview Features", features that are fully functional but require developer feedback. Try them out in IntelliJ IDEA.
If you want to use preview features in the latest versions of Java in IntelliJ IDEA, you need to set the language level to Preview.
Go to Project Structure, ⌘; (macOS) / Ctrl+Alt+Shift+S (Windows/Linux), make sure you have the correct JDK selected, and pick the "Preview" option from the Language Level dropdown.
Create a correct parent Sealed Class
Sealed types are a new idea for Java developers, let IntelliJ IDEA guide you in how to use them
Sealed classes (a preview feature in Java 16) are a way to take control of the inheritance hierarchy of our classes. If we have a sealed class like this one, IntelliJ IDEA will tell us we need to "permit" the child class so it can extend this parent. It will also automatically make changes if it can.
Experiment with Sealed Types, use Alt+Enter (Windows/Linux) to see and accept suggestions.
Create a correct child Sealed Class
Sealed types are a new idea for Java developers, let IntelliJ IDEA guide you in how to create child classes
Sealed classes (first preview feature in Java 16) are a way to take control of the inheritance hierarchy of our classes. IntelliJ IDEA can let us know about problems in the classes that extend sealed classes, and suggest solutions.
Experiment with Sealed Types, use Alt+Enter (Windows/Linux) to see and accept suggestions.