IntelliJ IDEA 2018.1 Help


Java annotations are pieces of metadata that provide information about the code they are used with, and can instruct the IDE how to process this code.

In Java, there is a set of built-in annotations. On top of that, IntelliJ IDEA provides a bundled package with proprietary annotations that you can add to the classpath and use in your code.

Add org.jetbrains.annotations to a project

  1. Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Libraries.
  2. Click new and under the <IDE_HOME> directory, select redist.

    For example, the path on Windows: C:\Program Files\JetBrains\IntelliJ IDEA 181.4203.6\redist.

    On macOS, annotations can be found in IntelliJ

  3. Select annotation.jar if you use Java 7 or lower, or select annotation-java8.jar if you use Java 8 or higher.
  4. In the next dialog, select the module in which you want to use annotations, and apply the changes.

Add org.jetbrains.annotations to Gradle or Maven projects

  1. To add the library with annotations to a Gradle project, add the compile 'org.jetbrains:annotations:16.0.1' dependency to the build.gradle file.
  2. For Maven projects, add the org.jetbrains:annotations:16.0.1 dependency to pom.xml.

Add org.jetbrains.annotations in the editor

You can also enable annotations using an intention action.

  1. In the editor, type an annotation, for example, @NotNull and press Alt+Enter:
    annotations NotNull
  2. From the list, select Add 'annotations' to classpath.
  3. In the next dialog, specify whether you want to use annotations from the IntelliJ IDEA distribution package or create a local copy of the library. If you share your project through VCS, it is recommended that you create a local copy of the library, as you will not be able to share annotations from the distribution package.

Bundled annotations

  • The @Nls annotation indicates that an annotated code element is a string that needs to be localized.
  • The @NonNls annotation indicates that an annotated code element is a string which is not visible to users, it doesn't require localization, and it doesn't contain strings requiring localization. When you annotate an element with @NonNls, localization tools will skip this element and strings inside it.
  • The @PropertyKey annotation indicates that a method parameter accepts arguments that must be valid property keys in a specific resource bundle. When a string literal that is not a property key in a bundle is passed as a parameter, IntelliJ IDEA highlights it as an error. The annotation is also used to provide completion in string literals passed as parameters.
  • The @TestOnly annotation indicates that a method or a constructor must be called from testing code only.
  • The @Contract annotation lets you specify a set of rules (a contract) that a method must follow. If the contract is violated, IntelliJ IDEA reports a problem.
  • The @Nullable annotation indicates a variable, parameter, or return value that can be null.
  • The @NotNull annotation indicates a variable, parameter, or return value that cannot be null.
Last modified: 15 May 2018

See Also