简体   繁体   中英

How to add @Contract annotation in IntelliJ IDEa?

I've seen that IntelliJ IDEa has this little @ mark at the side of some methods, when it can statically infer that a contract is met.

For example, in the following code:

public void method(Object arg1, Object arg2){
   assert arg1 != null;
   //Do something..
}

Te IDE proposes the following @Contract: @Contract("null, _ -> fail") . Can IntelliJ IDEa add this contracts via some command? I tried with left/right click but it does not help.

Also, is there a good explanation of how does these annotations work?

Statically inferred contract annotations are used by the code analysis as explicit annotations, so there is actually no need to explicitly add them. But if you still want to add them just position the text cursor on the method name and press Alt + Enter and invoke the Insert '@Contract(value = "XXX")' intention. This intention is called Make Inferred Annotations Explicit in the settings.

The syntax and usage of the @Contract intention is explained pretty well in the announcement blog post of 2013-10-21, Better Control Flow Analysis with Contract Annotations and IntelliJ IDEA 13 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM