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
- To add the library with annotations to a Gradle project, add the
compile 'org.jetbrains:annotations:16.0.2'dependency to the
- For Maven projects, add the
Add org.jetbrains.annotations to other projects
- Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Libraries.
- Click , and select From Maven.
In the search field, type
org.jetbrains:annotations:16.0.2if you use JDK 1.8 or higher.
For JDK 1.5, 1.6 or 1.7, type
- Click OK.
Add org.jetbrains.annotations in the editor
You can also enable annotations using an intention action.
- In the editor, type an annotation, for example,
@NotNulland press Alt+Enter:
From the list, select Add 'annotations' to classpath.
The IDE will prompt you to download the library with annotations from Maven.
@Nlsannotation indicates that an annotated code element is a string that needs to be localized.
@NonNlsannotation 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.
@PropertyKeyannotation 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.
@TestOnlyannotation 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.