Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @NotNull in a project where both IntelliJ and Eclipse developers are working

A co-worker on IntelliJ IDEA (working on another project) showed me the amazing @NotNull annotation. I've read messages here on SO about how starting to add @NotNull everywhere saved lots of time and headaches (and IntelliJ 10 can even add automatically @NotNull to old code when it detects that null would wreak havoc).

Since I read my first "Probable @NotNull violation" message (in real-time, in the IDE, even on a partial .java file that doesn't compile yet) my jaw dropped and I got hooked.

So I was wondering: is there anything that needs to be known if we want to start using @NotNull in a project where developers are using both Eclipse and IntelliJ?

I know IntelliJ ships with the annotations.jar. Is this compatible with Eclipse?

like image 842
Gugussee Avatar asked Jan 07 '11 14:01

Gugussee


People also ask

What is the use of @NotNull annotation in Java?

@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.

What does @NonNull do Java?

@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException. @ReadOnly – The compiler will flag any attempt to change the object.

What does Lombok NonNull do?

The @NonNull annotation generates a null check for fields and arguments annotated with this annotation. This annotation can be used on fields, constructor arguments, and method arguments.


4 Answers

There is also the possibility to store the annotations outside of the source. Settings > Code Style > Code generation > Use external annotations. The annotations are then stored in an annotations.xml file instead of in the source. That way Eclipse users never have to see the annotations. The location of the annotations.xml can be configured and it is probably best to store the file with the project in version control.

See

http://www.jetbrains.com/idea/webhelp/using-external-annotations.html http://blogs.jetbrains.com/idea/2008/02/external-annotations/

like image 68
Bas Leijdekkers Avatar answered Sep 22 '22 10:09

Bas Leijdekkers


You can use annotations.jar with Eclipse, but you probably won't get any benefits... See this SO post.

like image 25
darioo Avatar answered Sep 22 '22 10:09

darioo


You could support both IDEs with FindBugs's @NonNull and related IDE plugins.

like image 36
Andy Thomas Avatar answered Sep 21 '22 10:09

Andy Thomas


Findbugs has similar functionality, but the integration is a pale shadow of IntelliJ's built in power.

like image 35
sblundy Avatar answered Sep 24 '22 10:09

sblundy