IntelliJ IDEA 2022.1 Help

Annotations

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 collection of annotations that are available as a separate project. You can add them to the classpath and use in your code.

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.2' dependency to the build.gradle file.

  2. For Maven projects, add the org.jetbrains:annotations:16.0.2 dependency to pom.xml.

Add org.jetbrains.annotations to other projects

  1. Open the Project Structure dialog Ctrl+Alt+Shift+S and select Libraries.

  2. Click App general add, and select From Maven.

  3. In the search field, type org.jetbrains:annotations:16.0.2 if you use JDK 1.8 or later.

    For JDK 1.5, 1.6 or 1.7, type org.jetbrains:annotations-java5:16.0.2.

  4. Click OK.

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 not null
  2. From the list, select Add 'annotations' to classpath.

    The IDE will prompt you to download the library with annotations from Maven.

JetBrains 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: 10 August 2022