Better Control Flow Analysis with Contract Annotations and IntelliJ IDEA 13
As we’re closing in on the release date, we’re going to publish more details on the new features of IntelliJ IDEA 13. Today we’d like to tell you about the new @Contract annotation.
If you are aware of @Nullable/@NotNull annotations, you probably know how helpful they are against NullPointerException. The new @Contract annotation brings one more layer of safety for your code by defining dependencies between method arguments and return values. This information lets IntelliJ IDEA provide smarter control flow analysis for your code and avoid probable errors.
Here’s an example: say we’ve got a method sort() that returns a sorted list or a null value if the argument is null.
Now if we try to to call this method with a null value and check the result against null, IntelliJ IDEA will not complain because it doesn’t know that a null input yields a null output.
How can we help IntelliJ IDEA? That’s right… Decorate the sort() method with a @Contract annotation, specifying that null inputs yield null outputs:
Now IntelliJ IDEA can see that the sort() method always returns null and the if statement is redundant. The corresponding message appears in the editor.
The @Contract annotation value has the following syntax:
- contract ::= (clause ‘;’)* clause
- clause ::= args ‘->’ effect
- args ::= ((arg ‘,’)* arg )?
- arg ::= value-constraint
- value-constraint ::= ‘any’ | ‘null’ | ‘!null’ | ‘false’ | ‘true’
- effect ::= value-constraint | ‘fail’
The constraints denote the following:
- _ – any value
- null – null value
- !null – a value statically proved to be not-null
- true – true boolean value
- false – false boolean value
- fail – the method throws exception, if the arguments satisfy argument constraints
The @Contract annotation is a powerful and flexible tool that helps make your APIs safer. However, you can use it not only for annotating your own code but also for other existing libraries.
Once you’ve configured the annotations for the project libraries, the IDE stores this information in simple XML files so it can be shared with the team via version control.
To enable the annotations in your project, just add <IntelliJ IDEA Home>/lib/annotations.jar to the classpath via Project Structure.
UPDATE: Please find the latest annotations.jar in Maven repository.
Develop with Pleasure!
Subscribe to Blog updates
Thanks, we've got you!
Import Postman Collections to the HTTP Client
We know that many of you use Postman collections and want to stay in the flow while working with them. This means staying inside IntelliJ IDEA and not switching to a different app. Great news! You can now effortlessly transform Postman collections into .http and use them in the HTTP Client …
What’s New in IntelliJ IDEA 2023.1 for Spring Developers
The IntelliJ IDEA 2023.1 release came packed with updates for Spring. This blog post will highlight the most noticeable changes, including full support for Lombok, a reworked Spring tool window, navigation for Spring Security rules, Spring Security 6 support, and more. Remember that these featur…
‘Quick’ Features in IntelliJ IDEA
A few IntelliJ IDEA features or actions include the term ‘quick’. Do they speed up certain processes? Let’s find out. In this blog post, I’ll cover what these features are, why you need them, and how you can use them. 1. Quick Lists If you have actions that you use regularly but don’t have shortcut…
5 Best Features for Profiling Java Code In IntelliJ IDEA
When it comes to apps functioning efficiently, there is always room for improvement. Fast, lag-free, smooth operation – what else can a Java developer dream of? However, when you aim for this level of perfection in your app, you will need a comprehensive toolset to monitor performance and identif…