Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @Contract annotation

I am trying to learn how to develop start-up apps fastly by using libraries and annotations. I have already experinced @NonNull, @Nullable ones.

While reading javadocs i noticed there is a @Contract annotation which has usage @Contract("null->fail") that makes compilation error if null passed.

Thats really cool feature but i cant use it because there is no annotation class in support.annotations package.

How can i get this feature? Or which library to import?

Thanks.

like image 404
Emre Aktürk Avatar asked Jul 20 '14 19:07

Emre Aktürk


People also ask

What is @contract in Java?

The @Contract annotation is used for defining a contract that a method must meet. This lets the IDE find problems in methods which call methods that you have annotated. You can use this annotation not only for annotating your own code but also for other existing libraries.

What is an annotation in a contract?

Contract annotations let you define expected outputs for given inputs, or put in other words, define dependencies between reference type and boolean arguments of a function and its return value. The mechanism of contract annotations allows creating APIs that could be consumed in easier and safer way.

What are JetBrains annotations?

JetBrains. Annotations help reduce false positive warnings, explicitly declare purity and nullability in your code, deal with implicit usages of members, support special semantics of APIs in ASP.NET and XAML frameworks and otherwise increase accuracy of JetBrains Rider and ReSharper code inspections.


1 Answers

You need to add jetbrains' annotations jar to your classpath. You can find it in maven central or locally, in <IntelliJ IDEA Home>/lib/annotations.jar

enter image description here

like image 118
Amit Portnoy Avatar answered Oct 22 '22 11:10

Amit Portnoy