Ann is a DZone Zone Leader and has posted 19 posts at DZone. You can read more from them at their website. View Full User Profile

Using External Annotations

  • submit to reddit

If you had worked with IntelliJ IDEA for a while, you're most likely aware of @Nullable, @NotNull annotations which allow you to formally specify method contracts and validate whether these contracts are met, and @NonNls annotation which is helpful when you want to exclude strings from the internationalization process. (Read more about contract annotations in IntelliJ IDEA)

However, there're several cases when direct annotating code is not advisable: for example, project is shared between team members that use different IDEs, or you work with library classes. That does not mean you can't make use of these annotations, though – with IntelliJ IDEA you can store annotations outside of the source code.

Let's have a look at how it works. First, we need to open the Settings dialog (Ctrl + Alt + S) and go to Code Style (F), then check the option Use external annotations at the Code Generation tab. Now, add the annotations.jar library to either module or project. You can find it under INTELLIJ_IDEA_HOME/lib folder.









After that, open your current inspection profile and make sure Constant conditions& exceptions inspection is enabled and Suggest @Nullable annotation for methods that may possibly return null option is selected.

At this moment we can enjoy externally annotating methods, fields, parameters, etc. For example, we work on a module that contains a method that might return null.






Let's add annotation – just click the light bulb. IntelliJ IDEA asks whether we want to add annotation in code directly, or store it externally. Select the second option and specify the external annotation root for the current module.

Note: You only select the location for external annotations once, so each next external annotation within this module will be stored in the same folder. Alternatively, you can specify annotations root at any time: open Project Settings dialog and go to the Modules page, then select the needed module and open its Paths tab. There you can find External annotations area, where you can manage external annotations attached to the module.

Now, this method is annotated without placing anything to the source code directly. You can press Ctrl + Q to view the attached annotation.






With external annotations you can even annotate methods within JDK classes.






Just like when you add external annotation on the module level, you can specify the storage location right when you add the annotation, or using Project Settings dialog. In the latter case, open Project Settings dialog, go to JDKs and specify the path in the Annotations tab.

Your rating: None Average: 5 (2 votes)
Published at DZone with permission of its author, Ann Oreshnikova.


Alex Miller replied on Tue, 2008/02/19 - 12:57pm

FindBugs also defines an annotation for nullability.  But probably a better idea is to have some kind of standard annotation for this kind of thing and that idea is being worked on as JSR 305.  Also tangentially related is JSR 308 which would allow annotations on a much broader set of locations in Java (in particular, on types). 

Serkan Gunes replied on Wed, 2008/02/20 - 4:56am

I aggree to that,

Even though I like IntelliJ Idea, I wouldn't want my classes depending on their annotation library. 

Ann Oreshnikova replied on Wed, 2008/02/20 - 1:39pm

Oh, your concerns about JSR-305 and setting standards for use of annotations are absolutely understandable. With this regard, you might want to know that JetBrains is actually the active member of this JSR, and whatever is finally set, is going to be immediately supported in IntelliJ IDEA.

As for any worries regarding being hooked to the IntelliJ IDEA's annotation library, these external annotations are exactly the mean to avoid that. They allow you to keep your code free of explicit annotations, while still analyzable if you open it in IntelliJ IDEA.

Jeroen Wenting replied on Thu, 2008/02/21 - 1:51am

not quite Ann.

You will need the annotations.jar on the classpath at compile time whether you compile inside IntelliJ or out.
So there is a compile time dependency, which you may want to avoid if you have a project where not everyone uses IntelliJ.
Of course the same is true for whatever library you choose. You always end up with a dependency on it, which is why I don't worry about such things except for wanting to make sure I'm not hooking myself into something that's a dead or dying project (and I've no such worries about IntelliJ for the foreseeable future).


Qinxian Xiang replied on Sun, 2008/02/24 - 8:11pm

JSR 305 focus to assist defect detection tools. JSR 308  to extend scope of annotation apply.

The key problem is  that that not include compiler.

void doSth(Object @NotNull source){



@NotNull Object biz1(){return aNonNull;}

Object biz2(){return null;} 

void main(){




the Compiler should do thing like this:

void main(){

Object result = biz1();



if (result!=null)

throw HowProcessIsAnAnotherProblem();




Does someone knows who focus compiler extending?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.